ViSP  2.9.0
servoAfma4Point2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma4Point2DCamVelocity.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 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 #include <visp/vpDisplayOpenCV.h>
80 #include <visp/vpDisplayGTK.h>
81 
82 #include <visp/vpMath.h>
83 #include <visp/vpHomogeneousMatrix.h>
84 #include <visp/vpFeaturePoint.h>
85 #include <visp/vpPoint.h>
86 #include <visp/vpServo.h>
87 #include <visp/vpFeatureBuilder.h>
88 #include <visp/vpRobotAfma4.h>
89 #include <visp/vpIoTools.h>
90 
91 // Exception
92 #include <visp/vpException.h>
93 #include <visp/vpMatrixException.h>
94 #include <visp/vpServoDisplay.h>
95 
96 #include <visp/vpDot.h>
97 
98 int
99 main()
100 {
101  try {
102  // Log file creation in /tmp/$USERNAME/log.dat
103  // This file contains by line:
104  // - the 6 computed cam velocities (m/s, rad/s) to achieve the task
105  // - the 6 mesured joint velocities (m/s, rad/s)
106  // - the 6 mesured joint positions (m, rad)
107  // - the 2 values of s - s*
108  std::string username;
109  // Get the user login name
110  vpIoTools::getUserName(username);
111 
112  // Create a log filename to save velocities...
113  std::string logdirname;
114  logdirname ="/tmp/" + username;
115 
116  // Test if the output path exist. If no try to create it
117  if (vpIoTools::checkDirectory(logdirname) == false) {
118  try {
119  // Create the dirname
120  vpIoTools::makeDirectory(logdirname);
121  }
122  catch (...) {
123  std::cerr << std::endl
124  << "ERROR:" << std::endl;
125  std::cerr << " Cannot create " << logdirname << std::endl;
126  exit(-1);
127  }
128  }
129  std::string logfilename;
130  logfilename = logdirname + "/log.dat";
131 
132  // Open the log file name
133  std::ofstream flog(logfilename.c_str());
134 
135  vpRobotAfma4 robot ;
136  vpServo task ;
137 
138 
140 
141  vp1394TwoGrabber g(false);
143  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
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 
159  std::cout << std::endl ;
160  std::cout << "-------------------------------------------------------" << std::endl ;
161  std::cout << " Test program for vpServo " <<std::endl ;
162  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
163  std::cout << " Simulation " << std::endl ;
164  std::cout << " task : servo a point " << std::endl ;
165  std::cout << "-------------------------------------------------------" << std::endl ;
166  std::cout << std::endl ;
167 
168  vpDot dot ;
169 
170  std::cout << "Click on a dot..." << std::endl;
171  dot.initTracking(I) ;
172 
173  // Get the cog of the dot
174  vpImagePoint cog = dot.getCog();
175 
177  vpDisplay::flush(I);
178 
179 
180  vpCameraParameters cam ;
181 
182  vpTRACE("sets the current position of the visual feature ") ;
183  vpFeaturePoint p ;
184  vpFeatureBuilder::create(p, cam, dot) ; //retrieve x,y and Z of the vpPoint structure
185 
186  vpTRACE("sets the desired position of the visual feature ") ;
187  vpFeaturePoint pd ;
188  pd.buildFrom(0,0,1) ;
189 
190  vpTRACE("define the task") ;
191  vpTRACE("\t we want an eye-in-hand control law") ;
192  vpTRACE("\t robot is controlled in the camera frame") ;
194 
195  vpTRACE("\t we want to see a point on a point..") ;
196  std::cout << std::endl ;
197  task.addFeature(p,pd) ;
198 
199  vpTRACE("\t set the gain") ;
200  task.setLambda(0.8) ;
201 
202 
203  vpTRACE("Display task information " ) ;
204  task.print() ;
205 
206 
207  robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL) ;
208 
209  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
210  for ( ; ; ) {
211  // Acquire a new image from the camera
212  g.acquire(I) ;
213 
214  // Display this image
215  vpDisplay::display(I) ;
216 
217  // Achieve the tracking of the dot in the image
218  dot.track(I) ;
219 
220  // Get the cog of the dot
221  vpImagePoint cog = dot.getCog();
222 
223  // Display a green cross at the center of gravity position in the image
225 
226  // Update the point feature from the dot location
227  vpFeatureBuilder::create(p, cam, dot);
228 
229  vpColVector v ;
230  // Compute the visual servoing skew vector
231  v = task.computeControlLaw() ;
232 
233  // Display the current and desired feature points in the image display
234  vpServoDisplay::display(task, cam, I) ;
235 
236  // Apply the computed joint velocities to the robot
237  robot.setVelocity(vpRobot::CAMERA_FRAME, v) ;
238 
239  // Save velocities applied to the robot in the log file
240  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
241  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
242  flog << v[0] << " " << v[1] << " " << v[2] << " "
243  << v[3] << " " << v[4] << " " << v[5] << " ";
244 
245  // Get the measured joint velocities of the robot
246  vpColVector qvel;
247  robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
248  // Save measured joint velocities of the robot in the log file:
249  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
250  // velocities in m/s
251  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
252  // velocities in rad/s
253  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
254  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
255 
256  // Get the measured joint positions of the robot
257  vpColVector q;
258  robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
259  // Save measured joint positions of the robot in the log file
260  // - q[0], q[1], q[2] correspond to measured joint translation
261  // positions in m
262  // - q[3], q[4], q[5] correspond to measured joint rotation
263  // positions in rad
264  flog << q[0] << " " << q[1] << " " << q[2] << " "
265  << q[3] << " " << q[4] << " " << q[5] << " ";
266 
267  // Save feature error (s-s*) for the feature point. For this feature
268  // point, we have 2 errors (along x and y axis). This error is expressed
269  // in meters in the camera frame
270  flog << task.getError() << std::endl;
271 
272  // Flush the display
273  vpDisplay::flush(I) ;
274 
275  }
276 
277  flog.close() ; // Close the log file
278 
279  // Display task information
280  task.print() ;
281 
282  // Kill the task
283  task.kill();
284 
285  return 0;
286  }
287  catch(vpException e) {
288  std::cout << "Catch a ViSP exception: " << e << std::endl;
289  return 1;
290  }
291 }
292 
293 
294 #else
295 int
296 main()
297 {
298  vpERROR_TRACE("You do not have an afma4 robot or a firewire framegrabber connected to your computer...");
299 }
300 #endif
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
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
error that can be emited by ViSP classes.
Definition: vpException.h:76
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
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
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
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
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: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.
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