ViSP  2.6.2
servoAfma6FourPoints2DCamVelocityInteractionDesired.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6FourPoints2DCamVelocityInteractionDesired.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 camera frame
38  *
39  * Authors:
40  * Eric Marchand
41  * Fabien Spindler
42  *
43  *****************************************************************************/
44 
70 #include <visp/vpConfig.h>
71 #include <visp/vpDebug.h> // Debug trace
72 #include <stdlib.h>
73 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
74 
75 #include <visp/vp1394TwoGrabber.h>
76 #include <visp/vpImage.h>
77 #include <visp/vpImagePoint.h>
78 #include <visp/vpDisplay.h>
79 #include <visp/vpDisplayX.h>
80 
81 #include <visp/vpMath.h>
82 #include <visp/vpTranslationVector.h>
83 #include <visp/vpRxyzVector.h>
84 #include <visp/vpRotationMatrix.h>
85 #include <visp/vpHomogeneousMatrix.h>
86 #include <visp/vpFeaturePoint.h>
87 #include <visp/vpPoint.h>
88 #include <visp/vpServo.h>
89 #include <visp/vpFeatureBuilder.h>
90 #include <visp/vpDot.h>
91 #include <visp/vpRobotAfma6.h>
92 #include <visp/vpServoDisplay.h>
93 #include <visp/vpIoTools.h>
94 
95 // Exception
96 #include <visp/vpException.h>
97 #include <visp/vpMatrixException.h>
98 
99 #define L 0.05 // to deal with a 10cm by 10cm square
100 
101 int
102 main()
103 {
104  // Log file creation in /tmp/$USERNAME/log.dat
105  // This file contains by line:
106  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
107  // - the 6 mesured camera velocities (m/s, rad/s)
108  // - the 6 mesured joint positions (m, rad)
109  // - the 8 values of s - s*
110  std::string username;
111  // Get the user login name
112  vpIoTools::getUserName(username);
113 
114  // Create a log filename to save velocities...
115  std::string logdirname;
116  logdirname ="/tmp/" + username;
117 
118  // Test if the output path exist. If no try to create it
119  if (vpIoTools::checkDirectory(logdirname) == false) {
120  try {
121  // Create the dirname
122  vpIoTools::makeDirectory(logdirname);
123  }
124  catch (...) {
125  std::cerr << std::endl
126  << "ERROR:" << std::endl;
127  std::cerr << " Cannot create " << logdirname << std::endl;
128  exit(-1);
129  }
130  }
131  std::string logfilename;
132  logfilename = logdirname + "/log.dat";
133 
134  // Open the log file name
135  std::ofstream flog(logfilename.c_str());
136 
137  try
138  {
139  vpServo task ;
140 
142  int i ;
143 
147  g.open(I) ;
148 
149  vpDisplayX display(I,100,100,"Current image") ;
150 
151  g.acquire(I) ;
152 
153  vpDisplay::display(I) ;
154  vpDisplay::flush(I) ;
155 
156  std::cout << std::endl ;
157  std::cout << "-------------------------------------------------------" << std::endl ;
158  std::cout << " Test program for vpServo " <<std::endl ;
159  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
160  std::cout << " Use of the Afma6 robot " << std::endl ;
161  std::cout << " Interaction matrix computed with the desired features " << std::endl ;
162 
163  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl ;
164  std::cout << "-------------------------------------------------------" << std::endl ;
165  std::cout << std::endl ;
166 
167 
168  vpDot2 dot[4] ;
169  vpImagePoint cog;
170 
171  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..."
172  << std::endl;
173  for (i=0 ; i < 4 ; i++) {
174  dot[i].initTracking(I) ;
175  cog = dot[i].getCog();
177  vpDisplay::flush(I);
178  }
179 
180  vpRobotAfma6 robot;
181 
184 
185  // Load the end-effector to camera frame transformation obtained
186  // using a camera intrinsic model with distortion
187  robot.init(vpAfma6::TOOL_CCMOP, projModel);
188 
189  vpCameraParameters cam ;
190  // Update camera parameters
191  robot.getCameraParameters (cam, I);
192 
193  // Sets the current position of the visual feature
194  vpFeaturePoint p[4] ;
195  for (i=0 ; i < 4 ; i++)
196  vpFeatureBuilder::create(p[i], cam, dot[i]); //retrieve x,y of the vpFeaturePoint structure
197 
198  // Set the position of the square target in a frame which origin is
199  // centered in the middle of the square
200  vpPoint point[4] ;
201  point[0].setWorldCoordinates(-L, -L, 0) ;
202  point[1].setWorldCoordinates( L, -L, 0) ;
203  point[2].setWorldCoordinates( L, L, 0) ;
204  point[3].setWorldCoordinates(-L, L, 0) ;
205 
206  // Initialise a desired pose to compute s*, the desired 2D point features
208  vpTranslationVector cto(0, 0, 0.7); // tz = 0.7 meter
209  vpRxyzVector cro(vpMath::rad(0), vpMath::rad(0), vpMath::rad(0)); // No rotations
210  vpRotationMatrix cRo(cro); // Build the rotation matrix
211  cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
212 
213  // sets the desired position of the 2D visual feature
214  vpFeaturePoint pd[4] ;
215  // Compute the desired position of the features from the desired pose
216  for (int i=0; i < 4; i ++) {
217  vpColVector cP, p ;
218  point[i].changeFrame(cMo, cP) ;
219  point[i].projection(cP, p) ;
220 
221  pd[i].set_x(p[0]) ;
222  pd[i].set_y(p[1]) ;
223  pd[i].set_Z(cP[2]);
224  }
225 
226  // Define the task
227  // - we want an eye-in-hand control law
228  // - robot is controlled in the camera frame
229  // - Interaction matrix is computed with the desired visual features
232 
233  // We want to see a point on a point
234  std::cout << std::endl ;
235  for (i=0 ; i < 4 ; i++)
236  task.addFeature(p[i],pd[i]) ;
237 
238  // Set the proportional gain
239  task.setLambda(0.4) ;
240 
241  // Display task information
242  task.print() ;
243 
244  // Initialise the velocity control of the robot
246 
247  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
248 
249  for ( ; ; ) {
250  // Acquire a new image from the camera
251  g.acquire(I) ;
252 
253  // Display this image
254  vpDisplay::display(I) ;
255 
256  // For each point...
257  for (i=0 ; i < 4 ; i++) {
258  // Achieve the tracking of the dot in the image
259  dot[i].track(I) ;
260  // Get the dot cog
261  cog = dot[i].getCog();
262  // Display a green cross at the center of gravity position in the
263  // image
265  }
266 
267  // Printing on stdout concerning task information
268  // task.print() ;
269 
270  // Update the point feature from the dot location
271  for (i=0 ; i < 4 ; i++)
272  vpFeatureBuilder::create(p[i], cam, dot[i]);
273 
274  vpColVector v ;
275  // Compute the visual servoing skew vector
276  v = task.computeControlLaw() ;
277 
278  // Display the current and desired feature points in the image display
279  vpServoDisplay::display(task, cam, I);
280 
281  // Apply the computed camera velocities to the robot
283 
284  // Save velocities applied to the robot in the log file
285  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
286  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
287  flog << v[0] << " " << v[1] << " " << v[2] << " "
288  << v[3] << " " << v[4] << " " << v[5] << " ";
289 
290  // Get the measured joint velocities of the robot
291  vpColVector qvel;
292  robot.getVelocity(vpRobot::CAMERA_FRAME, qvel);
293  // Save measured camera velocities of the robot in the log file:
294  // - qvel[0], qvel[1], qvel[2] correspond to measured camera translation
295  // velocities in m/s
296  // - qvel[3], qvel[4], qvel[5] correspond to measured camera rotation
297  // velocities in rad/s
298  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
299  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
300 
301  // Get the measured joint positions of the robot
302  vpColVector q;
304  // Save measured joint positions of the robot in the log file
305  // - q[0], q[1], q[2] correspond to measured joint translation
306  // positions in m
307  // - q[3], q[4], q[5] correspond to measured joint rotation
308  // positions in rad
309  flog << q[0] << " " << q[1] << " " << q[2] << " "
310  << q[3] << " " << q[4] << " " << q[5] << " ";
311 
312  // Save feature error (s-s*) for the 4 feature points. For each feature
313  // point, we have 2 errors (along x and y axis). This error is expressed
314  // in meters in the camera frame
315  flog << ( task.getError() ).t() << std::endl;
316 
317  // Flush the display
318  vpDisplay::flush(I) ;
319  }
320 
321  flog.close() ; // Close the log file
322 
323  // Display task information
324  task.print() ;
325 
326  // Kill the task
327  task.kill();
328 
329  return 0;
330  }
331  catch (...) {
332  flog.close() ; // Close the log file
333  vpERROR_TRACE(" Test failed") ;
334  return 0;
335  }
336 }
337 
338 #else
339 int
340 main()
341 {
342  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
343 
344 }
345 
346 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
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
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
Definition: vpAfma6.cpp:1226
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
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static const vpColor green
Definition: vpColor.h:168
void acquire(vpImage< unsigned char > &I)
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
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
void set_y(const double y)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:214
Class that defines what is a point.
Definition: vpPoint.h:65
The vpRotationMatrix considers the particular case of a rotation matrix.
void init(void)
vpImagePoint getCog() const
Definition: vpDot2.h:254
void set_x(const double x)
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
void open(vpImage< unsigned char > &I)
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
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
static std::string getUserName()
Definition: vpIoTools.cpp:136
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
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
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 setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
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 setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
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
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