Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
servoAfma6FourPoints2DCamVelocityInteractionDesired.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * tests the control law
33  * eye-in-hand control
34  * velocity computed in the camera frame
35  *
36  * Authors:
37  * Eric Marchand
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
64 #include <stdlib.h>
65 #include <visp3/core/vpConfig.h>
66 #include <visp3/core/vpDebug.h> // Debug trace
67 #if (defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_DC1394))
68 
69 #include <visp3/core/vpDisplay.h>
70 #include <visp3/core/vpImage.h>
71 #include <visp3/core/vpImagePoint.h>
72 #include <visp3/gui/vpDisplayGTK.h>
73 #include <visp3/gui/vpDisplayOpenCV.h>
74 #include <visp3/gui/vpDisplayX.h>
75 #include <visp3/sensor/vp1394TwoGrabber.h>
76 
77 #include <visp3/blob/vpDot.h>
78 #include <visp3/core/vpHomogeneousMatrix.h>
79 #include <visp3/core/vpIoTools.h>
80 #include <visp3/core/vpMath.h>
81 #include <visp3/core/vpPoint.h>
82 #include <visp3/core/vpRotationMatrix.h>
83 #include <visp3/core/vpRxyzVector.h>
84 #include <visp3/core/vpTranslationVector.h>
85 #include <visp3/robot/vpRobotAfma6.h>
86 #include <visp3/visual_features/vpFeatureBuilder.h>
87 #include <visp3/visual_features/vpFeaturePoint.h>
88 #include <visp3/vs/vpServo.h>
89 #include <visp3/vs/vpServoDisplay.h>
90 
91 // Exception
92 #include <visp3/core/vpException.h>
93 
94 #define L 0.05 // to deal with a 10cm by 10cm square
95 
96 int main()
97 {
98  // Log file creation in /tmp/$USERNAME/log.dat
99  // This file contains by line:
100  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
101  // - the 6 mesured camera velocities (m/s, rad/s)
102  // - the 6 mesured joint positions (m, rad)
103  // - the 8 values of s - s*
104  std::string username;
105  // Get the user login name
106  vpIoTools::getUserName(username);
107 
108  // Create a log filename to save velocities...
109  std::string logdirname;
110  logdirname = "/tmp/" + username;
111 
112  // Test if the output path exist. If no try to create it
113  if (vpIoTools::checkDirectory(logdirname) == false) {
114  try {
115  // Create the dirname
116  vpIoTools::makeDirectory(logdirname);
117  } catch (...) {
118  std::cerr << std::endl << "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  vpServo task;
131 
133  int i;
134 
138  g.open(I);
139 
140 #ifdef VISP_HAVE_X11
141  vpDisplayX display(I, 100, 100, "Current image");
142 #elif defined(VISP_HAVE_OPENCV)
143  vpDisplayOpenCV display(I, 100, 100, "Current image");
144 #elif defined(VISP_HAVE_GTK)
145  vpDisplayGTK display(I, 100, 100, "Current image");
146 #endif
147 
148  g.acquire(I);
149 
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 << " Use of the Afma6 robot " << std::endl;
158  std::cout << " Interaction matrix computed with the desired features " << std::endl;
159 
160  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl;
161  std::cout << "-------------------------------------------------------" << std::endl;
162  std::cout << std::endl;
163 
164  vpDot2 dot[4];
165  vpImagePoint cog;
166 
167  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
168  for (i = 0; i < 4; i++) {
169  dot[i].initTracking(I);
170  cog = dot[i].getCog();
172  vpDisplay::flush(I);
173  }
174 
175  vpRobotAfma6 robot;
176 
178 
179  // Load the end-effector to camera frame transformation obtained
180  // using a camera intrinsic model with distortion
181  robot.init(vpAfma6::TOOL_CCMOP, projModel);
182 
183  vpCameraParameters cam;
184  // Update camera parameters
185  robot.getCameraParameters(cam, I);
186 
187  // Sets the current position of the visual feature
188  vpFeaturePoint p[4];
189  for (i = 0; i < 4; i++)
190  vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y of the vpFeaturePoint structure
191 
192  // Set the position of the square target in a frame which origin is
193  // centered in the middle of the square
194  vpPoint point[4];
195  point[0].setWorldCoordinates(-L, -L, 0);
196  point[1].setWorldCoordinates(L, -L, 0);
197  point[2].setWorldCoordinates(L, L, 0);
198  point[3].setWorldCoordinates(-L, L, 0);
199 
200  // Initialise a desired pose to compute s*, the desired 2D point features
202  vpTranslationVector cto(0, 0, 0.7); // tz = 0.7 meter
204  vpMath::rad(0)); // No rotations
205  vpRotationMatrix cRo(cro); // Build the rotation matrix
206  cMo.buildFrom(cto, cRo); // Build the homogeneous matrix
207 
208  // sets the desired position of the 2D visual feature
209  vpFeaturePoint pd[4];
210  // Compute the desired position of the features from the desired pose
211  for (int i = 0; i < 4; i++) {
212  vpColVector cP, p;
213  point[i].changeFrame(cMo, cP);
214  point[i].projection(cP, p);
215 
216  pd[i].set_x(p[0]);
217  pd[i].set_y(p[1]);
218  pd[i].set_Z(cP[2]);
219  }
220 
221  // Define the task
222  // - we want an eye-in-hand control law
223  // - robot is controlled in the camera frame
224  // - Interaction matrix is computed with the desired visual features
227 
228  // We want to see a point on a point
229  std::cout << std::endl;
230  for (i = 0; i < 4; i++)
231  task.addFeature(p[i], pd[i]);
232 
233  // Set the proportional gain
234  task.setLambda(0.4);
235 
236  // Display task information
237  task.print();
238 
239  // Initialise the velocity control of the robot
241 
242  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
243 
244  for (;;) {
245  // Acquire a new image from the camera
246  g.acquire(I);
247 
248  // Display this image
250 
251  // For each point...
252  for (i = 0; i < 4; i++) {
253  // Achieve the tracking of the dot in the image
254  dot[i].track(I);
255  // Get the dot cog
256  cog = dot[i].getCog();
257  // Display a green cross at the center of gravity position in the
258  // image
260  }
261 
262  // Printing on stdout concerning task information
263  // task.print() ;
264 
265  // Update the point feature from the dot location
266  for (i = 0; i < 4; i++)
267  vpFeatureBuilder::create(p[i], cam, dot[i]);
268 
269  vpColVector v;
270  // Compute the visual servoing skew vector
271  v = task.computeControlLaw();
272 
273  // Display the current and desired feature points in the image display
274  vpServoDisplay::display(task, cam, I);
275 
276  // Apply the computed camera velocities to the robot
278 
279  // Save velocities applied to the robot in the log file
280  // v[0], v[1], v[2] correspond to camera translation velocities in m/s
281  // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
282  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
283 
284  // Get the measured joint velocities of the robot
285  vpColVector qvel;
286  robot.getVelocity(vpRobot::CAMERA_FRAME, qvel);
287  // Save measured camera velocities of the robot in the log file:
288  // - qvel[0], qvel[1], qvel[2] correspond to measured camera translation
289  // velocities in m/s
290  // - qvel[3], qvel[4], qvel[5] correspond to measured camera rotation
291  // velocities in rad/s
292  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
293 
294  // Get the measured joint positions of the robot
295  vpColVector q;
297  // Save measured joint positions of the robot in the log file
298  // - q[0], q[1], q[2] correspond to measured joint translation
299  // positions in m
300  // - q[3], q[4], q[5] correspond to measured joint rotation
301  // positions in rad
302  flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
303 
304  // Save feature error (s-s*) for the 4 feature points. For each feature
305  // point, we have 2 errors (along x and y axis). This error is
306  // expressed in meters in the camera frame
307  flog << (task.getError()).t() << std::endl;
308 
309  // Flush the display
310  vpDisplay::flush(I);
311  }
312 
313  flog.close(); // Close the log file
314 
315  // Display task information
316  task.print();
317 
318  // Kill the task
319  task.kill();
320 
321  return EXIT_SUCCESS;
322  } catch (const vpException &e) {
323  flog.close(); // Close the log file
324  std::cout << "Test failed with exception: " << e << std::endl;
325  return EXIT_FAILURE;
326  }
327 }
328 
329 #else
330 int main()
331 {
332  std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
333  return EXIT_SUCCESS;
334 }
335 
336 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
void projection(const vpColVector &_cP, vpColVector &_p)
Definition: vpPoint.cpp:216
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1190
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:467
Implementation of an homogeneous matrix and operations on such kind of matrices.
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
error that can be emited by ViSP classes.
Definition: vpException.h:71
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:183
void acquire(vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:438
static void flush(const vpImage< unsigned char > &I)
void set_y(const double y)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Control of Irisa&#39;s gantry robot named Afma6.
Definition: vpRobotAfma6.h:212
Class that defines what is a point.
Definition: vpPoint.h:58
Implementation of a rotation matrix and operations on such kind of matrices.
void init(void)
vpImagePoint getCog() const
Definition: vpDot2.h:161
void set_x(const double x)
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:597
void open(vpImage< unsigned char > &I)
void kill()
Definition: vpServo.cpp:192
Initialize the velocity controller.
Definition: vpRobot.h:67
vpColVector getError() const
Definition: vpServo.h:282
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
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:406
static std::string getUserName()
Definition: vpIoTools.cpp:298
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 vpTranslationVector &t, const vpRotationMatrix &R)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
static double rad(double deg)
Definition: vpMath.h:102
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:113
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:156
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void set_Z(const double Z)
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 changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &_cP)
Definition: vpPoint.cpp:233
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
Class that consider the case of a translation vector.
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:186