Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoAfma6FourPoints2DArtVelocity.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 articular frame
34  *
35  * Authors:
36  * Eric Marchand
37  * Fabien Spindler
38  *
39  *****************************************************************************/
62 #include <visp3/core/vpConfig.h>
63 #include <visp3/core/vpDebug.h> // Debug trace
64 #include <stdio.h>
65 #include <iostream>
66 #include <fstream>
67 #include <sstream>
68 #include <stdlib.h>
69 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394))
70 
71 #include <visp3/sensor/vp1394TwoGrabber.h>
72 #include <visp3/core/vpImage.h>
73 #include <visp3/core/vpImagePoint.h>
74 #include <visp3/core/vpDisplay.h>
75 #include <visp3/gui/vpDisplayX.h>
76 #include <visp3/gui/vpDisplayOpenCV.h>
77 #include <visp3/gui/vpDisplayGTK.h>
78 
79 #include <visp3/core/vpMath.h>
80 #include <visp3/core/vpHomogeneousMatrix.h>
81 #include <visp3/visual_features/vpFeaturePoint.h>
82 #include <visp3/core/vpPoint.h>
83 #include <visp3/vs/vpServo.h>
84 #include <visp3/visual_features/vpFeatureBuilder.h>
85 #include <visp3/core/vpIoTools.h>
86 #include <visp3/robot/vpRobotAfma6.h>
87 
88 // Exception
89 #include <visp3/core/vpException.h>
90 #include <visp3/vs/vpServoDisplay.h>
91 
92 #include <visp3/blob/vpDot.h>
93 
94 int
95 main()
96 {
97  // Log file creation in /tmp/$USERNAME/log.dat
98  // This file contains by line:
99  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
100  // - the 6 mesured joint velocities (m/s, rad/s)
101  // - the 6 mesured joint positions (m, rad)
102  // - the 8 values of s - s*
103  std::string username;
104  // Get the user login name
105  vpIoTools::getUserName(username);
106 
107  // Create a log filename to save velocities...
108  std::string logdirname;
109  logdirname ="/tmp/" + username;
110 
111  // Test if the output path exist. If no try to create it
112  if (vpIoTools::checkDirectory(logdirname) == false) {
113  try {
114  // Create the dirname
115  vpIoTools::makeDirectory(logdirname);
116  }
117  catch (...) {
118  std::cerr << std::endl
119  << "ERROR:" << std::endl;
120  std::cerr << " Cannot create " << logdirname << std::endl;
121  exit(-1);
122  }
123  }
124  std::string logfilename;
125  logfilename = logdirname + "/log.dat";
126 
127  // Open the log file name
128  std::ofstream flog(logfilename.c_str());
129 
130  try {
131  // Define the square CAD model
132  // Square dimention
133 #define L 0.075
134  // Distance between the camera and the square at the desired
135  // position after visual servoing convergence
136 #define D 0.5
137 
138  vpServo task ;
139 
141  int i ;
142 
146  g.open(I) ;
147 
148  g.acquire(I) ;
149 
150 #ifdef VISP_HAVE_X11
151  vpDisplayX display(I,100,100,"Current image") ;
152 #elif defined(VISP_HAVE_OPENCV)
153  vpDisplayOpenCV display(I,100,100,"Current image") ;
154 #elif defined(VISP_HAVE_GTK)
155  vpDisplayGTK display(I,100,100,"Current image") ;
156 #endif
157 
158  vpDisplay::display(I) ;
159  vpDisplay::flush(I) ;
160 
161  std::cout << std::endl ;
162  std::cout << "-------------------------------------------------------" << std::endl ;
163  std::cout << " Test program for vpServo " <<std::endl ;
164  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
165  std::cout << " Use of the Afma6 robot " << std::endl ;
166  std::cout << " task : servo 4 points on a square with dimention " << L << " meters" << std::endl ;
167  std::cout << "-------------------------------------------------------" << std::endl ;
168  std::cout << std::endl ;
169 
170 
171  vpDot dot[4] ;
172  vpImagePoint cog;
173 
174  std::cout << "Click on the 4 dots clockwise starting from upper/left dot..."
175  << std::endl;
176 
177  for (i=0 ; i < 4 ; i++) {
178  dot[i].initTracking(I) ;
179  cog = dot[i].getCog();
181  vpDisplay::flush(I);
182  }
183 
184  vpRobotAfma6 robot ;
185 
186  vpCameraParameters cam ;
187 
188  // Update camera parameters
189  robot.getCameraParameters (cam, I);
190 
191  // Sets the current position of the visual feature
192  vpFeaturePoint p[4] ;
193  for (i=0 ; i < 4 ; i++)
194  vpFeatureBuilder::create(p[i],cam, dot[i]) ; //retrieve x,y and Z of the vpPoint structure
195 
196  // sets the desired position of the visual feature
197  vpFeaturePoint pd[4] ;
198 
199  pd[0].buildFrom(-L,-L,D) ;
200  pd[1].buildFrom(L,-L,D) ;
201  pd[2].buildFrom(L,L,D) ;
202  pd[3].buildFrom(-L,L,D) ;
203 
204  // We want to see a point on a point
205  std::cout << std::endl ;
206  for (i=0 ; i < 4 ; i++)
207  task.addFeature(p[i],pd[i]) ;
208 
209  // Set the proportional gain
210  task.setLambda(0.2) ;
211 
212  // Display task information
213  task.print() ;
214 
215  // Define the task
216  // - we want an eye-in-hand control law
217  // - articular velocity are computed
220  task.print() ;
221 
223  robot.get_cVe(cVe) ;
224  task.set_cVe(cVe) ;
225  task.print() ;
226 
227  // Set the Jacobian (expressed in the end-effector frame)
228  vpMatrix eJe ;
229  robot.get_eJe(eJe) ;
230  task.set_eJe(eJe) ;
231  task.print() ;
232 
233  // Initialise the velocity control of the robot
235 
236  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
237  for ( ; ; ) {
238  // Acquire a new image from the camera
239  g.acquire(I) ;
240 
241  // Display this image
242  vpDisplay::display(I) ;
243 
244  try {
245  // For each point...
246  for (i=0 ; i < 4 ; i++) {
247  // Achieve the tracking of the dot in the image
248  dot[i].track(I) ;
249  // Get the dot cog
250  cog = dot[i].getCog();
251  // Display a green cross at the center of gravity position in the
252  // image
254  }
255  }
256  catch(...) {
257  flog.close() ; // Close the log file
258  vpTRACE("Error detected while tracking visual features") ;
259  robot.stopMotion() ;
260  exit(1) ;
261  }
262 
263  // Update the point feature from the dot location
264  for (i=0 ; i < 4 ; i++)
265  vpFeatureBuilder::create(p[i],cam, dot[i]);
266 
267  // Get the jacobian of the robot
268  robot.get_eJe(eJe) ;
269  // Update this jacobian in the task structure. It will be used to compute
270  // the velocity skew (as an articular velocity)
271  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
272  task.set_eJe(eJe) ;
273 
274  vpColVector v ;
275  // Compute the visual servoing skew vector
276  v = task.computeControlLaw() ;
277 
278  // Display the current and desired feature points in the image display
279  vpServoDisplay::display(task,cam,I) ;
280 
281  // Apply the computed joint velocities to the robot
283 
284  // Save velocities applied to the robot in the log file
285  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
286  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
287  flog << v[0] << " " << v[1] << " " << v[2] << " "
288  << v[3] << " " << v[4] << " " << v[5] << " ";
289 
290  // Get the measured joint velocities of the robot
291  vpColVector qvel;
293  // Save measured joint velocities of the robot in the log file:
294  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
295  // velocities in m/s
296  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
297  // velocities in rad/s
298  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
299  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
300 
301  // Get the measured joint positions of the robot
302  vpColVector q;
304  // Save measured joint positions of the robot in the log file
305  // - q[0], q[1], q[2] correspond to measured joint translation
306  // positions in m
307  // - q[3], q[4], q[5] correspond to measured joint rotation
308  // positions in rad
309  flog << q[0] << " " << q[1] << " " << q[2] << " "
310  << q[3] << " " << q[4] << " " << q[5] << " ";
311 
312  // Save feature error (s-s*) for the 4 feature points. For each feature
313  // point, we have 2 errors (along x and y axis). This error is expressed
314  // in meters in the camera frame
315  flog << ( task.getError() ).t() << std::endl;
316 
317  // Flush the display
318  vpDisplay::flush(I) ;
319 
320  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
321  }
322 
323  vpTRACE("Display task information " ) ;
324  task.print() ;
325  task.kill();
326  flog.close() ; // Close the log file
327  return 0;
328  }
329  catch (...)
330  {
331  flog.close() ; // Close the log file
332  vpERROR_TRACE(" Test failed") ;
333  return 0;
334  }
335 }
336 
337 #else
338 int
339 main()
340 {
341  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
342 
343 }
344 
345 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1235
void get_eJe(vpMatrix &_eJe)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
#define vpERROR_TRACE
Definition: vpDebug.h:391
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:460
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'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)
vpImagePoint getCog() const
Definition: vpDot.h:225
void kill()
Definition: vpServo.cpp:191
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
#define vpTRACE
Definition: vpDebug.h:414
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)
Implementation of a velocity twist matrix and operations on such kind of matrices.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
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 set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:435
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
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
void get_cVe(vpVelocityTwistMatrix &_cVe) const