Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
tutorial-ibvs-4pts-plotter.cpp
1 
2 #include <visp3/gui/vpPlot.h>
3 #include <visp3/robot/vpSimulatorCamera.h>
4 #include <visp3/visual_features/vpFeatureBuilder.h>
5 #include <visp3/vs/vpServo.h>
6 
7 int main()
8 {
9  try {
10  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
11  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
12 
13  vpPoint point[4];
14  point[0].setWorldCoordinates(-0.1, -0.1, 0);
15  point[1].setWorldCoordinates(0.1, -0.1, 0);
16  point[2].setWorldCoordinates(0.1, 0.1, 0);
17  point[3].setWorldCoordinates(-0.1, 0.1, 0);
18 
19  vpServo task;
22  task.setLambda(0.5);
23 
24  vpFeaturePoint p[4], pd[4];
25  for (unsigned int i = 0; i < 4; i++) {
26  point[i].track(cdMo);
27  vpFeatureBuilder::create(pd[i], point[i]);
28  point[i].track(cMo);
29  vpFeatureBuilder::create(p[i], point[i]);
30  task.addFeature(p[i], pd[i]);
31  }
32 
33  vpHomogeneousMatrix wMc, wMo;
34  vpSimulatorCamera robot;
35  robot.setSamplingTime(0.040);
36  robot.getPosition(wMc);
37  wMo = wMc * cMo;
38 
39 #ifdef VISP_HAVE_DISPLAY
40  vpPlot plotter(2, 250 * 2, 500, 100, 200, "Real time curves plotter");
41  plotter.setTitle(0, "Visual features error");
42  plotter.setTitle(1, "Camera velocities");
43 
44  plotter.initGraph(0, 8);
45  plotter.initGraph(1, 6);
46 
47  plotter.setLegend(0, 0, "x1");
48  plotter.setLegend(0, 1, "y1");
49  plotter.setLegend(0, 2, "x2");
50  plotter.setLegend(0, 3, "y2");
51  plotter.setLegend(0, 4, "x3");
52  plotter.setLegend(0, 5, "y3");
53  plotter.setLegend(0, 6, "x4");
54  plotter.setLegend(0, 7, "y4");
55 
56  plotter.setLegend(1, 0, "v_x");
57  plotter.setLegend(1, 1, "v_y");
58  plotter.setLegend(1, 2, "v_z");
59  plotter.setLegend(1, 3, "w_x");
60  plotter.setLegend(1, 4, "w_y");
61  plotter.setLegend(1, 5, "w_z");
62 #endif
63 
64  unsigned int iter = 0;
65  while (1) {
66  robot.getPosition(wMc);
67  cMo = wMc.inverse() * wMo;
68  for (unsigned int i = 0; i < 4; i++) {
69  point[i].track(cMo);
70  vpFeatureBuilder::create(p[i], point[i]);
71  }
72  vpColVector v = task.computeControlLaw();
74 
75 #ifdef VISP_HAVE_DISPLAY
76  plotter.plot(0, iter, task.getError());
77  plotter.plot(1, iter, v);
78 #endif
79  if ((task.getError()).sumSquare() < 0.0001)
80  break;
81 
82  vpTime::wait(100);
83 
84  iter++;
85  }
86  std::cout << "Convergence in " << iter << " iterations" << std::endl;
87 
88 #ifdef VISP_HAVE_DISPLAY
89  plotter.saveData(0, "error.dat");
90  plotter.saveData(1, "vc.dat");
91 
92  vpDisplay::getClick(plotter.I);
93 #endif
94  } catch (const vpException &e) {
95  std::cout << "Catch an exception: " << e << std::endl;
96  }
97 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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:127
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:109
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:110
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
@ CAMERA_FRAME
Definition: vpRobot.h:82
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:378
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:329
void setLambda(double c)
Definition: vpServo.h:976
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:132
vpColVector getError() const
Definition: vpServo.h:504
vpColVector computeControlLaw()
Definition: vpServo.cpp:703
@ CURRENT
Definition: vpServo.h:196
Class that defines the simplest robot: a free flying camera.
VISP_EXPORT int wait(double t0, double t)