Visual Servoing Platform  version 3.0.0
servoAfma4Point2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * tests the control law
32  * eye-in-hand control
33  * velocity computed in the camera frame
34  *
35  * Authors:
36  * Eric Marchand
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
65 #include <visp3/core/vpConfig.h>
66 #include <visp3/core/vpDebug.h> // Debug trace
67 #include <stdlib.h>
68 #if (defined (VISP_HAVE_AFMA4) && defined (VISP_HAVE_DC1394))
69 
70 #include <visp3/sensor/vp1394TwoGrabber.h>
71 #include <visp3/core/vpImage.h>
72 #include <visp3/core/vpImagePoint.h>
73 #include <visp3/core/vpDisplay.h>
74 #include <visp3/gui/vpDisplayX.h>
75 #include <visp3/gui/vpDisplayOpenCV.h>
76 #include <visp3/gui/vpDisplayGTK.h>
77 
78 #include <visp3/core/vpMath.h>
79 #include <visp3/core/vpHomogeneousMatrix.h>
80 #include <visp3/visual_features/vpFeaturePoint.h>
81 #include <visp3/core/vpPoint.h>
82 #include <visp3/vs/vpServo.h>
83 #include <visp3/visual_features/vpFeatureBuilder.h>
84 #include <visp3/robot/vpRobotAfma4.h>
85 #include <visp3/core/vpIoTools.h>
86 
87 // Exception
88 #include <visp3/core/vpException.h>
89 #include <visp3/vs/vpServoDisplay.h>
90 
91 #include <visp3/blob/vpDot.h>
92 
93 int
94 main()
95 {
96  try {
97  // Log file creation in /tmp/$USERNAME/log.dat
98  // This file contains by line:
99  // - the 6 computed cam velocities (m/s, rad/s) to achieve the task
100  // - the 6 mesured joint velocities (m/s, rad/s)
101  // - the 6 mesured joint positions (m, rad)
102  // - the 2 values of s - s*
103  std::string username;
104  // Get the user login name
105  vpIoTools::getUserName(username);
106 
107  // Create a log filename to save velocities...
108  std::string logdirname;
109  logdirname ="/tmp/" + username;
110 
111  // Test if the output path exist. If no try to create it
112  if (vpIoTools::checkDirectory(logdirname) == false) {
113  try {
114  // Create the dirname
115  vpIoTools::makeDirectory(logdirname);
116  }
117  catch (...) {
118  std::cerr << std::endl
119  << "ERROR:" << std::endl;
120  std::cerr << " Cannot create " << logdirname << std::endl;
121  exit(-1);
122  }
123  }
124  std::string logfilename;
125  logfilename = logdirname + "/log.dat";
126 
127  // Open the log file name
128  std::ofstream flog(logfilename.c_str());
129 
130  vpRobotAfma4 robot ;
131  vpServo task ;
132 
133 
135 
136  vp1394TwoGrabber g(false);
138  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
139  g.open(I) ;
140 
141  g.acquire(I) ;
142 
143 #ifdef VISP_HAVE_X11
144  vpDisplayX display(I,100,100,"Current image") ;
145 #elif defined(VISP_HAVE_OPENCV)
146  vpDisplayOpenCV display(I,100,100,"Current image") ;
147 #elif defined(VISP_HAVE_GTK)
148  vpDisplayGTK display(I,100,100,"Current image") ;
149 #endif
150 
151  vpDisplay::display(I) ;
152  vpDisplay::flush(I) ;
153 
154  std::cout << std::endl ;
155  std::cout << "-------------------------------------------------------" << std::endl ;
156  std::cout << " Test program for vpServo " <<std::endl ;
157  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
158  std::cout << " Simulation " << std::endl ;
159  std::cout << " task : servo a point " << std::endl ;
160  std::cout << "-------------------------------------------------------" << std::endl ;
161  std::cout << std::endl ;
162 
163  vpDot dot ;
164 
165  std::cout << "Click on a dot..." << std::endl;
166  dot.initTracking(I) ;
167 
168  // Get the cog of the dot
169  vpImagePoint cog = dot.getCog();
170 
172  vpDisplay::flush(I);
173 
174 
175  vpCameraParameters cam ;
176 
177  vpTRACE("sets the current position of the visual feature ") ;
178  vpFeaturePoint p ;
179  vpFeatureBuilder::create(p, cam, dot) ; //retrieve x,y and Z of the vpPoint structure
180 
181  vpTRACE("sets the desired position of the visual feature ") ;
182  vpFeaturePoint pd ;
183  pd.buildFrom(0,0,1) ;
184 
185  vpTRACE("define the task") ;
186  vpTRACE("\t we want an eye-in-hand control law") ;
187  vpTRACE("\t robot is controlled in the camera frame") ;
189 
190  vpTRACE("\t we want to see a point on a point..") ;
191  std::cout << std::endl ;
192  task.addFeature(p,pd) ;
193 
194  vpTRACE("\t set the gain") ;
195  task.setLambda(0.8) ;
196 
197 
198  vpTRACE("Display task information " ) ;
199  task.print() ;
200 
201 
202  robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL) ;
203 
204  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
205  for ( ; ; ) {
206  // Acquire a new image from the camera
207  g.acquire(I) ;
208 
209  // Display this image
210  vpDisplay::display(I) ;
211 
212  // Achieve the tracking of the dot in the image
213  dot.track(I) ;
214 
215  // Get the cog of the dot
216  vpImagePoint cog = dot.getCog();
217 
218  // Display a green cross at the center of gravity position in the image
220 
221  // Update the point feature from the dot location
222  vpFeatureBuilder::create(p, cam, dot);
223 
224  vpColVector v ;
225  // Compute the visual servoing skew vector
226  v = task.computeControlLaw() ;
227 
228  // Display the current and desired feature points in the image display
229  vpServoDisplay::display(task, cam, I) ;
230 
231  // Apply the computed joint velocities to the robot
232  robot.setVelocity(vpRobot::CAMERA_FRAME, v) ;
233 
234  // Save velocities applied to the robot in the log file
235  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
236  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
237  flog << v[0] << " " << v[1] << " " << v[2] << " "
238  << v[3] << " " << v[4] << " " << v[5] << " ";
239 
240  // Get the measured joint velocities of the robot
241  vpColVector qvel;
242  robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
243  // Save measured joint velocities of the robot in the log file:
244  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
245  // velocities in m/s
246  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
247  // velocities in rad/s
248  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
249  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
250 
251  // Get the measured joint positions of the robot
252  vpColVector q;
253  robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
254  // Save measured joint positions of the robot in the log file
255  // - q[0], q[1], q[2] correspond to measured joint translation
256  // positions in m
257  // - q[3], q[4], q[5] correspond to measured joint rotation
258  // positions in rad
259  flog << q[0] << " " << q[1] << " " << q[2] << " "
260  << q[3] << " " << q[4] << " " << q[5] << " ";
261 
262  // Save feature error (s-s*) for the feature point. For this feature
263  // point, we have 2 errors (along x and y axis). This error is expressed
264  // in meters in the camera frame
265  flog << task.getError() << std::endl;
266 
267  // Flush the display
268  vpDisplay::flush(I) ;
269 
270  }
271 
272  flog.close() ; // Close the log file
273 
274  // Display task information
275  task.print() ;
276 
277  // Kill the task
278  task.kill();
279 
280  return 0;
281  }
282  catch(vpException e) {
283  std::cout << "Catch a ViSP exception: " << e << std::endl;
284  return 1;
285  }
286 }
287 
288 
289 #else
290 int
291 main()
292 {
293  vpERROR_TRACE("You do not have an afma4 robot or a firewire framegrabber connected to your computer...");
294 }
295 #endif
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
#define vpERROR_TRACE
Definition: vpDebug.h:391
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:800
error that can be emited by ViSP classes.
Definition: vpException.h:73
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:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
vpImagePoint getCog() const
Definition: vpDot.h:224
void kill()
Definition: vpServo.cpp:186
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
#define vpTRACE
Definition: vpDebug.h:414
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:390
static std::string getUserName()
Definition: vpIoTools.cpp:161
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
void buildFrom(const double x, const double y, const double Z)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Control of Irisa's cylindrical robot named Afma4.
Definition: vpRobotAfma4.h:177
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:248
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:115
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:88
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:217
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:654
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:169