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