Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
servoAfma6FourPoints2DArtVelocity.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 articular frame
35  *
36  * Authors:
37  * Eric Marchand
38  * Fabien Spindler
39  *
40  *****************************************************************************/
60 #include <fstream>
61 #include <iostream>
62 #include <sstream>
63 #include <stdio.h>
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/core/vpHomogeneousMatrix.h>
78 #include <visp3/core/vpIoTools.h>
79 #include <visp3/core/vpMath.h>
80 #include <visp3/core/vpPoint.h>
81 #include <visp3/robot/vpRobotAfma6.h>
82 #include <visp3/visual_features/vpFeatureBuilder.h>
83 #include <visp3/visual_features/vpFeaturePoint.h>
84 #include <visp3/vs/vpServo.h>
85 
86 // Exception
87 #include <visp3/core/vpException.h>
88 #include <visp3/vs/vpServoDisplay.h>
89 
90 #include <visp3/blob/vpDot.h>
91 
92 int main()
93 {
94  // Log file creation in /tmp/$USERNAME/log.dat
95  // This file contains by line:
96  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
97  // - the 6 mesured joint velocities (m/s, rad/s)
98  // - the 6 mesured joint positions (m, rad)
99  // - the 8 values of s - s*
100  std::string username;
101  // Get the user login name
102  vpIoTools::getUserName(username);
103 
104  // Create a log filename to save velocities...
105  std::string logdirname;
106  logdirname = "/tmp/" + username;
107 
108  // Test if the output path exist. If no try to create it
109  if (vpIoTools::checkDirectory(logdirname) == false) {
110  try {
111  // Create the dirname
112  vpIoTools::makeDirectory(logdirname);
113  } catch (...) {
114  std::cerr << std::endl << "ERROR:" << std::endl;
115  std::cerr << " Cannot create " << logdirname << std::endl;
116  exit(-1);
117  }
118  }
119  std::string logfilename;
120  logfilename = logdirname + "/log.dat";
121 
122  // Open the log file name
123  std::ofstream flog(logfilename.c_str());
124 
125  try {
126 // Define the square CAD model
127 // Square dimention
128 #define L 0.075
129 // Distance between the camera and the square at the desired
130 // position after visual servoing convergence
131 #define D 0.5
132 
133  vpServo task;
134 
136  int i;
137 
141  g.open(I);
142 
143  g.acquire(I);
144 
145 #ifdef VISP_HAVE_X11
146  vpDisplayX display(I, 100, 100, "Current image");
147 #elif defined(VISP_HAVE_OPENCV)
148  vpDisplayOpenCV display(I, 100, 100, "Current image");
149 #elif defined(VISP_HAVE_GTK)
150  vpDisplayGTK display(I, 100, 100, "Current image");
151 #endif
152 
154  vpDisplay::flush(I);
155 
156  std::cout << std::endl;
157  std::cout << "-------------------------------------------------------" << std::endl;
158  std::cout << " Test program for vpServo " << std::endl;
159  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
160  std::cout << " Use of the Afma6 robot " << std::endl;
161  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl;
162  std::cout << "-------------------------------------------------------" << std::endl;
163  std::cout << std::endl;
164 
165  vpDot dot[4];
166  vpImagePoint cog;
167 
168  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..." << std::endl;
169 
170  for (i = 0; i < 4; i++) {
171  dot[i].initTracking(I);
172  cog = dot[i].getCog();
174  vpDisplay::flush(I);
175  }
176 
177  vpRobotAfma6 robot;
178 
179  vpCameraParameters cam;
180 
181  // Update camera parameters
182  robot.getCameraParameters(cam, I);
183 
184  // Sets the current position of the visual feature
185  vpFeaturePoint p[4];
186  for (i = 0; i < 4; i++)
187  vpFeatureBuilder::create(p[i], cam, dot[i]); // retrieve x,y and Z of the vpPoint structure
188 
189  // sets the desired position of the visual feature
190  vpFeaturePoint pd[4];
191 
192  pd[0].buildFrom(-L, -L, D);
193  pd[1].buildFrom(L, -L, D);
194  pd[2].buildFrom(L, L, D);
195  pd[3].buildFrom(-L, L, D);
196 
197  // We want to see a point on a point
198  std::cout << std::endl;
199  for (i = 0; i < 4; i++)
200  task.addFeature(p[i], pd[i]);
201 
202  // Set the proportional gain
203  task.setLambda(0.2);
204 
205  // Display task information
206  task.print();
207 
208  // Define the task
209  // - we want an eye-in-hand control law
210  // - articular velocity are computed
213  task.print();
214 
216  robot.get_cVe(cVe);
217  task.set_cVe(cVe);
218  task.print();
219 
220  // Set the Jacobian (expressed in the end-effector frame)
221  vpMatrix eJe;
222  robot.get_eJe(eJe);
223  task.set_eJe(eJe);
224  task.print();
225 
226  // Initialise the velocity control of the robot
228 
229  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
230  for (;;) {
231  // Acquire a new image from the camera
232  g.acquire(I);
233 
234  // Display this image
236 
237  try {
238  // For each point...
239  for (i = 0; i < 4; i++) {
240  // Achieve the tracking of the dot in the image
241  dot[i].track(I);
242  // Get the dot cog
243  cog = dot[i].getCog();
244  // Display a green cross at the center of gravity position in the
245  // image
247  }
248  } catch (...) {
249  flog.close(); // Close the log file
250  vpTRACE("Error detected while tracking visual features");
251  robot.stopMotion();
252  exit(1);
253  }
254 
255  // Update the point feature from the dot location
256  for (i = 0; i < 4; i++)
257  vpFeatureBuilder::create(p[i], cam, dot[i]);
258 
259  // Get the jacobian of the robot
260  robot.get_eJe(eJe);
261  // Update this jacobian in the task structure. It will be used to
262  // compute the velocity skew (as an articular velocity) qdot = -lambda *
263  // L^+ * cVe * eJe * (s-s*)
264  task.set_eJe(eJe);
265 
266  vpColVector v;
267  // Compute the visual servoing skew vector
268  v = task.computeControlLaw();
269 
270  // Display the current and desired feature points in the image display
271  vpServoDisplay::display(task, cam, I);
272 
273  // Apply the computed joint velocities to the robot
275 
276  // Save velocities applied to the robot in the log file
277  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
278  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
279  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
280 
281  // Get the measured joint velocities of the robot
282  vpColVector qvel;
284  // Save measured joint velocities of the robot in the log file:
285  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
286  // velocities in m/s
287  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
288  // velocities in rad/s
289  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
290 
291  // Get the measured joint positions of the robot
292  vpColVector q;
294  // Save measured joint positions of the robot in the log file
295  // - q[0], q[1], q[2] correspond to measured joint translation
296  // positions in m
297  // - q[3], q[4], q[5] correspond to measured joint rotation
298  // positions in rad
299  flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
300 
301  // Save feature error (s-s*) for the 4 feature points. For each feature
302  // point, we have 2 errors (along x and y axis). This error is
303  // expressed in meters in the camera frame
304  flog << (task.getError()).t() << std::endl;
305 
306  // Flush the display
307  vpDisplay::flush(I);
308 
309  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError()
310  //).sumSquare()) ;
311  }
312 
313  vpTRACE("Display task information ");
314  task.print();
315  task.kill();
316  flog.close(); // Close the log file
317  return EXIT_SUCCESS;
318  }
319  catch (const vpException &e) {
320  flog.close(); // Close the log file
321  std::cout << "Test failed with exception: " << e << std::endl;
322  return EXIT_FAILURE;
323  }
324 }
325 
326 #else
327 int main()
328 {
329  std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
330  return EXIT_SUCCESS;
331 }
332 
333 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:164
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:572
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1189
void get_eJe(vpMatrix &_eJe)
void buildFrom(double x, double y, double Z)
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:508
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:770
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:182
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:211
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:422
void open(vpImage< unsigned char > &I)
void get_cVe(vpVelocityTwistMatrix &_cVe) const
void kill()
Definition: vpServo.cpp:192
vpImagePoint getCog() const
Definition: vpDot.h:221
Initialize the velocity controller.
Definition: vpRobot.h:66
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
#define vpTRACE
Definition: vpDebug.h:416
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:318
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
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:130
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:450
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:115
vpColVector getError() const
Definition: vpServo.h:282
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:223
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:635
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:185