ViSP  2.9.0
tutorial-simu-pioneer-pan.cpp
1 
19 #include <iostream>
20 
21 #include <visp/vpFeatureBuilder.h>
22 #include <visp/vpFeatureDepth.h>
23 #include <visp/vpFeaturePoint.h>
24 #include <visp/vpHomogeneousMatrix.h>
25 #include <visp/vpPlot.h>
26 #include <visp/vpServo.h>
27 #include <visp/vpSimulatorPioneerPan.h>
28 #include <visp/vpVelocityTwistMatrix.h>
29 
30 int main()
31 {
32  try {
33  // Set the position the camera has to reach
34  vpHomogeneousMatrix cdMo ;
35  cdMo[1][3] = 1.2; // t_y should be different from zero to be non singular
36  cdMo[2][3] = 0.5;
37 
38  // Set the initial camera position
40  cMo[0][3] = 0.3;
41  cMo[1][3] = cdMo[1][3];
42  cMo[2][3] = 1.;
43  vpRotationMatrix cdRo(0, atan2(cMo[0][3], cMo[1][3]), 0);
44  cMo.insert(cdRo);
45 
46  vpSimulatorPioneerPan robot ;
47  robot.setSamplingTime(0.04);
48  vpHomogeneousMatrix wMc, wMo;
49 
50  // Get robot position world frame
51  robot.getPosition(wMc);
52 
53  // Compute the position of the object in the world frame
54  wMo = wMc * cMo;
55 
56  // Define the target
57  vpPoint point;
58  point.setWorldCoordinates(0,0,0); // Coordinates in the object frame
59  point.track(cMo);
60 
61  vpServo task;
64  task.setLambda(0.2);
65 
67  cVe = robot.get_cVe();
68  task.set_cVe(cVe);
69 
70  vpMatrix eJe;
71  robot.get_eJe(eJe);
72  task.set_eJe(eJe);
73 
74  // Current and desired visual feature associated later to the x coordinate of the point
75  vpFeaturePoint s_x, s_xd;
76 
77  // Create the current x visual feature
78  vpFeatureBuilder::create(s_x, point);
79 
80  // Create the desired x* visual feature
81  s_xd.buildFrom(0, 0, cdMo[2][3]);
82 
83  // Add the feature
84  task.addFeature(s_x, s_xd, vpFeaturePoint::selectX());
85 
86  // Create the current and desired log(Z/Z*) visual feature
87  vpFeatureDepth s_Z, s_Zd;
88  // Initial depth of the target in front of the camera
89  double Z = point.get_Z();
90  // Desired depth Z* of the target.
91  double Zd = cdMo[2][3];
92  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z/Zd));
93  s_Zd.buildFrom(0, 0, Zd, 0); // log(Z/Z*) = 0 that's why the last parameter is 0
94 
95  // Add the feature
96  task.addFeature(s_Z, s_Zd);
97 
98 #ifdef VISP_HAVE_DISPLAY
99  // Create a window (800 by 500) at position (400, 10) with 3 graphics
100  vpPlot graph(3, 800, 500, 400, 10, "Curves...");
101 
102  // Init the curve plotter
103  graph.initGraph(0,3);
104  graph.initGraph(1,2);
105  graph.initGraph(2,1);
106  graph.setTitle(0, "Velocities");
107  graph.setTitle(1, "Error s-s*");
108  graph.setTitle(2, "Depth");
109  graph.setLegend(0, 0, "vx");
110  graph.setLegend(0, 1, "wz");
111  graph.setLegend(0, 2, "qdot_pan");
112  graph.setLegend(1, 0, "x");
113  graph.setLegend(1, 1, "log(Z/Z*)");
114  graph.setLegend(2, 0, "Z");
115 #endif
116 
117  int iter = 0;
118  for (; ;)
119  {
120  robot.getPosition(wMc) ;
121  cMo = wMc.inverse() * wMo;
122 
123  point.track(cMo);
124 
125  // Update the current x feature
126  vpFeatureBuilder::create(s_x, point);
127 
128  // Update log(Z/Z*) feature. Since the depth Z change, we need to update the intection matrix
129  Z = point.get_Z() ;
130  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z/Zd));
131 
132  robot.get_cVe(cVe);
133  task.set_cVe(cVe);
134  robot.get_eJe(eJe);
135  task.set_eJe(eJe);
136 
137  // Compute the control law. Velocities are computed in the mobile robot reference frame
138  vpColVector v = task.computeControlLaw();
139 
140  // Send the velocity to the robot
142 
143 #ifdef VISP_HAVE_DISPLAY
144  graph.plot(0, iter, v); // plot velocities applied to the robot
145  graph.plot(1, iter, task.getError()); // plot error vector
146  graph.plot(2, 0, iter, Z); // plot the depth
147 #endif
148  iter ++;
149 
150  if (task.getError().sumSquare() < 0.0001) {
151  std::cout << "Reached a small error. We stop the loop... " << std::endl;
152  break;
153  }
154  }
155 #ifdef VISP_HAVE_DISPLAY
156  const char *legend = "Click to quit...";
157  vpDisplay::displayCharString(graph.I, (int)graph.I.getHeight()-60, (int)graph.I.getWidth()-150, legend, vpColor::red);
158  vpDisplay::flush(graph.I);
159  vpDisplay::getClick(graph.I);
160 #endif
161 
162  // Kill the servo task
163  task.print();
164  task.kill();
165  }
166  catch(vpException e) {
167  std::cout << "Catch an exception: " << e << std::endl;
168  }
169 }
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 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...
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...
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
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
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
Class that defines the Pioneer mobile robot simulator equipped with a camera able to move in pan...
void getPosition(vpHomogeneousMatrix &wMc) 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 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