ViSP  3.0.0
servoAfma6Point2DArtVelocity.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 articular
34  *
35  * Authors:
36  * Eric Marchand
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
63 #include <visp3/core/vpConfig.h>
64 #include <visp3/core/vpDebug.h> // Debug trace
65 #include <stdio.h>
66 #include <iostream>
67 #include <fstream>
68 #include <sstream>
69 #include <stdlib.h>
70 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394))
71 
72 #include <visp3/sensor/vp1394TwoGrabber.h>
73 #include <visp3/core/vpImage.h>
74 #include <visp3/core/vpImagePoint.h>
75 #include <visp3/core/vpDisplay.h>
76 #include <visp3/gui/vpDisplayX.h>
77 #include <visp3/gui/vpDisplayOpenCV.h>
78 #include <visp3/gui/vpDisplayGTK.h>
79 
80 #include <visp3/core/vpMath.h>
81 #include <visp3/core/vpHomogeneousMatrix.h>
82 #include <visp3/visual_features/vpFeaturePoint.h>
83 #include <visp3/core/vpPoint.h>
84 #include <visp3/vs/vpServo.h>
85 #include <visp3/visual_features/vpFeatureBuilder.h>
86 #include <visp3/robot/vpRobotAfma6.h>
87 #include <visp3/core/vpIoTools.h>
88 
89 // Exception
90 #include <visp3/core/vpException.h>
91 #include <visp3/vs/vpServoDisplay.h>
92 
93 #include <visp3/blob/vpDot.h>
94 
95 int
96 main()
97 {
98  // Log file creation in /tmp/$USERNAME/log.dat
99  // This file contains by line:
100  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
101  // - the 6 mesured joint velocities (m/s, rad/s)
102  // - the 6 mesured joint positions (m, rad)
103  // - the 2 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  }
118  catch (...) {
119  std::cerr << std::endl
120  << "ERROR:" << std::endl;
121  std::cerr << " Cannot create " << logdirname << std::endl;
122  exit(-1);
123  }
124  }
125  std::string logfilename;
126  logfilename = logdirname + "/log.dat";
127 
128  // Open the log file name
129  std::ofstream flog(logfilename.c_str());
130 
131  try {
132  vpServo task ;
133 
135 
139  g.open(I) ;
140 
141  g.acquire(I) ;
142 
143 #ifdef VISP_HAVE_X11
144  vpDisplayX display(I,100,100,"Current image") ;
145 #elif defined(VISP_HAVE_OPENCV)
146  vpDisplayOpenCV display(I,100,100,"Current image") ;
147 #elif defined(VISP_HAVE_GTK)
148  vpDisplayGTK display(I,100,100,"Current image") ;
149 #endif
150 
151  vpDisplay::display(I) ;
152  vpDisplay::flush(I) ;
153  // exit(1) ;
154 
155  std::cout << std::endl ;
156  std::cout << "-------------------------------------------------------" << std::endl ;
157  std::cout << " Test program for vpServo " <<std::endl ;
158  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
159  std::cout << " Use of the Afma6 robot " << std::endl ;
160  std::cout << " task : servo a point " << std::endl ;
161  std::cout << "-------------------------------------------------------" << std::endl ;
162  std::cout << std::endl ;
163 
164 
165  vpDot dot ;
166  vpImagePoint cog;
167 
168  std::cout << "Click on a dot..." << std::endl;
169  dot.initTracking(I) ;
170  // Get the dot cog
171  cog = dot.getCog();
173  vpDisplay::flush(I);
174 
175  vpRobotAfma6 robot ;
176 
177  vpCameraParameters cam ;
178  // Update camera parameters
179  robot.getCameraParameters (cam, I);
180 
181  vpTRACE("sets the current position of the visual feature ") ;
182  vpFeaturePoint p ;
183  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
184 
185  p.set_Z(1) ;
186  vpTRACE("sets the desired position of the visual feature ") ;
187  vpFeaturePoint pd ;
188  pd.buildFrom(0,0,1) ;
189 
190  vpTRACE("define the task") ;
191  vpTRACE("\t we want an eye-in-hand control law") ;
192  vpTRACE("\t articular velocity are computed") ;
195 
196 
197  vpTRACE("Set the position of the camera in the end-effector frame ") ;
198  vpHomogeneousMatrix cMe ;
199  // robot.get_cMe(cMe) ;
200 
202  robot.get_cVe(cVe) ;
203  std::cout << cVe <<std::endl ;
204  task.set_cVe(cVe) ;
205 
206  // vpDisplay::getClick(I) ;
207  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
208  vpMatrix eJe ;
209  robot.get_eJe(eJe) ;
210  task.set_eJe(eJe) ;
211 
212 
213  vpTRACE("\t we want to see a point on a point..") ;
214  std::cout << std::endl ;
215  task.addFeature(p,pd) ;
216 
217  vpTRACE("\t set the gain") ;
218  task.setLambda(0.8) ;
219 
220  vpTRACE("Display task information " ) ;
221  task.print() ;
222 
224 
225  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
226  for ( ; ; ) {
227  // Acquire a new image from the camera
228  g.acquire(I) ;
229 
230  // Display this image
231  vpDisplay::display(I) ;
232 
233  // Achieve the tracking of the dot in the image
234  dot.track(I) ;
235 
236  // Get the dot cog
237  cog = dot.getCog();
238 
239  // Display a green cross at the center of gravity position in the image
241 
242  // Update the point feature from the dot location
243  vpFeatureBuilder::create(p, cam, dot);
244 
245  // Get the jacobian of the robot
246  robot.get_eJe(eJe) ;
247  // Update this jacobian in the task structure. It will be used to compute
248  // the velocity skew (as an articular velocity)
249  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
250  task.set_eJe(eJe) ;
251 
252  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
253 
254  vpColVector v ;
255  // Compute the visual servoing skew vector
256  v = task.computeControlLaw() ;
257 
258  // Display the current and desired feature points in the image display
259  vpServoDisplay::display(task, cam, I) ;
260 
261  // Apply the computed joint velocities to the robot
263 
264  // Save velocities applied to the robot in the log file
265  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
266  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
267  flog << v[0] << " " << v[1] << " " << v[2] << " "
268  << v[3] << " " << v[4] << " " << v[5] << " ";
269 
270  // Get the measured joint velocities of the robot
271  vpColVector qvel;
273  // Save measured joint velocities of the robot in the log file:
274  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
275  // velocities in m/s
276  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
277  // velocities in rad/s
278  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
279  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
280 
281  // Get the measured joint positions of the robot
282  vpColVector q;
284  // Save measured joint positions of the robot in the log file
285  // - q[0], q[1], q[2] correspond to measured joint translation
286  // positions in m
287  // - q[3], q[4], q[5] correspond to measured joint rotation
288  // positions in rad
289  flog << q[0] << " " << q[1] << " " << q[2] << " "
290  << q[3] << " " << q[4] << " " << q[5] << " ";
291 
292  // Save feature error (s-s*) for the feature point. For this feature
293  // point, we have 2 errors (along x and y axis). This error is expressed
294  // in meters in the camera frame
295  flog << ( task.getError() ).t() << std::endl;
296  vpDisplay::flush(I) ;
297 
298  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
299  }
300 
301  flog.close() ; // Close the log file
302 
303  vpTRACE("Display task information " ) ;
304  task.print() ;
305  task.kill();
306  return 0;
307  }
308  catch (...)
309  {
310  flog.close() ; // Close the log file
311  vpERROR_TRACE(" Test failed") ;
312  return 0;
313  }
314 }
315 
316 
317 #else
318 int
319 main()
320 {
321  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
322 }
323 #endif
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1244
void get_eJe(vpMatrix &_eJe)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
Implementation of an homogeneous matrix and operations on such kind of matrices.
#define vpERROR_TRACE
Definition: vpDebug.h:391
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:459
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
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)
Definition: vpDisplay.cpp:2233
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:404
void open(vpImage< unsigned char > &I)
vpImagePoint getCog() const
Definition: vpDot.h:224
void kill()
Definition: vpServo.cpp:186
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
#define vpTRACE
Definition: vpDebug.h:414
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:390
static std::string getUserName()
Definition: vpIoTools.cpp:161
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
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:519
void buildFrom(const double x, const double y, const double Z)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:434
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:248
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)
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 setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:217
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