ViSP  2.9.0
servoViper850Point2DArtVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoViper850Point2DArtVelocity.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * tests the control law
36  * eye-in-hand control
37  * velocity computed in articular
38  *
39  * Authors:
40  * Eric Marchand
41  * Fabien Spindler
42  *
43  *****************************************************************************/
44 
54 #include <visp/vpConfig.h>
55 #include <visp/vpDebug.h> // Debug trace
56 
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <iostream>
60 #include <fstream>
61 #include <sstream>
62 
63 #if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394_2))
64 
65 #include <visp/vp1394TwoGrabber.h>
66 #include <visp/vpImage.h>
67 #include <visp/vpDisplay.h>
68 #include <visp/vpDisplayX.h>
69 #include <visp/vpDisplayOpenCV.h>
70 #include <visp/vpDisplayGTK.h>
71 #include <visp/vpMath.h>
72 #include <visp/vpHomogeneousMatrix.h>
73 #include <visp/vpFeaturePoint.h>
74 #include <visp/vpPoint.h>
75 #include <visp/vpServo.h>
76 #include <visp/vpFeatureBuilder.h>
77 #include <visp/vpRobotViper850.h>
78 #include <visp/vpIoTools.h>
79 
80 // Exception
81 #include <visp/vpException.h>
82 #include <visp/vpMatrixException.h>
83 #include <visp/vpServoDisplay.h>
84 
85 #include <visp/vpDot2.h>
86 
87 int
88 main()
89 {
90  // Log file creation in /tmp/$USERNAME/log.dat
91  // This file contains by line:
92  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
93  // - the 6 mesured joint velocities (m/s, rad/s)
94  // - the 6 mesured joint positions (m, rad)
95  // - the 2 values of s - s*
96  std::string username;
97  // Get the user login name
98  vpIoTools::getUserName(username);
99 
100  // Create a log filename to save velocities...
101  std::string logdirname;
102  logdirname ="/tmp/" + username;
103 
104  // Test if the output path exist. If no try to create it
105  if (vpIoTools::checkDirectory(logdirname) == false) {
106  try {
107  // Create the dirname
108  vpIoTools::makeDirectory(logdirname);
109  }
110  catch (...) {
111  std::cerr << std::endl
112  << "ERROR:" << std::endl;
113  std::cerr << " Cannot create " << logdirname << std::endl;
114  exit(-1);
115  }
116  }
117  std::string logfilename;
118  logfilename = logdirname + "/log.dat";
119 
120  // Open the log file name
121  std::ofstream flog(logfilename.c_str());
122 
123  try {
124  vpRobotViper850 robot ;
125 
126  vpServo task ;
127 
129 
130  bool reset = false;
131  vp1394TwoGrabber g(reset);
133  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
134  g.open(I) ;
135 
136  g.acquire(I) ;
137 
138 #ifdef VISP_HAVE_X11
139  vpDisplayX display(I,800,100,"Current image") ;
140 #elif defined(VISP_HAVE_OPENCV)
141  vpDisplayOpenCV display(I,800,100,"Current image") ;
142 #elif defined(VISP_HAVE_GTK)
143  vpDisplayGTK display(I,800,100,"Current image") ;
144 #endif
145 
146  vpDisplay::display(I) ;
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 
160  vpDot2 dot ;
161 
162  std::cout << "Click on a dot..." << std::endl;
163  dot.initTracking(I) ;
164  vpImagePoint cog = dot.getCog();
166  vpDisplay::flush(I);
167 
168  vpCameraParameters cam ;
169  // Update camera parameters
170  robot.getCameraParameters (cam, I);
171 
172  vpTRACE("sets the current position of the visual feature ") ;
173  vpFeaturePoint p ;
174  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
175 
176  p.set_Z(1) ;
177  vpTRACE("sets the desired position of the visual feature ") ;
178  vpFeaturePoint pd ;
179  pd.buildFrom(0,0,1) ;
180 
181  vpTRACE("define the task") ;
182  vpTRACE("\t we want an eye-in-hand control law") ;
183  vpTRACE("\t articular velocity are computed") ;
186 
187 
188  vpTRACE("Set the position of the camera in the end-effector frame ") ;
189  vpHomogeneousMatrix cMe ;
190  // robot.get_cMe(cMe) ;
191 
193  robot.get_cVe(cVe) ;
194  std::cout << cVe <<std::endl ;
195  task.set_cVe(cVe) ;
196 
197  // vpDisplay::getClick(I) ;
198  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
199  vpMatrix eJe ;
200  robot.get_eJe(eJe) ;
201  task.set_eJe(eJe) ;
202 
203 
204  vpTRACE("\t we want to see a point on a point..") ;
205  std::cout << std::endl ;
206  task.addFeature(p,pd) ;
207 
208  vpTRACE("\t set the gain") ;
209  task.setLambda(0.8) ;
210 
211  vpTRACE("Display task information " ) ;
212  task.print() ;
213 
215 
216  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
217  for ( ; ; ) {
218  // Acquire a new image from the camera
219  g.acquire(I) ;
220 
221  // Display this image
222  vpDisplay::display(I) ;
223 
224  // Achieve the tracking of the dot in the image
225  dot.track(I) ;
226  cog = dot.getCog();
227 
228  // Display a green cross at the center of gravity position in the image
230 
231 
232  // Update the point feature from the dot location
233  vpFeatureBuilder::create(p, cam, dot);
234 
235  // Get the jacobian of the robot
236  robot.get_eJe(eJe) ;
237  // Update this jacobian in the task structure. It will be used to compute
238  // the velocity skew (as an articular velocity)
239  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
240  task.set_eJe(eJe) ;
241 
242  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
243 
244  vpColVector v ;
245  // Compute the visual servoing skew vector
246  v = task.computeControlLaw() ;
247 
248  // Display the current and desired feature points in the image display
249  vpServoDisplay::display(task, cam, I) ;
250 
251  // Apply the computed joint velocities to the robot
253 
254  // Save velocities applied to the robot in the log file
255  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
256  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
257  flog << v[0] << " " << v[1] << " " << v[2] << " "
258  << v[3] << " " << v[4] << " " << v[5] << " ";
259 
260  // Get the measured joint velocities of the robot
261  vpColVector qvel;
263  // Save measured joint velocities of the robot in the log file:
264  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
265  // velocities in m/s
266  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
267  // velocities in rad/s
268  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
269  << 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] << " "
280  << q[3] << " " << q[4] << " " << q[5] << " ";
281 
282  // Save feature error (s-s*) for the feature point. For this feature
283  // point, we have 2 errors (along x and y axis). This error is expressed
284  // in meters in the camera frame
285  flog << ( task.getError() ).t() << std::endl; // s-s* for point
286 
287  vpDisplay::flush(I) ;
288 
289  // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl;
290  }
291 
292  flog.close() ; // Close the log file
293 
294  std::cout << "Display task information: " << std::endl;
295  task.print() ;
296  task.kill();
297  return 0;
298  }
299  catch (...)
300  {
301  flog.close() ; // Close the log file
302  vpERROR_TRACE(" Test failed") ;
303  return 0;
304  }
305 }
306 
307 
308 #else
309 int
310 main()
311 {
312  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
313 }
314 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:395
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:580
Control of Irisa's Viper S850 robot named Viper850.
#define vpTRACE
Definition: vpDebug.h:418
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void get_eJe(vpMatrix &eJe)
static const vpColor green
Definition: vpColor.h:170
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:127
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:465
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
vpImagePoint getCog() const
Definition: vpDot2.h:163
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
void kill()
Definition: vpServo.cpp:189
Initialize the velocity controller.
Definition: vpRobot.h:70
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
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:370
static std::string getUserName()
Definition: vpIoTools.cpp:140
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void buildFrom(const double x, const double y, const double Z)
void get_cVe(vpVelocityTwistMatrix &cVe) const
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &velocity)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:266
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
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:92
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
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:173