Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
tutorial-ibvs-4pts-plotter.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpPlot.h>
4 #include <visp3/robot/vpSimulatorCamera.h>
5 #include <visp3/visual_features/vpFeatureBuilder.h>
6 #include <visp3/vs/vpServo.h>
7 
8 int main()
9 {
10 #ifdef ENABLE_VISP_NAMESPACE
11  using namespace VISP_NAMESPACE_NAME;
12 #endif
13 
14  try {
15  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
16  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
17 
18  vpPoint point[4];
19  point[0].setWorldCoordinates(-0.1, -0.1, 0);
20  point[1].setWorldCoordinates(0.1, -0.1, 0);
21  point[2].setWorldCoordinates(0.1, 0.1, 0);
22  point[3].setWorldCoordinates(-0.1, 0.1, 0);
23 
24  vpServo task;
27  task.setLambda(0.5);
28 
29  vpFeaturePoint p[4], pd[4];
30  for (unsigned int i = 0; i < 4; i++) {
31  point[i].track(cdMo);
32  vpFeatureBuilder::create(pd[i], point[i]);
33  point[i].track(cMo);
34  vpFeatureBuilder::create(p[i], point[i]);
35  task.addFeature(p[i], pd[i]);
36  }
37 
38  vpHomogeneousMatrix wMc, wMo;
39  vpSimulatorCamera robot;
40  robot.setSamplingTime(0.040);
41  robot.getPosition(wMc);
42  wMo = wMc * cMo;
43 
44 #ifdef VISP_HAVE_DISPLAY
45  vpPlot plotter(2, 250 * 2, 500, 100, 200, "Real time curves plotter");
46  plotter.setTitle(0, "Visual features error");
47  plotter.setTitle(1, "Camera velocities");
48 
49  plotter.initGraph(0, 8);
50  plotter.initGraph(1, 6);
51 
52  plotter.setLegend(0, 0, "x1");
53  plotter.setLegend(0, 1, "y1");
54  plotter.setLegend(0, 2, "x2");
55  plotter.setLegend(0, 3, "y2");
56  plotter.setLegend(0, 4, "x3");
57  plotter.setLegend(0, 5, "y3");
58  plotter.setLegend(0, 6, "x4");
59  plotter.setLegend(0, 7, "y4");
60 
61  plotter.setLegend(1, 0, "v_x");
62  plotter.setLegend(1, 1, "v_y");
63  plotter.setLegend(1, 2, "v_z");
64  plotter.setLegend(1, 3, "w_x");
65  plotter.setLegend(1, 4, "w_y");
66  plotter.setLegend(1, 5, "w_z");
67 #endif
68 
69  unsigned int iter = 0;
70  while (1) {
71  robot.getPosition(wMc);
72  cMo = wMc.inverse() * wMo;
73  for (unsigned int i = 0; i < 4; i++) {
74  point[i].track(cMo);
75  vpFeatureBuilder::create(p[i], point[i]);
76  }
77  vpColVector v = task.computeControlLaw();
79 
80 #ifdef VISP_HAVE_DISPLAY
81  plotter.plot(0, iter, task.getError());
82  plotter.plot(1, iter, v);
83 #endif
84  if ((task.getError()).sumSquare() < 0.0001)
85  break;
86 
87  vpTime::wait(100);
88 
89  iter++;
90  }
91  std::cout << "Convergence in " << iter << " iterations" << std::endl;
92 
93 #ifdef VISP_HAVE_DISPLAY
94  plotter.saveData(0, "error.dat");
95  plotter.saveData(1, "vc.dat");
96 
97  vpDisplay::getClick(plotter.I);
98 #endif
99  }
100  catch (const vpException &e) {
101  std::cout << "Catch an exception: " << e << std::endl;
102  }
103 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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 2D point visual feature which is composed by two parameters that are the cartes...
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:129
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 setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:111
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void setLambda(double c)
Definition: vpServo.h:986
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
Class that defines the simplest robot: a free flying camera.
VISP_EXPORT int wait(double t0, double t)