Visual Servoing Platform  version 3.6.1 under development (2024-07-17)
servoSimuCylinder.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Demonstration of the wireframe simulator with a simple visual servoing
33  *
34 *****************************************************************************/
35 
42 #include <stdlib.h>
43 
44 #include <visp3/core/vpCameraParameters.h>
45 #include <visp3/core/vpCylinder.h>
46 #include <visp3/core/vpHomogeneousMatrix.h>
47 #include <visp3/core/vpImage.h>
48 #include <visp3/core/vpIoTools.h>
49 #include <visp3/core/vpMath.h>
50 #include <visp3/core/vpTime.h>
51 #include <visp3/core/vpVelocityTwistMatrix.h>
52 #include <visp3/gui/vpDisplayD3D.h>
53 #include <visp3/gui/vpDisplayGDI.h>
54 #include <visp3/gui/vpDisplayGTK.h>
55 #include <visp3/gui/vpDisplayOpenCV.h>
56 #include <visp3/gui/vpDisplayX.h>
57 #include <visp3/gui/vpPlot.h>
58 #include <visp3/io/vpImageIo.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/robot/vpSimulatorCamera.h>
61 #include <visp3/robot/vpWireFrameSimulator.h>
62 #include <visp3/visual_features/vpFeatureBuilder.h>
63 #include <visp3/vs/vpServo.h>
64 
65 #define GETOPTARGS "dhp"
66 
67 #if defined(VISP_HAVE_DISPLAY) && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
68 
69 #if defined(ENABLE_VISP_NAMESPACE)
70 using namespace VISP_NAMESPACE_NAME;
71 #endif
72 
81 void usage(const char *name, const char *badparam)
82 {
83  fprintf(stdout, "\n\
84 Demonstration of the wireframe simulator with a simple visual servoing.\n\
85  \n\
86 The visual servoing consists in bringing the camera at a desired position\n\
87 from the object.\n\
88  \n\
89 The visual features used to compute the pose of the camera and \n\
90 thus the control law are two lines. These features are computed thanks \n\
91 to the equation of a cylinder.\n\
92  \n\
93 This demonstration explains also how to move the object around a world \n\
94 reference frame. Here, the movment is a rotation around the x and y axis \n\
95 at a given distance from the world frame. In fact the object trajectory \n\
96 is on a sphere whose center is the origin of the world frame.\n\
97  \n\
98 SYNOPSIS\n\
99  %s [-d] [-p] [-h]\n",
100  name);
101 
102  fprintf(stdout, "\n\
103 OPTIONS: \n\
104  -d \n\
105  Turn off the display.\n\
106  \n\
107  -p \n\
108  Turn off the plotter.\n\
109  \n\
110  -h\n\
111  Print the help.\n");
112 
113  if (badparam)
114  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
115 }
116 
129 bool getOptions(int argc, const char **argv, bool &display, bool &plot)
130 {
131  const char *optarg_;
132  int c;
133  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
134 
135  switch (c) {
136  case 'd':
137  display = false;
138  break;
139  case 'p':
140  plot = false;
141  break;
142  case 'h':
143  usage(argv[0], nullptr);
144  return false;
145 
146  default:
147  usage(argv[0], optarg_);
148  return false;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], nullptr);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161 }
162 
163 int main(int argc, const char **argv)
164 {
165  try {
166  bool opt_display = true;
167  bool opt_plot = true;
168 
169  // Read the command line options
170  if (getOptions(argc, argv, opt_display, opt_plot) == false) {
171  return EXIT_FAILURE;
172  }
173 
174  vpImage<vpRGBa> Iint(480, 640, vpRGBa(255));
175  vpImage<vpRGBa> Iext(480, 640, vpRGBa(255));
176 
177 #if defined(VISP_HAVE_X11)
178  vpDisplayX display[2];
179 #elif defined(HAVE_OPENCV_HIGHGUI)
180  vpDisplayOpenCV display[2];
181 #elif defined(VISP_HAVE_GDI)
182  vpDisplayGDI display[2];
183 #elif defined(VISP_HAVE_D3D9)
184  vpDisplayD3D display[2];
185 #elif defined(VISP_HAVE_GTK)
186  vpDisplayGTK display[2];
187 #endif
188 
189  if (opt_display) {
190  // Display size is automatically defined by the image (I) size
191  display[0].init(Iint, 100, 100, "The internal view");
192  display[1].init(Iext, 100, 100, "The first external view");
193  vpDisplay::setWindowPosition(Iint, 0, 0);
194  vpDisplay::setWindowPosition(Iext, 750, 0);
195  vpDisplay::display(Iint);
196  vpDisplay::flush(Iint);
197  vpDisplay::display(Iext);
198  vpDisplay::flush(Iext);
199  }
200 
201  vpPlot *plotter = nullptr;
202 
203  vpServo task;
204  vpSimulatorCamera robot;
205  float sampling_time = 0.020f; // Sampling period in second
206  robot.setSamplingTime(sampling_time);
207 
208  // Set initial position of the object in the camera frame
209  vpHomogeneousMatrix cMo(0, 0.1, 0.3, vpMath::rad(35), vpMath::rad(25), vpMath::rad(75));
210  // Set desired position of the object in the camera frame
211  vpHomogeneousMatrix cdMo(0.0, 0.0, 0.5, vpMath::rad(90), vpMath::rad(0), vpMath::rad(0));
212  // Set initial position of the object in the world frame
213  vpHomogeneousMatrix wMo(0.0, 0.0, 0, 0, 0, 0);
214  // Position of the camera in the world frame
216  wMc = wMo * cMo.inverse();
217 
218  // Create a cylinder
219  vpCylinder cylinder(0, 0, 1, 0, 0, 0, 0.1);
220 
221  // Projection of the cylinder
222  cylinder.track(cMo);
223 
224  // Set the current visual feature
225  vpFeatureLine l[2];
228 
229  // Projection of the cylinder
230  cylinder.track(cdMo);
231 
232  vpFeatureLine ld[2];
235 
238 
240  vpVelocityTwistMatrix cVe(cMe);
241  task.set_cVe(cVe);
242 
243  vpMatrix eJe;
244  robot.get_eJe(eJe);
245  task.set_eJe(eJe);
246 
247  for (int i = 0; i < 2; i++)
248  task.addFeature(l[i], ld[i]);
249 
250  if (opt_plot) {
251  plotter = new vpPlot(2, 480, 640, 750, 550, "Real time curves plotter");
252  plotter->setTitle(0, "Visual features error");
253  plotter->setTitle(1, "Camera velocities");
254  plotter->initGraph(0, task.getDimension());
255  plotter->initGraph(1, 6);
256  plotter->setLegend(0, 0, "error_feat_l1_rho");
257  plotter->setLegend(0, 1, "error_feat_l1_theta");
258  plotter->setLegend(0, 2, "error_feat_l2_rho");
259  plotter->setLegend(0, 3, "error_feat_l2_theta");
260  plotter->setLegend(1, 0, "vc_x");
261  plotter->setLegend(1, 1, "vc_y");
262  plotter->setLegend(1, 2, "vc_z");
263  plotter->setLegend(1, 3, "wc_x");
264  plotter->setLegend(1, 4, "wc_y");
265  plotter->setLegend(1, 5, "wc_z");
266  }
267 
268  task.setLambda(1);
269 
271 
272  // Set the scene
274 
275  // Initialize simulator frames
276  sim.set_fMo(wMo); // Position of the object in the world reference frame
277  sim.setCameraPositionRelObj(cMo); // Initial position of the object in the camera frame
278  sim.setDesiredCameraPosition(cdMo); // Desired position of the object in the camera frame
279 
280  // Set the External camera position
281  vpHomogeneousMatrix camMf(vpHomogeneousMatrix(0.0, 0, 3.5, vpMath::rad(0), vpMath::rad(30), 0));
282  sim.setExternalCameraPosition(camMf);
283 
284  // Set the parameters of the cameras (internal and external)
285  vpCameraParameters camera(1000, 1000, 320, 240);
286  sim.setInternalCameraParameters(camera);
287  sim.setExternalCameraParameters(camera);
288 
289  int max_iter = 10;
290 
291  if (opt_display) {
292  max_iter = 2500;
293 
294  // Get the internal and external views
295  sim.getInternalImage(Iint);
296  sim.getExternalImage(Iext);
297 
298  // Display the object frame (current and desired position)
299  vpDisplay::displayFrame(Iint, cMo, camera, 0.2, vpColor::none);
300  vpDisplay::displayFrame(Iint, cdMo, camera, 0.2, vpColor::none);
301 
302  // Display the object frame the world reference frame and the camera
303  // frame
304  vpDisplay::displayFrame(Iext, camMf * sim.get_fMo() * cMo.inverse(), camera, 0.2, vpColor::none);
305  vpDisplay::displayFrame(Iext, camMf * sim.get_fMo(), camera, 0.2, vpColor::none);
306  vpDisplay::displayFrame(Iext, camMf, camera, 0.2, vpColor::none);
307 
308  vpDisplay::displayText(Iint, 20, 20, "Click to start visual servo", vpColor::red);
309 
310  vpDisplay::flush(Iint);
311  vpDisplay::flush(Iext);
312 
313  std::cout << "Click on a display" << std::endl;
314  while (!vpDisplay::getClick(Iint, false) && !vpDisplay::getClick(Iext, false)) {
315  };
316  }
317 
318  robot.setPosition(wMc);
319 
320  // Print the task
321  task.print();
322 
323  int iter = 0;
324  bool stop = false;
325  vpColVector v;
326 
327  // Set the secondary task parameters
328  vpColVector e1(6, 0);
329  vpColVector e2(6, 0);
330  vpColVector proj_e1;
331  vpColVector proj_e2;
332  double rapport = 0;
333  double vitesse = 0.3;
334  int tempo = 600;
335 
336  double t_prev, t = vpTime::measureTimeMs();
337 
338  while (iter++ < max_iter && !stop) {
339  t_prev = t;
340  t = vpTime::measureTimeMs();
341 
342  if (opt_display) {
343  vpDisplay::display(Iint);
344  vpDisplay::display(Iext);
345  }
346 
347  robot.get_eJe(eJe);
348  task.set_eJe(eJe);
349 
350  wMc = robot.getPosition();
351  cMo = wMc.inverse() * wMo;
352 
353  cylinder.track(cMo);
356 
357  v = task.computeControlLaw();
358 
359  // Compute the velocity with the secondary task
360  if (iter % tempo < 200 && iter % tempo >= 0) {
361  e2 = 0;
362  e1[0] = -fabs(vitesse);
363  proj_e1 = task.secondaryTask(e1, true);
364  rapport = -vitesse / proj_e1[0];
365  proj_e1 *= rapport;
366  v += proj_e1;
367  }
368 
369  else if (iter % tempo < 300 && iter % tempo >= 200) {
370  e1 = 0;
371  e2[1] = -fabs(vitesse);
372  proj_e2 = task.secondaryTask(e2, true);
373  rapport = -vitesse / proj_e2[1];
374  proj_e2 *= rapport;
375  v += proj_e2;
376  }
377 
378  else if (iter % tempo < 500 && iter % tempo >= 300) {
379  e2 = 0;
380  e1[0] = -fabs(vitesse);
381  proj_e1 = task.secondaryTask(e1, true);
382  rapport = vitesse / proj_e1[0];
383  proj_e1 *= rapport;
384  v += proj_e1;
385  }
386 
387  else if (iter % tempo < 600 && iter % tempo >= 500) {
388  e1 = 0;
389  e2[1] = -fabs(vitesse);
390  proj_e2 = task.secondaryTask(e2, true);
391  rapport = vitesse / proj_e2[1];
392  proj_e2 *= rapport;
393  v += proj_e2;
394  }
395 
397 
398  // Update the simulator frames
399  sim.set_fMo(wMo); // This line is not really requested since the object
400  // doesn't move
401  sim.setCameraPositionRelObj(cMo);
402 
403  if (opt_plot) {
404  plotter->plot(0, iter, task.getError());
405  plotter->plot(1, iter, v);
406  }
407 
408  if (opt_display) {
409  // Get the internal and external views
410  sim.getInternalImage(Iint);
411  sim.getExternalImage(Iext);
412 
413  // Display the object frame (current and desired position)
414  vpDisplay::displayFrame(Iint, cMo, camera, 0.2, vpColor::none);
415  vpDisplay::displayFrame(Iint, cdMo, camera, 0.2, vpColor::none);
416 
417  // Display the object frame the world reference frame and the camera
418  // frame
419  vpDisplay::displayFrame(Iext, sim.getExternalCameraPosition() * sim.get_fMo() * cMo.inverse(), camera, 0.2,
420  vpColor::none);
421  vpDisplay::displayFrame(Iext, sim.getExternalCameraPosition() * sim.get_fMo(), camera, 0.2, vpColor::none);
423 
424  vpDisplay::displayText(Iint, 20, 20, "Click to stop visual servo", vpColor::red);
425 
426  std::stringstream ss;
427  ss << "Loop time: " << t - t_prev << " ms";
428  vpDisplay::displayText(Iint, 40, 20, ss.str(), vpColor::red);
429 
430  if (vpDisplay::getClick(Iint, false)) {
431  stop = true;
432  }
433 
434  vpDisplay::flush(Iext);
435  vpDisplay::flush(Iint);
436 
437  vpTime::wait(t, sampling_time * 1000); // Wait ms
438  }
439 
440  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
441  }
442 
443  if (opt_plot && plotter != nullptr) {
444  vpDisplay::display(Iint);
445  sim.getInternalImage(Iint);
446  vpDisplay::displayFrame(Iint, cMo, camera, 0.2, vpColor::none);
447  vpDisplay::displayFrame(Iint, cdMo, camera, 0.2, vpColor::none);
448  vpDisplay::displayText(Iint, 20, 20, "Click to quit", vpColor::red);
449  if (vpDisplay::getClick(Iint)) {
450  stop = true;
451  }
452  vpDisplay::flush(Iint);
453 
454  delete plotter;
455  }
456 
457  task.print();
458 
459  return EXIT_SUCCESS;
460  }
461  catch (const vpException &e) {
462  std::cout << "Catch an exception: " << e << std::endl;
463  return EXIT_FAILURE;
464  }
465 }
466 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
467 int main()
468 {
469  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
470  return EXIT_SUCCESS;
471 }
472 #else
473 int main()
474 {
475  std::cout << "You do not have X11, or GDI (Graphical Device Interface), or GTK functionalities to display images..."
476  << std::endl;
477  std::cout << "Tip if you are on a unix-like system:" << std::endl;
478  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
479  std::cout << "Tip if you are on a windows-like system:" << std::endl;
480  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
481  return EXIT_SUCCESS;
482 }
483 
484 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
Class that defines a 3D cylinder in the object frame and allows forward projection of a 3D cylinder i...
Definition: vpCylinder.h:101
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:106
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
static void setWindowPosition(const vpImage< unsigned char > &I, int winx, int winy)
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 vpDot &d)
Class that defines a 2D line visual feature which is composed by two parameters that are and ,...
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:112
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:203
void setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
Definition: vpPlot.cpp:552
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
Definition: vpPlot.cpp:270
void setTitle(unsigned int graphNum, const std::string &title)
Definition: vpPlot.cpp:510
Definition: vpRGBa.h:65
void get_eJe(vpMatrix &eJe) VP_OVERRIDE
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_L_cVe_eJe
Definition: vpServo.h:168
unsigned int getDimension() const
Definition: vpServo.cpp:366
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
vpColVector secondaryTask(const vpColVector &de2dt, const bool &useLargeProjectionOperator=false)
Definition: vpServo.cpp:1089
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ DESIRED
Definition: vpServo.h:208
Class that defines the simplest robot: a free flying camera.
Implementation of a wire frame simulator. Compared to the vpSimulator class, it does not require thir...
vpHomogeneousMatrix getExternalCameraPosition() const
void setCameraPositionRelObj(const vpHomogeneousMatrix &cMo_)
void getInternalImage(vpImage< unsigned char > &I)
void initScene(const vpSceneObject &obj, const vpSceneDesiredObject &desiredObject)
void setExternalCameraPosition(const vpHomogeneousMatrix &cam_Mf)
void set_fMo(const vpHomogeneousMatrix &fMo_)
vpHomogeneousMatrix get_fMo() const
void setDesiredCameraPosition(const vpHomogeneousMatrix &cdMo_)
void setInternalCameraParameters(const vpCameraParameters &cam)
void setExternalCameraParameters(const vpCameraParameters &cam)
@ CYLINDER
A cylinder of 80cm length and 10cm radius.
void getExternalImage(vpImage< unsigned char > &I)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()