ViSP  2.6.2
servoAfma6FourPoints2DArtVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6FourPoints2DArtVelocity.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 articular frame
38  *
39  * Authors:
40  * Eric Marchand
41  * Fabien Spindler
42  *
43  *****************************************************************************/
66 #include <visp/vpConfig.h>
67 #include <visp/vpDebug.h> // Debug trace
68 #include <stdio.h>
69 #include <iostream>
70 #include <fstream>
71 #include <sstream>
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 #include <visp/vpMath.h>
81 #include <visp/vpHomogeneousMatrix.h>
82 #include <visp/vpFeaturePoint.h>
83 #include <visp/vpPoint.h>
84 #include <visp/vpServo.h>
85 #include <visp/vpFeatureBuilder.h>
86 #include <visp/vpIoTools.h>
87 #include <visp/vpRobotAfma6.h>
88 
89 // Exception
90 #include <visp/vpException.h>
91 #include <visp/vpMatrixException.h>
92 #include <visp/vpServoDisplay.h>
93 
94 #include <visp/vpDot.h>
95 
96 int
97 main()
98 {
99  // Log file creation in /tmp/$USERNAME/log.dat
100  // This file contains by line:
101  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
102  // - the 6 mesured joint velocities (m/s, rad/s)
103  // - the 6 mesured joint positions (m, rad)
104  // - the 8 values of s - s*
105  std::string username;
106  // Get the user login name
107  vpIoTools::getUserName(username);
108 
109  // Create a log filename to save velocities...
110  std::string logdirname;
111  logdirname ="/tmp/" + username;
112 
113  // Test if the output path exist. If no try to create it
114  if (vpIoTools::checkDirectory(logdirname) == false) {
115  try {
116  // Create the dirname
117  vpIoTools::makeDirectory(logdirname);
118  }
119  catch (...) {
120  std::cerr << std::endl
121  << "ERROR:" << std::endl;
122  std::cerr << " Cannot create " << logdirname << std::endl;
123  exit(-1);
124  }
125  }
126  std::string logfilename;
127  logfilename = logdirname + "/log.dat";
128 
129  // Open the log file name
130  std::ofstream flog(logfilename.c_str());
131 
132  try {
133  // Define the square CAD model
134  // Square dimention
135 #define L 0.075
136  // Distance between the camera and the square at the desired
137  // position after visual servoing convergence
138 #define D 0.5
139 
140  vpServo task ;
141 
143  int i ;
144 
148  g.open(I) ;
149 
150  g.acquire(I) ;
151 
152  vpDisplayX display(I,100,100,"testDisplayX.cpp ") ;
153  vpTRACE(" ") ;
154 
155  vpDisplay::display(I) ;
156  vpDisplay::flush(I) ;
157 
158  std::cout << std::endl ;
159  std::cout << "-------------------------------------------------------" << std::endl ;
160  std::cout << " Test program for vpServo " <<std::endl ;
161  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
162  std::cout << " Use of the Afma6 robot " << std::endl ;
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  vpDot dot[4] ;
169  vpImagePoint cog;
170 
171  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..."
172  << std::endl;
173 
174  for (i=0 ; i < 4 ; i++) {
175  dot[i].initTracking(I) ;
176  cog = dot[i].getCog();
178  vpDisplay::flush(I);
179  }
180 
181  vpRobotAfma6 robot ;
182 
183  vpCameraParameters cam ;
184 
185  // Update camera parameters
186  robot.getCameraParameters (cam, I);
187 
188  // Sets the current position of the visual feature
189  vpFeaturePoint p[4] ;
190  for (i=0 ; i < 4 ; i++)
191  vpFeatureBuilder::create(p[i],cam, dot[i]) ; //retrieve x,y and Z of the vpPoint structure
192 
193  // sets the desired position of the visual feature
194  vpFeaturePoint pd[4] ;
195 
196  pd[0].buildFrom(-L,-L,D) ;
197  pd[1].buildFrom(L,-L,D) ;
198  pd[2].buildFrom(L,L,D) ;
199  pd[3].buildFrom(-L,L,D) ;
200 
201  // We want to see a point on a point
202  std::cout << std::endl ;
203  for (i=0 ; i < 4 ; i++)
204  task.addFeature(p[i],pd[i]) ;
205 
206  // Set the proportional gain
207  task.setLambda(0.2) ;
208 
209  // Display task information
210  task.print() ;
211 
212  // Define the task
213  // - we want an eye-in-hand control law
214  // - articular velocity are computed
217  task.print() ;
218 
220  robot.get_cVe(cVe) ;
221  task.set_cVe(cVe) ;
222  task.print() ;
223 
224  // Set the Jacobian (expressed in the end-effector frame)
225  vpMatrix eJe ;
226  robot.get_eJe(eJe) ;
227  task.set_eJe(eJe) ;
228  task.print() ;
229 
230  // Initialise the velocity control of the robot
232 
233  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
234  for ( ; ; ) {
235  // Acquire a new image from the camera
236  g.acquire(I) ;
237 
238  // Display this image
239  vpDisplay::display(I) ;
240 
241  try {
242  // For each point...
243  for (i=0 ; i < 4 ; i++) {
244  // Achieve the tracking of the dot in the image
245  dot[i].track(I) ;
246  // Get the dot cog
247  cog = dot[i].getCog();
248  // Display a green cross at the center of gravity position in the
249  // image
251  }
252  }
253  catch(...) {
254  flog.close() ; // Close the log file
255  vpTRACE("Error detected while tracking visual features") ;
256  robot.stopMotion() ;
257  exit(1) ;
258  }
259 
260  // Update the point feature from the dot location
261  for (i=0 ; i < 4 ; i++)
262  vpFeatureBuilder::create(p[i],cam, dot[i]);
263 
264  // Get the jacobian of the robot
265  robot.get_eJe(eJe) ;
266  // Update this jacobian in the task structure. It will be used to compute
267  // the velocity skew (as an articular velocity)
268  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
269  task.set_eJe(eJe) ;
270 
271  vpColVector v ;
272  // Compute the visual servoing skew vector
273  v = task.computeControlLaw() ;
274 
275  // Display the current and desired feature points in the image display
276  vpServoDisplay::display(task,cam,I) ;
277 
278  // Apply the computed joint velocities to the robot
280 
281  // Save velocities applied to the robot in the log file
282  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
283  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
284  flog << v[0] << " " << v[1] << " " << v[2] << " "
285  << v[3] << " " << v[4] << " " << v[5] << " ";
286 
287  // Get the measured joint velocities of the robot
288  vpColVector qvel;
290  // Save measured joint velocities of the robot in the log file:
291  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
292  // velocities in m/s
293  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
294  // velocities in rad/s
295  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
296  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
297 
298  // Get the measured joint positions of the robot
299  vpColVector q;
301  // Save measured joint positions of the robot in the log file
302  // - q[0], q[1], q[2] correspond to measured joint translation
303  // positions in m
304  // - q[3], q[4], q[5] correspond to measured joint rotation
305  // positions in rad
306  flog << q[0] << " " << q[1] << " " << q[2] << " "
307  << q[3] << " " << q[4] << " " << q[5] << " ";
308 
309  // Save feature error (s-s*) for the 4 feature points. For each feature
310  // point, we have 2 errors (along x and y axis). This error is expressed
311  // in meters in the camera frame
312  flog << ( task.getError() ).t() << std::endl;
313 
314  // Flush the display
315  vpDisplay::flush(I) ;
316 
317  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
318  }
319 
320  vpTRACE("Display task information " ) ;
321  task.print() ;
322  task.kill();
323  flog.close() ; // Close the log file
324  return 0;
325  }
326  catch (...)
327  {
328  flog.close() ; // Close the log file
329  vpERROR_TRACE(" Test failed") ;
330  return 0;
331  }
332 }
333 
334 #else
335 int
336 main()
337 {
338  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
339 
340 }
341 
342 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
void get_eJe(vpMatrix &_eJe)
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
#define vpERROR_TRACE
Definition: vpDebug.h:379
#define vpTRACE
Definition: vpDebug.h:401
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
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:787
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)
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:227
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:214
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
void open(vpImage< unsigned char > &I)
vpImagePoint getCog() const
Definition: vpDot.h:249
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
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:235
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)
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void get_cVe(vpVelocityTwistMatrix &_cVe)
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
void buildFrom(const double x, const double y, const double Z)
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 print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
This tracker is meant to track a dot (connex pixels with same gray level) on a vpImage.
Definition: vpDot.h:80
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
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)
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:638
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
static const vpColor blue
Definition: vpColor.h:171