ViSP  2.9.0
tutorial-simu-pioneer.cpp
1 
18 #include <iostream>
19 
20 #include <visp/vpFeatureBuilder.h>
21 #include <visp/vpFeatureDepth.h>
22 #include <visp/vpFeaturePoint.h>
23 #include <visp/vpHomogeneousMatrix.h>
24 #include <visp/vpPlot.h>
25 #include <visp/vpServo.h>
26 #include <visp/vpSimulatorPioneer.h>
27 #include <visp/vpVelocityTwistMatrix.h>
28 
29 int main()
30 {
31  try {
33  cdMo[1][3] = 1.2;
34  cdMo[2][3] = 0.5;
35 
37  cMo[0][3] = 0.3;
38  cMo[1][3] = cdMo[1][3];
39  cMo[2][3] = 1.;
40  vpRotationMatrix cRo(0, atan2( cMo[0][3], cMo[1][3]), 0);
41  cMo.insert(cRo);
42 
43  vpSimulatorPioneer robot ;
44  robot.setSamplingTime(0.04);
45  vpHomogeneousMatrix wMc, wMo;
46  robot.getPosition(wMc);
47  wMo = wMc * cMo;
48 
49  vpPoint point;
50  point.setWorldCoordinates(0,0,0);
51  point.track(cMo);
52 
53  vpServo task;
56  task.setLambda(0.2);
58  cVe = robot.get_cVe();
59  task.set_cVe(cVe);
60 
61  vpMatrix eJe;
62  robot.get_eJe(eJe);
63  task.set_eJe(eJe);
64 
65  vpFeaturePoint s_x, s_xd;
66  vpFeatureBuilder::create(s_x, point);
67  s_xd.buildFrom(0, 0, cdMo[2][3]);
68  task.addFeature(s_x, s_xd, vpFeaturePoint::selectX());
69 
70  vpFeatureDepth s_Z, s_Zd;
71  double Z = point.get_Z();
72  double Zd = cdMo[2][3];
73  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z/Zd));
74  s_Zd.buildFrom(0, 0, Zd, 0);
75  task.addFeature(s_Z, s_Zd);
76 
77 #ifdef VISP_HAVE_DISPLAY
78  // Create a window (800 by 500) at position (400, 10) with 3 graphics
79  vpPlot graph(3, 800, 500, 400, 10, "Curves...");
80 
81  // Init the curve plotter
82  graph.initGraph(0,2);
83  graph.initGraph(1,2);
84  graph.initGraph(2,1);
85  graph.setTitle(0, "Velocities");
86  graph.setTitle(1, "Error s-s*");
87  graph.setTitle(2, "Depth");
88  graph.setLegend(0, 0, "vx");
89  graph.setLegend(0, 1, "wz");
90  graph.setLegend(1, 0, "x");
91  graph.setLegend(1, 1, "log(Z/Z*)");
92  graph.setLegend(2, 0, "Z");
93 #endif
94 
95  int iter = 0;
96  for (; ;)
97  {
98  robot.getPosition(wMc) ;
99  cMo = wMc.inverse() * wMo;
100 
101  point.track(cMo);
102 
103  vpFeatureBuilder::create(s_x, point);
104 
105  Z = point.get_Z() ;
106  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z/Zd)) ;
107 
108  robot.get_cVe(cVe);
109  task.set_cVe(cVe);
110  robot.get_eJe(eJe);
111  task.set_eJe(eJe);
112 
113  vpColVector v = task.computeControlLaw();
115 
116 #ifdef VISP_HAVE_DISPLAY
117  graph.plot(0, iter, v); // plot velocities applied to the robot
118  graph.plot(1, iter, task.getError()); // plot error vector
119  graph.plot(2, 0, iter, Z); // plot the depth
120 #endif
121 
122  iter ++;
123 
124  if (task.getError().sumSquare() < 0.0001) {
125  std::cout << "Reached a small error. We stop the loop... " << std::endl;
126  break;
127  }
128  }
129 #ifdef VISP_HAVE_DISPLAY
130  graph.saveData(0, "./v2.dat");
131  graph.saveData(1, "./error2.dat");
132 
133  const char *legend = "Click to quit...";
134  vpDisplay::displayCharString(graph.I, (int)graph.I.getHeight()-60, (int)graph.I.getWidth()-150, legend, vpColor::red);
135  vpDisplay::flush(graph.I);
136  vpDisplay::getClick(graph.I);
137 #endif
138 
139 
140  // Kill the servo task
141  task.print() ;
142  task.kill();
143 
144  }
145  catch(vpException e) {
146  std::cout << "Catch an exception: " << e << std::endl;
147  }
148 }
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:89
void buildFrom(const double x, const double y, const double Z, const double LogZoverZstar)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
Class that defines the Pioneer mobile robot simulator equipped with a static camera.
error that can be emited by ViSP classes.
Definition: vpException.h:76
void track(const vpHomogeneousMatrix &cMo)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void getPosition(vpHomogeneousMatrix &wMc) const
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:809
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static const vpColor red
Definition: vpColor.h:167
Class that defines what is a point.
Definition: vpPoint.h:65
The vpRotationMatrix considers the particular case of a rotation matrix.
virtual void setSamplingTime(const double &delta_t)
void insert(const vpRotationMatrix &R)
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
void setLambda(double c)
Definition: vpServo.h:370
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
void buildFrom(const double x, const double y, const double Z)
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.h:122
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
double get_y() const
double get_x() const
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:117
virtual bool getClick(bool blocking=true)=0
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
static unsigned int selectX()
void get_eJe(vpMatrix &eJe)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74