Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
servoSimuCylinder2DCamVelocityDisplaySecondaryTask.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  * Simulation of a 2D visual servoing on a cylinder.
33  *
34 *****************************************************************************/
51 #include <iostream>
52 #include <stdio.h>
53 #include <stdlib.h>
54 
55 #include <visp3/core/vpCameraParameters.h>
56 #include <visp3/core/vpConfig.h>
57 #include <visp3/core/vpCylinder.h>
58 #include <visp3/core/vpHomogeneousMatrix.h>
59 #include <visp3/core/vpImage.h>
60 #include <visp3/core/vpMath.h>
61 #include <visp3/gui/vpDisplayD3D.h>
62 #include <visp3/gui/vpDisplayGDI.h>
63 #include <visp3/gui/vpDisplayGTK.h>
64 #include <visp3/gui/vpDisplayOpenCV.h>
65 #include <visp3/gui/vpDisplayX.h>
66 #include <visp3/gui/vpProjectionDisplay.h>
67 #include <visp3/io/vpParseArgv.h>
68 #include <visp3/robot/vpSimulatorCamera.h>
69 #include <visp3/visual_features/vpFeatureBuilder.h>
70 #include <visp3/visual_features/vpFeatureLine.h>
71 #include <visp3/vs/vpServo.h>
72 #include <visp3/vs/vpServoDisplay.h>
73 
74 // List of allowed command line options
75 #define GETOPTARGS "cdho"
76 
77 #ifdef ENABLE_VISP_NAMESPACE
78 using namespace VISP_NAMESPACE_NAME;
79 #endif
80 
81 void usage(const char *name, const char *badparam);
82 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
83 
92 void usage(const char *name, const char *badparam)
93 {
94  fprintf(stdout, "\n\
95 Simulation of a 2D visual servoing on a cylinder:\n\
96 - eye-in-hand control law,\n\
97 - velocity computed in the camera frame,\n\
98 - display the camera view.\n\
99  \n\
100 SYNOPSIS\n\
101  %s [-c] [-d] [-o] [-h]\n",
102  name);
103 
104  fprintf(stdout, "\n\
105 OPTIONS: Default\n\
106  \n\
107  -c\n\
108  Disable the mouse click. Useful to automate the \n\
109  execution of this program without human intervention.\n\
110  \n\
111  -d \n\
112  Turn off the display.\n\
113  \n\
114  -o \n\
115  Disable new projection operator usage for secondary task.\n\
116  \n\
117  -h\n\
118  Print the help.\n");
119 
120  if (badparam)
121  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
122 }
123 
136 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display, bool &new_proj_operator)
137 {
138  const char *optarg_;
139  int c;
140  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
141 
142  switch (c) {
143  case 'c':
144  click_allowed = false;
145  break;
146  case 'd':
147  display = false;
148  break;
149  case 'o':
150  new_proj_operator = false;
151  break;
152  case 'h':
153  usage(argv[0], nullptr);
154  return false;
155 
156  default:
157  usage(argv[0], optarg_);
158  return false;
159  }
160  }
161 
162  if ((c == 1) || (c == -1)) {
163  // standalone param or error
164  usage(argv[0], nullptr);
165  std::cerr << "ERROR: " << std::endl;
166  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
167  return false;
168  }
169 
170  return true;
171 }
172 
173 int main(int argc, const char **argv)
174 {
175 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
176  try {
177  bool opt_display = true;
178  bool opt_click_allowed = true;
179  bool opt_new_proj_operator = true;
180 
181  // Read the command line options
182  if (getOptions(argc, argv, opt_click_allowed, opt_display, opt_new_proj_operator) == false) {
183  return EXIT_FAILURE;
184  }
185 
186  vpImage<unsigned char> Iint(512, 512, 0);
187  vpImage<unsigned char> Iext(512, 512, 0);
188 
189 // We open a window if a display is available
190 #ifdef VISP_HAVE_DISPLAY
191 #if defined(VISP_HAVE_X11)
192  vpDisplayX displayInt;
193  vpDisplayX displayExt;
194 #elif defined(VISP_HAVE_GTK)
195  vpDisplayGTK displayInt;
196  vpDisplayGTK displayExt;
197 #elif defined(VISP_HAVE_GDI)
198  vpDisplayGDI displayInt;
199  vpDisplayGDI displayExt;
200 #elif defined(HAVE_OPENCV_HIGHGUI)
201  vpDisplayOpenCV displayInt;
202  vpDisplayOpenCV displayExt;
203 #elif defined(VISP_HAVE_D3D9)
204  vpDisplayD3D displayInt;
205  vpDisplayD3D displayExt;
206 #endif
207 #endif
208 
209  if (opt_display) {
210 #ifdef VISP_HAVE_DISPLAY
211  // Display size is automatically defined by the image (Iint) and
212  // (Iext) size
213  displayInt.init(Iint, 100, 100, "Internal view");
214  displayExt.init(Iext, 130 + static_cast<int>(Iint.getWidth()), 100, "External view");
215 #endif
216  // Display the image
217  // The image class has a member that specify a pointer toward
218  // the display that has been initialized in the display declaration
219  // therefore is is no longer necessary to make a reference to the
220  // display variable.
221  vpDisplay::display(Iint);
222  vpDisplay::display(Iext);
223  vpDisplay::flush(Iint);
224  vpDisplay::flush(Iext);
225  }
226 
227 #ifdef VISP_HAVE_DISPLAY
228  vpProjectionDisplay externalview;
229 #endif
230 
231  // Set the camera parameters
232  double px, py;
233  px = py = 600;
234  double u0, v0;
235  u0 = v0 = 256;
236 
237  vpCameraParameters cam(px, py, u0, v0);
238 
239  vpServo task;
240  vpSimulatorCamera robot;
241 
242  // sets the initial camera location
243  vpHomogeneousMatrix cMo(-0.2, 0.1, 2, vpMath::rad(5), vpMath::rad(5), vpMath::rad(20));
244 
245  vpHomogeneousMatrix wMc, wMo;
246  robot.getPosition(wMc);
247  wMo = wMc * cMo; // Compute the position of the object in the world frame
248 
249  // sets the final camera location (for simulation purpose)
250  vpHomogeneousMatrix cMod(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
251 
252  // sets the cylinder coordinates in the world frame
253  vpCylinder cylinder(0, 1, 0, // direction
254  0, 0, 0, // point of the axis
255  0.1); // radius
256 
257 #ifdef VISP_HAVE_DISPLAY
258  externalview.insert(cylinder);
259 #endif
260  // sets the desired position of the visual feature
261  cylinder.track(cMod);
262  cylinder.print();
263 
264  // Build the desired line features thanks to the cylinder and especially
265  // its paramaters in the image frame
266  vpFeatureLine ld[2];
267  for (unsigned int i = 0; i < 2; i++)
268  vpFeatureBuilder::create(ld[i], cylinder, i);
269 
270  // computes the cylinder coordinates in the camera frame and its 2D
271  // coordinates sets the current position of the visual feature
272  cylinder.track(cMo);
273  cylinder.print();
274 
275  // Build the current line features thanks to the cylinder and especially
276  // its paramaters in the image frame
277  vpFeatureLine l[2];
278  for (unsigned int i = 0; i < 2; i++) {
279  vpFeatureBuilder::create(l[i], cylinder, i);
280  l[i].print();
281  }
282 
283  // define the task
284  // - we want an eye-in-hand control law
285  // - robot is controlled in the camera frame
288  // it can also be interesting to test these possibilities
289  // task.setInteractionMatrixType(vpServo::CURRENT,vpServo::PSEUDO_INVERSE)
290  // ; task.setInteractionMatrixType(vpServo::MEAN, vpServo::PSEUDO_INVERSE)
291  // ; task.setInteractionMatrixType(vpServo::CURRENT,
292  // vpServo::PSEUDO_INVERSE) ;
293  // task.setInteractionMatrixType(vpServo::DESIRED, vpServo::TRANSPOSE) ;
294  // task.setInteractionMatrixType(vpServo::CURRENT, vpServo::TRANSPOSE) ;
295 
296  // we want to see 2 lines on 2 lines
297  task.addFeature(l[0], ld[0]);
298  task.addFeature(l[1], ld[1]);
299 
300  // Set the point of view of the external view
301  vpHomogeneousMatrix cextMo(0, 0, 6, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
302 
303  // Display the initial scene
304  vpServoDisplay::display(task, cam, Iint);
305 #ifdef VISP_HAVE_DISPLAY
306  externalview.display(Iext, cextMo, cMo, cam, vpColor::red);
307 #endif
308  vpDisplay::flush(Iint);
309  vpDisplay::flush(Iext);
310 
311  // Display task information
312  task.print();
313 
314  if (opt_display && opt_click_allowed) {
315  vpDisplay::displayText(Iint, 20, 20, "Click to start visual servo...", vpColor::white);
316  vpDisplay::flush(Iint);
317  vpDisplay::getClick(Iint);
318  }
319 
320  // set the gain
321  task.setLambda(1);
322 
323  // Display task information
324  task.print();
325 
326  unsigned int iter = 0;
327  bool stop = false;
328  bool start_secondary_task = false;
329 
330  while (!stop) {
331  std::cout << "---------------------------------------------" << iter++ << std::endl;
332 
333  // get the robot position
334  robot.getPosition(wMc);
335  // Compute the position of the object frame in the camera frame
336  cMo = wMc.inverse() * wMo;
337 
338  // new line position
339  // retrieve x,y and Z of the vpLine structure
340  // Compute the parameters of the cylinder in the camera frame and in the
341  // image frame
342  cylinder.track(cMo);
343 
344  // Build the current line features thanks to the cylinder and especially
345  // its paramaters in the image frame
346  for (unsigned int i = 0; i < 2; i++) {
347  vpFeatureBuilder::create(l[i], cylinder, i);
348  }
349 
350  // Display the current scene
351  if (opt_display) {
352  vpDisplay::display(Iint);
353  vpDisplay::display(Iext);
354  vpServoDisplay::display(task, cam, Iint);
355 #ifdef VISP_HAVE_DISPLAY
356  externalview.display(Iext, cextMo, cMo, cam, vpColor::red);
357 #endif
358  }
359 
360  // compute the control law
361  vpColVector v = task.computeControlLaw();
362 
363  // Wait primary task convergence before considering secondary task
364  if (task.getError().sumSquare() < 1e-6) {
365  start_secondary_task = true;
366  }
367 
368  if (start_secondary_task) {
369  // In this example the secondary task is cut in four
370  // steps. The first one consists in imposing a movement of the robot along
371  // the x axis of the object frame with a velocity of 0.5. The second one
372  // consists in imposing a movement of the robot along the y axis of the
373  // object frame with a velocity of 0.5. The third one consists in imposing a
374  // movement of the robot along the x axis of the object frame with a
375  // velocity of -0.5. The last one consists in imposing a movement of the
376  // robot along the y axis of the object frame with a velocity of -0.5.
377  // Each steps is made during 200 iterations.
378  vpColVector e1(6);
379  vpColVector e2(6);
380  vpColVector proj_e1;
381  vpColVector proj_e2;
382  static unsigned int iter_sec = 0;
383  double rapport = 0;
384  double vitesse = 0.5;
385  unsigned int tempo = 800;
386 
387  if (iter_sec > tempo) {
388  stop = true;
389  }
390 
391  if (iter_sec % tempo < 200) {
392  e2 = 0;
393  e1[0] = fabs(vitesse);
394  proj_e1 = task.secondaryTask(e1, opt_new_proj_operator);
395  rapport = vitesse / proj_e1[0];
396  proj_e1 *= rapport;
397  v += proj_e1;
398  }
399 
400  if (iter_sec % tempo < 400 && iter_sec % tempo >= 200) {
401  e1 = 0;
402  e2[1] = fabs(vitesse);
403  proj_e2 = task.secondaryTask(e2, opt_new_proj_operator);
404  rapport = vitesse / proj_e2[1];
405  proj_e2 *= rapport;
406  v += proj_e2;
407  }
408 
409  if (iter_sec % tempo < 600 && iter_sec % tempo >= 400) {
410  e2 = 0;
411  e1[0] = -fabs(vitesse);
412  proj_e1 = task.secondaryTask(e1, opt_new_proj_operator);
413  rapport = -vitesse / proj_e1[0];
414  proj_e1 *= rapport;
415  v += proj_e1;
416  }
417 
418  if (iter_sec % tempo < 800 && iter_sec % tempo >= 600) {
419  e1 = 0;
420  e2[1] = -fabs(vitesse);
421  proj_e2 = task.secondaryTask(e2, opt_new_proj_operator);
422  rapport = -vitesse / proj_e2[1];
423  proj_e2 *= rapport;
424  v += proj_e2;
425  }
426 
427  if (opt_display && opt_click_allowed) {
428  std::stringstream ss;
429  ss << std::string("New projection operator: ") +
430  (opt_new_proj_operator ? std::string("yes (use option -o to use old one)") : std::string("no"));
431  vpDisplay::displayText(Iint, 20, 20, "Secondary task enabled: yes", vpColor::white);
432  vpDisplay::displayText(Iint, 40, 20, ss.str(), vpColor::white);
433  }
434 
435  iter_sec++;
436  }
437  else {
438  if (opt_display && opt_click_allowed) {
439  vpDisplay::displayText(Iint, 20, 20, "Secondary task: no", vpColor::white);
440  }
441  }
442 
443  // send the camera velocity to the controller
445 
446  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
447 
448  if (opt_display) {
449  vpDisplay::displayText(Iint, 60, 20, "Click to stop visual servo...", vpColor::white);
450  if (vpDisplay::getClick(Iint, false)) {
451  stop = true;
452  }
453  vpDisplay::flush(Iint);
454  vpDisplay::flush(Iext);
455  }
456 
457  iter++;
458  }
459 
460  if (opt_display && opt_click_allowed) {
461  vpDisplay::display(Iint);
462  vpServoDisplay::display(task, cam, Iint);
463  vpDisplay::displayText(Iint, 20, 20, "Click to quit...", vpColor::white);
464  vpDisplay::flush(Iint);
465  vpDisplay::getClick(Iint);
466  }
467 
468  // Display task information
469  task.print();
470  return EXIT_SUCCESS;
471  }
472  catch (const vpException &e) {
473  std::cout << "Catch a ViSP exception: " << e << std::endl;
474  return EXIT_FAILURE;
475  }
476 #else
477  (void)argc;
478  (void)argv;
479  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
480  return EXIT_SUCCESS;
481 #endif
482  }
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
double sumSquare() const
static const vpColor white
Definition: vpColor.h:212
static const vpColor red
Definition: vpColor.h:217
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...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") VP_OVERRIDE
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
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 vpImagePoint &t)
Class that defines a 2D line visual feature which is composed by two parameters that are and ,...
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:129
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
interface with the image for feature display
void insert(vpForwardProjection &fp)
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &color, const bool &displayTraj=false, unsigned int thickness=1)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
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 print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:986
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
@ PSEUDO_INVERSE
Definition: vpServo.h:235
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ DESIRED
Definition: vpServo.h:208
Class that defines the simplest robot: a free flying camera.