ViSP  2.6.2
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 - 2012 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 
62 #include <visp/vpDebug.h>
63 #include <visp/vpConfig.h>
64 
65 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
66 
67 #include <stdlib.h>
68 #include <stdio.h>
69 
70 #include <visp/vpMath.h>
71 #include <visp/vpHomogeneousMatrix.h>
72 #include <visp/vpServo.h>
73 #include <visp/vpRobotCamera.h>
74 #include <visp/vpDebug.h>
75 #include <visp/vpFeatureBuilder.h>
76 #include <visp/vpFeaturePointPolar.h>
77 
78 #include <visp/vpServoDisplay.h>
79 #include <visp/vpProjectionDisplay.h>
80 #include <visp/vpMeterPixelConversion.h>
81 
82 #include <visp/vpImage.h>
83 #include <visp/vpImagePoint.h>
84 #include <visp/vpDisplayX.h>
85 #include <visp/vpDisplayGTK.h>
86 #include <visp/vpDisplayGDI.h>
87 #include <visp/vpCameraParameters.h>
88 #include <visp/vpParseArgv.h>
89 #include <visp/vpIoTools.h>
90 
91 // List of allowed command line options
92 #define GETOPTARGS "cdh"
93 
102 void usage(const char *name, const char *badparam)
103 {
104  fprintf(stdout, "\n\
105 Tests a control law with the following characteristics:\n\
106 - eye-in-hand control\n\
107 - articular velocity are computed\n\
108 - servo on 4 points,\n\
109 - internal and external camera view displays.\n\
110 \n\
111 SYNOPSIS\n\
112  %s [-c] [-d] [-h]\n", name);
113 
114  fprintf(stdout, "\n\
115 OPTIONS: Default\n\
116  -c\n\
117  Disable the mouse click. Useful to automaze the \n\
118  execution of this program without humain intervention.\n\
119 \n\
120  -d \n\
121  Turn off the display.\n\
122 \n\
123  -h\n\
124  Print the help.\n");
125 
126  if (badparam)
127  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
128 }
141 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
142 {
143  const char *optarg;
144  int c;
145  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
146 
147  switch (c) {
148  case 'c': click_allowed = false; break;
149  case 'd': display = false; break;
150  case 'h': usage(argv[0], NULL); return false; break;
151 
152  default:
153  usage(argv[0], optarg);
154  return false; break;
155  }
156  }
157 
158  if ((c == 1) || (c == -1)) {
159  // standalone param or error
160  usage(argv[0], NULL);
161  std::cerr << "ERROR: " << std::endl;
162  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
163  return false;
164  }
165 
166  return true;
167 }
168 
169 int
170 main(int argc, const char ** argv)
171 {
172 
173  // Log file creation in /tmp/$USERNAME/log.dat
174  // This file contains by line:
175  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
176  // - the 6 mesured camera velocities (m/s, rad/s)
177  // - the 6 mesured joint positions (m, rad)
178  // - the 8 values of s - s*
179  std::string username;
180  // Get the user login name
181  vpIoTools::getUserName(username);
182 
183  // Create a log filename to save velocities...
184  std::string logdirname;
185  logdirname ="C://" + username;
186 
187  // Test if the output path exist. If no try to create it
188  if (vpIoTools::checkDirectory(logdirname) == false) {
189  try {
190  // Create the dirname
191  vpIoTools::makeDirectory(logdirname);
192  }
193  catch (...) {
194  std::cerr << std::endl
195  << "ERROR:" << std::endl;
196  std::cerr << " Cannot create " << logdirname << std::endl;
197  exit(-1);
198  }
199  }
200  std::string logfilename;
201  logfilename = logdirname + "/log.dat";
202 
203  // Open the log file name
204  std::ofstream flog(logfilename.c_str());
205 
206 
207  bool opt_click_allowed = true;
208  bool opt_display = true;
209 
210  // Read the command line options
211  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
212  exit (-1);
213  }
214 
215  // We open two displays, one for the internal camera view, the other one for
216  // the external view, using either X11, GTK or GDI.
217 #if defined VISP_HAVE_X11
218  vpDisplayX displayInt;
219  vpDisplayX displayExt;
220 #elif defined VISP_HAVE_GTK
221  vpDisplayGTK displayInt;
222  vpDisplayGTK displayExt;
223 #elif defined VISP_HAVE_GDI
224  vpDisplayGDI displayInt;
225  vpDisplayGDI displayExt;
226 #endif
227 
228  // open a display for the visualization
229 
230  vpImage<unsigned char> Iint(300, 300, 0) ;
231  vpImage<unsigned char> Iext(300, 300, 0) ;
232 
233  if (opt_display) {
234  displayInt.init(Iint,0,0, "Internal view") ;
235  displayExt.init(Iext,330,000, "External view") ;
236 
237  }
238  vpProjectionDisplay externalview ;
239 
240  double px, py ; px = py = 500 ;
241  double u0, v0 ; u0 = 150, v0 = 160 ;
242 
243  vpCameraParameters cam(px,py,u0,v0);
244 
245  int i ;
246  vpServo task ;
247  vpRobotCamera robot ;
248 
249 
250  std::cout << std::endl ;
251  std::cout << "----------------------------------------------" << std::endl ;
252  std::cout << " Test program for vpServo " <<std::endl ;
253  std::cout << " Eye-in-hand task control, articular velocity are computed"
254  << std::endl ;
255  std::cout << " Simulation " << std::endl ;
256  std::cout << " task : servo 4 points " << std::endl ;
257  std::cout << "----------------------------------------------" << std::endl ;
258  std::cout << std::endl ;
259 
260  // #define TRANS_Z_PURE
261  // #define TRANS_X_PURE
262  // #define ROT_Z_PURE
263  // #define ROT_X_PURE
264 #define COMPLEX
265  //#define PROBLEM
266 
267 #if defined(TRANS_Z_PURE)
268  // sets the initial camera location
269  vpHomogeneousMatrix cMo(0, 0, 3,
270  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
271  // sets the desired camera location
272  vpHomogeneousMatrix cMod(0, 0, 2,
273  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
274 #elif defined(TRANS_X_PURE)
275  // sets the initial camera location
276  vpHomogeneousMatrix cMo(0.3, 0.3, 3,
277  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
278  // sets the desired camera location
279  vpHomogeneousMatrix cMod(0.5, 0.3, 3,
280  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
281 
282 #elif defined(ROT_Z_PURE)
283  // sets the initial camera location
284  vpHomogeneousMatrix cMo(0, 0, 3,
285  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
286  // sets the desired camera location
287  vpHomogeneousMatrix cMod(0, 0, 3,
288  vpMath::rad(0), vpMath::rad(0), vpMath::rad(180));
289 
290 #elif defined(ROT_X_PURE)
291  // sets the initial camera location
292  vpHomogeneousMatrix cMo(0, 0, 3,
293  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
294  // sets the desired camera location
295  vpHomogeneousMatrix cMod(0, 0, 3,
296  vpMath::rad(45), vpMath::rad(0), vpMath::rad(0));
297 
298 #elif defined(COMPLEX)
299  // sets the initial camera location
300  vpHomogeneousMatrix cMo(0.2, 0.2, 3,
301  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
302  // sets the desired camera location
303  vpHomogeneousMatrix cMod(0, 0, 2.5,
304  vpMath::rad(45), vpMath::rad(10), vpMath::rad(30));
305 
306 #elif defined(PROBLEM)
307  // Bad behavior with an interaction matrix computed from the desired features
308  // sets the initial camera location
309  vpHomogeneousMatrix cMo(0.2, 0.2, 3,
310  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
311  // sets the desired camera location
312  vpHomogeneousMatrix cMod(0.4, 0.2, 3,
313  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
314 
315 #endif
316  robot.setPosition(cMo) ;
317 
318  vpHomogeneousMatrix cextMo(0,0,6,
319  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
320 
321 
322  // sets the point coordinates in the object frame
323  vpPoint point[4] ;
324  point[0].setWorldCoordinates(-0.25,-0.25,0) ;
325  point[1].setWorldCoordinates(0.25,-0.25,0) ;
326  point[2].setWorldCoordinates(0.25,0.25,0) ;
327  point[3].setWorldCoordinates(-0.25,0.25,0) ;
328 
329 
330  for (i = 0 ; i < 4 ; i++)
331  externalview.insert(point[i]) ;
332 
333  // sets the desired position of the feature point s*"
334  vpFeaturePointPolar pd[4] ;
335 
336  // computes the point coordinates in the desired camera frame and
337  // its 2D coordinates
338  for (i = 0 ; i < 4 ; i++) {
339  point[i].track(cMod);
340  // Computes the polar coordinates from the image point
341  // cartesian coordinates
342  vpFeatureBuilder::create(pd[i],point[i]);
343  }
344 
345 
346  // computes the point coordinates in the camera frame and its 2D
347  // coordinates
348  for (i = 0 ; i < 4 ; i++)
349  point[i].track(cMo) ;
350 
351  // sets the desired position of the point
352  vpFeaturePointPolar p[4] ;
353  for (i = 0 ; i < 4 ; i++) {
354  // retrieve x,y and Z of the vpPoint structure to initialize the
355  // visual feature
356  vpFeatureBuilder::create(p[i], point[i]);
357  }
358 
359  // Define the task;
360  // - we want an eye-in-hand control law
361  // - articular velocity are computed
363  // task.setInteractionMatrixType(vpServo::MEAN) ;
364  // task.setInteractionMatrixType(vpServo::DESIRED) ;
366 
367 
368  // Set the position of the camera in the end-effector frame
369  vpHomogeneousMatrix cMe ;
370  vpVelocityTwistMatrix cVe(cMe) ;
371  task.set_cVe(cVe) ;
372 
373  // Set the Jacobian (expressed in the end-effector frame)
374  vpMatrix eJe ;
375  robot.get_eJe(eJe) ;
376  task.set_eJe(eJe) ;
377 
378  // we want to see a point on a point
379  for (i = 0 ; i < 4 ; i++)
380  task.addFeature(p[i],pd[i]) ;
381 
382  // set the gain
383  task.setLambda(1) ;
384 
385 
386  std::cout << "\nDisplay task information: " << std::endl;
387  task.print() ;
388 
389  unsigned int iter=0 ;
390  // loop
391  while(iter++ < 200) {
392  std::cout << "---------------------------------------------"
393  << iter <<std::endl ;
394  vpColVector v ;
395 
396 
397  // Set the Jacobian (expressed in the end-effector frame)
398  // Since q is modified eJe is modified
399  robot.get_eJe(eJe) ;
400  task.set_eJe(eJe) ;
401 
402  robot.getPosition(cMo) ;
403 
404  if (iter==1) {
405  std::cout <<"Initial robot position with respect to the object frame:\n";
406  cMo.print();
407  }
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 << "Error (s-s*): " << ( task.getError() ).t() << "\n";// s-s*
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 
471  "A click to continue...",vpColor::red);
472  vpDisplay::getClick(Iint);
473  vpDisplay::flush(Iint);
474  }
475 
476  }
477 
478 
479  flog.close() ; // Close the log file
480 
481  // Display task information
482  task.print() ;
483 
484  // Kill the task
485  task.kill();
486 
487  std::cout <<"Final robot position with respect to the object frame:\n";
488  cMo.print();
489 
490  if (opt_display && opt_click_allowed) {
491  // suppressed for automate test
492  std::cout << "\n\nClick in the internal view to end..." << std::endl;
493  vpDisplay::getClick(Iint) ;
494  }
495 }
496 #else
497 int
498 main()
499 {
500  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
501 }
502 
503 #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:289
void print()
Print the matrix as a vector [T thetaU].
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
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
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:250
void track(const vpHomogeneousMatrix &cMo)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:145
static const vpColor green
Definition: vpColor.h:168
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:227
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
Class that defines 2D image point visual feature with polar coordinates .
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static const vpColor red
Definition: vpColor.h:165
Class that defines what is a point.
Definition: vpPoint.h:65
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:298
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
Class that defines the simplest robot: a free flying camera.
Definition: vpRobotCamera.h:65
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:235
vpRowVector t() const
transpose of Vector
Generic class defining intrinsic camera parameters.
static std::string getUserName()
Definition: vpIoTools.cpp:136
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:143
void insert(vpForwardProjection &fp)
void getPosition(vpColVector &q)
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setPosition(const vpRobot::vpControlFrameType, const vpColVector &)
Set a displacement (frame has to be specified) in position control.
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor color, const bool &displayTraj=false)
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
void get_eJe(vpMatrix &_eJe)
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.
Definition: vpServo.h:150
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