ViSP
servoSimuFourPoints2DPolarCamVelocityDisplay.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuFourPoints2DPolarCamVelocityDisplay.cpp 2503 2010-02-16 18:55:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Simulation of a 2D visual servoing using 4 points with polar
36  * coordinates as visual feature.
37  *
38  * Authors:
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
60 #include <visp/vpDebug.h>
61 #include <visp/vpConfig.h>
62 
63 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
64 
65 #include <stdlib.h>
66 #include <stdio.h>
67 
68 #include <visp/vpCameraParameters.h>
69 #include <visp/vpDisplayX.h>
70 #include <visp/vpDisplayGTK.h>
71 #include <visp/vpDisplayGDI.h>
72 #include <visp/vpDisplayOpenCV.h>
73 #include <visp/vpFeatureBuilder.h>
74 #include <visp/vpFeaturePointPolar.h>
75 #include <visp/vpHomogeneousMatrix.h>
76 #include <visp/vpImage.h>
77 #include <visp/vpImagePoint.h>
78 #include <visp/vpIoTools.h>
79 #include <visp/vpMath.h>
80 #include <visp/vpMeterPixelConversion.h>
81 #include <visp/vpProjectionDisplay.h>
82 #include <visp/vpServo.h>
83 #include <visp/vpServoDisplay.h>
84 #include <visp/vpSimulatorCamera.h>
85 #include <visp/vpParseArgv.h>
86 
87 // List of allowed command line options
88 #define GETOPTARGS "cdh"
89 
90 void usage(const char *name, const char *badparam);
91 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
92 
101 void usage(const char *name, const char *badparam)
102 {
103  fprintf(stdout, "\n\
104 Tests a control law with the following characteristics:\n\
105 - eye-in-hand control\n\
106 - articular velocity are computed\n\
107 - servo on 4 points,\n\
108 - internal and external camera view displays.\n\
109 \n\
110 SYNOPSIS\n\
111  %s [-c] [-d] [-h]\n", name);
112 
113  fprintf(stdout, "\n\
114 OPTIONS: Default\n\
115  -c\n\
116  Disable the mouse click. Useful to automaze the \n\
117  execution of this program without humain intervention.\n\
118 \n\
119  -d \n\
120  Turn off the display.\n\
121 \n\
122  -h\n\
123  Print the help.\n");
124 
125  if (badparam)
126  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
127 }
140 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
141 {
142  const char *optarg_;
143  int c;
144  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
145 
146  switch (c) {
147  case 'c': click_allowed = false; break;
148  case 'd': display = false; break;
149  case 'h': usage(argv[0], NULL); return false; break;
150 
151  default:
152  usage(argv[0], optarg_);
153  return false; break;
154  }
155  }
156 
157  if ((c == 1) || (c == -1)) {
158  // standalone param or error
159  usage(argv[0], NULL);
160  std::cerr << "ERROR: " << std::endl;
161  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
162  return false;
163  }
164 
165  return true;
166 }
167 
168 int
169 main(int argc, const char ** argv)
170 {
171  try {
172  // Log file creation in /tmp/$USERNAME/log.dat
173  // This file contains by line:
174  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
175  // - the 6 mesured camera velocities (m/s, rad/s)
176  // - the 6 mesured joint positions (m, rad)
177  // - the 8 values of s - s*
178  std::string username;
179  // Get the user login name
180  vpIoTools::getUserName(username);
181 
182  // Create a log filename to save velocities...
183  std::string logdirname;
184 #if defined(_WIN32)
185  logdirname ="C:/temp/" + username;
186 #else
187  logdirname ="/tmp/" + username;
188 #endif
189 
190  // Test if the output path exist. If no try to create it
191  if (vpIoTools::checkDirectory(logdirname) == false) {
192  try {
193  // Create the dirname
194  vpIoTools::makeDirectory(logdirname);
195  }
196  catch (...) {
197  std::cerr << std::endl
198  << "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 
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  }
244  vpProjectionDisplay externalview ;
245 
246  double px, py ; px = py = 500 ;
247  double u0, v0 ; u0 = 150, v0 = 160 ;
248 
249  vpCameraParameters cam(px,py,u0,v0);
250 
251  int i ;
252  vpServo task ;
253  vpSimulatorCamera robot ;
254 
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"
260  << std::endl ;
261  std::cout << " Simulation " << std::endl ;
262  std::cout << " task : servo 4 points " << std::endl ;
263  std::cout << "----------------------------------------------" << std::endl ;
264  std::cout << std::endl ;
265 
266  // #define TRANS_Z_PURE
267  // #define TRANS_X_PURE
268  // #define ROT_Z_PURE
269  // #define ROT_X_PURE
270 #define COMPLEX
271  //#define PROBLEM
272 
273 #if defined(TRANS_Z_PURE)
274  // sets the initial camera location
275  vpHomogeneousMatrix cMo(0, 0, 3,
276  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
277  // sets the desired camera location
278  vpHomogeneousMatrix cMod(0, 0, 2,
279  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
280 #elif defined(TRANS_X_PURE)
281  // sets the initial camera location
282  vpHomogeneousMatrix cMo(0.3, 0.3, 3,
283  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
284  // sets the desired camera location
285  vpHomogeneousMatrix cMod(0.5, 0.3, 3,
286  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
287 
288 #elif defined(ROT_Z_PURE)
289  // sets the initial camera location
290  vpHomogeneousMatrix cMo(0, 0, 3,
291  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
292  // sets the desired camera location
293  vpHomogeneousMatrix cMod(0, 0, 3,
294  vpMath::rad(0), vpMath::rad(0), vpMath::rad(180));
295 
296 #elif defined(ROT_X_PURE)
297  // sets the initial camera location
298  vpHomogeneousMatrix cMo(0, 0, 3,
299  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
300  // sets the desired camera location
301  vpHomogeneousMatrix cMod(0, 0, 3,
302  vpMath::rad(45), vpMath::rad(0), vpMath::rad(0));
303 
304 #elif defined(COMPLEX)
305  // sets the initial camera location
306  vpHomogeneousMatrix cMo(0.2, 0.2, 3,
307  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
308  // sets the desired camera location
309  vpHomogeneousMatrix cMod(0, 0, 2.5,
310  vpMath::rad(45), vpMath::rad(10), vpMath::rad(30));
311 
312 #elif defined(PROBLEM)
313  // Bad behavior with an interaction matrix computed from the desired features
314  // sets the initial camera location
315  vpHomogeneousMatrix cMo(0.2, 0.2, 3,
316  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
317  // sets the desired camera location
318  vpHomogeneousMatrix cMod(0.4, 0.2, 3,
319  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
320 
321 #endif
322  // Compute the position of the object in the world frame
323  vpHomogeneousMatrix wMc, wMo;
324  robot.getPosition(wMc) ;
325  wMo = wMc * cMo;
326 
327  vpHomogeneousMatrix cextMo(0,0,6,
328  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
329 
330 
331  // sets the point coordinates in the object frame
332  vpPoint point[4] ;
333  point[0].setWorldCoordinates(-0.25,-0.25,0) ;
334  point[1].setWorldCoordinates(0.25,-0.25,0) ;
335  point[2].setWorldCoordinates(0.25,0.25,0) ;
336  point[3].setWorldCoordinates(-0.25,0.25,0) ;
337 
338 
339  for (i = 0 ; i < 4 ; i++)
340  externalview.insert(point[i]) ;
341 
342  // sets the desired position of the feature point s*"
343  vpFeaturePointPolar pd[4] ;
344 
345  // computes the point coordinates in the desired camera frame and
346  // its 2D coordinates
347  for (i = 0 ; i < 4 ; i++) {
348  point[i].track(cMod);
349  // Computes the polar coordinates from the image point
350  // cartesian coordinates
351  vpFeatureBuilder::create(pd[i],point[i]);
352  }
353 
354 
355  // computes the point coordinates in the camera frame and its 2D
356  // coordinates
357  for (i = 0 ; i < 4 ; i++)
358  point[i].track(cMo) ;
359 
360  // sets the desired position of the point
361  vpFeaturePointPolar p[4] ;
362  for (i = 0 ; i < 4 ; i++) {
363  // retrieve x,y and Z of the vpPoint structure to initialize the
364  // visual feature
365  vpFeatureBuilder::create(p[i], point[i]);
366  }
367 
368  // Define the task;
369  // - we want an eye-in-hand control law
370  // - articular velocity are computed
372  // task.setInteractionMatrixType(vpServo::MEAN) ;
373  // task.setInteractionMatrixType(vpServo::DESIRED) ;
375 
376 
377  // Set the position of the camera in the end-effector frame
378  vpHomogeneousMatrix cMe ;
379  vpVelocityTwistMatrix cVe(cMe) ;
380  task.set_cVe(cVe) ;
381 
382  // Set the Jacobian (expressed in the end-effector frame)
383  vpMatrix eJe ;
384  robot.get_eJe(eJe) ;
385  task.set_eJe(eJe) ;
386 
387  // we want to see a point on a point
388  for (i = 0 ; i < 4 ; i++)
389  task.addFeature(p[i],pd[i]) ;
390 
391  // set the gain
392  task.setLambda(1) ;
393 
394 
395  std::cout << "\nDisplay task information: " << std::endl;
396  task.print() ;
397 
398  unsigned int iter=0 ;
399  // loop
400  while(iter++ < 200) {
401  std::cout << "---------------------------------------------"
402  << iter <<std::endl ;
403  vpColVector v ;
404 
405 
406  // Set the Jacobian (expressed in the end-effector frame)
407  // Since q is modified eJe is modified
408  robot.get_eJe(eJe) ;
409  task.set_eJe(eJe) ;
410 
411  // get the robot position
412  robot.getPosition(wMc) ;
413  // Compute the position of the camera wrt the object frame
414  cMo = wMc.inverse() * wMo;
415 
416  // Compute new point position
417  for (i = 0 ; i < 4 ; i++) {
418  point[i].track(cMo) ;
419  // retrieve x,y and Z of the vpPoint structure to compute the feature
420  vpFeatureBuilder::create(p[i],point[i]) ;
421  }
422 
423  if (opt_display) {
424  vpDisplay::display(Iint) ;
425  vpDisplay::display(Iext) ;
426 
427  vpServoDisplay::display(task,cam,Iint) ;
428  externalview.display(Iext,cextMo, cMo, cam, vpColor::green);
429  vpDisplay::flush(Iint);
430  vpDisplay::flush(Iext);
431  }
432 
433  // Compute the control law
434  v = task.computeControlLaw() ;
435 
436  if (iter==1) {
437  std::cout << "Display task information: " << std::endl;
438  task.print() ;
439  }
440 
443 
444  // Send the camera velocity to the controller
446  // Save velocities applied to the robot in the log file
447  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
448  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
449  flog << v[0] << " " << v[1] << " " << v[2] << " "
450  << v[3] << " " << v[4] << " " << v[5] << " ";
451 
452  std::cout << "v: " << v.t() << std::endl;
453 
454  std::cout << "|| s - s* || = "<< ( task.getError() ).sumSquare() << std::endl;
455 
456  // Save feature error (s-s*) for the 4 feature points. For each feature
457  // point, we have 2 errors (along x and y axis). This error is expressed
458  // in meters in the camera frame
459  flog << ( task.getError() ).t() << " ";// s-s* for point 4
460  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
461 
462  // Save current visual feature s = (rho,theta)
463  for (i = 0 ; i < 4 ; i++) {
464  flog << p[i].get_rho() << " " << p[i].get_theta() << " ";
465  }
466  // Save current position of the points
467  for (i = 0 ; i < 4 ; i++) {
468  flog << point[i].get_x() << " " << point[i].get_y() << " ";
469  }
470  flog << std::endl;
471 
472  if (iter == 1) {
473  vpImagePoint ip;
474  ip.set_i( 10 );
475  ip.set_j( 10 );
476 
477  std::cout << "\nClick in the internal camera view to continue..." << std::endl;
478  vpDisplay::displayText(Iint, ip,
479  "A click to continue...",vpColor::red);
480  vpDisplay::flush(Iint);
481  vpDisplay::getClick(Iint);
482  }
483 
484  }
485 
486 
487  flog.close() ; // Close the log file
488 
489  // Display task information
490  task.print() ;
491 
492  // Kill the task
493  task.kill();
494 
495  std::cout <<"Final robot position with respect to the object frame:\n";
496  cMo.print();
497 
498  if (opt_display && opt_click_allowed) {
499  // suppressed for automate test
500  std::cout << "\n\nClick in the internal view to end..." << std::endl;
501  vpDisplay::getClick(Iint) ;
502  }
503  return 0;
504  }
505  catch(vpException e) {
506  std::cout << "Catch a ViSP exception: " << e << std::endl;
507  return 1;
508  }
509 }
510 #else
511 int
512 main()
513 {
514  std::cout << "You do not have X11, GTK, GDI or OpenCV display functionalities..." << std::endl;
515 }
516 
517 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:315
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)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:887
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
void track(const vpHomogeneousMatrix &cMo)
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:138
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
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:80
static const vpColor red
Definition: vpColor.h:167
Class that defines what is a point.
Definition: vpPoint.h:65
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:384
void set_i(const double ii)
Definition: vpImagePoint.h:159
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
vpRowVector t() const
Transpose of a vector.
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
static std::string getUserName()
Definition: vpIoTools.cpp:141
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.h:136
void insert(vpForwardProjection &fp)
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
void set_j(const double jj)
Definition: vpImagePoint.h:170
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
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:220
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 setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74