ViSP  2.8.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 - 2013 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 
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 
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 #ifdef 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 #endif
226 
227  // open a display for the visualization
228 
229  vpImage<unsigned char> Iint(300, 300, 0) ;
230  vpImage<unsigned char> Iext(300, 300, 0) ;
231 
232  if (opt_display) {
233  displayInt.init(Iint,0,0, "Internal view") ;
234  displayExt.init(Iext,330,000, "External view") ;
235 
236  }
237  vpProjectionDisplay externalview ;
238 
239  double px, py ; px = py = 500 ;
240  double u0, v0 ; u0 = 150, v0 = 160 ;
241 
242  vpCameraParameters cam(px,py,u0,v0);
243 
244  int i ;
245  vpServo task ;
246  vpSimulatorCamera robot ;
247 
248 
249  std::cout << std::endl ;
250  std::cout << "----------------------------------------------" << std::endl ;
251  std::cout << " Test program for vpServo " <<std::endl ;
252  std::cout << " Eye-in-hand task control, articular velocity are computed"
253  << std::endl ;
254  std::cout << " Simulation " << std::endl ;
255  std::cout << " task : servo 4 points " << std::endl ;
256  std::cout << "----------------------------------------------" << std::endl ;
257  std::cout << std::endl ;
258 
259  // #define TRANS_Z_PURE
260  // #define TRANS_X_PURE
261  // #define ROT_Z_PURE
262  // #define ROT_X_PURE
263 #define COMPLEX
264  //#define PROBLEM
265 
266 #if defined(TRANS_Z_PURE)
267  // sets the initial camera location
268  vpHomogeneousMatrix cMo(0, 0, 3,
269  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
270  // sets the desired camera location
271  vpHomogeneousMatrix cMod(0, 0, 2,
272  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,
276  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
277  // sets the desired camera location
278  vpHomogeneousMatrix cMod(0.5, 0.3, 3,
279  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
280 
281 #elif defined(ROT_Z_PURE)
282  // sets the initial camera location
283  vpHomogeneousMatrix cMo(0, 0, 3,
284  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
285  // sets the desired camera location
286  vpHomogeneousMatrix cMod(0, 0, 3,
287  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,
292  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
293  // sets the desired camera location
294  vpHomogeneousMatrix cMod(0, 0, 3,
295  vpMath::rad(45), vpMath::rad(0), vpMath::rad(0));
296 
297 #elif defined(COMPLEX)
298  // sets the initial camera location
299  vpHomogeneousMatrix cMo(0.2, 0.2, 3,
300  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
301  // sets the desired camera location
302  vpHomogeneousMatrix cMod(0, 0, 2.5,
303  vpMath::rad(45), vpMath::rad(10), vpMath::rad(30));
304 
305 #elif defined(PROBLEM)
306  // Bad behavior with an interaction matrix computed from the desired features
307  // sets the initial camera location
308  vpHomogeneousMatrix cMo(0.2, 0.2, 3,
309  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
310  // sets the desired camera location
311  vpHomogeneousMatrix cMod(0.4, 0.2, 3,
312  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
313 
314 #endif
315  // Compute the position of the object in the world frame
316  vpHomogeneousMatrix wMc, wMo;
317  robot.getPosition(wMc) ;
318  wMo = wMc * cMo;
319 
320  vpHomogeneousMatrix cextMo(0,0,6,
321  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
322 
323 
324  // sets the point coordinates in the object frame
325  vpPoint point[4] ;
326  point[0].setWorldCoordinates(-0.25,-0.25,0) ;
327  point[1].setWorldCoordinates(0.25,-0.25,0) ;
328  point[2].setWorldCoordinates(0.25,0.25,0) ;
329  point[3].setWorldCoordinates(-0.25,0.25,0) ;
330 
331 
332  for (i = 0 ; i < 4 ; i++)
333  externalview.insert(point[i]) ;
334 
335  // sets the desired position of the feature point s*"
336  vpFeaturePointPolar pd[4] ;
337 
338  // computes the point coordinates in the desired camera frame and
339  // its 2D coordinates
340  for (i = 0 ; i < 4 ; i++) {
341  point[i].track(cMod);
342  // Computes the polar coordinates from the image point
343  // cartesian coordinates
344  vpFeatureBuilder::create(pd[i],point[i]);
345  }
346 
347 
348  // computes the point coordinates in the camera frame and its 2D
349  // coordinates
350  for (i = 0 ; i < 4 ; i++)
351  point[i].track(cMo) ;
352 
353  // sets the desired position of the point
354  vpFeaturePointPolar p[4] ;
355  for (i = 0 ; i < 4 ; i++) {
356  // retrieve x,y and Z of the vpPoint structure to initialize the
357  // visual feature
358  vpFeatureBuilder::create(p[i], point[i]);
359  }
360 
361  // Define the task;
362  // - we want an eye-in-hand control law
363  // - articular velocity are computed
365  // task.setInteractionMatrixType(vpServo::MEAN) ;
366  // task.setInteractionMatrixType(vpServo::DESIRED) ;
368 
369 
370  // Set the position of the camera in the end-effector frame
371  vpHomogeneousMatrix cMe ;
372  vpVelocityTwistMatrix cVe(cMe) ;
373  task.set_cVe(cVe) ;
374 
375  // Set the Jacobian (expressed in the end-effector frame)
376  vpMatrix eJe ;
377  robot.get_eJe(eJe) ;
378  task.set_eJe(eJe) ;
379 
380  // we want to see a point on a point
381  for (i = 0 ; i < 4 ; i++)
382  task.addFeature(p[i],pd[i]) ;
383 
384  // set the gain
385  task.setLambda(1) ;
386 
387 
388  std::cout << "\nDisplay task information: " << std::endl;
389  task.print() ;
390 
391  unsigned int iter=0 ;
392  // loop
393  while(iter++ < 200) {
394  std::cout << "---------------------------------------------"
395  << iter <<std::endl ;
396  vpColVector v ;
397 
398 
399  // Set the Jacobian (expressed in the end-effector frame)
400  // Since q is modified eJe is modified
401  robot.get_eJe(eJe) ;
402  task.set_eJe(eJe) ;
403 
404  // get the robot position
405  robot.getPosition(wMc) ;
406  // Compute the position of the camera wrt the object frame
407  cMo = wMc.inverse() * wMo;
408 
409  // Compute new point position
410  for (i = 0 ; i < 4 ; i++) {
411  point[i].track(cMo) ;
412  // retrieve x,y and Z of the vpPoint structure to compute the feature
413  vpFeatureBuilder::create(p[i],point[i]) ;
414  }
415 
416  if (opt_display) {
417  vpDisplay::display(Iint) ;
418  vpDisplay::display(Iext) ;
419 
420  vpServoDisplay::display(task,cam,Iint) ;
421  externalview.display(Iext,cextMo, cMo, cam, vpColor::green);
422  vpDisplay::flush(Iint);
423  vpDisplay::flush(Iext);
424  }
425 
426  // Compute the control law
427  v = task.computeControlLaw() ;
428 
429  if (iter==1) {
430  std::cout << "Display task information: " << std::endl;
431  task.print() ;
432  }
433 
436 
437  // Send the camera velocity to the controller
439  // Save velocities applied to the robot in the log file
440  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
441  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
442  flog << v[0] << " " << v[1] << " " << v[2] << " "
443  << v[3] << " " << v[4] << " " << v[5] << " ";
444 
445  std::cout << "v: " << v.t() << std::endl;
446 
447  std::cout << "|| s - s* || = "<< ( task.getError() ).sumSquare() << std::endl;
448 
449  // Save feature error (s-s*) for the 4 feature points. For each feature
450  // point, we have 2 errors (along x and y axis). This error is expressed
451  // in meters in the camera frame
452  flog << ( task.getError() ).t() << " ";// s-s* for point 4
453  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
454 
455  // Save current visual feature s = (rho,theta)
456  for (i = 0 ; i < 4 ; i++) {
457  flog << p[i].get_rho() << " " << p[i].get_theta() << " ";
458  }
459  // Save current position of the points
460  for (i = 0 ; i < 4 ; i++) {
461  flog << point[i].get_x() << " " << point[i].get_y() << " ";
462  }
463  flog << std::endl;
464 
465  if (iter == 1) {
466  vpImagePoint ip;
467  ip.set_i( 10 );
468  ip.set_j( 10 );
469 
470  std::cout << "\nClick in the internal camera view to continue..." << std::endl;
472  "A click to continue...",vpColor::red);
473  vpDisplay::flush(Iint);
474  vpDisplay::getClick(Iint);
475  }
476 
477  }
478 
479 
480  flog.close() ; // Close the log file
481 
482  // Display task information
483  task.print() ;
484 
485  // Kill the task
486  task.kill();
487 
488  std::cout <<"Final robot position with respect to the object frame:\n";
489  cMo.print();
490 
491  if (opt_display && opt_click_allowed) {
492  // suppressed for automate test
493  std::cout << "\n\nClick in the internal view to end..." << std::endl;
494  vpDisplay::getClick(Iint) ;
495  }
496 }
497 #else
498 int
499 main()
500 {
501  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
502 }
503 
504 #endif
void set_j(const double j)
Definition: vpImagePoint.h:156
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
static void display(vpServo &s, const vpCameraParameters &cam, vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
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:379
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
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)
create a new ste of two visual features
Definition: vpServo.cpp:444
void set_i(const double i)
Definition: vpImagePoint.h:145
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:253
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
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:230
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
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 kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:238
vpRowVector t() const
transpose of Vector
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
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)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
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:258
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)
Class required to compute the visual servoing control law descbribed in and .
Definition: vpServo.h:153
void get_eJe(vpMatrix &eJe)
interface with the image for feature display
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
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