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