ViSP  2.6.2
servoAfma6FourPoints2DCamVelocityInteractionCurrent.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6FourPoints2DCamVelocityInteractionCurrent.cpp 3616 2012-03-09 14:31:52Z 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  * tests the control law
36  * eye-in-hand control
37  * velocity computed in the camera frame
38  *
39  * Authors:
40  * Eric Marchand
41  * Fabien Spindler
42  *
43  *****************************************************************************/
44 
70 #include <visp/vpConfig.h>
71 #include <visp/vpDebug.h> // Debug trace
72 #include <stdlib.h>
73 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
74 
75 #include <visp/vp1394TwoGrabber.h>
76 #include <visp/vpImage.h>
77 #include <visp/vpImagePoint.h>
78 #include <visp/vpDisplay.h>
79 #include <visp/vpDisplayX.h>
80 
81 #include <visp/vpMath.h>
82 #include <visp/vpTranslationVector.h>
83 #include <visp/vpRxyzVector.h>
84 #include <visp/vpRotationMatrix.h>
85 #include <visp/vpHomogeneousMatrix.h>
86 #include <visp/vpFeaturePoint.h>
87 #include <visp/vpPoint.h>
88 #include <visp/vpServo.h>
89 #include <visp/vpFeatureBuilder.h>
90 #include <visp/vpDot.h>
91 #include <visp/vpRobotAfma6.h>
92 #include <visp/vpServoDisplay.h>
93 #include <visp/vpPose.h>
94 #include <visp/vpIoTools.h>
95 
96 // Exception
97 #include <visp/vpException.h>
98 #include <visp/vpMatrixException.h>
99 
100 #define L 0.05 // to deal with a 10cm by 10cm square
101 
102 
128 void compute_pose(vpPoint point[], vpDot2 dot[], int ndot,
129  vpCameraParameters cam,
130  vpHomogeneousMatrix &cMo,
131  vpTranslationVector &cto,
132  vpRxyzVector &cro, bool init)
133 {
134  vpHomogeneousMatrix cMo_dementhon; // computed pose with dementhon
135  vpHomogeneousMatrix cMo_lagrange; // computed pose with dementhon
136  vpRotationMatrix cRo;
137  vpPose pose;
138  vpImagePoint cog;
139  for (int i=0; i < ndot; i ++) {
140 
141  double x=0, y=0;
142 
143  cog = dot[i].getCog();
144  vpPixelMeterConversion::convertPoint(cam, cog, x, y) ; //pixel to meter conversion
145  // std::cout << "point cam: " << i << x << " " << y << std::endl;
146  point[i].set_x(x) ;//projection perspective p
147  point[i].set_y(y) ;
148  pose.addPoint(point[i]) ;
149  // std::cout << "point " << i << std::endl;
150  // point[i].print();
151 
152  }
153 
154  if (init == true) {
155  pose.computePose(vpPose::DEMENTHON, cMo_dementhon) ;
156  //compute the pose for a given method
157  // cMo_dementhon.extract(cto);
158  // cMo_dementhon.extract(cRo);
159  // cro.buildFrom(cRo);
160  // Compute and return the residual expressed in meter for the pose matrix
161  // 'cMo'
162  double residual_dementhon = pose.computeResidual(cMo_dementhon);
163 
164  // std::cout << "\nPose Dementhon "
165  // << "(residual: " << residual_dementhon << ")\n "
166  // << "cdto[0] = " << cto[0] << ";\n "
167  // << "cdto[1] = " << cto[1] << ";\n "
168  // << "cdto[2] = " << cto[2] << ";\n "
169  // << "cdro[0] = vpMath::rad(" << vpMath::deg(cro[0]) << ");\n "
170  // << "cdro[1] = vpMath::rad(" << vpMath::deg(cro[1]) << ");\n "
171  // << "cdro[2] = vpMath::rad(" << vpMath::deg(cro[2]) << ");\n"
172  // << std::endl;
173 
174  pose.computePose(vpPose::LAGRANGE, cMo_lagrange) ;
175  // cMo_lagrange.extract(cto);
176  // cMo_lagrange.extract(cRo);
177  // cro.buildFrom(cRo);
178  double residual_lagrange = pose.computeResidual(cMo_lagrange);
179 
180  // std::cout << "\nPose Lagrange "
181  // << "(residual: " << residual_lagrange << ")\n "
182  // << "cdto[0] = " << cto[0] << ";\n "
183  // << "cdto[1] = " << cto[1] << ";\n "
184  // << "cdto[2] = " << cto[2] << ";\n "
185  // << "cdro[0] = vpMath::rad(" << vpMath::deg(cro[0]) << ");\n "
186  // << "cdro[1] = vpMath::rad(" << vpMath::deg(cro[1]) << ");\n "
187  // << "cdro[2] = vpMath::rad(" << vpMath::deg(cro[2]) << ");\n"
188  // << std::endl;
189 
190  // cout << "Lagrange residual term: " << residual_lagrange <<endl ;
191 
192  // Select the best pose to initialize the lowe pose computation
193  if (residual_lagrange < residual_dementhon) //on garde le cMo
194  cMo = cMo_lagrange;
195  else
196  cMo = cMo_dementhon;
197 
198  // cout <<"------------------------------------------------------------"<<endl
199  }
200  else { // init = false; use of the previous pose to initialise LOWE
201  cRo.buildFrom(cro);
202  cMo.buildFrom(cto, cRo);
203  }
204  pose.computePose(vpPose::LOWE, cMo) ;
205  cMo.extract(cto);
206  cMo.extract(cRo);
207  cro.buildFrom(cRo);
208  // double residual_lowe = pose.computeResidual(cMo);
209 
210  // std::cout << "\nPose LOWE "
211  // << "(residual: " << residual_lowe << ")\n "
212  // << "cdto[0] = " << cto[0] << ";\n "
213  // << "cdto[1] = " << cto[1] << ";\n "
214  // << "cdto[2] = " << cto[2] << ";\n "
215  // << "cdro[0] = vpMath::rad(" << vpMath::deg(cro[0]) << ");\n "
216  // << "cdro[1] = vpMath::rad(" << vpMath::deg(cro[1]) << ");\n "
217  // << "cdro[2] = vpMath::rad(" << vpMath::deg(cro[2]) << ");\n"
218  // << std::endl;
219 
220  // vpTRACE( "LOWE pose :" ) ;
221  // std::cout << cMo << std::endl ;
222 }
223 
224 int
225 main()
226 {
227  // Log file creation in /tmp/$USERNAME/log.dat
228  // This file contains by line:
229  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
230  // - the 6 mesured joint velocities (m/s, rad/s)
231  // - the 6 mesured joint positions (m, rad)
232  // - the 8 values of s - s*
233  // - the 6 values of the pose cMo (tx,ty,tz, rx,ry,rz) with translation
234  // in meters and rotations in radians
235  std::string username;
236  // Get the user login name
237  vpIoTools::getUserName(username);
238 
239  // Create a log filename to save velocities...
240  std::string logdirname;
241  logdirname ="/tmp/" + username;
242 
243  // Test if the output path exist. If no try to create it
244  if (vpIoTools::checkDirectory(logdirname) == false) {
245  try {
246  // Create the dirname
247  vpIoTools::makeDirectory(logdirname);
248  }
249  catch (...) {
250  std::cerr << std::endl
251  << "ERROR:" << std::endl;
252  std::cerr << " Cannot create " << logdirname << std::endl;
253  exit(-1);
254  }
255  }
256  std::string logfilename;
257  logfilename = logdirname + "/log.dat";
258 
259  // Open the log file name
260  std::ofstream flog(logfilename.c_str());
261 
262  try
263  {
264  vpServo task ;
265 
267  int i ;
268 
272  g.open(I) ;
273 
274  vpDisplayX display(I,100,100,"Current image") ;
275 
276  g.acquire(I) ;
277 
278  vpDisplay::display(I) ;
279  vpDisplay::flush(I) ;
280 
281  std::cout << std::endl ;
282  std::cout << "-------------------------------------------------------" << std::endl ;
283  std::cout << " Test program for vpServo " <<std::endl ;
284  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
285  std::cout << " Use of the Afma6 robot " << std::endl ;
286  std::cout << " Interaction matrix computed with the current features " << std::endl ;
287  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl ;
288  std::cout << "-------------------------------------------------------" << std::endl ;
289  std::cout << std::endl ;
290 
291 
292  vpDot2 dot[4] ;
293  vpImagePoint cog;
294 
295  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..."
296  << std::endl;
297  for (i=0 ; i < 4 ; i++) {
298  dot[i].initTracking(I) ;
299  cog = dot[i].getCog();
301  vpDisplay::flush(I);
302  }
303 
306  vpRobotAfma6 robot;
307 
308  // Load the end-effector to camera frame transformation obtained
309  // using a camera intrinsic model with distortion
310  robot.init(vpAfma6::TOOL_CCMOP, projModel);
311 
312  vpCameraParameters cam ;
313  // Update camera parameters
314  robot.getCameraParameters (cam, I);
315 
316  // Sets the current position of the visual feature
317  vpFeaturePoint p[4] ;
318  for (i=0 ; i < 4 ; i++)
319  vpFeatureBuilder::create(p[i], cam, dot[i]); //retrieve x,y of the vpFeaturePoint structure
320 
321  // Set the position of the square target in a frame which origin is
322  // centered in the middle of the square
323  vpPoint point[4] ;
324  point[0].setWorldCoordinates(-L, -L, 0) ;
325  point[1].setWorldCoordinates( L, -L, 0) ;
326  point[2].setWorldCoordinates( L, L, 0) ;
327  point[3].setWorldCoordinates(-L, L, 0) ;
328 
329  // Initialise a desired pose to compute s*, the desired 2D point features
331  vpTranslationVector cto(0, 0, 0.7); // tz = 0.7 meter
332  vpRxyzVector cro(vpMath::rad(0), vpMath::rad(0), vpMath::rad(0)); // No rotations
333  vpRotationMatrix cRo(cro); // Build the rotation matrix
334  cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
335 
336  // Sets the desired position of the 2D visual feature
337  vpFeaturePoint pd[4] ;
338  // Compute the desired position of the features from the desired pose
339  for (int i=0; i < 4; i ++) {
340  vpColVector cP, p ;
341  point[i].changeFrame(cMo, cP) ;
342  point[i].projection(cP, p) ;
343 
344  pd[i].set_x(p[0]) ;
345  pd[i].set_y(p[1]) ;
346  pd[i].set_Z(cP[2]);
347  }
348 
349  // Define the task
350  // - we want an eye-in-hand control law
351  // - robot is controlled in the camera frame
352  // - Interaction matrix is computed with the current visual features
355 
356  // We want to see a point on a point
357  std::cout << std::endl ;
358  for (i=0 ; i < 4 ; i++)
359  task.addFeature(p[i],pd[i]) ;
360 
361  // Set the proportional gain
362  task.setLambda(0.1) ;
363 
364  // Display task information
365  task.print() ;
366 
367  // Initialise the velocity control of the robot
369 
370  // Initialise the pose using Lagrange and Dementhon methods, chose the best
371  // estimated pose (either Lagrange or Dementhon) and than compute the pose
372  // using LOWE method with Lagrange or Dementhon pose as initialisation.
373  // compute_pose(point, dot, 4, cam, cMo, cto, cro, true);
374 
375  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
376 
377  for ( ; ; ) {
378  // Acquire a new image from the camera
379  g.acquire(I) ;
380 
381  // Display this image
382  vpDisplay::display(I) ;
383 
384  // For each point...
385  for (i=0 ; i < 4 ; i++) {
386  // Achieve the tracking of the dot in the image
387  dot[i].track(I) ;
388  // Get the dot cog
389  cog = dot[i].getCog();
390  // Display a green cross at the center of gravity position in the
391  // image
393  }
394 
395  // During the servo, we compute the pose using LOWE method. For the
396  // initial pose used in the non linear minimisation we use the pose
397  // computed at the previous iteration.
398  compute_pose(point, dot, 4, cam, cMo, cto, cro, false);
399 
400  for (i=0 ; i < 4 ; i++) {
401  // Update the point feature from the dot location
402  vpFeatureBuilder::create(p[i], cam, dot[i]);
403  // Set the feature Z coordinate from the pose
404  vpColVector cP;
405  point[i].changeFrame(cMo, cP) ;
406 
407  p[i].set_Z(cP[2]);
408  }
409 
410  // Printing on stdout concerning task information
411  // task.print() ;
412 
413  vpColVector v ;
414  // Compute the visual servoing skew vector
415  v = task.computeControlLaw() ;
416 
417  // Display the current and desired feature points in the image display
418  vpServoDisplay::display(task, cam, I);
419 
420  // Apply the computed camera velocities to the robot
422 
423  // Save velocities applied to the robot in the log file
424  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
425  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
426  flog << v[0] << " " << v[1] << " " << v[2] << " "
427  << v[3] << " " << v[4] << " " << v[5] << " ";
428 
429  // Get the measured joint velocities of the robot
430  vpColVector qvel;
432  // Save measured joint velocities of the robot in the log file:
433  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
434  // velocities in m/s
435  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
436  // velocities in rad/s
437  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
438  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
439 
440  // Get the measured joint positions of the robot
441  vpColVector q;
443  // Save measured joint positions of the robot in the log file
444  // - q[0], q[1], q[2] correspond to measured joint translation
445  // positions in m
446  // - q[3], q[4], q[5] correspond to measured joint rotation
447  // positions in rad
448  flog << q[0] << " " << q[1] << " " << q[2] << " "
449  << q[3] << " " << q[4] << " " << q[5] << " ";
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 points
455 
456  // Save the current cMo pose: translations in meters, rotations (rx, ry,
457  // rz) in radians
458  flog << cto[0] << " " << cto[1] << " " << cto[2] << " " // translation
459  << cro[0] << " " << cro[1] << " " << cro[2] << std::endl; // rot
460 
461  // Flush the display
462  vpDisplay::flush(I) ;
463  }
464 
465  flog.close() ; // Close the log file
466 
467  // Display task information
468  task.print() ;
469 
470  // Kill the task
471  task.kill();
472 
473  return 0;
474  }
475  catch (...) {
476  flog.close() ; // Close the log file
477 
478  vpERROR_TRACE(" Test failed") ;
479  return 0;
480  }
481 }
482 
483 #else
484 int
485 main()
486 {
487  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
488 
489 }
490 
491 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
void projection(const vpColVector &_cP, vpColVector &_p)
Projection onto the image plane of a point. Input: the 3D coordinates in the camera frame _cP...
Definition: vpPoint.cpp:132
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
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
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
Definition: vpAfma6.cpp:1226
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 setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:250
void set_x(const double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.h:183
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Point coordinates conversion from pixel coordinates to normalized coordinates in meter...
static const vpColor green
Definition: vpColor.h:168
void acquire(vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:114
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:439
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
void set_y(const double y)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:214
Class that defines what is a point.
Definition: vpPoint.h:65
The vpRotationMatrix considers the particular case of a rotation matrix.
void init(void)
vpImagePoint getCog() const
Definition: vpDot2.h:254
void set_x(const double x)
vpRotationMatrix buildFrom(const vpThetaUVector &v)
Transform a vector vpThetaUVector into an rotation matrix.
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
void open(vpImage< unsigned char > &I)
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
Initialize the velocity controller.
Definition: vpRobot.h:70
vpColVector getError() const
Definition: vpServo.h:298
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Class used for pose computation from N points (pose from point only).
Definition: vpPose.h:80
double computeResidual(vpHomogeneousMatrix &cMo)
Compute and return the residual expressed in meter for the pose matrix 'cMo'.
Definition: vpPose.cpp:255
Generic class defining intrinsic camera parameters.
void set_y(const double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.h:185
static std::string getUserName()
Definition: vpIoTools.cpp:136
void extract(vpRotationMatrix &R) const
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Perspective projection with distortion model.
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
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
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:240
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
Class that consider the case of the Euler angle using the x-y-z convention, where are respectively ...
Definition: vpRxyzVector.h:152
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo)
compute the pose for a given method
Definition: vpPose.cpp:298
void set_Z(const double Z)
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 changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &_cP)
Definition: vpPoint.cpp:150
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:148
void buildFrom(const double phi, const double theta, const double psi)
Definition: vpRxyzVector.h:188
Class that consider the case of a translation vector.
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
static const vpColor blue
Definition: vpColor.h:171
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