ViSP  3.0.0
servoViper850FourPoints2DCamVelocityInteractionCurrent.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  * Fabien Spindler
37  *
38  *****************************************************************************/
52 #include <visp3/core/vpConfig.h>
53 #include <visp3/core/vpDebug.h> // Debug trace
54 
55 #include <stdio.h>
56 #include <iostream>
57 #include <fstream>
58 #include <sstream>
59 #include <stdlib.h>
60 #if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394))
61 
62 #include <visp3/sensor/vp1394TwoGrabber.h>
63 #include <visp3/core/vpDisplay.h>
64 #include <visp3/gui/vpDisplayGTK.h>
65 #include <visp3/gui/vpDisplayX.h>
66 #include <visp3/gui/vpDisplayOpenCV.h>
67 #include <visp3/blob/vpDot2.h>
68 #include <visp3/visual_features/vpFeatureBuilder.h>
69 #include <visp3/visual_features/vpFeaturePoint.h>
70 #include <visp3/core/vpHomogeneousMatrix.h>
71 #include <visp3/core/vpImage.h>
72 #include <visp3/core/vpIoTools.h>
73 #include <visp3/core/vpMath.h>
74 #include <visp3/core/vpPoint.h>
75 #include <visp3/vision/vpPose.h>
76 #include <visp3/robot/vpRobotViper850.h>
77 #include <visp3/vs/vpServo.h>
78 #include <visp3/vs/vpServoDisplay.h>
79 
80 #define L 0.05 // to deal with a 10cm by 10cm square
81 
82 
108 void compute_pose(vpPoint point[], vpDot2 dot[], int ndot,
109  vpCameraParameters cam,
110  vpHomogeneousMatrix &cMo,
111  vpTranslationVector &cto,
112  vpRxyzVector &cro, bool init)
113 {
114  vpHomogeneousMatrix cMo_dementhon; // computed pose with dementhon
115  vpHomogeneousMatrix cMo_lagrange; // computed pose with dementhon
116  vpRotationMatrix cRo;
117  vpPose pose;
118  vpImagePoint cog;
119  for (int i=0; i < ndot; i ++) {
120 
121  double x=0, y=0;
122  cog = dot[i].getCog();
123  vpPixelMeterConversion::convertPoint(cam, cog, x, y) ; //pixel to meter conversion
124  point[i].set_x(x) ;//projection perspective p
125  point[i].set_y(y) ;
126  pose.addPoint(point[i]) ;
127  }
128 
129  if (init == true) {
130  pose.computePose(vpPose::DEMENTHON, cMo_dementhon) ;
131  // Compute and return the residual expressed in meter for the pose matrix
132  // 'cMo'
133  double residual_dementhon = pose.computeResidual(cMo_dementhon);
134  pose.computePose(vpPose::LAGRANGE, cMo_lagrange) ;
135  double residual_lagrange = pose.computeResidual(cMo_lagrange);
136 
137  // Select the best pose to initialize the lowe pose computation
138  if (residual_lagrange < residual_dementhon)
139  cMo = cMo_lagrange;
140  else
141  cMo = cMo_dementhon;
142 
143  }
144  else { // init = false; use of the previous pose to initialise LOWE
145  cRo.buildFrom(cro);
146  cMo.buildFrom(cto, cRo);
147  }
148  pose.computePose(vpPose::LOWE, cMo) ;
149  cMo.extract(cto);
150  cMo.extract(cRo);
151  cro.buildFrom(cRo);
152 }
153 
154 int
155 main()
156 {
157  // Log file creation in /tmp/$USERNAME/log.dat
158  // This file contains by line:
159  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
160  // - the 6 mesured joint velocities (m/s, rad/s)
161  // - the 6 mesured joint positions (m, rad)
162  // - the 8 values of s - s*
163  std::string username;
164  // Get the user login name
165  vpIoTools::getUserName(username);
166 
167  // Create a log filename to save velocities...
168  std::string logdirname;
169  logdirname ="/tmp/" + username;
170 
171  // Test if the output path exist. If no try to create it
172  if (vpIoTools::checkDirectory(logdirname) == false) {
173  try {
174  // Create the dirname
175  vpIoTools::makeDirectory(logdirname);
176  }
177  catch (...) {
178  std::cerr << std::endl
179  << "ERROR:" << std::endl;
180  std::cerr << " Cannot create " << logdirname << std::endl;
181  return(-1);
182  }
183  }
184  std::string logfilename;
185  logfilename = logdirname + "/log.dat";
186 
187  // Open the log file name
188  std::ofstream flog(logfilename.c_str());
189 
190  try {
191  vpRobotViper850 robot ;
192  // Load the end-effector to camera frame transformation obtained
193  // using a camera intrinsic model with distortion
197 
198  vpServo task ;
199 
201  int i ;
202 
203  bool reset = false;
204  vp1394TwoGrabber g(reset);
206  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
207  g.open(I) ;
208 
209  g.acquire(I) ;
210 
211 #ifdef VISP_HAVE_X11
212  vpDisplayX display(I,100,100,"Current image") ;
213 #elif defined(VISP_HAVE_OPENCV)
214  vpDisplayOpenCV display(I,100,100,"Current image") ;
215 #elif defined(VISP_HAVE_GTK)
216  vpDisplayGTK display(I,100,100,"Current image") ;
217 #endif
218 
219  vpDisplay::display(I) ;
220  vpDisplay::flush(I) ;
221 
222  std::cout << std::endl ;
223  std::cout << "-------------------------------------------------------" << std::endl ;
224  std::cout << " Test program for vpServo " <<std::endl ;
225  std::cout << " Eye-in-hand task control, velocity computed in the camera space" << std::endl ;
226  std::cout << " Use of the Viper850 robot " << std::endl ;
227  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl ;
228  std::cout << "-------------------------------------------------------" << std::endl ;
229  std::cout << std::endl ;
230 
231 
232  vpDot2 dot[4] ;
233  vpImagePoint cog;
234 
235  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..."
236  << std::endl;
237 
238  for (i=0 ; i < 4 ; i++) {
239  dot[i].setGraphics(true) ;
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.5 meter
270  vpRxyzVector cro(vpMath::rad(10), vpMath::rad(30), vpMath::rad(20));
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 
304 
305  // Initialise the velocity control of the robot
307 
308  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
309  for ( ; ; ) {
310  // Acquire a new image from the camera
311  g.acquire(I) ;
312 
313  // Display this image
314  vpDisplay::display(I) ;
315 
316  try {
317  // For each point...
318  for (i=0 ; i < 4 ; i++) {
319  // Achieve the tracking of the dot in the image
320  dot[i].track(I) ;
321  // Display a green cross at the center of gravity position in the
322  // image
323  cog = dot[i].getCog();
325  }
326  }
327  catch(...) {
328  flog.close() ; // Close the log file
329  vpTRACE("Error detected while tracking visual features") ;
330  robot.stopMotion() ;
331  return(1) ;
332  }
333 
334  // During the servo, we compute the pose using LOWE method. For the
335  // initial pose used in the non linear minimisation we use the pose
336  // computed at the previous iteration.
337  compute_pose(point, dot, 4, cam, cMo, cto, cro, false);
338 
339  for (i=0 ; i < 4 ; i++) {
340  // Update the point feature from the dot location
341  vpFeatureBuilder::create(p[i], cam, dot[i]);
342  // Set the feature Z coordinate from the pose
343  vpColVector cP;
344  point[i].changeFrame(cMo, cP) ;
345 
346  p[i].set_Z(cP[2]);
347  }
348 
349  vpColVector v ;
350  // Compute the visual servoing skew vector
351  v = task.computeControlLaw() ;
352 
353  // Display the current and desired feature points in the image display
354  vpServoDisplay::display(task,cam,I) ;
355 
356  // Apply the computed joint velocities to the robot
358 
359  // Save velocities applied to the robot in the log file
360  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
361  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
362  flog << v[0] << " " << v[1] << " " << v[2] << " "
363  << v[3] << " " << v[4] << " " << v[5] << " ";
364 
365  // Get the measured joint velocities of the robot
366  vpColVector qvel;
368  // Save measured joint velocities of the robot in the log file:
369  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
370  // velocities in m/s
371  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
372  // velocities in rad/s
373  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
374  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
375 
376  // Get the measured joint positions of the robot
377  vpColVector q;
379  // Save measured joint positions of the robot in the log file
380  // - q[0], q[1], q[2] correspond to measured joint translation
381  // positions in m
382  // - q[3], q[4], q[5] correspond to measured joint rotation
383  // positions in rad
384  flog << q[0] << " " << q[1] << " " << q[2] << " "
385  << q[3] << " " << q[4] << " " << q[5] << " ";
386 
387  // Save feature error (s-s*) for the 4 feature points. For each feature
388  // point, we have 2 errors (along x and y axis). This error is expressed
389  // in meters in the camera frame
390  flog << task.getError() << std::endl;
391 
392  // Flush the display
393  vpDisplay::flush(I) ;
394 
395  // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl;
396  }
397 
398  std::cout << "Display task information: " << std::endl;
399  task.print() ;
400  task.kill();
401  flog.close() ; // Close the log file
402  return 0;
403  }
404  catch (...)
405  {
406  flog.close() ; // Close the log file
407  vpERROR_TRACE(" Test failed") ;
408  return 0;
409  }
410 }
411 
412 #else
413 int
414 main()
415 {
416  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
417 }
418 
419 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
vpRxyzVector buildFrom(const vpRotationMatrix &R)
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:335
Implementation of an homogeneous matrix and operations on such kind of matrices.
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:584
Control of Irisa&#39;s Viper S850 robot named Viper850.
#define vpERROR_TRACE
Definition: vpDebug.h:391
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
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...
static const vpColor green
Definition: vpColor.h:166
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:124
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
void set_y(const double y)
Class that defines what is a point.
Definition: vpPoint.h:59
Implementation of a rotation matrix and operations on such kind of matrices.
vpImagePoint getCog() const
Definition: vpDot2.h:160
void set_x(const double x)
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
void kill()
Definition: vpServo.cpp:186
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
#define vpTRACE
Definition: vpDebug.h:414
bool computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
compute the pose for a given method
Definition: vpPose.cpp:382
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:74
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:390
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:161
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
void extract(vpRotationMatrix &R) const
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:519
static double rad(double deg)
Definition: vpMath.h:104
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
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:248
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:154
Class for firewire ieee1394 video devices using libdc1394-2.x api.
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)
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &_cP)
Definition: vpPoint.cpp:247
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:151
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:217
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:309
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix &#39;cMo&#39;.
Definition: vpPose.cpp:340
static const vpColor blue
Definition: vpColor.h:169