Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
tutorial-simu-pioneer-pan.cpp
1 
20 #include <iostream>
21 
22 #include <visp3/core/vpConfig.h>
23 #include <visp3/core/vpHomogeneousMatrix.h>
24 #include <visp3/core/vpVelocityTwistMatrix.h>
25 #include <visp3/gui/vpPlot.h>
26 #include <visp3/robot/vpSimulatorPioneerPan.h>
27 #include <visp3/visual_features/vpFeatureBuilder.h>
28 #include <visp3/visual_features/vpFeatureDepth.h>
29 #include <visp3/visual_features/vpFeaturePoint.h>
30 #include <visp3/vs/vpServo.h>
31 
32 int main()
33 {
34 #if defined(ENABLE_VISP_NAMESPACE)
35  using namespace VISP_NAMESPACE_NAME;
36 #endif
37  try {
38  // Set the position the camera has to reach
40  cdMo[1][3] = 1.2; // t_y should be different from zero to be non singular
41  cdMo[2][3] = 0.5;
42 
43  // Set the initial camera position
45  cMo[0][3] = 0.3;
46  cMo[1][3] = cdMo[1][3];
47  cMo[2][3] = 1.;
48  vpRotationMatrix cdRo(0, atan2(cMo[0][3], cMo[1][3]), 0);
49  cMo.insert(cdRo);
50 
52  robot.setSamplingTime(0.04);
53  vpHomogeneousMatrix wMc, wMo;
54 
55  // Get robot position world frame
56  robot.getPosition(wMc);
57 
58  // Compute the position of the object in the world frame
59  wMo = wMc * cMo;
60 
61  // Define the target
62  vpPoint point(0, 0, 0); // Coordinates in the object frame
63  point.track(cMo);
64 
65  vpServo task;
68  task.setLambda(0.2);
69 
71  cVe = robot.get_cVe();
72  task.set_cVe(cVe);
73 
74  vpMatrix eJe;
75  robot.get_eJe(eJe);
76  task.set_eJe(eJe);
77 
78  // Current and desired visual feature associated later to the x coordinate
79  // of the point
80  vpFeaturePoint s_x, s_xd;
81 
82  // Create the current x visual feature
83  vpFeatureBuilder::create(s_x, point);
84 
85  // Create the desired x* visual feature
86  s_xd.buildFrom(0, 0, cdMo[2][3]);
87 
88  // Add the feature
89  task.addFeature(s_x, s_xd, vpFeaturePoint::selectX());
90 
91  // Create the current and desired log(Z/Z*) visual feature
92  vpFeatureDepth s_Z, s_Zd;
93  // Initial depth of the target in front of the camera
94  double Z = point.get_Z();
95  // Desired depth Z* of the target.
96  double Zd = cdMo[2][3];
97  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z / Zd));
98  s_Zd.buildFrom(0, 0, Zd,
99  0); // log(Z/Z*) = 0 that's why the last parameter is 0
100 
101  // Add the feature
102  task.addFeature(s_Z, s_Zd);
103 
104 #ifdef VISP_HAVE_DISPLAY
105  // Create a window (800 by 500) at position (400, 10) with 3 graphics
106  vpPlot graph(3, 800, 500, 400, 10, "Curves...");
107 
108  // Init the curve plotter
109  graph.initGraph(0, 3);
110  graph.initGraph(1, 2);
111  graph.initGraph(2, 1);
112  graph.setTitle(0, "Velocities");
113  graph.setTitle(1, "Error s-s*");
114  graph.setTitle(2, "Depth");
115  graph.setLegend(0, 0, "vx");
116  graph.setLegend(0, 1, "wz");
117  graph.setLegend(0, 2, "qdot_pan");
118  graph.setLegend(1, 0, "x");
119  graph.setLegend(1, 1, "log(Z/Z*)");
120  graph.setLegend(2, 0, "Z");
121 #endif
122 
123  int iter = 0;
124  for (;;) {
125  robot.getPosition(wMc);
126  cMo = wMc.inverse() * wMo;
127 
128  point.track(cMo);
129 
130  // Update the current x feature
131  vpFeatureBuilder::create(s_x, point);
132 
133  // Update log(Z/Z*) feature. Since the depth Z change, we need to update
134  // the intection matrix
135  Z = point.get_Z();
136  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z / Zd));
137 
138  robot.get_cVe(cVe);
139  task.set_cVe(cVe);
140  robot.get_eJe(eJe);
141  task.set_eJe(eJe);
142 
143  // Compute the control law. Velocities are computed in the mobile robot
144  // reference frame
145  vpColVector v = task.computeControlLaw();
146 
147  // Send the velocity to the robot
149 
150 #ifdef VISP_HAVE_DISPLAY
151  graph.plot(0, iter, v); // plot velocities applied to the robot
152  graph.plot(1, iter, task.getError()); // plot error vector
153  graph.plot(2, 0, iter, Z); // plot the depth
154 #endif
155  iter++;
156 
157  if (task.getError().sumSquare() < 0.0001) {
158  std::cout << "Reached a small error. We stop the loop... " << std::endl;
159  break;
160  }
161  }
162 #ifdef VISP_HAVE_DISPLAY
163  const char *legend = "Click to quit...";
164  vpDisplay::displayText(graph.I, (int)graph.I.getHeight() - 60, (int)graph.I.getWidth() - 150, legend, vpColor::red);
165  vpDisplay::flush(graph.I);
166  vpDisplay::getClick(graph.I);
167 #endif
168 
169  // Kill the servo task
170  task.print();
171  }
172  catch (const vpException &e) {
173  std::cout << "Catch an exception: " << e << std::endl;
174  }
175 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
double sumSquare() const
static const vpColor red
Definition: vpColor.h:217
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
vpFeatureDepth & buildFrom(const double &x, const double &y, const double &Z, const double &LogZoverZstar)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
static unsigned int selectX()
double get_y() const
double get_x() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
void insert(const vpRotationMatrix &R)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:112
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
void get_eJe(vpMatrix &eJe) VP_OVERRIDE
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ ARTICULAR_FRAME
Definition: vpRobot.h:80
Implementation of a rotation matrix and operations on such kind of matrices.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_L_cVe_eJe
Definition: vpServo.h:168
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:1038
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:986
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:1101
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
@ PSEUDO_INVERSE
Definition: vpServo.h:235
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
Class that defines the Pioneer mobile robot simulator equipped with a camera able to move in pan.
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:72