Visual Servoing Platform  version 3.4.0
servoAfma6Point2DArtVelocity.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 articular
35  *
36  * Authors:
37  * Eric Marchand
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
61 #include <fstream>
62 #include <iostream>
63 #include <sstream>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <visp3/core/vpConfig.h>
67 #include <visp3/core/vpDebug.h> // Debug trace
68 #if (defined(VISP_HAVE_AFMA6) && defined(VISP_HAVE_DC1394))
69 
70 #include <visp3/core/vpDisplay.h>
71 #include <visp3/core/vpImage.h>
72 #include <visp3/core/vpImagePoint.h>
73 #include <visp3/gui/vpDisplayGTK.h>
74 #include <visp3/gui/vpDisplayOpenCV.h>
75 #include <visp3/gui/vpDisplayX.h>
76 #include <visp3/sensor/vp1394TwoGrabber.h>
77 
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/robot/vpRobotAfma6.h>
83 #include <visp3/visual_features/vpFeatureBuilder.h>
84 #include <visp3/visual_features/vpFeaturePoint.h>
85 #include <visp3/vs/vpServo.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 main()
94 {
95  // Log file creation in /tmp/$USERNAME/log.dat
96  // This file contains by line:
97  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
98  // - the 6 mesured joint velocities (m/s, rad/s)
99  // - the 6 mesured joint positions (m, rad)
100  // - the 2 values of s - s*
101  std::string username;
102  // Get the user login name
103  vpIoTools::getUserName(username);
104 
105  // Create a log filename to save velocities...
106  std::string logdirname;
107  logdirname = "/tmp/" + username;
108 
109  // Test if the output path exist. If no try to create it
110  if (vpIoTools::checkDirectory(logdirname) == false) {
111  try {
112  // Create the dirname
113  vpIoTools::makeDirectory(logdirname);
114  } catch (...) {
115  std::cerr << std::endl << "ERROR:" << std::endl;
116  std::cerr << " Cannot create " << logdirname << std::endl;
117  exit(-1);
118  }
119  }
120  std::string logfilename;
121  logfilename = logdirname + "/log.dat";
122 
123  // Open the log file name
124  std::ofstream flog(logfilename.c_str());
125 
126  try {
127  vpServo task;
128 
130 
134  g.open(I);
135 
136  g.acquire(I);
137 
138 #ifdef VISP_HAVE_X11
139  vpDisplayX display(I, 100, 100, "Current image");
140 #elif defined(VISP_HAVE_OPENCV)
141  vpDisplayOpenCV display(I, 100, 100, "Current image");
142 #elif defined(VISP_HAVE_GTK)
143  vpDisplayGTK display(I, 100, 100, "Current image");
144 #endif
145 
147  vpDisplay::flush(I);
148  // exit(1) ;
149 
150  std::cout << std::endl;
151  std::cout << "-------------------------------------------------------" << std::endl;
152  std::cout << " Test program for vpServo " << std::endl;
153  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl;
154  std::cout << " Use of the Afma6 robot " << std::endl;
155  std::cout << " task : servo a point " << std::endl;
156  std::cout << "-------------------------------------------------------" << std::endl;
157  std::cout << std::endl;
158 
159  vpDot dot;
160  vpImagePoint cog;
161 
162  std::cout << "Click on a dot..." << std::endl;
163  dot.initTracking(I);
164  // Get the dot cog
165  cog = dot.getCog();
167  vpDisplay::flush(I);
168 
169  vpRobotAfma6 robot;
170 
171  vpCameraParameters cam;
172  // Update camera parameters
173  robot.getCameraParameters(cam, I);
174 
175  vpTRACE("sets the current position of the visual feature ");
176  vpFeaturePoint p;
177  vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
178 
179  p.set_Z(1);
180  vpTRACE("sets the desired position of the visual feature ");
181  vpFeaturePoint pd;
182  pd.buildFrom(0, 0, 1);
183 
184  vpTRACE("define the task");
185  vpTRACE("\t we want an eye-in-hand control law");
186  vpTRACE("\t articular velocity are computed");
189 
190  vpTRACE("Set the position of the end-effector frame in the camera frame");
192  // robot.get_cMe(cMe) ;
193 
195  robot.get_cVe(cVe);
196  std::cout << cVe << std::endl;
197  task.set_cVe(cVe);
198 
199  // vpDisplay::getClick(I) ;
200  vpTRACE("Set the Jacobian (expressed in the end-effector frame)");
201  vpMatrix eJe;
202  robot.get_eJe(eJe);
203  task.set_eJe(eJe);
204 
205  vpTRACE("\t we want to see a point on a point..");
206  std::cout << std::endl;
207  task.addFeature(p, pd);
208 
209  vpTRACE("\t set the gain");
210  task.setLambda(0.8);
211 
212  vpTRACE("Display task information ");
213  task.print();
214 
216 
217  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
218  for (;;) {
219  // Acquire a new image from the camera
220  g.acquire(I);
221 
222  // Display this image
224 
225  // Achieve the tracking of the dot in the image
226  dot.track(I);
227 
228  // Get the dot cog
229  cog = dot.getCog();
230 
231  // Display a green cross at the center of gravity position in the image
233 
234  // Update the point feature from the dot location
235  vpFeatureBuilder::create(p, cam, dot);
236 
237  // Get the jacobian of the robot
238  robot.get_eJe(eJe);
239  // Update this jacobian in the task structure. It will be used to
240  // compute the velocity skew (as an articular velocity) qdot = -lambda *
241  // L^+ * cVe * eJe * (s-s*)
242  task.set_eJe(eJe);
243 
244  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
245 
246  vpColVector v;
247  // Compute the visual servoing skew vector
248  v = task.computeControlLaw();
249 
250  // Display the current and desired feature points in the image display
251  vpServoDisplay::display(task, cam, I);
252 
253  // Apply the computed joint velocities to the robot
255 
256  // Save velocities applied to the robot in the log file
257  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
258  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
259  flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
260 
261  // Get the measured joint velocities of the robot
262  vpColVector qvel;
264  // Save measured joint velocities of the robot in the log file:
265  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
266  // velocities in m/s
267  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
268  // velocities in rad/s
269  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
270 
271  // Get the measured joint positions of the robot
272  vpColVector q;
274  // Save measured joint positions of the robot in the log file
275  // - q[0], q[1], q[2] correspond to measured joint translation
276  // positions in m
277  // - q[3], q[4], q[5] correspond to measured joint rotation
278  // positions in rad
279  flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
280 
281  // Save feature error (s-s*) for the feature point. For this feature
282  // point, we have 2 errors (along x and y axis). This error is
283  // expressed in meters in the camera frame
284  flog << (task.getError()).t() << std::endl;
285  vpDisplay::flush(I);
286 
287  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError()
288  // ).sumSquare()) ;
289  }
290 
291  flog.close(); // Close the log file
292 
293  vpTRACE("Display task information ");
294  task.print();
295  return EXIT_SUCCESS;
296  }
297  catch (const vpException &e) {
298  flog.close(); // Close the log file
299  std::cout << "Test failed with exception: " << e << std::endl;
300  return EXIT_FAILURE;
301  }
302 }
303 
304 #else
305 int main()
306 {
307  std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
308  return EXIT_SUCCESS;
309 }
310 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:153
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:482
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1256
void get_eJe(vpMatrix &_eJe)
void buildFrom(double x, double y, double Z)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:506
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:220
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:332
void open(vpImage< unsigned char > &I)
vpImagePoint getCog() const
Definition: vpDot.h:247
Initialize the velocity controller.
Definition: vpRobot.h:66
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
void set_Z(double Z)
#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:404
static std::string getUserName()
Definition: vpIoTools.cpp:228
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
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:448
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
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.
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:87
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
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:223
void get_cVe(vpVelocityTwistMatrix &_cVe) const