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