ViSP  2.9.0
servoAfma6Point2DArtVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6Point2DArtVelocity.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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 
67 #include <visp/vpConfig.h>
68 #include <visp/vpDebug.h> // Debug trace
69 #include <stdio.h>
70 #include <iostream>
71 #include <fstream>
72 #include <sstream>
73 #include <stdlib.h>
74 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
75 
76 #include <visp/vp1394TwoGrabber.h>
77 #include <visp/vpImage.h>
78 #include <visp/vpImagePoint.h>
79 #include <visp/vpDisplay.h>
80 #include <visp/vpDisplayX.h>
81 #include <visp/vpDisplayOpenCV.h>
82 #include <visp/vpDisplayGTK.h>
83 
84 #include <visp/vpMath.h>
85 #include <visp/vpHomogeneousMatrix.h>
86 #include <visp/vpFeaturePoint.h>
87 #include <visp/vpPoint.h>
88 #include <visp/vpServo.h>
89 #include <visp/vpFeatureBuilder.h>
90 #include <visp/vpRobotAfma6.h>
91 #include <visp/vpIoTools.h>
92 
93 // Exception
94 #include <visp/vpException.h>
95 #include <visp/vpMatrixException.h>
96 #include <visp/vpServoDisplay.h>
97 
98 #include <visp/vpDot.h>
99 
100 int
101 main()
102 {
103  // Log file creation in /tmp/$USERNAME/log.dat
104  // This file contains by line:
105  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
106  // - the 6 mesured joint velocities (m/s, rad/s)
107  // - the 6 mesured joint positions (m, rad)
108  // - the 2 values of s - s*
109  std::string username;
110  // Get the user login name
111  vpIoTools::getUserName(username);
112 
113  // Create a log filename to save velocities...
114  std::string logdirname;
115  logdirname ="/tmp/" + username;
116 
117  // Test if the output path exist. If no try to create it
118  if (vpIoTools::checkDirectory(logdirname) == false) {
119  try {
120  // Create the dirname
121  vpIoTools::makeDirectory(logdirname);
122  }
123  catch (...) {
124  std::cerr << std::endl
125  << "ERROR:" << std::endl;
126  std::cerr << " Cannot create " << logdirname << std::endl;
127  exit(-1);
128  }
129  }
130  std::string logfilename;
131  logfilename = logdirname + "/log.dat";
132 
133  // Open the log file name
134  std::ofstream flog(logfilename.c_str());
135 
136  try {
137  vpServo task ;
138 
140 
144  g.open(I) ;
145 
146  g.acquire(I) ;
147 
148 #ifdef VISP_HAVE_X11
149  vpDisplayX display(I,100,100,"Current image") ;
150 #elif defined(VISP_HAVE_OPENCV)
151  vpDisplayOpenCV display(I,100,100,"Current image") ;
152 #elif defined(VISP_HAVE_GTK)
153  vpDisplayGTK display(I,100,100,"Current image") ;
154 #endif
155 
156  vpDisplay::display(I) ;
157  vpDisplay::flush(I) ;
158  // exit(1) ;
159 
160  std::cout << std::endl ;
161  std::cout << "-------------------------------------------------------" << std::endl ;
162  std::cout << " Test program for vpServo " <<std::endl ;
163  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
164  std::cout << " Use of the Afma6 robot " << std::endl ;
165  std::cout << " task : servo a point " << std::endl ;
166  std::cout << "-------------------------------------------------------" << std::endl ;
167  std::cout << std::endl ;
168 
169 
170  vpDot dot ;
171  vpImagePoint cog;
172 
173  std::cout << "Click on a dot..." << std::endl;
174  dot.initTracking(I) ;
175  // Get the dot cog
176  cog = dot.getCog();
178  vpDisplay::flush(I);
179 
180  vpRobotAfma6 robot ;
181 
182  vpCameraParameters cam ;
183  // Update camera parameters
184  robot.getCameraParameters (cam, I);
185 
186  vpTRACE("sets the current position of the visual feature ") ;
187  vpFeaturePoint p ;
188  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
189 
190  p.set_Z(1) ;
191  vpTRACE("sets the desired position of the visual feature ") ;
192  vpFeaturePoint pd ;
193  pd.buildFrom(0,0,1) ;
194 
195  vpTRACE("define the task") ;
196  vpTRACE("\t we want an eye-in-hand control law") ;
197  vpTRACE("\t articular velocity are computed") ;
200 
201 
202  vpTRACE("Set the position of the camera in the end-effector frame ") ;
203  vpHomogeneousMatrix cMe ;
204  // robot.get_cMe(cMe) ;
205 
207  robot.get_cVe(cVe) ;
208  std::cout << cVe <<std::endl ;
209  task.set_cVe(cVe) ;
210 
211  // vpDisplay::getClick(I) ;
212  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
213  vpMatrix eJe ;
214  robot.get_eJe(eJe) ;
215  task.set_eJe(eJe) ;
216 
217 
218  vpTRACE("\t we want to see a point on a point..") ;
219  std::cout << std::endl ;
220  task.addFeature(p,pd) ;
221 
222  vpTRACE("\t set the gain") ;
223  task.setLambda(0.8) ;
224 
225  vpTRACE("Display task information " ) ;
226  task.print() ;
227 
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  // Achieve the tracking of the dot in the image
239  dot.track(I) ;
240 
241  // Get the dot cog
242  cog = dot.getCog();
243 
244  // Display a green cross at the center of gravity position in the image
246 
247  // Update the point feature from the dot location
248  vpFeatureBuilder::create(p, cam, dot);
249 
250  // Get the jacobian of the robot
251  robot.get_eJe(eJe) ;
252  // Update this jacobian in the task structure. It will be used to compute
253  // the velocity skew (as an articular velocity)
254  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
255  task.set_eJe(eJe) ;
256 
257  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
258 
259  vpColVector v ;
260  // Compute the visual servoing skew vector
261  v = task.computeControlLaw() ;
262 
263  // Display the current and desired feature points in the image display
264  vpServoDisplay::display(task, cam, I) ;
265 
266  // Apply the computed joint velocities to the robot
268 
269  // Save velocities applied to the robot in the log file
270  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
271  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
272  flog << v[0] << " " << v[1] << " " << v[2] << " "
273  << v[3] << " " << v[4] << " " << v[5] << " ";
274 
275  // Get the measured joint velocities of the robot
276  vpColVector qvel;
278  // Save measured joint velocities of the robot in the log file:
279  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
280  // velocities in m/s
281  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
282  // velocities in rad/s
283  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
284  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
285 
286  // Get the measured joint positions of the robot
287  vpColVector q;
289  // Save measured joint positions of the robot in the log file
290  // - q[0], q[1], q[2] correspond to measured joint translation
291  // positions in m
292  // - q[3], q[4], q[5] correspond to measured joint rotation
293  // positions in rad
294  flog << q[0] << " " << q[1] << " " << q[2] << " "
295  << q[3] << " " << q[4] << " " << q[5] << " ";
296 
297  // Save feature error (s-s*) for the feature point. For this feature
298  // point, we have 2 errors (along x and y axis). This error is expressed
299  // in meters in the camera frame
300  flog << ( task.getError() ).t() << std::endl;
301  vpDisplay::flush(I) ;
302 
303  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
304  }
305 
306  flog.close() ; // Close the log file
307 
308  vpTRACE("Display task information " ) ;
309  task.print() ;
310  task.kill();
311  return 0;
312  }
313  catch (...)
314  {
315  flog.close() ; // Close the log file
316  vpERROR_TRACE(" Test failed") ;
317  return 0;
318  }
319 }
320 
321 
322 #else
323 int
324 main()
325 {
326  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
327 }
328 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1248
void get_eJe(vpMatrix &_eJe)
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:395
#define vpTRACE
Definition: vpDebug.h:418
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
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)
Definition: vpServo.cpp:449
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:807
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)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
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:404
void open(vpImage< unsigned char > &I)
vpImagePoint getCog() const
Definition: vpDot.h:223
void kill()
Definition: vpServo.cpp:189
Initialize the velocity controller.
Definition: vpRobot.h:70
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
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:370
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
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
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)
Definition: vpServo.cpp:522
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 set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:114
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:92
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:658
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:173
void get_cVe(vpVelocityTwistMatrix &_cVe) const