Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuFourPoints2DPolarCamVelocityDisplay.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Simulation of a 2D visual servoing using 4 points with polar
32  * coordinates as visual feature.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
56 #include <visp3/core/vpDebug.h>
57 #include <visp3/core/vpConfig.h>
58 
59 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
60 
61 #include <stdlib.h>
62 #include <stdio.h>
63 
64 #include <visp3/core/vpCameraParameters.h>
65 #include <visp3/gui/vpDisplayX.h>
66 #include <visp3/gui/vpDisplayGTK.h>
67 #include <visp3/gui/vpDisplayGDI.h>
68 #include <visp3/gui/vpDisplayOpenCV.h>
69 #include <visp3/visual_features/vpFeatureBuilder.h>
70 #include <visp3/visual_features/vpFeaturePointPolar.h>
71 #include <visp3/core/vpHomogeneousMatrix.h>
72 #include <visp3/core/vpImage.h>
73 #include <visp3/core/vpImagePoint.h>
74 #include <visp3/core/vpIoTools.h>
75 #include <visp3/core/vpMath.h>
76 #include <visp3/core/vpMeterPixelConversion.h>
77 #include <visp3/gui/vpProjectionDisplay.h>
78 #include <visp3/vs/vpServo.h>
79 #include <visp3/vs/vpServoDisplay.h>
80 #include <visp3/robot/vpSimulatorCamera.h>
81 #include <visp3/io/vpParseArgv.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': click_allowed = false; break;
144  case 'd': display = false; break;
145  case 'h': usage(argv[0], NULL); return false; break;
146 
147  default:
148  usage(argv[0], optarg_);
149  return false; break;
150  }
151  }
152 
153  if ((c == 1) || (c == -1)) {
154  // standalone param or error
155  usage(argv[0], NULL);
156  std::cerr << "ERROR: " << std::endl;
157  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
158  return false;
159  }
160 
161  return true;
162 }
163 
164 int
165 main(int argc, const char ** argv)
166 {
167  try {
168  // Log file creation in /tmp/$USERNAME/log.dat
169  // This file contains by line:
170  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
171  // - the 6 mesured camera velocities (m/s, rad/s)
172  // - the 6 mesured joint positions (m, rad)
173  // - the 8 values of s - s*
174  std::string username;
175  // Get the user login name
176  vpIoTools::getUserName(username);
177 
178  // Create a log filename to save velocities...
179  std::string logdirname;
180 #if defined(_WIN32)
181  logdirname ="C:/temp/" + username;
182 #else
183  logdirname ="/tmp/" + username;
184 #endif
185 
186  // Test if the output path exist. If no try to create it
187  if (vpIoTools::checkDirectory(logdirname) == false) {
188  try {
189  // Create the dirname
190  vpIoTools::makeDirectory(logdirname);
191  }
192  catch (...) {
193  std::cerr << std::endl
194  << "ERROR:" << std::endl;
195  std::cerr << " Cannot create " << logdirname << std::endl;
196  exit(-1);
197  }
198  }
199  std::string logfilename;
200  logfilename = logdirname + "/log.dat";
201 
202  // Open the log file name
203  std::ofstream flog(logfilename.c_str());
204 
205 
206  bool opt_click_allowed = true;
207  bool opt_display = true;
208 
209  // Read the command line options
210  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
211  exit (-1);
212  }
213 
214  // We open two displays, one for the internal camera view, the other one for
215  // the external view, using either X11, GTK or GDI.
216 #if defined VISP_HAVE_X11
217  vpDisplayX displayInt;
218  vpDisplayX displayExt;
219 #elif defined VISP_HAVE_GTK
220  vpDisplayGTK displayInt;
221  vpDisplayGTK displayExt;
222 #elif defined VISP_HAVE_GDI
223  vpDisplayGDI displayInt;
224  vpDisplayGDI displayExt;
225 #elif defined VISP_HAVE_OPENCV
226  vpDisplayOpenCV displayInt;
227  vpDisplayOpenCV displayExt;
228 #endif
229 
230  // open a display for the visualization
231 
232  vpImage<unsigned char> Iint(300, 300, 0) ;
233  vpImage<unsigned char> Iext(300, 300, 0) ;
234 
235  if (opt_display) {
236  displayInt.init(Iint,0,0, "Internal view") ;
237  displayExt.init(Iext,330,000, "External view") ;
238 
239  }
240  vpProjectionDisplay externalview ;
241 
242  double px, py ; px = py = 500 ;
243  double u0, v0 ; u0 = 150, v0 = 160 ;
244 
245  vpCameraParameters cam(px,py,u0,v0);
246 
247  int i ;
248  vpServo task ;
249  vpSimulatorCamera robot ;
250 
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"
256  << std::endl ;
257  std::cout << " Simulation " << std::endl ;
258  std::cout << " task : servo 4 points " << std::endl ;
259  std::cout << "----------------------------------------------" << std::endl ;
260  std::cout << std::endl ;
261 
262  // #define TRANS_Z_PURE
263  // #define TRANS_X_PURE
264  // #define ROT_Z_PURE
265  // #define ROT_X_PURE
266 #define COMPLEX
267  //#define PROBLEM
268 
269 #if defined(TRANS_Z_PURE)
270  // sets the initial camera location
271  vpHomogeneousMatrix cMo(0, 0, 3,
272  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
273  // sets the desired camera location
274  vpHomogeneousMatrix cMod(0, 0, 2,
275  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
276 #elif defined(TRANS_X_PURE)
277  // sets the initial camera location
278  vpHomogeneousMatrix cMo(0.3, 0.3, 3,
279  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
280  // sets the desired camera location
281  vpHomogeneousMatrix cMod(0.5, 0.3, 3,
282  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
283 
284 #elif defined(ROT_Z_PURE)
285  // sets the initial camera location
286  vpHomogeneousMatrix cMo(0, 0, 3,
287  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
288  // sets the desired camera location
289  vpHomogeneousMatrix cMod(0, 0, 3,
290  vpMath::rad(0), vpMath::rad(0), vpMath::rad(180));
291 
292 #elif defined(ROT_X_PURE)
293  // sets the initial camera location
294  vpHomogeneousMatrix cMo(0, 0, 3,
295  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
296  // sets the desired camera location
297  vpHomogeneousMatrix cMod(0, 0, 3,
298  vpMath::rad(45), vpMath::rad(0), vpMath::rad(0));
299 
300 #elif defined(COMPLEX)
301  // sets the initial camera location
302  vpHomogeneousMatrix cMo(0.2, 0.2, 3,
303  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
304  // sets the desired camera location
305  vpHomogeneousMatrix cMod(0, 0, 2.5,
306  vpMath::rad(45), vpMath::rad(10), vpMath::rad(30));
307 
308 #elif defined(PROBLEM)
309  // Bad behavior with an interaction matrix computed from the desired features
310  // sets the initial camera location
311  vpHomogeneousMatrix cMo(0.2, 0.2, 3,
312  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
313  // sets the desired camera location
314  vpHomogeneousMatrix cMod(0.4, 0.2, 3,
315  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
316 
317 #endif
318  // Compute the position of the object in the world frame
319  vpHomogeneousMatrix wMc, wMo;
320  robot.getPosition(wMc) ;
321  wMo = wMc * cMo;
322 
323  vpHomogeneousMatrix cextMo(0,0,6,
324  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
325 
326 
327  // sets the point coordinates in the object frame
328  vpPoint point[4] ;
329  point[0].setWorldCoordinates(-0.25,-0.25,0) ;
330  point[1].setWorldCoordinates(0.25,-0.25,0) ;
331  point[2].setWorldCoordinates(0.25,0.25,0) ;
332  point[3].setWorldCoordinates(-0.25,0.25,0) ;
333 
334  for (i = 0 ; i < 4 ; i++)
335  externalview.insert(point[i]) ;
336 
337  // sets the desired position of the feature point s*"
338  vpFeaturePointPolar pd[4] ;
339 
340  // computes the point coordinates in the desired camera frame and
341  // its 2D coordinates
342  for (i = 0 ; i < 4 ; i++) {
343  point[i].track(cMod);
344  // Computes the polar coordinates from the image point
345  // cartesian coordinates
346  vpFeatureBuilder::create(pd[i],point[i]);
347  }
348 
349 
350  // computes the point coordinates in the camera frame and its 2D
351  // coordinates
352  for (i = 0 ; i < 4 ; i++)
353  point[i].track(cMo) ;
354 
355  // sets the desired position of the point
356  vpFeaturePointPolar p[4] ;
357  for (i = 0 ; i < 4 ; i++) {
358  // retrieve x,y and Z of the vpPoint structure to initialize the
359  // visual feature
360  vpFeatureBuilder::create(p[i], point[i]);
361  }
362 
363  // Define the task;
364  // - we want an eye-in-hand control law
365  // - articular velocity are computed
367  // task.setInteractionMatrixType(vpServo::MEAN) ;
368  // task.setInteractionMatrixType(vpServo::DESIRED) ;
370 
371 
372  // Set the position of the camera in the end-effector frame
373  vpHomogeneousMatrix cMe ;
374  vpVelocityTwistMatrix cVe(cMe) ;
375  task.set_cVe(cVe) ;
376 
377  // Set the Jacobian (expressed in the end-effector frame)
378  vpMatrix eJe ;
379  robot.get_eJe(eJe) ;
380  task.set_eJe(eJe) ;
381 
382  // we want to see a point on a point
383  for (i = 0 ; i < 4 ; i++)
384  task.addFeature(p[i],pd[i]) ;
385 
386  // set the gain
387  task.setLambda(1) ;
388 
389 
390  std::cout << "\nDisplay task information: " << std::endl;
391  task.print() ;
392 
393  unsigned int iter=0 ;
394  // loop
395  while(iter++ < 200) {
396  std::cout << "---------------------------------------------"
397  << iter <<std::endl ;
398  vpColVector v ;
399 
400 
401  // Set the Jacobian (expressed in the end-effector frame)
402  // Since q is modified eJe is modified
403  robot.get_eJe(eJe) ;
404  task.set_eJe(eJe) ;
405 
406  // get the robot position
407  robot.getPosition(wMc) ;
408  // Compute the position of the camera wrt the object frame
409  cMo = wMc.inverse() * wMo;
410 
411  // Compute new point position
412  for (i = 0 ; i < 4 ; i++) {
413  point[i].track(cMo) ;
414  // retrieve x,y and Z of the vpPoint structure to compute the feature
415  vpFeatureBuilder::create(p[i],point[i]) ;
416  }
417 
418  if (opt_display) {
419  vpDisplay::display(Iint) ;
420  vpDisplay::display(Iext) ;
421 
422  vpServoDisplay::display(task,cam,Iint) ;
423  externalview.display(Iext,cextMo, cMo, cam, vpColor::green);
424  vpDisplay::flush(Iint);
425  vpDisplay::flush(Iext);
426  }
427 
428  // Compute the control law
429  v = task.computeControlLaw() ;
430 
431  if (iter==1) {
432  std::cout << "Display task information: " << std::endl;
433  task.print() ;
434  }
435 
438 
439  // Send the camera velocity to the controller
441  // Save velocities applied to the robot in the log file
442  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
443  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
444  flog << v[0] << " " << v[1] << " " << v[2] << " "
445  << v[3] << " " << v[4] << " " << v[5] << " ";
446 
447  std::cout << "v: " << v.t() << std::endl;
448 
449  std::cout << "|| s - s* || = "<< ( task.getError() ).sumSquare() << std::endl;
450 
451  // Save feature error (s-s*) for the 4 feature points. For each feature
452  // point, we have 2 errors (along x and y axis). This error is expressed
453  // in meters in the camera frame
454  flog << ( task.getError() ).t() << " ";// s-s* for point 4
455  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
456 
457  // Save current visual feature s = (rho,theta)
458  for (i = 0 ; i < 4 ; i++) {
459  flog << p[i].get_rho() << " " << p[i].get_theta() << " ";
460  }
461  // Save current position of the points
462  for (i = 0 ; i < 4 ; i++) {
463  flog << point[i].get_x() << " " << point[i].get_y() << " ";
464  }
465  flog << std::endl;
466 
467  if (iter == 1) {
468  vpImagePoint ip;
469  ip.set_i( 10 );
470  ip.set_j( 10 );
471 
472  std::cout << "\nClick in the internal camera view to continue..." << std::endl;
473  vpDisplay::displayText(Iint, ip,
474  "A click to continue...",vpColor::red);
475  vpDisplay::flush(Iint);
476  vpDisplay::getClick(Iint);
477  }
478 
479  }
480 
481 
482  flog.close() ; // Close the log file
483 
484  // Display task information
485  task.print() ;
486 
487  // Kill the task
488  task.kill();
489 
490  std::cout <<"Final robot position with respect to the object frame:\n";
491  cMo.print();
492 
493  if (opt_display && opt_click_allowed) {
494  // suppressed for automate test
495  std::cout << "\n\nClick in the internal view to end..." << std::endl;
496  vpDisplay::getClick(Iint) ;
497  }
498  return 0;
499  }
500  catch(vpException &e) {
501  std::cout << "Catch a ViSP exception: " << e << std::endl;
502  return 1;
503  }
504 }
505 #else
506 int
507 main()
508 {
509  std::cout << "You do not have X11, GTK, GDI or OpenCV display functionalities..." << std::endl;
510 }
511 
512 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
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:128
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:460
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:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
error that can be emited by ViSP classes.
Definition: vpException.h:73
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:458
static const vpColor green
Definition: vpColor.h:166
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:76
static const vpColor red
Definition: vpColor.h:163
Class that defines what is a point.
Definition: vpPoint.h:59
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
void set_i(const double ii)
Definition: vpImagePoint.h:163
void kill()
Definition: vpServo.cpp:191
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
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:391
static std::string getUserName()
Definition: vpIoTools.cpp:177
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:456
void insert(vpForwardProjection &fp)
vpHomogeneousMatrix getPosition() const
Implementation of a velocity twist matrix and operations on such kind of matrices.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
void set_j(const double jj)
Definition: vpImagePoint.h:174
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:435
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:314
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:222
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)