Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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", name);
108 
109  fprintf(stdout, "\n\
110 OPTIONS: Default\n\
111  -c\n\
112  Disable the mouse click. Useful to automaze the \n\
113  execution of this program without humain intervention.\n\
114 \n\
115  -d \n\
116  Turn off the display.\n\
117 \n\
118  -h\n\
119  Print the help.\n");
120 
121  if (badparam)
122  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
123 }
136 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
137 {
138  const char *optarg_;
139  int c;
140  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
141 
142  switch (c) {
143  case 'c':
144  click_allowed = false;
145  break;
146  case 'd':
147  display = false;
148  break;
149  case 'h':
150  usage(argv[0], NULL);
151  return false;
152  break;
153 
154  default:
155  usage(argv[0], optarg_);
156  return false;
157  break;
158  }
159  }
160 
161  if ((c == 1) || (c == -1)) {
162  // standalone param or error
163  usage(argv[0], NULL);
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 mesured camera velocities (m/s, rad/s)
179  // - the 6 mesured 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  } catch (...) {
199  std::cerr << std::endl << "ERROR:" << std::endl;
200  std::cerr << " Cannot create " << logdirname << std::endl;
201  exit(-1);
202  }
203  }
204  std::string logfilename;
205  logfilename = logdirname + "/log.dat";
206 
207  // Open the log file name
208  std::ofstream flog(logfilename.c_str());
209 
210  bool opt_click_allowed = true;
211  bool opt_display = true;
212 
213  // Read the command line options
214  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
215  exit(-1);
216  }
217 
218 // We open two displays, one for the internal camera view, the other one for
219 // the external view, using either X11, GTK or GDI.
220 #if defined VISP_HAVE_X11
221  vpDisplayX displayInt;
222  vpDisplayX displayExt;
223 #elif defined VISP_HAVE_GTK
224  vpDisplayGTK displayInt;
225  vpDisplayGTK displayExt;
226 #elif defined VISP_HAVE_GDI
227  vpDisplayGDI displayInt;
228  vpDisplayGDI displayExt;
229 #elif defined VISP_HAVE_OPENCV
230  vpDisplayOpenCV displayInt;
231  vpDisplayOpenCV displayExt;
232 #endif
233 
234  // open a display for the visualization
235 
236  vpImage<unsigned char> Iint(300, 300, 0);
237  vpImage<unsigned char> Iext(300, 300, 0);
238 
239  if (opt_display) {
240  displayInt.init(Iint, 0, 0, "Internal view");
241  displayExt.init(Iext, 330, 000, "External view");
242  }
243  vpProjectionDisplay externalview;
244 
245  double px, py;
246  px = py = 500;
247  double u0, v0;
248  u0 = 150, v0 = 160;
249 
250  vpCameraParameters cam(px, py, u0, v0);
251 
252  int i;
253  vpServo task;
254  vpSimulatorCamera robot;
255 
256  std::cout << std::endl;
257  std::cout << "----------------------------------------------" << std::endl;
258  std::cout << " Test program for vpServo " << std::endl;
259  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
260  std::cout << " Simulation " << std::endl;
261  std::cout << " task : servo 4 points " << std::endl;
262  std::cout << "----------------------------------------------" << std::endl;
263  std::cout << std::endl;
264 
265 // #define TRANS_Z_PURE
266 // #define TRANS_X_PURE
267 // #define ROT_Z_PURE
268 // #define ROT_X_PURE
269 #define COMPLEX
270 //#define PROBLEM
271 
272 #if defined(TRANS_Z_PURE)
273  // sets the initial camera location
274  vpHomogeneousMatrix cMo(0, 0, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
275  // sets the desired camera location
276  vpHomogeneousMatrix cMod(0, 0, 2, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
277 #elif defined(TRANS_X_PURE)
278  // sets the initial camera location
279  vpHomogeneousMatrix cMo(0.3, 0.3, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
280  // sets the desired camera location
281  vpHomogeneousMatrix cMod(0.5, 0.3, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
282 
283 #elif defined(ROT_Z_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(0), vpMath::rad(0), vpMath::rad(180));
288 
289 #elif defined(ROT_X_PURE)
290  // sets the initial camera location
291  vpHomogeneousMatrix cMo(0, 0, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
292  // sets the desired camera location
293  vpHomogeneousMatrix cMod(0, 0, 3, vpMath::rad(45), vpMath::rad(0), vpMath::rad(0));
294 
295 #elif defined(COMPLEX)
296  // sets the initial camera location
297  vpHomogeneousMatrix cMo(0.2, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
298  // sets the desired camera location
299  vpHomogeneousMatrix cMod(0, 0, 2.5, vpMath::rad(45), vpMath::rad(10), vpMath::rad(30));
300 
301 #elif defined(PROBLEM)
302  // Bad behavior with an interaction matrix computed from the desired
303  // features sets the initial camera location
304  vpHomogeneousMatrix cMo(0.2, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
305  // sets the desired camera location
306  vpHomogeneousMatrix cMod(0.4, 0.2, 3, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
307 
308 #endif
309  // Compute the position of the object in the world frame
310  vpHomogeneousMatrix wMc, wMo;
311  robot.getPosition(wMc);
312  wMo = wMc * cMo;
313 
314  vpHomogeneousMatrix cextMo(0, 0, 6, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
315 
316  // sets the point coordinates in the object frame
317  vpPoint point[4];
318  point[0].setWorldCoordinates(-0.25, -0.25, 0);
319  point[1].setWorldCoordinates(0.25, -0.25, 0);
320  point[2].setWorldCoordinates(0.25, 0.25, 0);
321  point[3].setWorldCoordinates(-0.25, 0.25, 0);
322 
323  for (i = 0; i < 4; i++)
324  externalview.insert(point[i]);
325 
326  // sets the desired position of the feature point s*"
327  vpFeaturePointPolar pd[4];
328 
329  // computes the point coordinates in the desired camera frame and
330  // its 2D coordinates
331  for (i = 0; i < 4; i++) {
332  point[i].track(cMod);
333  // Computes the polar coordinates from the image point
334  // cartesian coordinates
335  vpFeatureBuilder::create(pd[i], point[i]);
336  }
337 
338  // computes the point coordinates in the camera frame and its 2D
339  // coordinates
340  for (i = 0; i < 4; i++)
341  point[i].track(cMo);
342 
343  // sets the desired position of the point
344  vpFeaturePointPolar p[4];
345  for (i = 0; i < 4; i++) {
346  // retrieve x,y and Z of the vpPoint structure to initialize the
347  // visual feature
348  vpFeatureBuilder::create(p[i], point[i]);
349  }
350 
351  // Define the task;
352  // - we want an eye-in-hand control law
353  // - articular velocity are computed
355  // task.setInteractionMatrixType(vpServo::MEAN) ;
356  // task.setInteractionMatrixType(vpServo::DESIRED) ;
358 
359  // Set the position of the camera in the end-effector frame
361  vpVelocityTwistMatrix cVe(cMe);
362  task.set_cVe(cVe);
363 
364  // Set the Jacobian (expressed in the end-effector frame)
365  vpMatrix eJe;
366  robot.get_eJe(eJe);
367  task.set_eJe(eJe);
368 
369  // we want to see a point on a point
370  for (i = 0; i < 4; i++)
371  task.addFeature(p[i], pd[i]);
372 
373  // set the gain
374  task.setLambda(1);
375 
376  std::cout << "\nDisplay task information: " << std::endl;
377  task.print();
378 
379  unsigned int iter = 0;
380  // loop
381  while (iter++ < 200) {
382  std::cout << "---------------------------------------------" << iter << std::endl;
383  vpColVector v;
384 
385  // Set the Jacobian (expressed in the end-effector frame)
386  // Since q is modified eJe is modified
387  robot.get_eJe(eJe);
388  task.set_eJe(eJe);
389 
390  // get the robot position
391  robot.getPosition(wMc);
392  // Compute the position of the camera wrt the object frame
393  cMo = wMc.inverse() * wMo;
394 
395  // Compute new point position
396  for (i = 0; i < 4; i++) {
397  point[i].track(cMo);
398  // retrieve x,y and Z of the vpPoint structure to compute the feature
399  vpFeatureBuilder::create(p[i], point[i]);
400  }
401 
402  if (opt_display) {
403  vpDisplay::display(Iint);
404  vpDisplay::display(Iext);
405 
406  vpServoDisplay::display(task, cam, Iint);
407  externalview.display(Iext, cextMo, cMo, cam, vpColor::green);
408  vpDisplay::flush(Iint);
409  vpDisplay::flush(Iext);
410  }
411 
412  // Compute the control law
413  v = task.computeControlLaw();
414 
415  if (iter == 1) {
416  std::cout << "Display task information: " << std::endl;
417  task.print();
418  }
419 
422 
423  // Send the camera velocity to the controller
425  // Save velocities applied to the robot in the log file
426  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
427  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
428  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
429 
430  std::cout << "v: " << v.t() << std::endl;
431 
432  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
433 
434  // Save feature error (s-s*) for the 4 feature points. For each feature
435  // point, we have 2 errors (along x and y axis). This error is
436  // expressed in meters in the camera frame
437  flog << (task.getError()).t() << " "; // s-s* for point 4
438  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
439 
440  // Save current visual feature s = (rho,theta)
441  for (i = 0; i < 4; i++) {
442  flog << p[i].get_rho() << " " << p[i].get_theta() << " ";
443  }
444  // Save current position of the points
445  for (i = 0; i < 4; i++) {
446  flog << point[i].get_x() << " " << point[i].get_y() << " ";
447  }
448  flog << std::endl;
449 
450  if (iter == 1) {
451  vpImagePoint ip;
452  ip.set_i(10);
453  ip.set_j(10);
454 
455  std::cout << "\nClick in the internal camera view to continue..." << std::endl;
456  vpDisplay::displayText(Iint, ip, "A click to continue...", vpColor::red);
457  vpDisplay::flush(Iint);
458  vpDisplay::getClick(Iint);
459  }
460  }
461 
462  flog.close(); // Close the log file
463 
464  // Display task information
465  task.print();
466 
467  // Kill the task
468  task.kill();
469 
470  std::cout << "Final robot position with respect to the object frame:\n";
471  cMo.print();
472 
473  if (opt_display && opt_click_allowed) {
474  // suppressed for automate test
475  std::cout << "\n\nClick in the internal view to end..." << std::endl;
476  vpDisplay::getClick(Iint);
477  }
478  return EXIT_SUCCESS;
479  } catch (const vpException &e) {
480  std::cout << "Catch a ViSP exception: " << e << std::endl;
481  return EXIT_FAILURE;
482  }
483 }
484 #else
485 int main()
486 {
487  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..." << std::endl;
488  std::cout << "Tip if you are on a unix-like system:" << std::endl;
489  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
490  std::cout << "Tip if you are on a windows-like system:" << std::endl;
491  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
492  return EXIT_SUCCESS;
493 }
494 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:467
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &color, const bool &displayTraj=false, const unsigned int thickness=1)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:508
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void track(const vpHomogeneousMatrix &cMo)
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:431
static const vpColor green
Definition: vpColor.h:183
static void flush(const vpImage< unsigned char > &I)
Class that defines 2D image point visual feature with polar coordinates described in ...
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:180
Class that defines what is a point.
Definition: vpPoint.h:58
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:597
void set_i(const double ii)
Definition: vpImagePoint.h:167
void kill()
Definition: vpServo.cpp:192
vpColVector getError() const
Definition: vpServo.h:282
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
vpRowVector t() const
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:406
static std::string getUserName()
Definition: vpIoTools.cpp:298
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:429
void insert(vpForwardProjection &fp)
vpHomogeneousMatrix getPosition() const
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
static double rad(double deg)
Definition: vpMath.h:102
void set_j(const double jj)
Definition: vpImagePoint.h:178
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:113
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:450
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void get_eJe(vpMatrix &eJe)
interface with the image for feature display
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
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)