ViSP  2.9.0
servoAfma6Point2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6Point2DCamVelocity.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 
68 #include <visp/vpConfig.h>
69 #include <visp/vpDebug.h> // Debug trace
70 #include <stdlib.h>
71 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
72 
73 #include <visp/vp1394TwoGrabber.h>
74 #include <visp/vpImage.h>
75 #include <visp/vpImagePoint.h>
76 #include <visp/vpMath.h>
77 #include <visp/vpHomogeneousMatrix.h>
78 #include <visp/vpFeaturePoint.h>
79 #include <visp/vpPoint.h>
80 #include <visp/vpServo.h>
81 #include <visp/vpFeatureBuilder.h>
82 #include <visp/vpRobotAfma6.h>
83 #include <visp/vpIoTools.h>
84 #include <visp/vpException.h>
85 #include <visp/vpMatrixException.h>
86 #include <visp/vpServoDisplay.h>
87 #include <visp/vpDot.h>
88 #include <visp/vpDisplay.h>
89 #include <visp/vpDisplayX.h>
90 #include <visp/vpDisplayOpenCV.h>
91 #include <visp/vpDisplayGTK.h>
92 
93 int
94 main()
95 {
96  // Log file creation in /tmp/$USERNAME/log.dat
97  // This file contains by line:
98  // - the 6 computed cam velocities (m/s, rad/s) to achieve the task
99  // - the 6 mesured joint velocities (m/s, rad/s)
100  // - the 6 mesured joint positions (m, rad)
101  // - the 2 values of s - s*
102  std::string username;
103  // Get the user login name
104  vpIoTools::getUserName(username);
105 
106  // Create a log filename to save velocities...
107  std::string logdirname;
108  logdirname ="/tmp/" + username;
109 
110  // Test if the output path exist. If no try to create it
111  if (vpIoTools::checkDirectory(logdirname) == false) {
112  try {
113  // Create the dirname
114  vpIoTools::makeDirectory(logdirname);
115  }
116  catch (...) {
117  std::cerr << std::endl
118  << "ERROR:" << std::endl;
119  std::cerr << " Cannot create " << logdirname << std::endl;
120  exit(-1);
121  }
122  }
123  std::string logfilename;
124  logfilename = logdirname + "/log.dat";
125 
126  // Open the log file name
127  std::ofstream flog(logfilename.c_str());
128 
129  try
130  {
131  vpServo task ;
132 
134 
138  g.open(I) ;
139 
140  g.acquire(I) ;
141 
142 #ifdef VISP_HAVE_X11
143  vpDisplayX display(I,100,100,"Current image") ;
144 #elif defined(VISP_HAVE_OPENCV)
145  vpDisplayOpenCV display(I,100,100,"Current image") ;
146 #elif defined(VISP_HAVE_GTK)
147  vpDisplayGTK display(I,100,100,"Current image") ;
148 #endif
149 
150  vpDisplay::display(I) ;
151  vpDisplay::flush(I) ;
152 
153  vpDot dot ;
154  vpImagePoint cog;
155 
156  std::cout << "Click on a dot..." << std::endl;
157  dot.initTracking(I) ;
158  cog = dot.getCog();
160  vpDisplay::flush(I);
161 
162  vpRobotAfma6 robot ;
163 
164  vpCameraParameters cam ;
165  // Update camera parameters
166  robot.getCameraParameters (cam, I);
167 
168  // sets the current position of the visual feature
169  vpFeaturePoint p ;
170  // retrieve x,y and Z of the vpPoint structure
171  vpFeatureBuilder::create(p,cam, dot);
172 
173  // sets the desired position of the visual feature
174  vpFeaturePoint pd ;
175  pd.buildFrom(0,0,1) ;
176 
177  // define the task
178  // - we want an eye-in-hand control law
179  // - robot is controlled in the camera frame
181 
182  // - we want to see a point on a point
183  task.addFeature(p,pd) ;
184 
185  // - set the constant gain
186  task.setLambda(0.8) ;
187 
188  // Display task information
189  task.print() ;
190 
191  // Now the robot will be controlled in velocity
193 
194  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
195  for ( ; ; ) {
196  // Acquire a new image from the camera
197  g.acquire(I) ;
198 
199  // Display this image
200  vpDisplay::display(I) ;
201 
202  // Achieve the tracking of the dot in the image
203  dot.track(I) ;
204 
205  // Get the dot cog
206  cog = dot.getCog();
207 
208  // Display a green cross at the center of gravity position in the image
210 
211  // Update the point feature from the dot location
212  vpFeatureBuilder::create(p, cam, dot);
213 
214  vpColVector v ;
215  // Compute the visual servoing skew vector
216  v = task.computeControlLaw() ;
217 
218  // Display the current and desired feature points in the image display
219  vpServoDisplay::display(task, cam, I) ;
220 
221  // Apply the computed joint velocities to the robot
223 
224  // Save velocities applied to the robot in the log file
225  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
226  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
227  flog << v[0] << " " << v[1] << " " << v[2] << " "
228  << v[3] << " " << v[4] << " " << v[5] << " ";
229 
230  // Get the measured joint velocities of the robot
231  vpColVector qvel;
233  // Save measured joint velocities of the robot in the log file:
234  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
235  // velocities in m/s
236  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
237  // velocities in rad/s
238  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
239  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
240 
241  // Get the measured joint positions of the robot
242  vpColVector q;
244  // Save measured joint positions of the robot in the log file
245  // - q[0], q[1], q[2] correspond to measured joint translation
246  // positions in m
247  // - q[3], q[4], q[5] correspond to measured joint rotation
248  // positions in rad
249  flog << q[0] << " " << q[1] << " " << q[2] << " "
250  << q[3] << " " << q[4] << " " << q[5] << " ";
251 
252  // Save feature error (s-s*) for the feature point. For this feature
253  // point, we have 2 errors (along x and y axis). This error is expressed
254  // in meters in the camera frame
255  flog << ( task.getError() ).t() << std::endl;
256 
257  // Flush the display
258  vpDisplay::flush(I) ;
259 
260  }
261 
262  flog.close() ; // Close the log file
263 
264  // Display task information
265  task.print() ;
266 
267  // Kill the task
268  task.kill();
269 
270  return 0;
271  }
272  catch (...)
273  {
274  flog.close() ; // Close the log file
275  vpERROR_TRACE(" Test failed") ;
276  return 0;
277  }
278 }
279 
280 
281 #else
282 int
283 main()
284 {
285  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
286 }
287 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1248
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
#define vpERROR_TRACE
Definition: vpDebug.h:395
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)
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 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)
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