Visual Servoing Platform  version 3.0.1
servoViper650FourPoints2DCamVelocityInteractionCurrent.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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>
54 
55 #include <stdio.h>
56 #include <iostream>
57 #include <fstream>
58 #include <sstream>
59 #include <stdlib.h>
60 #if (defined (VISP_HAVE_VIPER650) && defined (VISP_HAVE_DC1394))
61 
62 #include <visp3/sensor/vp1394TwoGrabber.h>
63 #include <visp3/core/vpImage.h>
64 #include <visp3/core/vpDisplay.h>
65 #include <visp3/gui/vpDisplayX.h>
66 #include <visp3/gui/vpDisplayOpenCV.h>
67 #include <visp3/gui/vpDisplayGTK.h>
68 #include <visp3/blob/vpDot2.h>
69 #include <visp3/visual_features/vpFeatureBuilder.h>
70 #include <visp3/visual_features/vpFeaturePoint.h>
71 #include <visp3/core/vpHomogeneousMatrix.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/vpRobotViper650.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  vpRobotViper650 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 Viper650 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  vpDot2 dot[4] ;
232  vpImagePoint cog;
233 
234  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..."
235  << std::endl;
236 
237  for (i=0 ; i < 4 ; i++) {
238  dot[i].setGraphics(true) ;
239  dot[i].initTracking(I) ;
240  cog = dot[i].getCog();
242  vpDisplay::flush(I);
243  }
244 
245  vpCameraParameters cam ;
246 
247  // Update camera parameters
248  robot.getCameraParameters (cam, I);
249 
250  std::cout << "Camera parameters: \n" << cam << std::endl;
251 
252  // Sets the current position of the visual feature
253  vpFeaturePoint p[4] ;
254  for (i=0 ; i < 4 ; i++)
255  vpFeatureBuilder::create(p[i], cam, dot[i]); //retrieve x,y of the vpFeaturePoint structure
256 
257  // Set the position of the square target in a frame which origin is
258  // centered in the middle of the square
259  vpPoint point[4] ;
260  point[0].setWorldCoordinates(-L, -L, 0) ;
261  point[1].setWorldCoordinates( L, -L, 0) ;
262  point[2].setWorldCoordinates( L, L, 0) ;
263  point[3].setWorldCoordinates(-L, L, 0) ;
264 
265  // Initialise a desired pose to compute s*, the desired 2D point features
267  vpTranslationVector cto(0, 0, 0.5); // tz = 0.5 meter
269  vpRotationMatrix cRo(cro); // Build the rotation matrix
270  cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
271 
272  // Sets the desired position of the 2D visual feature
273  vpFeaturePoint pd[4] ;
274  // Compute the desired position of the features from the desired pose
275  for (int i=0; i < 4; i ++) {
276  vpColVector cP, p ;
277  point[i].changeFrame(cMo, cP) ;
278  point[i].projection(cP, p) ;
279 
280  pd[i].set_x(p[0]) ;
281  pd[i].set_y(p[1]) ;
282  pd[i].set_Z(cP[2]);
283  }
284 
285  // We want to see a point on a point
286  for (i=0 ; i < 4 ; i++)
287  task.addFeature(p[i],pd[i]) ;
288 
289  // Set the proportional gain
290  task.setLambda(0.3) ;
291 
292  // Display task information
293  task.print() ;
294 
295  // Define the task
296  // - we want an eye-in-hand control law
297  // - articular velocity are computed
300  task.print() ;
301 
302 
303  // Initialise the velocity control of the robot
305 
306  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
307  for ( ; ; ) {
308  // Acquire a new image from the camera
309  g.acquire(I) ;
310 
311  // Display this image
312  vpDisplay::display(I) ;
313 
314  try {
315  // For each point...
316  for (i=0 ; i < 4 ; i++) {
317  // Achieve the tracking of the dot in the image
318  dot[i].track(I) ;
319  // Display a green cross at the center of gravity position in the
320  // image
321  cog = dot[i].getCog();
323  }
324  }
325  catch(...) {
326  flog.close() ; // Close the log file
327  vpTRACE("Error detected while tracking visual features") ;
328  robot.stopMotion() ;
329  return(1) ;
330  }
331 
332  // During the servo, we compute the pose using LOWE method. For the
333  // initial pose used in the non linear minimisation we use the pose
334  // computed at the previous iteration.
335  compute_pose(point, dot, 4, cam, cMo, cto, cro, false);
336 
337  for (i=0 ; i < 4 ; i++) {
338  // Update the point feature from the dot location
339  vpFeatureBuilder::create(p[i], cam, dot[i]);
340  // Set the feature Z coordinate from the pose
341  vpColVector cP;
342  point[i].changeFrame(cMo, cP) ;
343 
344  p[i].set_Z(cP[2]);
345  }
346 
347  vpColVector v ;
348  // Compute the visual servoing skew vector
349  v = task.computeControlLaw() ;
350 
351  // Display the current and desired feature points in the image display
352  vpServoDisplay::display(task,cam,I) ;
353 
354  // Apply the computed joint velocities to the robot
356 
357  // Save velocities applied to the robot in the log file
358  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
359  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
360  flog << v[0] << " " << v[1] << " " << v[2] << " "
361  << v[3] << " " << v[4] << " " << v[5] << " ";
362 
363  // Get the measured joint velocities of the robot
364  vpColVector qvel;
366  // Save measured joint velocities of the robot in the log file:
367  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
368  // velocities in m/s
369  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
370  // velocities in rad/s
371  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
372  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
373 
374  // Get the measured joint positions of the robot
375  vpColVector q;
377  // Save measured joint positions of the robot in the log file
378  // - q[0], q[1], q[2] correspond to measured joint translation
379  // positions in m
380  // - q[3], q[4], q[5] correspond to measured joint rotation
381  // positions in rad
382  flog << q[0] << " " << q[1] << " " << q[2] << " "
383  << q[3] << " " << q[4] << " " << q[5] << " ";
384 
385  // Save feature error (s-s*) for the 4 feature points. For each feature
386  // point, we have 2 errors (along x and y axis). This error is expressed
387  // in meters in the camera frame
388  flog << task.getError() << std::endl;
389 
390  // Flush the display
391  vpDisplay::flush(I) ;
392 
393  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
394  }
395 
396  std::cout << "Display task information: " << std::endl;
397  task.print() ;
398  task.kill();
399  flog.close() ; // Close the log file
400  return 0;
401  }
402  catch (...)
403  {
404  flog.close() ; // Close the log file
405  vpERROR_TRACE(" Test failed") ;
406  return 0;
407  }
408 }
409 
410 #else
411 int
412 main()
413 {
414  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
415 
416 }
417 
418 #endif
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
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Control of Irisa&#39;s Viper S650 robot named Viper650.
#define vpERROR_TRACE
Definition: vpDebug.h:391
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
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 getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper650.cpp:565
void extract(vpRotationMatrix &R) const
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:125
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
static void flush(const vpImage< unsigned char > &I)
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.
void set_x(const double x)
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
void kill()
Definition: vpServo.cpp:191
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
vpRotationMatrix buildFrom(const vpHomogeneousMatrix &M)
#define vpTRACE
Definition: vpDebug.h:414
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:76
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:391
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:177
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
Definition: vpPose.cpp:372
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:585
static double rad(double deg)
Definition: vpMath.h:104
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
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:314
vpImagePoint getCog() const
Definition: vpDot2.h:161
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:154
vpColVector getError() const
Definition: vpServo.h:271
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)
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix &#39;cMo&#39;.
Definition: vpPose.cpp:337
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &_cP)
Definition: vpPoint.cpp:247
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:145
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
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:316
static const vpColor blue
Definition: vpColor.h:169
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)