Visual Servoing Platform  version 3.0.1
servoViper850Point2DArtVelocity.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 articular
34  *
35  * Authors:
36  * Eric Marchand
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
50 #include <visp3/core/vpConfig.h>
51 #include <visp3/core/vpDebug.h> // Debug trace
52 
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <iostream>
56 #include <fstream>
57 #include <sstream>
58 
59 #if (defined (VISP_HAVE_VIPER850) && defined (VISP_HAVE_DC1394))
60 
61 #include <visp3/sensor/vp1394TwoGrabber.h>
62 #include <visp3/core/vpImage.h>
63 #include <visp3/core/vpDisplay.h>
64 #include <visp3/gui/vpDisplayX.h>
65 #include <visp3/gui/vpDisplayOpenCV.h>
66 #include <visp3/gui/vpDisplayGTK.h>
67 #include <visp3/core/vpMath.h>
68 #include <visp3/core/vpHomogeneousMatrix.h>
69 #include <visp3/visual_features/vpFeaturePoint.h>
70 #include <visp3/core/vpPoint.h>
71 #include <visp3/vs/vpServo.h>
72 #include <visp3/visual_features/vpFeatureBuilder.h>
73 #include <visp3/robot/vpRobotViper850.h>
74 #include <visp3/core/vpIoTools.h>
75 
76 // Exception
77 #include <visp3/core/vpException.h>
78 #include <visp3/vs/vpServoDisplay.h>
79 
80 #include <visp3/blob/vpDot2.h>
81 
82 int
83 main()
84 {
85  // Log file creation in /tmp/$USERNAME/log.dat
86  // This file contains by line:
87  // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
88  // - the 6 mesured joint velocities (m/s, rad/s)
89  // - the 6 mesured joint positions (m, rad)
90  // - the 2 values of s - s*
91  std::string username;
92  // Get the user login name
93  vpIoTools::getUserName(username);
94 
95  // Create a log filename to save velocities...
96  std::string logdirname;
97  logdirname ="/tmp/" + username;
98 
99  // Test if the output path exist. If no try to create it
100  if (vpIoTools::checkDirectory(logdirname) == false) {
101  try {
102  // Create the dirname
103  vpIoTools::makeDirectory(logdirname);
104  }
105  catch (...) {
106  std::cerr << std::endl
107  << "ERROR:" << std::endl;
108  std::cerr << " Cannot create " << logdirname << std::endl;
109  exit(-1);
110  }
111  }
112  std::string logfilename;
113  logfilename = logdirname + "/log.dat";
114 
115  // Open the log file name
116  std::ofstream flog(logfilename.c_str());
117 
118  try {
119  vpRobotViper850 robot ;
120 
121  vpServo task ;
122 
124 
125  bool reset = false;
126  vp1394TwoGrabber g(reset);
128  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
129  g.open(I) ;
130 
131  g.acquire(I) ;
132 
133 #ifdef VISP_HAVE_X11
134  vpDisplayX display(I,800,100,"Current image") ;
135 #elif defined(VISP_HAVE_OPENCV)
136  vpDisplayOpenCV display(I,800,100,"Current image") ;
137 #elif defined(VISP_HAVE_GTK)
138  vpDisplayGTK display(I,800,100,"Current image") ;
139 #endif
140 
141  vpDisplay::display(I) ;
142  vpDisplay::flush(I) ;
143  // exit(1) ;
144 
145  std::cout << std::endl ;
146  std::cout << "-------------------------------------------------------" << std::endl ;
147  std::cout << " Test program for vpServo " <<std::endl ;
148  std::cout << " Eye-in-hand task control, velocity computed in the joint space" << std::endl ;
149  std::cout << " Use of the Afma6 robot " << std::endl ;
150  std::cout << " task : servo a point " << std::endl ;
151  std::cout << "-------------------------------------------------------" << std::endl ;
152  std::cout << std::endl ;
153 
154 
155  vpDot2 dot ;
156 
157  std::cout << "Click on a dot..." << std::endl;
158  dot.initTracking(I) ;
159  vpImagePoint cog = dot.getCog();
161  vpDisplay::flush(I);
162 
163  vpCameraParameters cam ;
164  // Update camera parameters
165  robot.getCameraParameters (cam, I);
166 
167  vpTRACE("sets the current position of the visual feature ") ;
168  vpFeaturePoint p ;
169  vpFeatureBuilder::create(p,cam, dot) ; //retrieve x,y and Z of the vpPoint structure
170 
171  p.set_Z(1) ;
172  vpTRACE("sets the desired position of the visual feature ") ;
173  vpFeaturePoint pd ;
174  pd.buildFrom(0,0,1) ;
175 
176  vpTRACE("define the task") ;
177  vpTRACE("\t we want an eye-in-hand control law") ;
178  vpTRACE("\t articular velocity are computed") ;
181 
182 
183  vpTRACE("Set the position of the camera in the end-effector frame ") ;
184  vpHomogeneousMatrix cMe ;
185  // robot.get_cMe(cMe) ;
186 
188  robot.get_cVe(cVe) ;
189  std::cout << cVe <<std::endl ;
190  task.set_cVe(cVe) ;
191 
192  // vpDisplay::getClick(I) ;
193  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
194  vpMatrix eJe ;
195  robot.get_eJe(eJe) ;
196  task.set_eJe(eJe) ;
197 
198 
199  vpTRACE("\t we want to see a point on a point..") ;
200  std::cout << std::endl ;
201  task.addFeature(p,pd) ;
202 
203  vpTRACE("\t set the gain") ;
204  task.setLambda(0.8) ;
205 
206  vpTRACE("Display task information " ) ;
207  task.print() ;
208 
210 
211  std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
212  for ( ; ; ) {
213  // Acquire a new image from the camera
214  g.acquire(I) ;
215 
216  // Display this image
217  vpDisplay::display(I) ;
218 
219  // Achieve the tracking of the dot in the image
220  dot.track(I) ;
221  cog = dot.getCog();
222 
223  // Display a green cross at the center of gravity position in the image
225 
226 
227  // Update the point feature from the dot location
228  vpFeatureBuilder::create(p, cam, dot);
229 
230  // Get the jacobian of the robot
231  robot.get_eJe(eJe) ;
232  // Update this jacobian in the task structure. It will be used to compute
233  // the velocity skew (as an articular velocity)
234  // qdot = -lambda * L^+ * cVe * eJe * (s-s*)
235  task.set_eJe(eJe) ;
236 
237  // std::cout << (vpMatrix)cVe*eJe << std::endl ;
238 
239  vpColVector v ;
240  // Compute the visual servoing skew vector
241  v = task.computeControlLaw() ;
242 
243  // Display the current and desired feature points in the image display
244  vpServoDisplay::display(task, cam, I) ;
245 
246  // Apply the computed joint velocities to the robot
248 
249  // Save velocities applied to the robot in the log file
250  // v[0], v[1], v[2] correspond to joint translation velocities in m/s
251  // v[3], v[4], v[5] correspond to joint rotation velocities in rad/s
252  flog << v[0] << " " << v[1] << " " << v[2] << " "
253  << v[3] << " " << v[4] << " " << v[5] << " ";
254 
255  // Get the measured joint velocities of the robot
256  vpColVector qvel;
258  // Save measured joint velocities of the robot in the log file:
259  // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
260  // velocities in m/s
261  // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
262  // velocities in rad/s
263  flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " "
264  << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
265 
266  // Get the measured joint positions of the robot
267  vpColVector q;
269  // Save measured joint positions of the robot in the log file
270  // - q[0], q[1], q[2] correspond to measured joint translation
271  // positions in m
272  // - q[3], q[4], q[5] correspond to measured joint rotation
273  // positions in rad
274  flog << q[0] << " " << q[1] << " " << q[2] << " "
275  << q[3] << " " << q[4] << " " << q[5] << " ";
276 
277  // Save feature error (s-s*) for the feature point. For this feature
278  // point, we have 2 errors (along x and y axis). This error is expressed
279  // in meters in the camera frame
280  flog << ( task.getError() ).t() << std::endl; // s-s* for point
281 
282  vpDisplay::flush(I) ;
283 
284  // std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl;
285  }
286 
287  flog.close() ; // Close the log file
288 
289  std::cout << "Display task information: " << std::endl;
290  task.print() ;
291  task.kill();
292  return 0;
293  }
294  catch (...)
295  {
296  flog.close() ; // Close the log file
297  vpERROR_TRACE(" Test failed") ;
298  return 0;
299  }
300 }
301 
302 
303 #else
304 int
305 main()
306 {
307  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
308 }
309 #endif
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpViper850.cpp:564
Implementation of an homogeneous matrix and operations on such kind of matrices.
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: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
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
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:125
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
static void flush(const vpImage< unsigned char > &I)
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
void kill()
Definition: vpServo.cpp:191
Initialize the velocity controller.
Definition: vpRobot.h:68
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
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 setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
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)
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 set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:435
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:262
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:314
vpImagePoint getCog() const
Definition: vpDot2.h:161
vpColVector getError() const
Definition: vpServo.h:271
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:222
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