Visual Servoing Platform  version 3.0.1
servoAfma6Point2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
64 #include <visp3/core/vpConfig.h>
65 #include <visp3/core/vpDebug.h> // Debug trace
66 #include <stdlib.h>
67 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394))
68 
69 #include <visp3/sensor/vp1394TwoGrabber.h>
70 #include <visp3/core/vpImage.h>
71 #include <visp3/core/vpImagePoint.h>
72 #include <visp3/core/vpMath.h>
73 #include <visp3/core/vpHomogeneousMatrix.h>
74 #include <visp3/visual_features/vpFeaturePoint.h>
75 #include <visp3/core/vpPoint.h>
76 #include <visp3/vs/vpServo.h>
77 #include <visp3/visual_features/vpFeatureBuilder.h>
78 #include <visp3/robot/vpRobotAfma6.h>
79 #include <visp3/core/vpIoTools.h>
80 #include <visp3/core/vpException.h>
81 #include <visp3/vs/vpServoDisplay.h>
82 #include <visp3/blob/vpDot.h>
83 #include <visp3/core/vpDisplay.h>
84 #include <visp3/gui/vpDisplayX.h>
85 #include <visp3/gui/vpDisplayOpenCV.h>
86 #include <visp3/gui/vpDisplayGTK.h>
87 
88 int
89 main()
90 {
91  // Log file creation in /tmp/$USERNAME/log.dat
92  // This file contains by line:
93  // - the 6 computed cam velocities (m/s, rad/s) to achieve the task
94  // - the 6 mesured joint velocities (m/s, rad/s)
95  // - the 6 mesured joint positions (m, rad)
96  // - the 2 values of s - s*
97  std::string username;
98  // Get the user login name
99  vpIoTools::getUserName(username);
100 
101  // Create a log filename to save velocities...
102  std::string logdirname;
103  logdirname ="/tmp/" + username;
104 
105  // Test if the output path exist. If no try to create it
106  if (vpIoTools::checkDirectory(logdirname) == false) {
107  try {
108  // Create the dirname
109  vpIoTools::makeDirectory(logdirname);
110  }
111  catch (...) {
112  std::cerr << std::endl
113  << "ERROR:" << std::endl;
114  std::cerr << " Cannot create " << logdirname << std::endl;
115  exit(-1);
116  }
117  }
118  std::string logfilename;
119  logfilename = logdirname + "/log.dat";
120 
121  // Open the log file name
122  std::ofstream flog(logfilename.c_str());
123 
124  try
125  {
126  vpServo task ;
127 
129 
133  g.open(I) ;
134 
135  g.acquire(I) ;
136 
137 #ifdef VISP_HAVE_X11
138  vpDisplayX display(I,100,100,"Current image") ;
139 #elif defined(VISP_HAVE_OPENCV)
140  vpDisplayOpenCV display(I,100,100,"Current image") ;
141 #elif defined(VISP_HAVE_GTK)
142  vpDisplayGTK display(I,100,100,"Current image") ;
143 #endif
144 
145  vpDisplay::display(I) ;
146  vpDisplay::flush(I) ;
147 
148  vpDot dot ;
149  vpImagePoint cog;
150 
151  std::cout << "Click on a dot..." << std::endl;
152  dot.initTracking(I) ;
153  cog = dot.getCog();
155  vpDisplay::flush(I);
156 
157  vpRobotAfma6 robot ;
158 
159  vpCameraParameters cam ;
160  // Update camera parameters
161  robot.getCameraParameters (cam, I);
162 
163  // sets the current position of the visual feature
164  vpFeaturePoint p ;
165  // retrieve x,y and Z of the vpPoint structure
166  vpFeatureBuilder::create(p,cam, dot);
167 
168  // sets the desired position of the visual feature
169  vpFeaturePoint pd ;
170  pd.buildFrom(0,0,1) ;
171 
172  // define the task
173  // - we want an eye-in-hand control law
174  // - robot is controlled in the camera frame
176 
177  // - we want to see a point on a point
178  task.addFeature(p,pd) ;
179 
180  // - set the constant gain
181  task.setLambda(0.8) ;
182 
183  // Display task information
184  task.print() ;
185 
186  // Now the robot will be controlled in velocity
188 
189  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
190  for ( ; ; ) {
191  // Acquire a new image from the camera
192  g.acquire(I) ;
193 
194  // Display this image
195  vpDisplay::display(I) ;
196 
197  // Achieve the tracking of the dot in the image
198  dot.track(I) ;
199 
200  // Get the dot cog
201  cog = dot.getCog();
202 
203  // Display a green cross at the center of gravity position in the image
205 
206  // Update the point feature from the dot location
207  vpFeatureBuilder::create(p, cam, dot);
208 
209  vpColVector v ;
210  // Compute the visual servoing skew vector
211  v = task.computeControlLaw() ;
212 
213  // Display the current and desired feature points in the image display
214  vpServoDisplay::display(task, cam, I) ;
215 
216  // Apply the computed joint velocities to the robot
218 
219  // Save velocities applied to the robot in the log file
220  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
221  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
222  flog << v[0] << " " << v[1] << " " << v[2] << " "
223  << v[3] << " " << v[4] << " " << v[5] << " ";
224 
225  // Get the measured joint velocities of the robot
226  vpColVector qvel;
228  // Save measured joint velocities of the robot in the log file:
229  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
230  // velocities in m/s
231  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
232  // velocities in rad/s
233  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
234  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
235 
236  // Get the measured joint positions of the robot
237  vpColVector q;
239  // Save measured joint positions of the robot in the log file
240  // - q[0], q[1], q[2] correspond to measured joint translation
241  // positions in m
242  // - q[3], q[4], q[5] correspond to measured joint rotation
243  // positions in rad
244  flog << q[0] << " " << q[1] << " " << q[2] << " "
245  << q[3] << " " << q[4] << " " << q[5] << " ";
246 
247  // Save feature error (s-s*) for the feature point. For this feature
248  // point, we have 2 errors (along x and y axis). This error is expressed
249  // in meters in the camera frame
250  flog << ( task.getError() ).t() << std::endl;
251 
252  // Flush the display
253  vpDisplay::flush(I) ;
254 
255  }
256 
257  flog.close() ; // Close the log file
258 
259  // Display task information
260  task.print() ;
261 
262  // Kill the task
263  task.kill();
264 
265  return 0;
266  }
267  catch (...)
268  {
269  flog.close() ; // Close the log file
270  vpERROR_TRACE(" Test failed") ;
271  return 0;
272  }
273 }
274 
275 
276 #else
277 int
278 main()
279 {
280  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
281 }
282 #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:1235
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
#define vpERROR_TRACE
Definition: vpDebug.h:391
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:800
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
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Control of Irisa&#39;s gantry robot named Afma6.
Definition: vpRobotAfma6.h:210
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
void open(vpImage< unsigned char > &I)
void kill()
Definition: vpServo.cpp:191
vpImagePoint getCog() const
Definition: vpDot.h:225
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:391
static std::string getUserName()
Definition: vpIoTools.cpp:177
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void buildFrom(const double x, const double y, const double Z)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Implementation of column vector and the associated operations.
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:314
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:116
vpColVector getError() const
Definition: vpServo.h:271
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:88
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
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