Visual Servoing Platform  version 3.0.0
servoAfma4Point2DArtVelocity.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 
64 #include <visp3/core/vpConfig.h>
65 #include <visp3/core/vpDebug.h> // Debug trace
66 #include <stdio.h>
67 #include <iostream>
68 #include <fstream>
69 #include <sstream>
70 #include <stdlib.h>
71 #if (defined (VISP_HAVE_AFMA4) && defined (VISP_HAVE_DC1394))
72 
73 #include <visp3/sensor/vp1394TwoGrabber.h>
74 #include <visp3/core/vpImage.h>
75 #include <visp3/core/vpImagePoint.h>
76 #include <visp3/core/vpDisplay.h>
77 #include <visp3/gui/vpDisplayX.h>
78 #include <visp3/gui/vpDisplayOpenCV.h>
79 #include <visp3/gui/vpDisplayGTK.h>
80 
81 #include <visp3/core/vpMath.h>
82 #include <visp3/core/vpHomogeneousMatrix.h>
83 #include <visp3/visual_features/vpFeaturePoint.h>
84 #include <visp3/core/vpPoint.h>
85 #include <visp3/vs/vpServo.h>
86 #include <visp3/visual_features/vpFeatureBuilder.h>
87 #include <visp3/robot/vpRobotAfma4.h>
88 #include <visp3/core/vpIoTools.h>
89 
90 // Exception
91 #include <visp3/core/vpException.h>
92 #include <visp3/vs/vpServoDisplay.h>
93 
94 #include <visp3/blob/vpDot.h>
95 
96 int
97 main()
98 {
99  try {
100  // Log file creation in /tmp/$USERNAME/log.dat
101  // This file contains by line:
102  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
103  // - the 6 mesured joint velocities (m/s, rad/s)
104  // - the 6 mesured joint positions (m, rad)
105  // - the 2 values of s - s*
106  std::string username;
107  // Get the user login name
108  vpIoTools::getUserName(username);
109 
110  // Create a log filename to save velocities...
111  std::string logdirname;
112  logdirname ="/tmp/" + username;
113 
114  // Test if the output path exist. If no try to create it
115  if (vpIoTools::checkDirectory(logdirname) == false) {
116  try {
117  // Create the dirname
118  vpIoTools::makeDirectory(logdirname);
119  }
120  catch (...) {
121  std::cerr << std::endl
122  << "ERROR:" << std::endl;
123  std::cerr << " Cannot create " << logdirname << std::endl;
124  exit(-1);
125  }
126  }
127  std::string logfilename;
128  logfilename = logdirname + "/log.dat";
129 
130  // Open the log file name
131  std::ofstream flog(logfilename.c_str());
132 
133  // vpRobotAfma4 robot ;
134  vpRobotAfma4 robot ;
135 
136  vpServo task ;
137 
139 
143  g.open(I) ;
144 
145  g.acquire(I) ;
146 
147 #ifdef VISP_HAVE_X11
148  vpDisplayX display(I,100,100,"Current image") ;
149 #elif defined(VISP_HAVE_OPENCV)
150  vpDisplayOpenCV display(I,100,100,"Current image") ;
151 #elif defined(VISP_HAVE_GTK)
152  vpDisplayGTK display(I,100,100,"Current image") ;
153 #endif
154 
155  vpDisplay::display(I) ;
156  vpDisplay::flush(I) ;
157  // exit(1) ;
158 
159  std::cout << std::endl ;
160  std::cout << "-------------------------------------------------------" << std::endl ;
161  std::cout << " Test program for vpServo " <<std::endl ;
162  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
163  std::cout << " Use of the Afma4 robot " << std::endl ;
164  std::cout << " task : servo a point " << std::endl ;
165  std::cout << "-------------------------------------------------------" << std::endl ;
166  std::cout << std::endl ;
167 
168 
169  vpDot dot ;
170 
171  std::cout << "Click on a dot..." << std::endl;
172  dot.initTracking(I) ;
173 
174  vpImagePoint cog = dot.getCog();
175 
177  vpDisplay::flush(I);
178 
179  vpCameraParameters cam ;
180  // Update camera parameters
181  //robot.getCameraParameters (cam, I);
182 
183  vpTRACE("sets the current position of the visual feature ") ;
184  vpFeaturePoint p ;
185  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
186 
187  p.set_Z(1) ;
188  vpTRACE("sets the desired position of the visual feature ") ;
189  vpFeaturePoint pd ;
190  pd.buildFrom(0,0,1) ;
191 
192  vpTRACE("define the task") ;
193  vpTRACE("\t we want an eye-in-hand control law") ;
194  vpTRACE("\t articular velocity are computed") ;
197 
198 
199  vpTRACE("Set the position of the camera in the end-effector frame ") ;
200  vpHomogeneousMatrix cMe ;
201  // robot.get_cMe(cMe) ;
202 
204  robot.get_cVe(cVe) ;
205  std::cout << cVe <<std::endl ;
206  task.set_cVe(cVe) ;
207 
208  // vpDisplay::getClick(I) ;
209  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
210  vpMatrix eJe ;
211  robot.get_eJe(eJe) ;
212  task.set_eJe(eJe) ;
213 
214 
215  vpTRACE("\t we want to see a point on a point..") ;
216  std::cout << std::endl ;
217  task.addFeature(p,pd) ;
218 
219  vpTRACE("\t set the gain") ;
220  task.setLambda(0.8) ;
221 
222  vpTRACE("Display task information " ) ;
223  task.print() ;
224 
225  robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL) ;
226 
227  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
228  for ( ; ; ) {
229  // Acquire a new image from the camera
230  g.acquire(I) ;
231 
232  // Display this image
233  vpDisplay::display(I) ;
234 
235  // Achieve the tracking of the dot in the image
236  dot.track(I) ;
237 
238  // Get the cog of the dot
239  cog = dot.getCog();
240 
241  // Display a green cross at the center of gravity position in the image
243 
244 
245  // Update the point feature from the dot location
246  vpFeatureBuilder::create(p, cam, dot);
247 
248  // Get the jacobian of the robot
249  robot.get_eJe(eJe) ;
250  // Update this jacobian in the task structure. It will be used to compute
251  // the velocity skew (as an articular velocity)
252  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
253  task.set_eJe(eJe) ;
254 
255  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
256 
257  vpColVector v ;
258  // Compute the visual servoing skew vector
259  v = task.computeControlLaw() ;
260 
261  // Display the current and desired feature points in the image display
262  vpServoDisplay::display(task, cam, I) ;
263 
264  // Apply the computed joint velocities to the robot
265  robot.setVelocity(vpRobot::ARTICULAR_FRAME, v) ;
266 
267  // Save velocities applied to the robot in the log file
268  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
269  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
270  flog << v[0] << " " << v[1] << " " << v[2] << " "
271  << v[3] << " " << v[4] << " " << v[5] << " ";
272 
273  // Get the measured joint velocities of the robot
274  vpColVector qvel;
275  robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
276  // Save measured joint velocities of the robot in the log file:
277  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
278  // velocities in m/s
279  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
280  // velocities in rad/s
281  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
282  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
283 
284  // Get the measured joint positions of the robot
285  vpColVector q;
286  robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
287  // Save measured joint positions of the robot in the log file
288  // - q[0], q[1], q[2] correspond to measured joint translation
289  // positions in m
290  // - q[3], q[4], q[5] correspond to measured joint rotation
291  // positions in rad
292  flog << q[0] << " " << q[1] << " " << q[2] << " "
293  << q[3] << " " << q[4] << " " << q[5] << " ";
294 
295  // Save feature error (s-s*) for the feature point. For this feature
296  // point, we have 2 errors (along x and y axis). This error is expressed
297  // in meters in the camera frame
298  flog << task.getError() << std::endl;
299  vpDisplay::flush(I) ;
300 
301  // vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
302  }
303 
304  flog.close() ; // Close the log file
305 
306  vpTRACE("Display task information " ) ;
307  task.print() ;
308  task.kill();
309  return 0;
310  }
311  catch(vpException e) {
312  std::cout << "Catch a ViSP exception: " << e << std::endl;
313  return 1;
314  }
315 }
316 
317 
318 #else
319 int
320 main()
321 {
322  vpERROR_TRACE("You do not have an afma4 robot or a firewire framegrabber connected to your computer...");
323 }
324 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
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
error that can be emited by ViSP classes.
Definition: vpException.h:73
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
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
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)
Control of Irisa's cylindrical robot named Afma4.
Definition: vpRobotAfma4.h:177
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 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