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