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