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