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