Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
servoSimuSphere2DCamVelocityDisplaySecondaryTask.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 sphere.
33  *
34 *****************************************************************************/
35 
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpHomogeneousMatrix.h>
51 #include <visp3/core/vpMath.h>
52 #include <visp3/core/vpSphere.h>
53 #include <visp3/gui/vpDisplayD3D.h>
54 #include <visp3/gui/vpDisplayGDI.h>
55 #include <visp3/gui/vpDisplayGTK.h>
56 #include <visp3/gui/vpDisplayOpenCV.h>
57 #include <visp3/gui/vpDisplayX.h>
58 #include <visp3/gui/vpProjectionDisplay.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/robot/vpSimulatorCamera.h>
61 #include <visp3/visual_features/vpFeatureBuilder.h>
62 #include <visp3/visual_features/vpFeatureEllipse.h>
63 #include <visp3/vs/vpServo.h>
64 #include <visp3/vs/vpServoDisplay.h>
65 
66 // List of allowed command line options
67 #define GETOPTARGS "cdho"
68 
69 #ifdef ENABLE_VISP_NAMESPACE
70 using namespace VISP_NAMESPACE_NAME;
71 #endif
72 
73 void usage(const char *name, const char *badparam);
74 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
75 
84 void usage(const char *name, const char *badparam)
85 {
86  fprintf(stdout, "\n\
87 Simulation of a 2D visual servoing on a sphere:\n\
88 - eye-in-hand control law,\n\
89 - velocity computed in the camera frame,\n\
90 - display the camera view,\n\
91 - a secondary task is the added.\n\
92  \n\
93 SYNOPSIS\n\
94  %s [-c] [-d] [-o] [-h]\n",
95  name);
96 
97  fprintf(stdout, "\n\
98 OPTIONS: Default\n\
99  \n\
100  -c\n\
101  Disable the mouse click. Useful to automate the \n\
102  execution of this program without human intervention.\n\
103  \n\
104  -d \n\
105  Turn off the display.\n\
106  \n\
107  -o \n\
108  Disable new projection operator usage for secondary task.\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 
130 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display, bool &new_proj_operator)
131 {
132  const char *optarg_;
133  int c;
134  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
135 
136  switch (c) {
137  case 'c':
138  click_allowed = false;
139  break;
140  case 'd':
141  display = false;
142  break;
143  case 'o':
144  new_proj_operator = false;
145  break;
146  case 'h':
147  usage(argv[0], nullptr);
148  return false;
149 
150  default:
151  usage(argv[0], optarg_);
152  return false;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], nullptr);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 int main(int argc, const char **argv)
168 {
169 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
170  try {
171  bool opt_display = true;
172  bool opt_click_allowed = true;
173  bool opt_new_proj_operator = true;
174 
175  // Read the command line options
176  if (getOptions(argc, argv, opt_click_allowed, opt_display, opt_new_proj_operator) == false) {
177  return (EXIT_FAILURE);
178  }
179 
180  vpImage<unsigned char> I(512, 512, 0);
181  vpImage<unsigned char> Iext(512, 512, 0);
182 
183  // We open a window if a display is available
184 #ifdef VISP_HAVE_DISPLAY
185 #if defined(VISP_HAVE_X11)
186  vpDisplayX displayI;
187  vpDisplayX displayExt;
188 #elif defined(VISP_HAVE_GTK)
189  vpDisplayGTK displayI;
190  vpDisplayGTK displayExt;
191 #elif defined(VISP_HAVE_GDI)
192  vpDisplayGDI displayI;
193  vpDisplayGDI displayExt;
194 #elif defined(HAVE_OPENCV_HIGHGUI)
195  vpDisplayOpenCV displayI;
196  vpDisplayOpenCV displayExt;
197 #elif defined(VISP_HAVE_D3D9)
198  vpDisplayD3D displayI;
199  vpDisplayD3D displayExt;
200 #endif
201 #endif
202 
203  if (opt_display) {
204 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
205  // Display size is automatically defined by the image (I) size
206  displayI.init(I, 100, 100, "Camera view...");
207  displayExt.init(Iext, 130 + static_cast<int>(I.getWidth()), 100, "External view");
208 #endif
209  // Display the image
210  // The image class has a member that specify a pointer toward
211  // the display that has been initialized in the display declaration
212  // therefore is is no longer necessary to make a reference to the
213  // display variable.
215  vpDisplay::display(Iext);
216  vpDisplay::flush(I);
217  vpDisplay::flush(Iext);
218  }
219 
220 #ifdef VISP_HAVE_DISPLAY
221  vpProjectionDisplay externalview;
222 #endif
223 
224  double px = 600, py = 600;
225  double u0 = I.getWidth() / 2., v0 = I.getHeight() / 2.;
226 
227  vpCameraParameters cam(px, py, u0, v0);
228 
229  vpServo task;
230  vpSimulatorCamera robot;
231 
232  // sets the initial camera location
234  cMo[0][3] = 0.1;
235  cMo[1][3] = 0.2;
236  cMo[2][3] = 2;
237  // Compute the position of the object in the world frame
238  vpHomogeneousMatrix wMc, wMo;
239  robot.getPosition(wMc);
240  wMo = wMc * cMo;
241 
242  vpHomogeneousMatrix cMod;
243  cMod[0][3] = 0;
244  cMod[1][3] = 0;
245  cMod[2][3] = 1;
246 
247  // sets the sphere coordinates in the world frame
248  vpSphere sphere;
249  sphere.setWorldCoordinates(0, 0, 0, 0.1);
250 
251 #ifdef VISP_HAVE_DISPLAY
252  externalview.insert(sphere);
253 #endif
254  // sets the desired position of the visual feature
255  vpFeatureEllipse pd;
256  sphere.track(cMod);
257  vpFeatureBuilder::create(pd, sphere);
258 
259  // computes the sphere coordinates in the camera frame and its 2D
260  // coordinates sets the current position of the visual feature
262  sphere.track(cMo);
263  vpFeatureBuilder::create(p, sphere);
264 
265  // define the task
266  // - we want an eye-in-hand control law
267  // - robot is controlled in the camera frame
269 
270  // we want to see a sphere on a sphere
271  std::cout << std::endl;
272  task.addFeature(p, pd);
273 
274  // set the gain
275  task.setLambda(1);
276 
277  // Set the point of view of the external view
278  vpHomogeneousMatrix cextMo(0, 0, 4, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
279 
280  // Display the initial scene
281  vpServoDisplay::display(task, cam, I);
282 #ifdef VISP_HAVE_DISPLAY
283  externalview.display(Iext, cextMo, cMo, cam, vpColor::red);
284 #endif
285  vpDisplay::flush(I);
286  vpDisplay::flush(Iext);
287 
288  // Display task information
289  task.print();
290 
291  if (opt_display && opt_click_allowed) {
292  vpDisplay::displayText(I, 20, 20, "Click to start visual servo...", vpColor::white);
293  vpDisplay::flush(I);
295  }
296 
297  unsigned int iter = 0;
298  bool stop = false;
299  bool start_secondary_task = false;
300 
301  // loop
302  while (iter++ < 2000 && !stop) {
303  std::cout << "---------------------------------------------" << iter << std::endl;
304 
305  // get the robot position
306  robot.getPosition(wMc);
307  // Compute the position of the object frame in the camera frame
308  cMo = wMc.inverse() * wMo;
309 
310  // new sphere position: retrieve x,y and Z of the vpSphere structure
311  sphere.track(cMo);
312  vpFeatureBuilder::create(p, sphere);
313 
314  if (opt_display) {
316  vpDisplay::display(Iext);
317  vpServoDisplay::display(task, cam, I);
318 #ifdef VISP_HAVE_DISPLAY
319  externalview.display(Iext, cextMo, cMo, cam, vpColor::red);
320 #endif
321  }
322 
323  // compute the control law
324  vpColVector v = task.computeControlLaw();
325 
326  // Wait primary task convergence before considering secondary task
327  if (task.getError().sumSquare() < 1e-6) {
328  start_secondary_task = true;
329  }
330 
331  if (start_secondary_task) {
332  // Only 3 dof are required to achieve primary task: vz, wx, wy
333  // It remains 3 free dof (vx, vy, wz) that could be used in a secondary task for example to move around the
334  // sphere
335  vpColVector de2dt(6);
336  de2dt[0] = 0.50; // vx = 0.50 m/s should also generate a motion on wy = (I-WpW)de2dt[4]
337  de2dt[1] = 0.25; // vy = 0.25 m/s should generate a motion on wx = (I-WpW)de2dt[3]
338  de2dt[2] = 1; // vz = 1 m/s should be zero in vz = (I-WpW)de2dt[2]
339  de2dt[5] = vpMath::rad(10); // wz = 10 rad/s should generate a motion on (I-WpW)de2dt[5]
340 
341  std::cout << "de2dt :" << de2dt.t() << std::endl;
342  vpColVector sec = task.secondaryTask(de2dt, opt_new_proj_operator);
343  std::cout << "(I-WpW)de2dt :" << sec.t() << std::endl;
344 
345  v += sec;
346 
347  if (opt_display && opt_click_allowed) {
348  std::stringstream ss;
349  ss << std::string("New projection operator: ") +
350  (opt_new_proj_operator ? std::string("yes (use option -o to use old one)") : std::string("no"));
351  vpDisplay::displayText(I, 20, 20, "Secondary task enabled: yes", vpColor::white);
352  vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::white);
353  }
354  }
355  else {
356  if (opt_display && opt_click_allowed) {
357  vpDisplay::displayText(I, 20, 20, "Secondary task enabled: no", vpColor::white);
358  }
359  }
360 
361  // send the camera velocity to the controller
363 
364  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
365 
366  if (opt_display) {
367  vpDisplay::displayText(I, 60, 20, "Click to stop visual servo...", vpColor::white);
368  if (vpDisplay::getClick(I, false)) {
369  stop = true;
370  }
371  vpDisplay::flush(I);
372  vpDisplay::flush(Iext);
373  }
374  }
375 
376  if (opt_display && opt_click_allowed) {
378  vpServoDisplay::display(task, cam, I);
379  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::white);
380  vpDisplay::flush(I);
382  }
383 
384  // Display task information
385  task.print();
386  return EXIT_SUCCESS;
387  }
388  catch (const vpException &e) {
389  std::cout << "Catch a ViSP exception: " << e << std::endl;
390  return EXIT_FAILURE;
391  }
392 #else
393  (void)argc;
394  (void)argv;
395  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
396  return EXIT_SUCCESS;
397 #endif
398  }
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
double sumSquare() const
vpRowVector t() const
static const vpColor white
Definition: vpColor.h:212
static const vpColor red
Definition: vpColor.h:217
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 2D ellipse visual feature.
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
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)
@ 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
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
Class that defines the simplest robot: a free flying camera.
Class that defines a 3D sphere in the object frame and allows forward projection of a 3D sphere in th...
Definition: vpSphere.h:80
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition: vpSphere.cpp:59