Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
servoSimuFourPoints2DPolarCamVelocityDisplay.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 using 4 points with polar
33  * coordinates as visual feature.
34  *
35 *****************************************************************************/
36 
53 #include <visp3/core/vpConfig.h>
54 #include <visp3/core/vpDebug.h>
55 
56 #if defined(VISP_HAVE_DISPLAY)
57 
58 #include <stdio.h>
59 #include <stdlib.h>
60 
61 #include <visp3/core/vpCameraParameters.h>
62 #include <visp3/core/vpHomogeneousMatrix.h>
63 #include <visp3/core/vpImage.h>
64 #include <visp3/core/vpImagePoint.h>
65 #include <visp3/core/vpIoTools.h>
66 #include <visp3/core/vpMath.h>
67 #include <visp3/core/vpMeterPixelConversion.h>
68 #include <visp3/gui/vpDisplayFactory.h>
69 #include <visp3/gui/vpProjectionDisplay.h>
70 #include <visp3/io/vpParseArgv.h>
71 #include <visp3/robot/vpSimulatorCamera.h>
72 #include <visp3/visual_features/vpFeatureBuilder.h>
73 #include <visp3/visual_features/vpFeaturePointPolar.h>
74 #include <visp3/vs/vpServo.h>
75 #include <visp3/vs/vpServoDisplay.h>
76 
77 // List of allowed command line options
78 #define GETOPTARGS "cdh"
79 
80 #ifdef ENABLE_VISP_NAMESPACE
81 using namespace VISP_NAMESPACE_NAME;
82 #endif
83 
84 void usage(const char *name, const char *badparam);
85 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
86 
95 void usage(const char *name, const char *badparam)
96 {
97  fprintf(stdout, "\n\
98 Tests a control law with the following characteristics:\n\
99 - eye-in-hand control\n\
100 - articular velocity are computed\n\
101 - servo on 4 points,\n\
102 - internal and external camera view displays.\n\
103 \n\
104 SYNOPSIS\n\
105  %s [-c] [-d] [-h]\n",
106  name);
107 
108  fprintf(stdout, "\n\
109 OPTIONS: Default\n\
110  -c\n\
111  Disable the mouse click. Useful to automate the \n\
112  execution of this program without human intervention.\n\
113 \n\
114  -d \n\
115  Turn off the display.\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 }
135 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
136 {
137  const char *optarg_;
138  int c;
139  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
140 
141  switch (c) {
142  case 'c':
143  click_allowed = false;
144  break;
145  case 'd':
146  display = false;
147  break;
148  case 'h':
149  usage(argv[0], nullptr);
150  return false;
151 
152  default:
153  usage(argv[0], optarg_);
154  return false;
155  }
156  }
157 
158  if ((c == 1) || (c == -1)) {
159  // standalone param or error
160  usage(argv[0], nullptr);
161  std::cerr << "ERROR: " << std::endl;
162  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
163  return false;
164  }
165 
166  return true;
167 }
168 
169 int main(int argc, const char **argv)
170 {
171  // We declare the windows variables to be able to free the memory in the catch sections if needed
172 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
173  std::shared_ptr<vpDisplay> displayInt;
174  std::shared_ptr<vpDisplay> displayExt;
175 #else
176  vpDisplay *displayInt = nullptr;
177  vpDisplay *displayExt = nullptr;
178 #endif
179  try {
180  // Log file creation in /tmp/$USERNAME/log.dat
181  // This file contains by line:
182  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
183  // - the 6 measured camera velocities (m/s, rad/s)
184  // - the 6 measured joint positions (m, rad)
185  // - the 8 values of s - s*
186  std::string username;
187  // Get the user login name
188  vpIoTools::getUserName(username);
189 
190  // Create a log filename to save velocities...
191  std::string logdirname;
192 #if defined(_WIN32)
193  logdirname = "C:/temp/" + username;
194 #else
195  logdirname = "/tmp/" + username;
196 #endif
197 
198  // Test if the output path exist. If no try to create it
199  if (vpIoTools::checkDirectory(logdirname) == false) {
200  try {
201  // Create the dirname
202  vpIoTools::makeDirectory(logdirname);
203  }
204  catch (...) {
205  std::cerr << std::endl << "ERROR:" << std::endl;
206  std::cerr << " Cannot create " << logdirname << std::endl;
207  return EXIT_FAILURE;
208  }
209  }
210  std::string logfilename;
211  logfilename = logdirname + "/log.dat";
212 
213  // Open the log file name
214  std::ofstream flog(logfilename.c_str());
215 
216  bool opt_click_allowed = true;
217  bool opt_display = true;
218 
219  // Read the command line options
220  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
221  return EXIT_FAILURE;
222  }
223 
224  // open a display for the visualization
225 
226  vpImage<unsigned char> Iint(300, 300, 0);
227  vpImage<unsigned char> Iext(300, 300, 0);
228 
229  if (opt_display) {
230  // We open two displays, one for the internal camera view, the other one for
231  // the external view
232 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
233  displayInt = vpDisplayFactory::createDisplay(Iint, 0, 0, "Internal view");
234  displayExt = vpDisplayFactory::createDisplay(Iext, 330, 000, "External view");
235 #else
236  displayInt = vpDisplayFactory::allocateDisplay(Iint, 0, 0, "Internal view");
237  displayExt = vpDisplayFactory::allocateDisplay(Iext, 330, 000, "External view");
238 #endif
239  }
240  vpProjectionDisplay externalview;
241 
242  double px = 500, py = 500;
243  double u0 = 150, v0 = 160;
244 
245  vpCameraParameters cam(px, py, u0, v0);
246 
247  vpServo task;
248  vpSimulatorCamera robot;
249 
250  std::cout << std::endl;
251  std::cout << "----------------------------------------------" << std::endl;
252  std::cout << " Test program for vpServo " << std::endl;
253  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
254  std::cout << " Simulation " << std::endl;
255  std::cout << " task : servo 4 points " << std::endl;
256  std::cout << "----------------------------------------------" << std::endl;
257  std::cout << std::endl;
258 
259 // #define TRANS_Z_PURE
260 // #define TRANS_X_PURE
261 // #define ROT_Z_PURE
262 // #define ROT_X_PURE
263 #define COMPLEX
264  //#define PROBLEM
265 
266 #if defined(TRANS_Z_PURE)
267  // sets the initial camera location
268  vpHomogeneousMatrix cMo(0, 0, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
269  // sets the desired camera location
270  vpHomogeneousMatrix cMod(0, 0, 2, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
271 #elif defined(TRANS_X_PURE)
272  // sets the initial camera location
273  vpHomogeneousMatrix cMo(0.3, 0.3, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
274  // sets the desired camera location
275  vpHomogeneousMatrix cMod(0.5, 0.3, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
276 
277 #elif defined(ROT_Z_PURE)
278  // sets the initial camera location
279  vpHomogeneousMatrix cMo(0, 0, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
280  // sets the desired camera location
281  vpHomogeneousMatrix cMod(0, 0, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(180));
282 
283 #elif defined(ROT_X_PURE)
284  // sets the initial camera location
285  vpHomogeneousMatrix cMo(0, 0, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
286  // sets the desired camera location
287  vpHomogeneousMatrix cMod(0, 0, 3, vpMath::rad(45), vpMath::rad(0), vpMath::rad(0));
288 
289 #elif defined(COMPLEX)
290  // sets the initial camera location
291  vpHomogeneousMatrix cMo(0.2, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
292  // sets the desired camera location
293  vpHomogeneousMatrix cMod(0, 0, 2.5, vpMath::rad(45), vpMath::rad(10), vpMath::rad(30));
294 
295 #elif defined(PROBLEM)
296  // Bad behavior with an interaction matrix computed from the desired
297  // features sets the initial camera location
298  vpHomogeneousMatrix cMo(0.2, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
299  // sets the desired camera location
300  vpHomogeneousMatrix cMod(0.4, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
301 
302 #endif
303  // Compute the position of the object in the world frame
304  vpHomogeneousMatrix wMc, wMo;
305  robot.getPosition(wMc);
306  wMo = wMc * cMo;
307 
308  vpHomogeneousMatrix cextMo(0, 0, 6, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
309 
310  // sets the point coordinates in the object frame
311  vpPoint point[4];
312  point[0].setWorldCoordinates(-0.25, -0.25, 0);
313  point[1].setWorldCoordinates(0.25, -0.25, 0);
314  point[2].setWorldCoordinates(0.25, 0.25, 0);
315  point[3].setWorldCoordinates(-0.25, 0.25, 0);
316 
317  for (unsigned int i = 0; i < 4; i++)
318  externalview.insert(point[i]);
319 
320  // sets the desired position of the feature point s*"
321  vpFeaturePointPolar pd[4];
322 
323  // computes the point coordinates in the desired camera frame and
324  // its 2D coordinates
325  for (unsigned int i = 0; i < 4; i++) {
326  point[i].track(cMod);
327  // Computes the polar coordinates from the image point
328  // cartesian coordinates
329  vpFeatureBuilder::create(pd[i], point[i]);
330  }
331 
332  // computes the point coordinates in the camera frame and its 2D
333  // coordinates
334  for (unsigned int i = 0; i < 4; i++)
335  point[i].track(cMo);
336 
337  // sets the desired position of the point
338  vpFeaturePointPolar p[4];
339  for (unsigned int i = 0; i < 4; i++) {
340  // retrieve x,y and Z of the vpPoint structure to initialize the
341  // visual feature
342  vpFeatureBuilder::create(p[i], point[i]);
343  }
344 
345  // Define the task;
346  // - we want an eye-in-hand control law
347  // - articular velocity are computed
349  // task.setInteractionMatrixType(vpServo::MEAN) ;
350  // task.setInteractionMatrixType(vpServo::DESIRED) ;
352 
353  // Set the position of the end-effector frame in the camera frame as identity
355  vpVelocityTwistMatrix cVe(cMe);
356  task.set_cVe(cVe);
357 
358  // Set the Jacobian (expressed in the end-effector frame)
359  vpMatrix eJe;
360  robot.get_eJe(eJe);
361  task.set_eJe(eJe);
362 
363  // we want to see a point on a point
364  for (unsigned int i = 0; i < 4; i++)
365  task.addFeature(p[i], pd[i]);
366 
367  // set the gain
368  task.setLambda(1);
369 
370  std::cout << "\nDisplay task information: " << std::endl;
371  task.print();
372 
373  unsigned int iter = 0;
374  // loop
375  while (iter++ < 200) {
376  std::cout << "---------------------------------------------" << iter << std::endl;
377  vpColVector v;
378 
379  // Set the Jacobian (expressed in the end-effector frame)
380  // Since q is modified eJe is modified
381  robot.get_eJe(eJe);
382  task.set_eJe(eJe);
383 
384  // get the robot position
385  robot.getPosition(wMc);
386  // Compute the position of the object frame in the camera frame
387  cMo = wMc.inverse() * wMo;
388 
389  // Compute new point position
390  for (unsigned int i = 0; i < 4; i++) {
391  point[i].track(cMo);
392  // retrieve x,y and Z of the vpPoint structure to compute the feature
393  vpFeatureBuilder::create(p[i], point[i]);
394  }
395 
396  if (opt_display) {
397  vpDisplay::display(Iint);
398  vpDisplay::display(Iext);
399 
400  vpServoDisplay::display(task, cam, Iint);
401  externalview.display(Iext, cextMo, cMo, cam, vpColor::green);
402  vpDisplay::flush(Iint);
403  vpDisplay::flush(Iext);
404  }
405 
406  // Compute the control law
407  v = task.computeControlLaw();
408 
409  if (iter == 1) {
410  std::cout << "Display task information: " << std::endl;
411  task.print();
412  }
413 
416 
417  // Send the camera velocity to the controller
419  // Save velocities applied to the robot in the log file
420  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
421  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
422  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
423 
424  std::cout << "v: " << v.t() << std::endl;
425 
426  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
427 
428  // Save feature error (s-s*) for the 4 feature points. For each feature
429  // point, we have 2 errors (along x and y axis). This error is
430  // expressed in meters in the camera frame
431  flog << (task.getError()).t() << " "; // s-s* for point 4
432  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
433 
434  // Save current visual feature s = (rho,theta)
435  for (unsigned int i = 0; i < 4; i++) {
436  flog << p[i].get_rho() << " " << p[i].get_theta() << " ";
437  }
438  // Save current position of the points
439  for (unsigned int i = 0; i < 4; i++) {
440  flog << point[i].get_x() << " " << point[i].get_y() << " ";
441  }
442  flog << std::endl;
443 
444  if (iter == 1) {
445  vpImagePoint ip;
446  ip.set_i(10);
447  ip.set_j(10);
448 
449  std::cout << "\nClick in the internal camera view to continue..." << std::endl;
450  vpDisplay::displayText(Iint, ip, "A click to continue...", vpColor::red);
451  vpDisplay::flush(Iint);
452  vpDisplay::getClick(Iint);
453  }
454  }
455 
456  flog.close(); // Close the log file
457 
458  // Display task information
459  task.print();
460 
461  // Kill the task
462 
463  std::cout << "Final robot position with respect to the object frame:\n";
464  cMo.print();
465 
466  if (opt_display && opt_click_allowed) {
467  vpDisplay::displayText(Iint, 20, 20, "Click to quit...", vpColor::white);
468  vpDisplay::flush(Iint);
469  vpDisplay::getClick(Iint);
470  }
471 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
472  if (displayInt != nullptr) {
473  delete displayInt;
474  }
475  if (displayExt != nullptr) {
476  delete displayExt;
477  }
478 #endif
479  return EXIT_SUCCESS;
480  }
481  catch (const vpException &e) {
482  std::cout << "Catch a ViSP exception: " << e << std::endl;
483 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
484  if (displayInt != nullptr) {
485  delete displayInt;
486  }
487  if (displayExt != nullptr) {
488  delete displayExt;
489  }
490 #endif
491  return EXIT_FAILURE;
492 }
493 }
494 #else
495 int main()
496 {
497  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
498  << std::endl;
499  std::cout << "Tip if you are on a unix-like system:" << std::endl;
500  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
501  std::cout << "Tip if you are on a windows-like system:" << std::endl;
502  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
503  return EXIT_SUCCESS;
504 }
505 #endif
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpRowVector t() const
static const vpColor white
Definition: vpColor.h:193
static const vpColor red
Definition: vpColor.h:198
static const vpColor green
Definition: vpColor.h:201
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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 image point visual feature with polar coordinates described in .
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_j(double jj)
Definition: vpImagePoint.h:309
void set_i(double ii)
Definition: vpImagePoint.h:298
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string getUserName()
Definition: vpIoTools.cpp:285
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
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
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:426
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:424
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
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 get_eJe(vpMatrix &eJe) VP_OVERRIDE
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_L_cVe_eJe
Definition: vpServo.h:168
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:1043
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:991
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:1106
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:515
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ FEATURE_CURRENT
Print the current features .
Definition: vpServo.h:246
@ FEATURE_DESIRED
Print the desired features .
Definition: vpServo.h:247
@ CURRENT
Definition: vpServo.h:202
Class that defines the simplest robot: a free flying camera.
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.