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