ViSP  2.9.0
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))
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/vpFeatureBuilder.h>
73 #include <visp/vpFeaturePointPolar.h>
74 #include <visp/vpHomogeneousMatrix.h>
75 #include <visp/vpImage.h>
76 #include <visp/vpImagePoint.h>
77 #include <visp/vpIoTools.h>
78 #include <visp/vpMath.h>
79 #include <visp/vpMeterPixelConversion.h>
80 #include <visp/vpProjectionDisplay.h>
81 #include <visp/vpServo.h>
82 #include <visp/vpServoDisplay.h>
83 #include <visp/vpSimulatorCamera.h>
84 #include <visp/vpParseArgv.h>
85 
86 // List of allowed command line options
87 #define GETOPTARGS "cdh"
88 
89 void usage(const char *name, const char *badparam);
90 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
91 
100 void usage(const char *name, const char *badparam)
101 {
102  fprintf(stdout, "\n\
103 Tests a control law with the following characteristics:\n\
104 - eye-in-hand control\n\
105 - articular velocity are computed\n\
106 - servo on 4 points,\n\
107 - internal and external camera view displays.\n\
108 \n\
109 SYNOPSIS\n\
110  %s [-c] [-d] [-h]\n", name);
111 
112  fprintf(stdout, "\n\
113 OPTIONS: Default\n\
114  -c\n\
115  Disable the mouse click. Useful to automaze the \n\
116  execution of this program without humain intervention.\n\
117 \n\
118  -d \n\
119  Turn off the display.\n\
120 \n\
121  -h\n\
122  Print the help.\n");
123 
124  if (badparam)
125  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
126 }
139 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
140 {
141  const char *optarg_;
142  int c;
143  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
144 
145  switch (c) {
146  case 'c': click_allowed = false; break;
147  case 'd': display = false; break;
148  case 'h': usage(argv[0], NULL); return false; break;
149 
150  default:
151  usage(argv[0], optarg_);
152  return false; break;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], NULL);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 int
168 main(int argc, const char ** argv)
169 {
170  try {
171  // Log file creation in /tmp/$USERNAME/log.dat
172  // This file contains by line:
173  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
174  // - the 6 mesured camera velocities (m/s, rad/s)
175  // - the 6 mesured joint positions (m, rad)
176  // - the 8 values of s - s*
177  std::string username;
178  // Get the user login name
179  vpIoTools::getUserName(username);
180 
181  // Create a log filename to save velocities...
182  std::string logdirname;
183 #if defined(_WIN32)
184  logdirname ="C:/temp/" + username;
185 #else
186  logdirname ="/tmp/" + username;
187 #endif
188 
189  // Test if the output path exist. If no try to create it
190  if (vpIoTools::checkDirectory(logdirname) == false) {
191  try {
192  // Create the dirname
193  vpIoTools::makeDirectory(logdirname);
194  }
195  catch (...) {
196  std::cerr << std::endl
197  << "ERROR:" << std::endl;
198  std::cerr << " Cannot create " << logdirname << std::endl;
199  exit(-1);
200  }
201  }
202  std::string logfilename;
203  logfilename = logdirname + "/log.dat";
204 
205  // Open the log file name
206  std::ofstream flog(logfilename.c_str());
207 
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 #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 
335  for (i = 0 ; i < 4 ; i++)
336  externalview.insert(point[i]) ;
337 
338  // sets the desired position of the feature point s*"
339  vpFeaturePointPolar pd[4] ;
340 
341  // computes the point coordinates in the desired camera frame and
342  // its 2D coordinates
343  for (i = 0 ; i < 4 ; i++) {
344  point[i].track(cMod);
345  // Computes the polar coordinates from the image point
346  // cartesian coordinates
347  vpFeatureBuilder::create(pd[i],point[i]);
348  }
349 
350 
351  // computes the point coordinates in the camera frame and its 2D
352  // coordinates
353  for (i = 0 ; i < 4 ; i++)
354  point[i].track(cMo) ;
355 
356  // sets the desired position of the point
357  vpFeaturePointPolar p[4] ;
358  for (i = 0 ; i < 4 ; i++) {
359  // retrieve x,y and Z of the vpPoint structure to initialize the
360  // visual feature
361  vpFeatureBuilder::create(p[i], point[i]);
362  }
363 
364  // Define the task;
365  // - we want an eye-in-hand control law
366  // - articular velocity are computed
368  // task.setInteractionMatrixType(vpServo::MEAN) ;
369  // task.setInteractionMatrixType(vpServo::DESIRED) ;
371 
372 
373  // Set the position of the camera in the end-effector frame
374  vpHomogeneousMatrix cMe ;
375  vpVelocityTwistMatrix cVe(cMe) ;
376  task.set_cVe(cVe) ;
377 
378  // Set the Jacobian (expressed in the end-effector frame)
379  vpMatrix eJe ;
380  robot.get_eJe(eJe) ;
381  task.set_eJe(eJe) ;
382 
383  // we want to see a point on a point
384  for (i = 0 ; i < 4 ; i++)
385  task.addFeature(p[i],pd[i]) ;
386 
387  // set the gain
388  task.setLambda(1) ;
389 
390 
391  std::cout << "\nDisplay task information: " << std::endl;
392  task.print() ;
393 
394  unsigned int iter=0 ;
395  // loop
396  while(iter++ < 200) {
397  std::cout << "---------------------------------------------"
398  << iter <<std::endl ;
399  vpColVector v ;
400 
401 
402  // Set the Jacobian (expressed in the end-effector frame)
403  // Since q is modified eJe is modified
404  robot.get_eJe(eJe) ;
405  task.set_eJe(eJe) ;
406 
407  // get the robot position
408  robot.getPosition(wMc) ;
409  // Compute the position of the camera wrt the object frame
410  cMo = wMc.inverse() * wMo;
411 
412  // Compute new point position
413  for (i = 0 ; i < 4 ; i++) {
414  point[i].track(cMo) ;
415  // retrieve x,y and Z of the vpPoint structure to compute the feature
416  vpFeatureBuilder::create(p[i],point[i]) ;
417  }
418 
419  if (opt_display) {
420  vpDisplay::display(Iint) ;
421  vpDisplay::display(Iext) ;
422 
423  vpServoDisplay::display(task,cam,Iint) ;
424  externalview.display(Iext,cextMo, cMo, cam, vpColor::green);
425  vpDisplay::flush(Iint);
426  vpDisplay::flush(Iext);
427  }
428 
429  // Compute the control law
430  v = task.computeControlLaw() ;
431 
432  if (iter==1) {
433  std::cout << "Display task information: " << std::endl;
434  task.print() ;
435  }
436 
439 
440  // Send the camera velocity to the controller
442  // Save velocities applied to the robot in the log file
443  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
444  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
445  flog << v[0] << " " << v[1] << " " << v[2] << " "
446  << v[3] << " " << v[4] << " " << v[5] << " ";
447 
448  std::cout << "v: " << v.t() << std::endl;
449 
450  std::cout << "|| s - s* || = "<< ( task.getError() ).sumSquare() << std::endl;
451 
452  // Save feature error (s-s*) for the 4 feature points. For each feature
453  // point, we have 2 errors (along x and y axis). This error is expressed
454  // in meters in the camera frame
455  flog << ( task.getError() ).t() << " ";// s-s* for point 4
456  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
457 
458  // Save current visual feature s = (rho,theta)
459  for (i = 0 ; i < 4 ; i++) {
460  flog << p[i].get_rho() << " " << p[i].get_theta() << " ";
461  }
462  // Save current position of the points
463  for (i = 0 ; i < 4 ; i++) {
464  flog << point[i].get_x() << " " << point[i].get_y() << " ";
465  }
466  flog << std::endl;
467 
468  if (iter == 1) {
469  vpImagePoint ip;
470  ip.set_i( 10 );
471  ip.set_j( 10 );
472 
473  std::cout << "\nClick in the internal camera view to continue..." << std::endl;
475  "A click to continue...",vpColor::red);
476  vpDisplay::flush(Iint);
477  vpDisplay::getClick(Iint);
478  }
479 
480  }
481 
482 
483  flog.close() ; // Close the log file
484 
485  // Display task information
486  task.print() ;
487 
488  // Kill the task
489  task.kill();
490 
491  std::cout <<"Final robot position with respect to the object frame:\n";
492  cMo.print();
493 
494  if (opt_display && opt_click_allowed) {
495  // suppressed for automate test
496  std::cout << "\n\nClick in the internal view to end..." << std::endl;
497  vpDisplay::getClick(Iint) ;
498  }
499  return 0;
500  }
501  catch(vpException e) {
502  std::cout << "Catch a ViSP exception: " << e << std::endl;
503  return 1;
504  }
505 }
506 #else
507 int
508 main()
509 {
510  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
511 }
512 
513 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
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...
#define vpERROR_TRACE
Definition: vpDebug.h:395
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
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:1994
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:79
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:404
void set_i(const double ii)
Definition: vpImagePoint.h:158
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:206
vpRowVector t() const
transpose of 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:140
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 init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
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:169
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
virtual void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)=0
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:92
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