ViSP  2.8.0
servoAfma4Point2DArtVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma4Point2DArtVelocity.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
68 #include <visp/vpConfig.h>
69 #include <visp/vpDebug.h> // Debug trace
70 #include <stdio.h>
71 #include <iostream>
72 #include <fstream>
73 #include <sstream>
74 #include <stdlib.h>
75 #if (defined (VISP_HAVE_AFMA4) && defined (VISP_HAVE_DC1394_2))
76 
77 #include <visp/vp1394TwoGrabber.h>
78 #include <visp/vpImage.h>
79 #include <visp/vpImagePoint.h>
80 #include <visp/vpDisplay.h>
81 #include <visp/vpDisplayX.h>
82 #include <visp/vpDisplayOpenCV.h>
83 #include <visp/vpDisplayGTK.h>
84 
85 #include <visp/vpMath.h>
86 #include <visp/vpHomogeneousMatrix.h>
87 #include <visp/vpFeaturePoint.h>
88 #include <visp/vpPoint.h>
89 #include <visp/vpServo.h>
90 #include <visp/vpFeatureBuilder.h>
91 #include <visp/vpRobotAfma4.h>
92 #include <visp/vpIoTools.h>
93 
94 // Exception
95 #include <visp/vpException.h>
96 #include <visp/vpMatrixException.h>
97 #include <visp/vpServoDisplay.h>
98 
99 #include <visp/vpDot.h>
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 joint velocities (m/s, rad/s) to achieve the task
107  // - the 6 mesured joint velocities (m/s, rad/s)
108  // - the 6 mesured joint positions (m, rad)
109  // - the 2 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  // vpRobotAfma4 robot ;
139  vpRobotAfma4 robot ;
140 
141  vpServo task ;
142 
144 
148  g.open(I) ;
149 
150  g.acquire(I) ;
151 
152 #ifdef VISP_HAVE_X11
153  vpDisplayX display(I,100,100,"Current image") ;
154 #elif defined(VISP_HAVE_OPENCV)
155  vpDisplayOpenCV display(I,100,100,"Current image") ;
156 #elif defined(VISP_HAVE_GTK)
157  vpDisplayGTK display(I,100,100,"Current image") ;
158 #endif
159 
160  vpDisplay::display(I) ;
161  vpDisplay::flush(I) ;
162  // exit(1) ;
163 
164  std::cout << std::endl ;
165  std::cout << "-------------------------------------------------------" << std::endl ;
166  std::cout << " Test program for vpServo " <<std::endl ;
167  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
168  std::cout << " Use of the Afma4 robot " << std::endl ;
169  std::cout << " task : servo a point " << std::endl ;
170  std::cout << "-------------------------------------------------------" << std::endl ;
171  std::cout << std::endl ;
172 
173 
174  vpDot dot ;
175 
176  std::cout << "Click on a dot..." << std::endl;
177  dot.initTracking(I) ;
178 
179  vpImagePoint cog = dot.getCog();
180 
182  vpDisplay::flush(I);
183 
184  vpCameraParameters cam ;
185  // Update camera parameters
186  //robot.getCameraParameters (cam, I);
187 
188  vpTRACE("sets the current position of the visual feature ") ;
189  vpFeaturePoint p ;
190  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
191 
192  p.set_Z(1) ;
193  vpTRACE("sets the desired position of the visual feature ") ;
194  vpFeaturePoint pd ;
195  pd.buildFrom(0,0,1) ;
196 
197  vpTRACE("define the task") ;
198  vpTRACE("\t we want an eye-in-hand control law") ;
199  vpTRACE("\t articular velocity are computed") ;
202 
203 
204  vpTRACE("Set the position of the camera in the end-effector frame ") ;
205  vpHomogeneousMatrix cMe ;
206  // robot.get_cMe(cMe) ;
207 
209  robot.get_cVe(cVe) ;
210  std::cout << cVe <<std::endl ;
211  task.set_cVe(cVe) ;
212 
213  // vpDisplay::getClick(I) ;
214  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
215  vpMatrix eJe ;
216  robot.get_eJe(eJe) ;
217  task.set_eJe(eJe) ;
218 
219 
220  vpTRACE("\t we want to see a point on a point..") ;
221  std::cout << std::endl ;
222  task.addFeature(p,pd) ;
223 
224  vpTRACE("\t set the gain") ;
225  task.setLambda(0.8) ;
226 
227  vpTRACE("Display task information " ) ;
228  task.print() ;
229 
231 
232  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
233  for ( ; ; ) {
234  // Acquire a new image from the camera
235  g.acquire(I) ;
236 
237  // Display this image
238  vpDisplay::display(I) ;
239 
240  // Achieve the tracking of the dot in the image
241  dot.track(I) ;
242 
243  // Get the cog of the dot
244  cog = dot.getCog();
245 
246  // Display a green cross at the center of gravity position in the image
248 
249 
250  // Update the point feature from the dot location
251  vpFeatureBuilder::create(p, cam, dot);
252 
253  // Get the jacobian of the robot
254  robot.get_eJe(eJe) ;
255  // Update this jacobian in the task structure. It will be used to compute
256  // the velocity skew (as an articular velocity)
257  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
258  task.set_eJe(eJe) ;
259 
260  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
261 
262  vpColVector v ;
263  // Compute the visual servoing skew vector
264  v = task.computeControlLaw() ;
265 
266  // Display the current and desired feature points in the image display
267  vpServoDisplay::display(task, cam, I) ;
268 
269  // Apply the computed joint velocities to the robot
271 
272  // Save velocities applied to the robot in the log file
273  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
274  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
275  flog << v[0] << " " << v[1] << " " << v[2] << " "
276  << v[3] << " " << v[4] << " " << v[5] << " ";
277 
278  // Get the measured joint velocities of the robot
279  vpColVector qvel;
281  // Save measured joint velocities of the robot in the log file:
282  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
283  // velocities in m/s
284  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
285  // velocities in rad/s
286  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
287  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
288 
289  // Get the measured joint positions of the robot
290  vpColVector q;
292  // Save measured joint positions of the robot in the log file
293  // - q[0], q[1], q[2] correspond to measured joint translation
294  // positions in m
295  // - q[3], q[4], q[5] correspond to measured joint rotation
296  // positions in rad
297  flog << q[0] << " " << q[1] << " " << q[2] << " "
298  << q[3] << " " << q[4] << " " << q[5] << " ";
299 
300  // Save feature error (s-s*) for the feature point. For this feature
301  // point, we have 2 errors (along x and y axis). This error is expressed
302  // in meters in the camera frame
303  flog << task.getError() << std::endl;
304  vpDisplay::flush(I) ;
305 
306  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
307  }
308 
309  flog.close() ; // Close the log file
310 
311  vpTRACE("Display task information " ) ;
312  task.print() ;
313  task.kill();
314  return 0;
315  }
316  catch (...)
317  {
318  flog.close() ; // Close the log file
319  vpERROR_TRACE(" Test failed") ;
320  return 0;
321  }
322 }
323 
324 
325 #else
326 int
327 main()
328 {
329  vpERROR_TRACE("You do not have an afma4 robot or a firewire framegrabber connected to your computer...");
330 }
331 #endif
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:335
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
#define vpTRACE
Definition: vpDebug.h:401
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
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:253
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:791
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:170
void acquire(vpImage< unsigned char > &I)
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:230
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
void open(vpImage< unsigned char > &I)
vpImagePoint getCog() const
Definition: vpDot.h:227
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:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
void get_cVe(vpVelocityTwistMatrix &cVe)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:238
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.
static std::string getUserName()
Definition: vpIoTools.cpp:140
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
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 buildFrom(const double x, const double y, const double Z)
void get_eJe(vpMatrix &eJe)
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
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 getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
void setFramerate(vp1394TwoFramerateType fps)
Control of Irisa's cylindrical robot named Afma4.
Definition: vpRobotAfma4.h:181
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 (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:118
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 descbribed in and .
Definition: vpServo.h:153
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:642
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
static const vpColor blue
Definition: vpColor.h:173