Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
tutorial-simu-pioneer.cpp
1 
19 #include <iostream>
20 
21 #include <visp3/core/vpHomogeneousMatrix.h>
22 #include <visp3/core/vpVelocityTwistMatrix.h>
23 #include <visp3/gui/vpPlot.h>
24 #include <visp3/robot/vpSimulatorPioneer.h>
25 #include <visp3/visual_features/vpFeatureBuilder.h>
26 #include <visp3/visual_features/vpFeatureDepth.h>
27 #include <visp3/visual_features/vpFeaturePoint.h>
28 #include <visp3/vs/vpServo.h>
29 
30 int main()
31 {
32  try {
34  cdMo[1][3] = 1.2;
35  cdMo[2][3] = 0.5;
36 
38  cMo[0][3] = 0.3;
39  cMo[1][3] = cdMo[1][3];
40  cMo[2][3] = 1.;
41  vpRotationMatrix cRo(0, atan2(cMo[0][3], cMo[1][3]), 0);
42  cMo.insert(cRo);
43 
44  vpSimulatorPioneer robot;
45  robot.setSamplingTime(0.04);
46  vpHomogeneousMatrix wMc, wMo;
47  robot.getPosition(wMc);
48  wMo = wMc * cMo;
49 
50  vpPoint point(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  robot.getPosition(wMc);
98  cMo = wMc.inverse() * wMo;
99 
100  point.track(cMo);
101 
102  vpFeatureBuilder::create(s_x, point);
103 
104  Z = point.get_Z();
105  s_Z.buildFrom(s_x.get_x(), s_x.get_y(), Z, log(Z / Zd));
106 
107  robot.get_cVe(cVe);
108  task.set_cVe(cVe);
109  robot.get_eJe(eJe);
110  task.set_eJe(eJe);
111 
112  vpColVector v = task.computeControlLaw();
114 
115 #ifdef VISP_HAVE_DISPLAY
116  graph.plot(0, iter, v); // plot velocities applied to the robot
117  graph.plot(1, iter, task.getError()); // plot error vector
118  graph.plot(2, 0, iter, Z); // plot the depth
119 #endif
120 
121  iter++;
122 
123  if (task.getError().sumSquare() < 0.0001) {
124  std::cout << "Reached a small error. We stop the loop... " << std::endl;
125  break;
126  }
127  }
128 #ifdef VISP_HAVE_DISPLAY
129  graph.saveData(0, "./v2.dat");
130  graph.saveData(1, "./error2.dat");
131 
132  const char *legend = "Click to quit...";
133  vpDisplay::displayText(graph.I, (int)graph.I.getHeight() - 60, (int)graph.I.getWidth() - 150, legend, vpColor::red);
134  vpDisplay::flush(graph.I);
135  vpDisplay::getClick(graph.I);
136 #endif
137 
138  // Kill the servo task
139  task.print();
140 
141  } catch (const vpException &e) {
142  std::cout << "Catch an exception: " << e << std::endl;
143  }
144 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:82
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void buildFrom(double x, double y, double Z)
void getPosition(vpHomogeneousMatrix &wMc) const
Implementation of an homogeneous matrix and operations on such kind of matrices.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void buildFrom(double x, double y, double Z, double LogZoverZstar)
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:71
vpHomogeneousMatrix inverse() const
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
Implementation of a rotation matrix and operations on such kind of matrices.
virtual void setSamplingTime(const double &delta_t)
void insert(const vpRotationMatrix &R)
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
static unsigned int selectX()
void setLambda(double c)
Definition: vpServo.h:404
double get_x() const
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:448
double sumSquare() const
double get_y() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:115
vpColVector getError() const
Definition: vpServo.h:278
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void get_eJe(vpMatrix &eJe)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218