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