ViSP  2.6.2
servoViper850Point2DArtVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoViper850Point2DArtVelocity.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 articular
38  *
39  * Authors:
40  * Eric Marchand
41  * Fabien Spindler
42  *
43  *****************************************************************************/
44 
54 #include <visp/vpConfig.h>
55 #include <visp/vpDebug.h> // Debug trace
56 
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <iostream>
60 #include <fstream>
61 #include <sstream>
62 
63 #if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394_2))
64 
65 #include <visp/vp1394TwoGrabber.h>
66 #include <visp/vpImage.h>
67 #include <visp/vpDisplay.h>
68 #include <visp/vpDisplayX.h>
69 
70 #include <visp/vpMath.h>
71 #include <visp/vpHomogeneousMatrix.h>
72 #include <visp/vpFeaturePoint.h>
73 #include <visp/vpPoint.h>
74 #include <visp/vpServo.h>
75 #include <visp/vpFeatureBuilder.h>
76 #include <visp/vpRobotViper850.h>
77 #include <visp/vpIoTools.h>
78 
79 // Exception
80 #include <visp/vpException.h>
81 #include <visp/vpMatrixException.h>
82 #include <visp/vpServoDisplay.h>
83 
84 #include <visp/vpDot2.h>
85 
86 int
87 main()
88 {
89  // Log file creation in /tmp/$USERNAME/log.dat
90  // This file contains by line:
91  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
92  // - the 6 mesured joint velocities (m/s, rad/s)
93  // - the 6 mesured joint positions (m, rad)
94  // - the 2 values of s - s*
95  std::string username;
96  // Get the user login name
97  vpIoTools::getUserName(username);
98 
99  // Create a log filename to save velocities...
100  std::string logdirname;
101  logdirname ="/tmp/" + username;
102 
103  // Test if the output path exist. If no try to create it
104  if (vpIoTools::checkDirectory(logdirname) == false) {
105  try {
106  // Create the dirname
107  vpIoTools::makeDirectory(logdirname);
108  }
109  catch (...) {
110  std::cerr << std::endl
111  << "ERROR:" << std::endl;
112  std::cerr << " Cannot create " << logdirname << std::endl;
113  exit(-1);
114  }
115  }
116  std::string logfilename;
117  logfilename = logdirname + "/log.dat";
118 
119  // Open the log file name
120  std::ofstream flog(logfilename.c_str());
121 
122  try {
123  vpRobotViper850 robot ;
124 
125  vpServo task ;
126 
128 
129  bool reset = false;
130  vp1394TwoGrabber g(reset);
132  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
133  g.open(I) ;
134 
135  g.acquire(I) ;
136 
137  vpDisplayX display(I, 800, 100,"Camera view") ;
138  vpTRACE(" ") ;
139 
140  vpDisplay::display(I) ;
141  vpDisplay::flush(I) ;
142  // exit(1) ;
143 
144  std::cout << std::endl ;
145  std::cout << "-------------------------------------------------------" << std::endl ;
146  std::cout << " Test program for vpServo " <<std::endl ;
147  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
148  std::cout << " Use of the Afma6 robot " << std::endl ;
149  std::cout << " task : servo a point " << std::endl ;
150  std::cout << "-------------------------------------------------------" << std::endl ;
151  std::cout << std::endl ;
152 
153 
154  vpDot2 dot ;
155 
156  std::cout << "Click on a dot..." << std::endl;
157  dot.initTracking(I) ;
158  vpImagePoint cog = dot.getCog();
160  vpDisplay::flush(I);
161 
162  vpCameraParameters cam ;
163  // Update camera parameters
164  robot.getCameraParameters (cam, I);
165 
166  vpTRACE("sets the current position of the visual feature ") ;
167  vpFeaturePoint p ;
168  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
169 
170  p.set_Z(1) ;
171  vpTRACE("sets the desired position of the visual feature ") ;
172  vpFeaturePoint pd ;
173  pd.buildFrom(0,0,1) ;
174 
175  vpTRACE("define the task") ;
176  vpTRACE("\t we want an eye-in-hand control law") ;
177  vpTRACE("\t articular velocity are computed") ;
180 
181 
182  vpTRACE("Set the position of the camera in the end-effector frame ") ;
183  vpHomogeneousMatrix cMe ;
184  // robot.get_cMe(cMe) ;
185 
187  robot.get_cVe(cVe) ;
188  std::cout << cVe <<std::endl ;
189  task.set_cVe(cVe) ;
190 
191  // vpDisplay::getClick(I) ;
192  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
193  vpMatrix eJe ;
194  robot.get_eJe(eJe) ;
195  task.set_eJe(eJe) ;
196 
197 
198  vpTRACE("\t we want to see a point on a point..") ;
199  std::cout << std::endl ;
200  task.addFeature(p,pd) ;
201 
202  vpTRACE("\t set the gain") ;
203  task.setLambda(0.8) ;
204 
205  vpTRACE("Display task information " ) ;
206  task.print() ;
207 
209 
210  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
211  for ( ; ; ) {
212  // Acquire a new image from the camera
213  g.acquire(I) ;
214 
215  // Display this image
216  vpDisplay::display(I) ;
217 
218  // Achieve the tracking of the dot in the image
219  dot.track(I) ;
220  cog = dot.getCog();
221 
222  // Display a green cross at the center of gravity position in the image
224 
225 
226  // Update the point feature from the dot location
227  vpFeatureBuilder::create(p, cam, dot);
228 
229  // Get the jacobian of the robot
230  robot.get_eJe(eJe) ;
231  // Update this jacobian in the task structure. It will be used to compute
232  // the velocity skew (as an articular velocity)
233  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
234  task.set_eJe(eJe) ;
235 
236  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
237 
238  vpColVector v ;
239  // Compute the visual servoing skew vector
240  v = task.computeControlLaw() ;
241 
242  // Display the current and desired feature points in the image display
243  vpServoDisplay::display(task, cam, I) ;
244 
245  // Apply the computed joint velocities to the robot
247 
248  // Save velocities applied to the robot in the log file
249  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
250  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
251  flog << v[0] << " " << v[1] << " " << v[2] << " "
252  << v[3] << " " << v[4] << " " << v[5] << " ";
253 
254  // Get the measured joint velocities of the robot
255  vpColVector qvel;
257  // Save measured joint velocities of the robot in the log file:
258  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
259  // velocities in m/s
260  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
261  // velocities in rad/s
262  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
263  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
264 
265  // Get the measured joint positions of the robot
266  vpColVector q;
268  // Save measured joint positions of the robot in the log file
269  // - q[0], q[1], q[2] correspond to measured joint translation
270  // positions in m
271  // - q[3], q[4], q[5] correspond to measured joint rotation
272  // positions in rad
273  flog << q[0] << " " << q[1] << " " << q[2] << " "
274  << q[3] << " " << q[4] << " " << q[5] << " ";
275 
276  // Save feature error (s-s*) for the feature point. For this feature
277  // point, we have 2 errors (along x and y axis). This error is expressed
278  // in meters in the camera frame
279  flog << ( task.getError() ).t() << std::endl; // s-s* for point
280 
281  vpDisplay::flush(I) ;
282 
283  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
284  }
285 
286  flog.close() ; // Close the log file
287 
288  vpTRACE("Display task information " ) ;
289  task.print() ;
290  task.kill();
291  return 0;
292  }
293  catch (...)
294  {
295  flog.close() ; // Close the log file
296  vpERROR_TRACE(" Test failed") ;
297  return 0;
298  }
299 }
300 
301 
302 #else
303 int
304 main()
305 {
306  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
307 }
308 #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
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
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
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
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
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:227
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
vpImagePoint getCog() const
Definition: vpDot2.h:254
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
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...
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 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 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: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 setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
static const vpColor blue
Definition: vpColor.h:171