Visual Servoing Platform  version 3.0.1
servoAfma6FourPoints2DCamVelocityInteractionCurrent.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * tests the control law
32  * eye-in-hand control
33  * velocity computed in the camera frame
34  *
35  * Authors:
36  * Eric Marchand
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
66 #include <visp3/core/vpConfig.h>
67 #include <visp3/core/vpDebug.h> // Debug trace
68 #include <stdlib.h>
69 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394))
70 
71 #include <visp3/sensor/vp1394TwoGrabber.h>
72 #include <visp3/core/vpImage.h>
73 #include <visp3/core/vpImagePoint.h>
74 #include <visp3/core/vpDisplay.h>
75 #include <visp3/gui/vpDisplayX.h>
76 #include <visp3/gui/vpDisplayOpenCV.h>
77 #include <visp3/gui/vpDisplayGTK.h>
78 
79 #include <visp3/core/vpMath.h>
80 #include <visp3/core/vpTranslationVector.h>
81 #include <visp3/core/vpRxyzVector.h>
82 #include <visp3/core/vpRotationMatrix.h>
83 #include <visp3/core/vpHomogeneousMatrix.h>
84 #include <visp3/visual_features/vpFeaturePoint.h>
85 #include <visp3/core/vpPoint.h>
86 #include <visp3/vs/vpServo.h>
87 #include <visp3/visual_features/vpFeatureBuilder.h>
88 #include <visp3/blob/vpDot.h>
89 #include <visp3/robot/vpRobotAfma6.h>
90 #include <visp3/vs/vpServoDisplay.h>
91 #include <visp3/vision/vpPose.h>
92 #include <visp3/core/vpIoTools.h>
93 
94 // Exception
95 #include <visp3/core/vpException.h>
96 
97 #define L 0.05 // to deal with a 10cm by 10cm square
98 
99 
125 void compute_pose(vpPoint point[], vpDot2 dot[], int ndot,
126  vpCameraParameters cam,
127  vpHomogeneousMatrix &cMo,
128  vpTranslationVector &cto,
129  vpRxyzVector &cro, bool init)
130 {
131  vpHomogeneousMatrix cMo_dementhon; // computed pose with dementhon
132  vpHomogeneousMatrix cMo_lagrange; // computed pose with dementhon
133  vpRotationMatrix cRo;
134  vpPose pose;
135  vpImagePoint cog;
136  for (int i=0; i < ndot; i ++) {
137 
138  double x=0, y=0;
139 
140  cog = dot[i].getCog();
141  vpPixelMeterConversion::convertPoint(cam, cog, x, y) ; //pixel to meter conversion
142  // std::cout << "point cam: " << i << x << " " << y << std::endl;
143  point[i].set_x(x) ;//projection perspective p
144  point[i].set_y(y) ;
145  pose.addPoint(point[i]) ;
146  // std::cout << "point " << i << std::endl;
147  // point[i].print();
148 
149  }
150 
151  if (init == true) {
152  pose.computePose(vpPose::DEMENTHON, cMo_dementhon) ;
153  //compute the pose for a given method
154  // cMo_dementhon.extract(cto);
155  // cMo_dementhon.extract(cRo);
156  // cro.buildFrom(cRo);
157  // Compute and return the residual expressed in meter for the pose matrix
158  // 'cMo'
159  double residual_dementhon = pose.computeResidual(cMo_dementhon);
160 
161  // std::cout << "\nPose Dementhon "
162  // << "(residual: " << residual_dementhon << ")\n "
163  // << "cdto[0] = " << cto[0] << ";\n "
164  // << "cdto[1] = " << cto[1] << ";\n "
165  // << "cdto[2] = " << cto[2] << ";\n "
166  // << "cdro[0] = vpMath::rad(" << vpMath::deg(cro[0]) << ");\n "
167  // << "cdro[1] = vpMath::rad(" << vpMath::deg(cro[1]) << ");\n "
168  // << "cdro[2] = vpMath::rad(" << vpMath::deg(cro[2]) << ");\n"
169  // << std::endl;
170 
171  pose.computePose(vpPose::LAGRANGE, cMo_lagrange) ;
172  // cMo_lagrange.extract(cto);
173  // cMo_lagrange.extract(cRo);
174  // cro.buildFrom(cRo);
175  double residual_lagrange = pose.computeResidual(cMo_lagrange);
176 
177  // std::cout << "\nPose Lagrange "
178  // << "(residual: " << residual_lagrange << ")\n "
179  // << "cdto[0] = " << cto[0] << ";\n "
180  // << "cdto[1] = " << cto[1] << ";\n "
181  // << "cdto[2] = " << cto[2] << ";\n "
182  // << "cdro[0] = vpMath::rad(" << vpMath::deg(cro[0]) << ");\n "
183  // << "cdro[1] = vpMath::rad(" << vpMath::deg(cro[1]) << ");\n "
184  // << "cdro[2] = vpMath::rad(" << vpMath::deg(cro[2]) << ");\n"
185  // << std::endl;
186 
187  // cout << "Lagrange residual term: " << residual_lagrange <<endl ;
188 
189  // Select the best pose to initialize the lowe pose computation
190  if (residual_lagrange < residual_dementhon) //on garde le cMo
191  cMo = cMo_lagrange;
192  else
193  cMo = cMo_dementhon;
194 
195  // cout <<"------------------------------------------------------------"<<endl
196  }
197  else { // init = false; use of the previous pose to initialise LOWE
198  cRo.buildFrom(cro);
199  cMo.buildFrom(cto, cRo);
200  }
201  pose.computePose(vpPose::LOWE, cMo) ;
202  cMo.extract(cto);
203  cMo.extract(cRo);
204  cro.buildFrom(cRo);
205  // double residual_lowe = pose.computeResidual(cMo);
206 
207  // std::cout << "\nPose LOWE "
208  // << "(residual: " << residual_lowe << ")\n "
209  // << "cdto[0] = " << cto[0] << ";\n "
210  // << "cdto[1] = " << cto[1] << ";\n "
211  // << "cdto[2] = " << cto[2] << ";\n "
212  // << "cdro[0] = vpMath::rad(" << vpMath::deg(cro[0]) << ");\n "
213  // << "cdro[1] = vpMath::rad(" << vpMath::deg(cro[1]) << ");\n "
214  // << "cdro[2] = vpMath::rad(" << vpMath::deg(cro[2]) << ");\n"
215  // << std::endl;
216 
217  // vpTRACE( "LOWE pose :" ) ;
218  // std::cout << cMo << std::endl ;
219 }
220 
221 int
222 main()
223 {
224  // Log file creation in /tmp/$USERNAME/log.dat
225  // This file contains by line:
226  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
227  // - the 6 mesured joint velocities (m/s, rad/s)
228  // - the 6 mesured joint positions (m, rad)
229  // - the 8 values of s - s*
230  // - the 6 values of the pose cMo (tx,ty,tz, rx,ry,rz) with translation
231  // in meters and rotations in radians
232  std::string username;
233  // Get the user login name
234  vpIoTools::getUserName(username);
235 
236  // Create a log filename to save velocities...
237  std::string logdirname;
238  logdirname ="/tmp/" + username;
239 
240  // Test if the output path exist. If no try to create it
241  if (vpIoTools::checkDirectory(logdirname) == false) {
242  try {
243  // Create the dirname
244  vpIoTools::makeDirectory(logdirname);
245  }
246  catch (...) {
247  std::cerr << std::endl
248  << "ERROR:" << std::endl;
249  std::cerr << " Cannot create " << logdirname << std::endl;
250  exit(-1);
251  }
252  }
253  std::string logfilename;
254  logfilename = logdirname + "/log.dat";
255 
256  // Open the log file name
257  std::ofstream flog(logfilename.c_str());
258 
259  try
260  {
261  vpServo task ;
262 
264  int i ;
265 
269  g.open(I) ;
270 
271 #ifdef VISP_HAVE_X11
272  vpDisplayX display(I,100,100,"Current image") ;
273 #elif defined(VISP_HAVE_OPENCV)
274  vpDisplayOpenCV display(I,100,100,"Current image") ;
275 #elif defined(VISP_HAVE_GTK)
276  vpDisplayGTK display(I,100,100,"Current image") ;
277 #endif
278 
279  g.acquire(I) ;
280 
281  vpDisplay::display(I) ;
282  vpDisplay::flush(I) ;
283 
284  std::cout << std::endl ;
285  std::cout << "-------------------------------------------------------" << std::endl ;
286  std::cout << " Test program for vpServo " <<std::endl ;
287  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
288  std::cout << " Use of the Afma6 robot " << std::endl ;
289  std::cout << " Interaction matrix computed with the current features " << std::endl ;
290  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl ;
291  std::cout << "-------------------------------------------------------" << std::endl ;
292  std::cout << std::endl ;
293 
294 
295  vpDot2 dot[4] ;
296  vpImagePoint cog;
297 
298  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..."
299  << std::endl;
300  for (i=0 ; i < 4 ; i++) {
301  dot[i].initTracking(I) ;
302  cog = dot[i].getCog();
304  vpDisplay::flush(I);
305  }
306 
309  vpRobotAfma6 robot;
310 
311  // Load the end-effector to camera frame transformation obtained
312  // using a camera intrinsic model with distortion
313  robot.init(vpAfma6::TOOL_CCMOP, projModel);
314 
315  vpCameraParameters cam ;
316  // Update camera parameters
317  robot.getCameraParameters (cam, I);
318 
319  // Sets the current position of the visual feature
320  vpFeaturePoint p[4] ;
321  for (i=0 ; i < 4 ; i++)
322  vpFeatureBuilder::create(p[i], cam, dot[i]); //retrieve x,y of the vpFeaturePoint structure
323 
324  // Set the position of the square target in a frame which origin is
325  // centered in the middle of the square
326  vpPoint point[4] ;
327  point[0].setWorldCoordinates(-L, -L, 0) ;
328  point[1].setWorldCoordinates( L, -L, 0) ;
329  point[2].setWorldCoordinates( L, L, 0) ;
330  point[3].setWorldCoordinates(-L, L, 0) ;
331 
332  // Initialise a desired pose to compute s*, the desired 2D point features
334  vpTranslationVector cto(0, 0, 0.7); // tz = 0.7 meter
335  vpRxyzVector cro(vpMath::rad(0), vpMath::rad(0), vpMath::rad(0)); // No rotations
336  vpRotationMatrix cRo(cro); // Build the rotation matrix
337  cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
338 
339  // Sets the desired position of the 2D visual feature
340  vpFeaturePoint pd[4] ;
341  // Compute the desired position of the features from the desired pose
342  for (int i=0; i < 4; i ++) {
343  vpColVector cP, p ;
344  point[i].changeFrame(cMo, cP) ;
345  point[i].projection(cP, p) ;
346 
347  pd[i].set_x(p[0]) ;
348  pd[i].set_y(p[1]) ;
349  pd[i].set_Z(cP[2]);
350  }
351 
352  // Define the task
353  // - we want an eye-in-hand control law
354  // - robot is controlled in the camera frame
355  // - Interaction matrix is computed with the current visual features
358 
359  // We want to see a point on a point
360  std::cout << std::endl ;
361  for (i=0 ; i < 4 ; i++)
362  task.addFeature(p[i],pd[i]) ;
363 
364  // Set the proportional gain
365  task.setLambda(0.1) ;
366 
367  // Display task information
368  task.print() ;
369 
370  // Initialise the velocity control of the robot
372 
373  // Initialise the pose using Lagrange and Dementhon methods, chose the best
374  // estimated pose (either Lagrange or Dementhon) and than compute the pose
375  // using LOWE method with Lagrange or Dementhon pose as initialisation.
376  // compute_pose(point, dot, 4, cam, cMo, cto, cro, true);
377 
378  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
379 
380  for ( ; ; ) {
381  // Acquire a new image from the camera
382  g.acquire(I) ;
383 
384  // Display this image
385  vpDisplay::display(I) ;
386 
387  // For each point...
388  for (i=0 ; i < 4 ; i++) {
389  // Achieve the tracking of the dot in the image
390  dot[i].track(I) ;
391  // Get the dot cog
392  cog = dot[i].getCog();
393  // Display a green cross at the center of gravity position in the
394  // image
396  }
397 
398  // During the servo, we compute the pose using LOWE method. For the
399  // initial pose used in the non linear minimisation we use the pose
400  // computed at the previous iteration.
401  compute_pose(point, dot, 4, cam, cMo, cto, cro, false);
402 
403  for (i=0 ; i < 4 ; i++) {
404  // Update the point feature from the dot location
405  vpFeatureBuilder::create(p[i], cam, dot[i]);
406  // Set the feature Z coordinate from the pose
407  vpColVector cP;
408  point[i].changeFrame(cMo, cP) ;
409 
410  p[i].set_Z(cP[2]);
411  }
412 
413  // Printing on stdout concerning task information
414  // task.print() ;
415 
416  vpColVector v ;
417  // Compute the visual servoing skew vector
418  v = task.computeControlLaw() ;
419 
420  // Display the current and desired feature points in the image display
421  vpServoDisplay::display(task, cam, I);
422 
423  // Apply the computed camera velocities to the robot
425 
426  // Save velocities applied to the robot in the log file
427  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
428  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
429  flog << v[0] << " " << v[1] << " " << v[2] << " "
430  << v[3] << " " << v[4] << " " << v[5] << " ";
431 
432  // Get the measured joint velocities of the robot
433  vpColVector qvel;
435  // Save measured joint velocities of the robot in the log file:
436  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
437  // velocities in m/s
438  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
439  // velocities in rad/s
440  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
441  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
442 
443  // Get the measured joint positions of the robot
444  vpColVector q;
446  // Save measured joint positions of the robot in the log file
447  // - q[0], q[1], q[2] correspond to measured joint translation
448  // positions in m
449  // - q[3], q[4], q[5] correspond to measured joint rotation
450  // positions in rad
451  flog << q[0] << " " << q[1] << " " << q[2] << " "
452  << q[3] << " " << q[4] << " " << q[5] << " ";
453 
454  // Save feature error (s-s*) for the 4 feature points. For each feature
455  // point, we have 2 errors (along x and y axis). This error is expressed
456  // in meters in the camera frame
457  flog << ( task.getError() ).t()<< " "; // s-s* for points
458 
459  // Save the current cMo pose: translations in meters, rotations (rx, ry,
460  // rz) in radians
461  flog << cto[0] << " " << cto[1] << " " << cto[2] << " " // translation
462  << cro[0] << " " << cro[1] << " " << cro[2] << std::endl; // rot
463 
464  // Flush the display
465  vpDisplay::flush(I) ;
466  }
467 
468  flog.close() ; // Close the log file
469 
470  // Display task information
471  task.print() ;
472 
473  // Kill the task
474  task.kill();
475 
476  return 0;
477  }
478  catch (...) {
479  flog.close() ; // Close the log file
480 
481  vpERROR_TRACE(" Test failed") ;
482  return 0;
483  }
484 }
485 
486 #else
487 int
488 main()
489 {
490  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
491 
492 }
493 
494 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
vpRxyzVector buildFrom(const vpRotationMatrix &R)
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1235
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:229
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
Implementation of an homogeneous matrix and operations on such kind of matrices.
#define vpERROR_TRACE
Definition: vpDebug.h:391
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
void set_x(const double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:496
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...
void extract(vpRotationMatrix &R) const
static const vpColor green
Definition: vpColor.h:166
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:125
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
static void flush(const vpImage< unsigned char > &I)
void set_y(const double y)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Control of Irisa&#39;s gantry robot named Afma6.
Definition: vpRobotAfma6.h:210
Class that defines what is a point.
Definition: vpPoint.h:59
Implementation of a rotation matrix and operations on such kind of matrices.
void init(void)
void set_x(const double x)
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
void open(vpImage< unsigned char > &I)
void kill()
Definition: vpServo.cpp:191
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:76
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:391
void set_y(const double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:498
static std::string getUserName()
Definition: vpIoTools.cpp:177
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
Definition: vpPose.cpp:372
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Perspective projection with distortion model.
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of column vector and the associated operations.
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:262
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:314
vpImagePoint getCog() const
Definition: vpDot2.h:161
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:154
vpColVector getError() const
Definition: vpServo.h:271
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
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:88
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix &#39;cMo&#39;.
Definition: vpPose.cpp:337
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &_cP)
Definition: vpPoint.cpp:247
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:145
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
Class that consider the case of a translation vector.
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)
static const vpColor blue
Definition: vpColor.h:169