ViSP  2.9.0
servoViper850FourPoints2DArtVelocityInteractionCurrent.cpp
1 /****************************************************************************
2  *
3  * $Id: servoViper850FourPoints2DArtVelocityInteractionCurrent.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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 articular frame
38  *
39  * Authors:
40  * Fabien Spindler
41  *
42  *****************************************************************************/
55 #include <visp/vpConfig.h>
56 #include <visp/vpDebug.h> // Debug trace
57 
58 #include <stdio.h>
59 #include <iostream>
60 #include <fstream>
61 #include <sstream>
62 #include <stdlib.h>
63 #if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394_2))
64 
65 #include <visp/vp1394TwoGrabber.h>
66 #include <visp/vpDisplay.h>
67 #include <visp/vpDisplayGTK.h>
68 #include <visp/vpDisplayX.h>
69 #include <visp/vpDisplayOpenCV.h>
70 #include <visp/vpDot2.h>
71 #include <visp/vpFeatureBuilder.h>
72 #include <visp/vpFeaturePoint.h>
73 #include <visp/vpHomogeneousMatrix.h>
74 #include <visp/vpImage.h>
75 #include <visp/vpIoTools.h>
76 #include <visp/vpMath.h>
77 #include <visp/vpPoint.h>
78 #include <visp/vpPose.h>
79 #include <visp/vpRobotViper850.h>
80 #include <visp/vpServo.h>
81 #include <visp/vpServoDisplay.h>
82 
83 #define L 0.05 // to deal with a 10cm by 10cm square
84 
85 
111 void compute_pose(vpPoint point[], vpDot2 dot[], int ndot,
112  vpCameraParameters cam,
113  vpHomogeneousMatrix &cMo,
114  vpTranslationVector &cto,
115  vpRxyzVector &cro, bool init)
116 {
117  vpHomogeneousMatrix cMo_dementhon; // computed pose with dementhon
118  vpHomogeneousMatrix cMo_lagrange; // computed pose with dementhon
119  vpRotationMatrix cRo;
120  vpPose pose;
121  vpImagePoint cog;
122  for (int i=0; i < ndot; i ++) {
123 
124  double x=0, y=0;
125  cog = dot[i].getCog();
127  cog,
128  x, y) ; //pixel to meter conversion
129  point[i].set_x(x) ;//projection perspective p
130  point[i].set_y(y) ;
131  pose.addPoint(point[i]) ;
132  }
133 
134  if (init == true) {
135  pose.computePose(vpPose::DEMENTHON, cMo_dementhon) ;
136  // Compute and return the residual expressed in meter for the pose matrix
137  // 'cMo'
138  double residual_dementhon = pose.computeResidual(cMo_dementhon);
139  pose.computePose(vpPose::LAGRANGE, cMo_lagrange) ;
140  double residual_lagrange = pose.computeResidual(cMo_lagrange);
141 
142  // Select the best pose to initialize the lowe pose computation
143  if (residual_lagrange < residual_dementhon)
144  cMo = cMo_lagrange;
145  else
146  cMo = cMo_dementhon;
147 
148  }
149  else { // init = false; use of the previous pose to initialise LOWE
150  cRo.buildFrom(cro);
151  cMo.buildFrom(cto, cRo);
152  }
153  pose.computePose(vpPose::LOWE, cMo) ;
154  cMo.extract(cto);
155  cMo.extract(cRo);
156  cro.buildFrom(cRo);
157 }
158 
159 int
160 main()
161 {
162  // Log file creation in /tmp/$USERNAME/log.dat
163  // This file contains by line:
164  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
165  // - the 6 mesured joint velocities (m/s, rad/s)
166  // - the 6 mesured joint positions (m, rad)
167  // - the 8 values of s - s*
168  std::string username;
169  // Get the user login name
170  vpIoTools::getUserName(username);
171 
172  // Create a log filename to save velocities...
173  std::string logdirname;
174  logdirname ="/tmp/" + username;
175 
176  // Test if the output path exist. If no try to create it
177  if (vpIoTools::checkDirectory(logdirname) == false) {
178  try {
179  // Create the dirname
180  vpIoTools::makeDirectory(logdirname);
181  }
182  catch (...) {
183  std::cerr << std::endl
184  << "ERROR:" << std::endl;
185  std::cerr << " Cannot create " << logdirname << std::endl;
186  return(-1);
187  }
188  }
189  std::string logfilename;
190  logfilename = logdirname + "/log.dat";
191 
192  // Open the log file name
193  std::ofstream flog(logfilename.c_str());
194 
195  try {
196  vpRobotViper850 robot ;
197  // Load the end-effector to camera frame transformation obtained
198  // using a camera intrinsic model with distortion
202 
203  vpServo task ;
204 
206  int i ;
207 
208  bool reset = false;
209  vp1394TwoGrabber g(reset);
211  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
212  g.open(I) ;
213 
214  g.acquire(I) ;
215 
216 #ifdef VISP_HAVE_X11
217  vpDisplayX display(I,100,100,"Current image") ;
218 #elif defined(VISP_HAVE_OPENCV)
219  vpDisplayOpenCV display(I,100,100,"Current image") ;
220 #elif defined(VISP_HAVE_GTK)
221  vpDisplayGTK display(I,100,100,"Current image") ;
222 #endif
223 
224  vpDisplay::display(I) ;
225  vpDisplay::flush(I) ;
226 
227  std::cout << std::endl ;
228  std::cout << "-------------------------------------------------------" << std::endl ;
229  std::cout << " Test program for vpServo " <<std::endl ;
230  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
231  std::cout << " Use of the Afma6 robot " << std::endl ;
232  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl ;
233  std::cout << "-------------------------------------------------------" << std::endl ;
234  std::cout << std::endl ;
235 
236 
237  vpDot2 dot[4] ;
238  vpImagePoint cog;
239 
240  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..."
241  << std::endl;
242 
243  for (i=0 ; i < 4 ; i++) {
244  dot[i].setGraphics(true) ;
245  dot[i].initTracking(I) ;
246  cog = dot[i].getCog();
248  vpDisplay::flush(I);
249  }
250 
251  vpCameraParameters cam ;
252 
253  // Update camera parameters
254  robot.getCameraParameters (cam, I);
255 
256  cam.printParameters();
257 
258 
259  // Sets the current position of the visual feature
260  vpFeaturePoint p[4] ;
261  for (i=0 ; i < 4 ; i++)
262  vpFeatureBuilder::create(p[i], cam, dot[i]); //retrieve x,y of the vpFeaturePoint structure
263 
264  // Set the position of the square target in a frame which origin is
265  // centered in the middle of the square
266  vpPoint point[4] ;
267  point[0].setWorldCoordinates(-L, -L, 0) ;
268  point[1].setWorldCoordinates( L, -L, 0) ;
269  point[2].setWorldCoordinates( L, L, 0) ;
270  point[3].setWorldCoordinates(-L, L, 0) ;
271 
272  // Initialise a desired pose to compute s*, the desired 2D point features
274  vpTranslationVector cto(0, 0, 0.5); // tz = 0.5 meter
276  vpRotationMatrix cRo(cro); // Build the rotation matrix
277  cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
278 
279  // Sets the desired position of the 2D visual feature
280  vpFeaturePoint pd[4] ;
281  // Compute the desired position of the features from the desired pose
282  for (int i=0; i < 4; i ++) {
283  vpColVector cP, p ;
284  point[i].changeFrame(cMo, cP) ;
285  point[i].projection(cP, p) ;
286 
287  pd[i].set_x(p[0]) ;
288  pd[i].set_y(p[1]) ;
289  pd[i].set_Z(cP[2]);
290  }
291 
292  // We want to see a point on a point
293  for (i=0 ; i < 4 ; i++)
294  task.addFeature(p[i],pd[i]) ;
295 
296  // Set the proportional gain
297  task.setLambda(0.3) ;
298 
299  // Display task information
300  task.print() ;
301 
302  // Define the task
303  // - we want an eye-in-hand control law
304  // - articular velocity are computed
307  task.print() ;
308 
310  robot.get_cVe(cVe) ;
311  task.set_cVe(cVe) ;
312  task.print() ;
313 
314  // Set the Jacobian (expressed in the end-effector frame)
315  vpMatrix eJe ;
316  robot.get_eJe(eJe) ;
317  task.set_eJe(eJe) ;
318  task.print() ;
319 
320  // Initialise the velocity control of the robot
322 
323  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
324  for ( ; ; ) {
325  // Acquire a new image from the camera
326  g.acquire(I) ;
327 
328  // Display this image
329  vpDisplay::display(I) ;
330 
331  try {
332  // For each point...
333  for (i=0 ; i < 4 ; i++) {
334  // Achieve the tracking of the dot in the image
335  dot[i].track(I) ;
336  // Display a green cross at the center of gravity position in the
337  // image
338  cog = dot[i].getCog();
340  }
341  }
342  catch(...) {
343  flog.close() ; // Close the log file
344  vpTRACE("Error detected while tracking visual features") ;
345  robot.stopMotion() ;
346  return(1) ;
347  }
348 
349  // During the servo, we compute the pose using LOWE method. For the
350  // initial pose used in the non linear minimisation we use the pose
351  // computed at the previous iteration.
352  compute_pose(point, dot, 4, cam, cMo, cto, cro, false);
353 
354  for (i=0 ; i < 4 ; i++) {
355  // Update the point feature from the dot location
356  vpFeatureBuilder::create(p[i], cam, dot[i]);
357  // Set the feature Z coordinate from the pose
358  vpColVector cP;
359  point[i].changeFrame(cMo, cP) ;
360 
361  p[i].set_Z(cP[2]);
362  }
363 
364  // Get the jacobian of the robot
365  robot.get_eJe(eJe) ;
366  // Update this jacobian in the task structure. It will be used to compute
367  // the velocity skew (as an articular velocity)
368  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
369  task.set_eJe(eJe) ;
370 
371  vpColVector v ;
372  // Compute the visual servoing skew vector
373  v = task.computeControlLaw() ;
374 
375  // Display the current and desired feature points in the image display
376  vpServoDisplay::display(task,cam,I) ;
377 
378  // Apply the computed joint velocities to the robot
380 
381  // Save velocities applied to the robot in the log file
382  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
383  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
384  flog << v[0] << " " << v[1] << " " << v[2] << " "
385  << v[3] << " " << v[4] << " " << v[5] << " ";
386 
387  // Get the measured joint velocities of the robot
388  vpColVector qvel;
390  // Save measured joint velocities of the robot in the log file:
391  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
392  // velocities in m/s
393  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
394  // velocities in rad/s
395  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
396  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
397 
398  // Get the measured joint positions of the robot
399  vpColVector q;
401  // Save measured joint positions of the robot in the log file
402  // - q[0], q[1], q[2] correspond to measured joint translation
403  // positions in m
404  // - q[3], q[4], q[5] correspond to measured joint rotation
405  // positions in rad
406  flog << q[0] << " " << q[1] << " " << q[2] << " "
407  << q[3] << " " << q[4] << " " << q[5] << " ";
408 
409  // Save feature error (s-s*) for the 4 feature points. For each feature
410  // point, we have 2 errors (along x and y axis). This error is expressed
411  // in meters in the camera frame
412  flog << ( task.getError() ).t() << std::endl;
413 
414  // Flush the display
415  vpDisplay::flush(I) ;
416 
417  // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl;
418  }
419 
420  std::cout << "Display task information: " << std::endl;
421  task.print() ;
422  task.kill();
423  flog.close() ; // Close the log file
424  return 0;
425  }
426  catch (...)
427  {
428  flog.close() ; // Close the log file
429  vpERROR_TRACE(" Test failed") ;
430  return 0;
431  }
432 }
433 
434 #else
435 int
436 main()
437 {
438  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
439 }
440 
441 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
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 bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
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:395
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:580
Control of Irisa's Viper S850 robot named Viper850.
#define vpTRACE
Definition: vpDebug.h:418
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
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)
Definition: vpServo.cpp:449
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void set_x(const double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.h:194
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 get_eJe(vpMatrix &eJe)
static const vpColor green
Definition: vpColor.h:170
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:127
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:465
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
void set_y(const double y)
Class that defines what is a point.
Definition: vpPoint.h:65
The vpRotationMatrix considers the particular case of a rotation matrix.
vpImagePoint getCog() const
Definition: vpDot2.h:163
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:404
void kill()
Definition: vpServo.cpp:189
Initialize the velocity controller.
Definition: vpRobot.h:70
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
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:78
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
void set_y(const double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.h:196
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
void extract(vpRotationMatrix &R) const
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
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)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void get_cVe(vpVelocityTwistMatrix &cVe) const
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
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 set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:266
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
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 computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo)
compute the pose for a given method
Definition: vpPose.cpp:386
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
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:155
void buildFrom(const double phi, const double theta, const double psi)
Definition: vpRxyzVector.h:184
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
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)
void setGraphics(const bool activate)
Definition: vpDot2.h:312
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix 'cMo'.
Definition: vpPose.cpp:344
static const vpColor blue
Definition: vpColor.h:173
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