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