ViSP  2.6.2
servoAfma62DhalfCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma62DhalfCamVelocity.cpp 3668 2012-04-04 09:07:10Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 the camera frame
38  *
39  * Authors:
40  * Nicolas Melchior
41  *
42  *****************************************************************************/
63 #include <visp/vpConfig.h>
64 #include <visp/vpDebug.h> // Debug trace
65 #include <stdlib.h>
66 #include <cmath> // std::fabs
67 #include <limits> // numeric_limits
68 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
69 
70 #include <visp/vp1394TwoGrabber.h>
71 #include <visp/vpImage.h>
72 #include <visp/vpImagePoint.h>
73 #include <visp/vpImageIo.h>
74 #include <visp/vpDisplay.h>
75 #include <visp/vpDisplayX.h>
76 
77 #include <visp/vpMath.h>
78 #include <visp/vpHomogeneousMatrix.h>
79 #include <visp/vpFeatureLine.h>
80 #include <visp/vpFeaturePoint.h>
81 #include <visp/vpFeatureDepth.h>
82 #include <visp/vpGenericFeature.h>
83 #include <visp/vpLine.h>
84 #include <visp/vpServo.h>
85 #include <visp/vpFeatureBuilder.h>
86 #include <visp/vpPose.h>
87 
88 #include <visp/vpRobotAfma6.h>
89 
90 // Exception
91 #include <visp/vpException.h>
92 #include <visp/vpMatrixException.h>
93 #include <visp/vpServoDisplay.h>
94 
95 #include <visp/vpDot2.h>
96 #include <visp/vpPoint.h>
97 #include <visp/vpHomogeneousMatrix.h>
98 
99 
100 int
101 main()
102 {
103  try
104  {
106 
110  g.open(I) ;
111 
112  g.acquire(I) ;
113 
114 
115  vpDisplayX display(I,100,100,"testDisplayX.cpp ") ;
116  vpTRACE(" ") ;
117 
118  vpDisplay::display(I) ;
119  vpDisplay::flush(I);
120 
121  vpServo task ;
122 
123  vpRobotAfma6 robot ;
124  //robot.move("zero.pos") ;
125 
126  vpCameraParameters cam ;
127  // Update camera parameters
128  robot.getCameraParameters (cam, I);
129 
130 
131  std::cout << std::endl ;
132  std::cout << "-------------------------------------------------------" << std::endl ;
133  std::cout << " Test program for vpServo " <<std::endl ;
134  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
135  std::cout << " Simulation " << std::endl ;
136  std::cout << " task : servo a line " << std::endl ;
137  std::cout << "-------------------------------------------------------" << std::endl ;
138  std::cout << std::endl ;
139 
140  int nbline =4 ;
141  int nbpoint =4 ;
142 
143 
144  vpTRACE("sets the desired position of the visual feature ") ;
145  vpPoint pointd[nbpoint]; //position of the fours corners
146  vpPoint pointcd; //position of the center of the square
147  vpFeaturePoint pd;
148 
149  double L=0.05 ;
150  pointd[0].setWorldCoordinates(L,-L, 0 ) ;
151  pointd[1].setWorldCoordinates(L,L, 0 ) ;
152  pointd[2].setWorldCoordinates(-L,L, 0 ) ;
153  pointd[3].setWorldCoordinates(-L,-L, 0 ) ;
154 
155  //The coordinates in the object frame of the point used as a feature ie the center of the square
156  pointcd.setWorldCoordinates(0, 0, 0 ) ;
157 
158  //The desired homogeneous matrix.
159  vpHomogeneousMatrix cMod(0,0,0.4,0,0,vpMath::rad(10));
160 
161  pointd[0].project(cMod);
162  pointd[1].project(cMod);
163  pointd[2].project(cMod);
164  pointd[3].project(cMod);
165 
166  pointcd.project(cMod);
167 
168  vpFeatureBuilder::create(pd,pointcd);
169 
170  vpTRACE("Initialization of the tracking") ;
171  vpMeLine line[nbline] ;
172  vpPoint point[nbpoint];
173  int i ;
174 
175  vpMe me ;
176  me.setRange(10) ;
177  me.setPointsToTrack(100) ;
178  me.setThreshold(50000) ;
179  me.setSampleStep(10);
180 
181  //Initialize the tracking. Define the four lines to track
182  for (i=0 ; i < nbline ; i++)
183  {
184  line[i].setMe(&me) ;
185 
186  line[i].initTracking(I) ;
187  line[i].track(I) ;
188  }
189 
190  // Compute the position of the four corners. The goal is to
191  // compute the pose
192  vpImagePoint ip;
193  for (i=0 ; i < nbline ; i++)
194  {
195  double x=0, y=0;
196 
197  if (!vpMeLine::intersection (line[i%nbline], line[(i+1)%nbline], ip))
198  {
199  exit(-1);
200  }
201 
202  vpPixelMeterConversion::convertPoint(cam, ip, x, y) ;
203 
204  point[i].set_x(x) ;
205  point[i].set_y(y) ;
206  }
207 
208  //Compute the pose cMo
209  vpPose pose ;
210  pose.clearPoint() ;
211  vpHomogeneousMatrix cMo ;
212 
213  point[0].setWorldCoordinates(L,-L, 0 ) ;
214  point[1].setWorldCoordinates(L,L, 0 ) ;
215  point[2].setWorldCoordinates(-L,L, 0 ) ;
216  point[3].setWorldCoordinates(-L,-L, 0 ) ;
217 
218  for (i=0 ; i < nbline ; i++)
219  {
220  pose.addPoint(point[i]) ; // and added to the pose computation point list
221  }
222 
223  pose.computePose(vpPose::LAGRANGE, cMo) ;
224  pose.computePose(vpPose::VIRTUAL_VS, cMo);
225 
226 
227  vpTRACE("sets the current position of the visual feature ") ;
228 
229  //The first features are the position in the camera frame x and y of the square center
230  vpPoint pointc; //The current position of the center of the square
231  double xc = (point[0].get_x()+point[2].get_x())/2;
232  double yc = (point[0].get_y()+point[2].get_y())/2;
233  pointc.set_x(xc);
234  pointc.set_y(yc);
235  vpFeaturePoint p;
236  pointc.project(cMo);
237  vpFeatureBuilder::create(p,pointc);
238 
239  //The second feature is the depth of the current square center relative to the depth of the desired square center.
240  vpFeatureDepth logZ;
241  logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z()/pointcd.get_Z()));
242 
243  //The last three features are the rotations thetau between the current pose and the desired pose.
244  vpHomogeneousMatrix cdMc ;
245  cdMc = cMod*cMo.inverse() ;
247  tu.buildFrom(cdMc) ;
248 
249  vpTRACE("define the task") ;
250  vpTRACE("\t we want an eye-in-hand control law") ;
251  vpTRACE("\t robot is controlled in the camera frame") ;
254 
255  vpTRACE("\t we want to see a point on a point..") ;
256  std::cout << std::endl ;
257  task.addFeature(p,pd) ;
258  task.addFeature(logZ) ;
259  task.addFeature(tu);
260 
261 
262  vpTRACE("\t set the gain") ;
263  task.setLambda(0.2) ;
264 
265 
266  vpTRACE("Display task information " ) ;
267  task.print() ;
268 
270 
271  unsigned int iter=0 ;
272  vpTRACE("\t loop") ;
273  vpColVector v ;
274  vpImage<vpRGBa> Ic ;
275  double lambda_av =0.05;
276  double alpha = 0.05 ;
277  double beta =3 ;
278 
279  for ( ; ; )
280  {
281  std::cout << "---------------------------------------------" << iter <<std::endl ;
282 
283  try {
284  g.acquire(I) ;
285  vpDisplay::display(I) ;
286 
287  pose.clearPoint() ;
288 
289  //Track the lines and find the current position of the corners
290  for (i=0 ; i < nbline ; i++)
291  {
292  line[i].track(I) ;
293 
294  line[i].display(I,vpColor::green);
295 
296  double x=0, y=0;
297 
298  if (!vpMeLine::intersection (line[i%nbline], line[(i+1)%nbline], ip))
299  {
300  exit(-1);
301  }
302 
303  vpPixelMeterConversion::convertPoint(cam, ip, x, y) ;
304 
305  point[i].set_x(x);
306  point[i].set_y(y);
307 
308  pose.addPoint(point[i]) ;
309  }
310 
311  //Compute the pose
312  pose.computePose(vpPose::VIRTUAL_VS, cMo) ;
313 
314  //Update the two first features x and y (position of the square center)
315  xc = (point[0].get_x()+point[2].get_x())/2;
316  yc = (point[0].get_y()+point[2].get_y())/2;
317  pointc.set_x(xc);
318  pointc.set_y(yc);
319  pointc.project(cMo);
320  vpFeatureBuilder::create(p,pointc);
321  //Print the current and the desired position of the center of the square
322  //Print the desired position of the four corners
323  p.display(cam, I, vpColor::green) ;
324  pd.display(cam, I, vpColor::red) ;
325  for (i = 0; i < nbpoint; i++) pointd[i].display(I, cam, vpColor::red);
326 
327  //Update the second feature
328  logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z()/pointcd.get_Z()));
329 
330  //Update the last three features
331  cdMc = cMod*cMo.inverse() ;
332  tu.buildFrom(cdMc) ;
333 
334  //Adaptive gain
335  double gain ;
336  {
337  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
338  gain = lambda_av ;
339  else
340  {
341  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av ;
342  }
343  }
344 
345  task.setLambda(gain) ;
346 
347  v = task.computeControlLaw() ;
348 
349  vpDisplay::flush(I) ;
350  std::cout << v.sumSquare() <<std::endl ;
351  if (iter==0) vpDisplay::getClick(I) ;
352  if (v.sumSquare() > 0.5)
353  {
354  v =0 ;
356  robot.stopMotion() ;
358  }
359 
361 
362  }
363  catch(...)
364  {
365  v =0 ;
367  robot.stopMotion() ;
368  exit(1) ;
369  }
370 
371  vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
372  iter++;
373  }
374 
375  vpTRACE("Display task information " ) ;
376  task.print() ;
377  task.kill();
378  }
379  catch (...)
380  {
381  vpERROR_TRACE(" Test failed") ;
382  return 0;
383  }
384 }
385 
386 #else
387 int
388 main()
389 {
390  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
391 }
392 
393 #endif
void setPointsToTrack(const int &n)
Definition: vpMe.h:215
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:379
#define vpTRACE
Definition: vpDebug.h:401
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
Definition: vpAfma6.cpp:1226
void setSampleStep(const double &s)
Definition: vpMe.h:277
void buildFrom(const double x, const double y, const double Z, const double LogZoverZstar)
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)
create a new ste of two visual features
Definition: vpServo.cpp:444
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:250
void track(const vpImage< unsigned char > &Im)
Definition: vpMeLine.cpp:784
void set_x(const double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.h:183
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Point coordinates conversion from pixel coordinates to normalized coordinates in meter...
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:145
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:758
static const vpColor green
Definition: vpColor.h:168
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:214
static const vpColor red
Definition: vpColor.h:165
Class that defines what is a point.
Definition: vpPoint.h:65
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeLine.cpp:229
void open(vpImage< unsigned char > &I)
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
Initialize the velocity controller.
Definition: vpRobot.h:70
vpColVector getError() const
Definition: vpServo.h:298
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
Class that tracks in an image a line moving edges.
Definition: vpMeLine.h:149
Class used for pose computation from N points (pose from point only).
Definition: vpPose.h:80
Generic class defining intrinsic camera parameters.
void set_y(const double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.h:185
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.h:143
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:243
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
static double rad(double deg)
Definition: vpMath.h:100
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.h:129
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
void setThreshold(const double &t)
Definition: vpMe.h:305
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
static bool intersection(const vpMeLine &line1, const vpMeLine &line2, vpImagePoint &ip)
Definition: vpMeLine.cpp:1065
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
void computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo)
compute the pose for a given method
Definition: vpPose.cpp:298
virtual bool getClick(bool blocking=true)=0
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 setRange(const unsigned int &r)
Definition: vpMe.h:229
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:148
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
void setMe(vpMe *me)
Definition: vpMeTracker.h:140
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74
void clearPoint()
suppress all the point in the array of point
Definition: vpPose.cpp:126