Visual Servoing Platform  version 3.0.1
servoViper650FourPoints2DArtVelocityInteractionCurrent.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 articular frame
34  *
35  * Authors:
36  * Fabien Spindler
37  *
38  *****************************************************************************/
51 #include <visp3/core/vpConfig.h>
52 #include <visp3/core/vpDebug.h>
53 
54 #include <stdio.h>
55 #include <iostream>
56 #include <fstream>
57 #include <sstream>
58 #include <stdlib.h>
59 #if (defined (VISP_HAVE_VIPER650) && defined (VISP_HAVE_DC1394))
60 
61 #include <visp3/sensor/vp1394TwoGrabber.h>
62 #include <visp3/core/vpImage.h>
63 #include <visp3/core/vpDisplay.h>
64 #include <visp3/gui/vpDisplayX.h>
65 #include <visp3/gui/vpDisplayOpenCV.h>
66 #include <visp3/gui/vpDisplayGTK.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/vpIoTools.h>
72 #include <visp3/core/vpMath.h>
73 #include <visp3/core/vpPoint.h>
74 #include <visp3/vision/vpPose.h>
75 #include <visp3/robot/vpRobotViper650.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 
106 void compute_pose(vpPoint point[], vpDot2 dot[], int ndot,
107  vpCameraParameters cam,
108  vpHomogeneousMatrix &cMo,
109  vpTranslationVector &cto,
110  vpRxyzVector &cro, bool init)
111 {
112  vpHomogeneousMatrix cMo_dementhon; // computed pose with dementhon
113  vpHomogeneousMatrix cMo_lagrange; // computed pose with dementhon
114  vpRotationMatrix cRo;
115  vpPose pose;
116  vpImagePoint cog;
117  for (int i=0; i < ndot; i ++) {
118 
119  double x=0, y=0;
120  cog = dot[i].getCog();
122  cog,
123  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 joint space" << std::endl ;
226  std::cout << " Use of the Afma6 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 
303  robot.get_cVe(cVe) ;
304  task.set_cVe(cVe) ;
305  task.print() ;
306 
307  // Set the Jacobian (expressed in the end-effector frame)
308  vpMatrix eJe ;
309  robot.get_eJe(eJe) ;
310  task.set_eJe(eJe) ;
311  task.print() ;
312 
313  // Initialise the velocity control of the robot
315 
316  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
317  for ( ; ; ) {
318  // Acquire a new image from the camera
319  g.acquire(I) ;
320 
321  // Display this image
322  vpDisplay::display(I) ;
323 
324  try {
325  // For each point...
326  for (i=0 ; i < 4 ; i++) {
327  // Achieve the tracking of the dot in the image
328  dot[i].track(I) ;
329  // Display a green cross at the center of gravity position in the
330  // image
331  cog = dot[i].getCog();
333  }
334  }
335  catch(...) {
336  flog.close() ; // Close the log file
337  vpTRACE("Error detected while tracking visual features") ;
338  robot.stopMotion() ;
339  return(1) ;
340  }
341 
342  // During the servo, we compute the pose using LOWE method. For the
343  // initial pose used in the non linear minimisation we use the pose
344  // computed at the previous iteration.
345  compute_pose(point, dot, 4, cam, cMo, cto, cro, false);
346 
347  for (i=0 ; i < 4 ; i++) {
348  // Update the point feature from the dot location
349  vpFeatureBuilder::create(p[i], cam, dot[i]);
350  // Set the feature Z coordinate from the pose
351  vpColVector cP;
352  point[i].changeFrame(cMo, cP) ;
353 
354  p[i].set_Z(cP[2]);
355  }
356 
357  // Get the jacobian of the robot
358  robot.get_eJe(eJe) ;
359  // Update this jacobian in the task structure. It will be used to compute
360  // the velocity skew (as an articular velocity)
361  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
362  task.set_eJe(eJe) ;
363 
364  vpColVector v ;
365  // Compute the visual servoing skew vector
366  v = task.computeControlLaw() ;
367 
368  // Display the current and desired feature points in the image display
369  vpServoDisplay::display(task,cam,I) ;
370 
371  // Apply the computed joint velocities to the robot
373 
374  // Save velocities applied to the robot in the log file
375  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
376  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
377  flog << v[0] << " " << v[1] << " " << v[2] << " "
378  << v[3] << " " << v[4] << " " << v[5] << " ";
379 
380  // Get the measured joint velocities of the robot
381  vpColVector qvel;
383  // Save measured joint velocities of the robot in the log file:
384  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
385  // velocities in m/s
386  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
387  // velocities in rad/s
388  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
389  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
390 
391  // Get the measured joint positions of the robot
392  vpColVector q;
394  // Save measured joint positions of the robot in the log file
395  // - q[0], q[1], q[2] correspond to measured joint translation
396  // positions in m
397  // - q[3], q[4], q[5] correspond to measured joint rotation
398  // positions in rad
399  flog << q[0] << " " << q[1] << " " << q[2] << " "
400  << q[3] << " " << q[4] << " " << q[5] << " ";
401 
402  // Save feature error (s-s*) for the 4 feature points. For each feature
403  // point, we have 2 errors (along x and y axis). This error is expressed
404  // in meters in the camera frame
405  flog << ( task.getError() ).t() << std::endl;
406 
407  // Flush the display
408  vpDisplay::flush(I) ;
409 
410  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
411  }
412 
413  vpTRACE("Display task information " ) ;
414  task.print() ;
415  task.kill();
416  flog.close() ; // Close the log file
417  return 0;
418  }
419  catch (...)
420  {
421  flog.close() ; // Close the log file
422  vpERROR_TRACE(" Test failed") ;
423  return 0;
424  }
425 }
426 
427 #else
428 int
429 main()
430 {
431  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
432 
433 }
434 
435 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
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
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:460
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
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: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 set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:435
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)
void get_cVe(vpVelocityTwistMatrix &cVe) const
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
void get_eJe(vpMatrix &eJe)
static const vpColor blue
Definition: vpColor.h:169
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)