ViSP  2.6.2
servoAfma4Point2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma4Point2DCamVelocity.cpp 3619 2012-03-09 17:28:57Z 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 camera frame
38  *
39  * Authors:
40  * Eric Marchand
41  * Fabien Spindler
42  *
43  *****************************************************************************/
44 
69 #include <visp/vpConfig.h>
70 #include <visp/vpDebug.h> // Debug trace
71 #include <stdlib.h>
72 #if (defined (VISP_HAVE_AFMA4) && defined (VISP_HAVE_DC1394_2))
73 
74 #include <visp/vp1394TwoGrabber.h>
75 #include <visp/vpImage.h>
76 #include <visp/vpImagePoint.h>
77 #include <visp/vpDisplay.h>
78 #include <visp/vpDisplayX.h>
79 
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/vpRobotAfma4.h>
87 #include <visp/vpIoTools.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 cam 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 2 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  {
134  vpRobotAfma4 robot ;
135  vpServo task ;
136 
137 
139 
140  vp1394TwoGrabber g(false);
142  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
143  g.open(I) ;
144 
145  g.acquire(I) ;
146 
147  vpDisplayX display(I,100,100,"testDisplayX.cpp ") ;
148  vpTRACE(" ") ;
149 
150  vpDisplay::display(I) ;
151  vpDisplay::flush(I) ;
152 
153  std::cout << std::endl ;
154  std::cout << "-------------------------------------------------------" << std::endl ;
155  std::cout << " Test program for vpServo " <<std::endl ;
156  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
157  std::cout << " Simulation " << std::endl ;
158  std::cout << " task : servo a point " << std::endl ;
159  std::cout << "-------------------------------------------------------" << std::endl ;
160  std::cout << std::endl ;
161 
162  vpDot dot ;
163 
164  std::cout << "Click on a dot..." << std::endl;
165  dot.initTracking(I) ;
166 
167  // Get the cog of the dot
168  vpImagePoint cog = dot.getCog();
169 
171  vpDisplay::flush(I);
172 
173 
174  vpCameraParameters cam ;
175 
176  vpTRACE("sets the current position of the visual feature ") ;
177  vpFeaturePoint p ;
178  vpFeatureBuilder::create(p, cam, dot) ; //retrieve x,y and Z of the vpPoint structure
179 
180  vpTRACE("sets the desired position of the visual feature ") ;
181  vpFeaturePoint pd ;
182  pd.buildFrom(0,0,1) ;
183 
184  vpTRACE("define the task") ;
185  vpTRACE("\t we want an eye-in-hand control law") ;
186  vpTRACE("\t robot is controlled in the camera frame") ;
188 
189  vpTRACE("\t we want to see a point on a point..") ;
190  std::cout << std::endl ;
191  task.addFeature(p,pd) ;
192 
193  vpTRACE("\t set the gain") ;
194  task.setLambda(0.8) ;
195 
196 
197  vpTRACE("Display task information " ) ;
198  task.print() ;
199 
200 
202 
203  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
204  for ( ; ; ) {
205  // Acquire a new image from the camera
206  g.acquire(I) ;
207 
208  // Display this image
209  vpDisplay::display(I) ;
210 
211  // Achieve the tracking of the dot in the image
212  dot.track(I) ;
213 
214  // Get the cog of the dot
215  vpImagePoint cog = dot.getCog();
216 
217  // Display a green cross at the center of gravity position in the image
219 
220  // Update the point feature from the dot location
221  vpFeatureBuilder::create(p, cam, dot);
222 
223  vpColVector v ;
224  // Compute the visual servoing skew vector
225  v = task.computeControlLaw() ;
226 
227  // Display the current and desired feature points in the image display
228  vpServoDisplay::display(task, cam, I) ;
229 
230  // Apply the computed joint velocities to the robot
232 
233  // Save velocities applied to the robot in the log file
234  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
235  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
236  flog << v[0] << " " << v[1] << " " << v[2] << " "
237  << v[3] << " " << v[4] << " " << v[5] << " ";
238 
239  // Get the measured joint velocities of the robot
240  vpColVector qvel;
242  // Save measured joint velocities of the robot in the log file:
243  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
244  // velocities in m/s
245  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
246  // velocities in rad/s
247  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
248  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
249 
250  // Get the measured joint positions of the robot
251  vpColVector q;
253  // Save measured joint positions of the robot in the log file
254  // - q[0], q[1], q[2] correspond to measured joint translation
255  // positions in m
256  // - q[3], q[4], q[5] correspond to measured joint rotation
257  // positions in rad
258  flog << q[0] << " " << q[1] << " " << q[2] << " "
259  << q[3] << " " << q[4] << " " << q[5] << " ";
260 
261  // Save feature error (s-s*) for the feature point. For this feature
262  // point, we have 2 errors (along x and y axis). This error is expressed
263  // in meters in the camera frame
264  flog << task.getError() << std::endl;
265 
266  // Flush the display
267  vpDisplay::flush(I) ;
268 
269  }
270 
271  flog.close() ; // Close the log file
272 
273  // Display task information
274  task.print() ;
275 
276  // Kill the task
277  task.kill();
278 
279  return 0;
280  }
281  catch (...)
282  {
283  flog.close() ; // Close the log file
284  vpERROR_TRACE(" Test failed") ;
285  return 0;
286  }
287 }
288 
289 
290 #else
291 int
292 main()
293 {
294  vpERROR_TRACE("You do not have an afma4 robot or a firewire framegrabber connected to your computer...");
295 }
296 #endif
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 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: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
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
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
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
void buildFrom(const double x, const double y, const double Z)
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)
Control of Irisa's cylindrical robot named Afma4.
Definition: vpRobotAfma4.h:181
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