ViSP  2.9.0
servoAfma62DhalfCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma62DhalfCamVelocity.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 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 #include <visp/vpDisplayOpenCV.h>
77 #include <visp/vpDisplayGTK.h>
78 
79 #include <visp/vpMath.h>
80 #include <visp/vpHomogeneousMatrix.h>
81 #include <visp/vpFeatureLine.h>
82 #include <visp/vpFeaturePoint.h>
83 #include <visp/vpFeatureDepth.h>
84 #include <visp/vpGenericFeature.h>
85 #include <visp/vpLine.h>
86 #include <visp/vpServo.h>
87 #include <visp/vpFeatureBuilder.h>
88 #include <visp/vpPose.h>
89 
90 #include <visp/vpRobotAfma6.h>
91 
92 // Exception
93 #include <visp/vpException.h>
94 #include <visp/vpMatrixException.h>
95 #include <visp/vpServoDisplay.h>
96 
97 #include <visp/vpDot2.h>
98 #include <visp/vpPoint.h>
99 #include <visp/vpHomogeneousMatrix.h>
100 
101 
102 int
103 main()
104 {
105  try
106  {
108 
112  g.open(I) ;
113 
114  g.acquire(I) ;
115 
116 #ifdef VISP_HAVE_X11
117  vpDisplayX display(I,100,100,"Current image") ;
118 #elif defined(VISP_HAVE_OPENCV)
119  vpDisplayOpenCV display(I,100,100,"Current image") ;
120 #elif defined(VISP_HAVE_GTK)
121  vpDisplayGTK display(I,100,100,"Current image") ;
122 #endif
123 
124  vpDisplay::display(I) ;
125  vpDisplay::flush(I);
126 
127  vpServo task ;
128 
129  vpRobotAfma6 robot ;
130  //robot.move("zero.pos") ;
131 
132  vpCameraParameters cam ;
133  // Update camera parameters
134  robot.getCameraParameters (cam, I);
135 
136 
137  std::cout << std::endl ;
138  std::cout << "-------------------------------------------------------" << std::endl ;
139  std::cout << " Test program for vpServo " <<std::endl ;
140  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
141  std::cout << " Simulation " << std::endl ;
142  std::cout << " task : servo a line " << std::endl ;
143  std::cout << "-------------------------------------------------------" << std::endl ;
144  std::cout << std::endl ;
145 
146  int nbline =4 ;
147  int nbpoint =4 ;
148 
149 
150  vpTRACE("sets the desired position of the visual feature ") ;
151  vpPoint pointd[nbpoint]; //position of the fours corners
152  vpPoint pointcd; //position of the center of the square
153  vpFeaturePoint pd;
154 
155  double L=0.05 ;
156  pointd[0].setWorldCoordinates(L,-L, 0 ) ;
157  pointd[1].setWorldCoordinates(L,L, 0 ) ;
158  pointd[2].setWorldCoordinates(-L,L, 0 ) ;
159  pointd[3].setWorldCoordinates(-L,-L, 0 ) ;
160 
161  //The coordinates in the object frame of the point used as a feature ie the center of the square
162  pointcd.setWorldCoordinates(0, 0, 0 ) ;
163 
164  //The desired homogeneous matrix.
165  vpHomogeneousMatrix cMod(0,0,0.4,0,0,vpMath::rad(10));
166 
167  pointd[0].project(cMod);
168  pointd[1].project(cMod);
169  pointd[2].project(cMod);
170  pointd[3].project(cMod);
171 
172  pointcd.project(cMod);
173 
174  vpFeatureBuilder::create(pd,pointcd);
175 
176  vpTRACE("Initialization of the tracking") ;
177  vpMeLine line[nbline] ;
178  vpPoint point[nbpoint];
179  int i ;
180 
181  vpMe me ;
182  me.setRange(10) ;
183  me.setPointsToTrack(100) ;
184  me.setThreshold(50000) ;
185  me.setSampleStep(10);
186 
187  //Initialize the tracking. Define the four lines to track
188  for (i=0 ; i < nbline ; i++)
189  {
190  line[i].setMe(&me) ;
191 
192  line[i].initTracking(I) ;
193  line[i].track(I) ;
194  }
195 
196  // Compute the position of the four corners. The goal is to
197  // compute the pose
198  vpImagePoint ip;
199  for (i=0 ; i < nbline ; i++)
200  {
201  double x=0, y=0;
202 
203  if (!vpMeLine::intersection (line[i%nbline], line[(i+1)%nbline], ip))
204  {
205  exit(-1);
206  }
207 
208  vpPixelMeterConversion::convertPoint(cam, ip, x, y) ;
209 
210  point[i].set_x(x) ;
211  point[i].set_y(y) ;
212  }
213 
214  //Compute the pose cMo
215  vpPose pose ;
216  pose.clearPoint() ;
217  vpHomogeneousMatrix cMo ;
218 
219  point[0].setWorldCoordinates(L,-L, 0 ) ;
220  point[1].setWorldCoordinates(L,L, 0 ) ;
221  point[2].setWorldCoordinates(-L,L, 0 ) ;
222  point[3].setWorldCoordinates(-L,-L, 0 ) ;
223 
224  for (i=0 ; i < nbline ; i++)
225  {
226  pose.addPoint(point[i]) ; // and added to the pose computation point list
227  }
228 
229  pose.computePose(vpPose::LAGRANGE, cMo) ;
230  pose.computePose(vpPose::VIRTUAL_VS, cMo);
231 
232 
233  vpTRACE("sets the current position of the visual feature ") ;
234 
235  //The first features are the position in the camera frame x and y of the square center
236  vpPoint pointc; //The current position of the center of the square
237  double xc = (point[0].get_x()+point[2].get_x())/2;
238  double yc = (point[0].get_y()+point[2].get_y())/2;
239  pointc.set_x(xc);
240  pointc.set_y(yc);
241  vpFeaturePoint p;
242  pointc.project(cMo);
243  vpFeatureBuilder::create(p,pointc);
244 
245  //The second feature is the depth of the current square center relative to the depth of the desired square center.
246  vpFeatureDepth logZ;
247  logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z()/pointcd.get_Z()));
248 
249  //The last three features are the rotations thetau between the current pose and the desired pose.
250  vpHomogeneousMatrix cdMc ;
251  cdMc = cMod*cMo.inverse() ;
253  tu.buildFrom(cdMc) ;
254 
255  vpTRACE("define the task") ;
256  vpTRACE("\t we want an eye-in-hand control law") ;
257  vpTRACE("\t robot is controlled in the camera frame") ;
260 
261  vpTRACE("\t we want to see a point on a point..") ;
262  std::cout << std::endl ;
263  task.addFeature(p,pd) ;
264  task.addFeature(logZ) ;
265  task.addFeature(tu);
266 
267 
268  vpTRACE("\t set the gain") ;
269  task.setLambda(0.2) ;
270 
271 
272  vpTRACE("Display task information " ) ;
273  task.print() ;
274 
276 
277  unsigned int iter=0 ;
278  vpTRACE("\t loop") ;
279  vpColVector v ;
280  vpImage<vpRGBa> Ic ;
281  double lambda_av =0.05;
282  double alpha = 0.05 ;
283  double beta =3 ;
284 
285  for ( ; ; )
286  {
287  std::cout << "---------------------------------------------" << iter <<std::endl ;
288 
289  try {
290  g.acquire(I) ;
291  vpDisplay::display(I) ;
292 
293  pose.clearPoint() ;
294 
295  //Track the lines and find the current position of the corners
296  for (i=0 ; i < nbline ; i++)
297  {
298  line[i].track(I) ;
299 
300  line[i].display(I,vpColor::green);
301 
302  double x=0, y=0;
303 
304  if (!vpMeLine::intersection (line[i%nbline], line[(i+1)%nbline], ip))
305  {
306  exit(-1);
307  }
308 
309  vpPixelMeterConversion::convertPoint(cam, ip, x, y) ;
310 
311  point[i].set_x(x);
312  point[i].set_y(y);
313 
314  pose.addPoint(point[i]) ;
315  }
316 
317  //Compute the pose
318  pose.computePose(vpPose::VIRTUAL_VS, cMo) ;
319 
320  //Update the two first features x and y (position of the square center)
321  xc = (point[0].get_x()+point[2].get_x())/2;
322  yc = (point[0].get_y()+point[2].get_y())/2;
323  pointc.set_x(xc);
324  pointc.set_y(yc);
325  pointc.project(cMo);
326  vpFeatureBuilder::create(p,pointc);
327  //Print the current and the desired position of the center of the square
328  //Print the desired position of the four corners
329  p.display(cam, I, vpColor::green) ;
330  pd.display(cam, I, vpColor::red) ;
331  for (i = 0; i < nbpoint; i++) pointd[i].display(I, cam, vpColor::red);
332 
333  //Update the second feature
334  logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z()/pointcd.get_Z()));
335 
336  //Update the last three features
337  cdMc = cMod*cMo.inverse() ;
338  tu.buildFrom(cdMc) ;
339 
340  //Adaptive gain
341  double gain ;
342  {
343  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
344  gain = lambda_av ;
345  else
346  {
347  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av ;
348  }
349  }
350 
351  task.setLambda(gain) ;
352 
353  v = task.computeControlLaw() ;
354 
355  vpDisplay::flush(I) ;
356  std::cout << v.sumSquare() <<std::endl ;
357  if (iter==0) vpDisplay::getClick(I) ;
358  if (v.sumSquare() > 0.5)
359  {
360  v =0 ;
362  robot.stopMotion() ;
364  }
365 
367 
368  }
369  catch(...)
370  {
371  v =0 ;
373  robot.stopMotion() ;
374  exit(1) ;
375  }
376 
377  vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
378  iter++;
379  }
380 
381  vpTRACE("Display task information " ) ;
382  task.print() ;
383  task.kill();
384  }
385  catch (...)
386  {
387  vpERROR_TRACE(" Test failed") ;
388  return 0;
389  }
390 }
391 
392 #else
393 int
394 main()
395 {
396  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
397 }
398 
399 #endif
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1248
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:395
#define vpTRACE
Definition: vpDebug.h:418
void setSampleStep(const double &s)
Definition: vpMe.h:271
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)
Definition: vpServo.cpp:449
Class that defines a 3D point visual feature which is composed by one parameters that is that defin...
void track(const vpImage< unsigned char > &Im)
Definition: vpMeLine.cpp:816
void set_x(const double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.h:194
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:138
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:809
static const vpColor green
Definition: vpColor.h:170
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:214
static const vpColor red
Definition: vpColor.h:167
Class that defines what is a point.
Definition: vpPoint.h:65
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeLine.cpp:245
void open(vpImage< unsigned char > &I)
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
Class that tracks in an image a line moving edges.
Definition: vpMeLine.h:156
The vpDisplayOpenCV allows to display image using the opencv library.
Class used for pose computation from N points (pose from point only).
Definition: vpPose.h:78
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
void set_y(const double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.h:196
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.h:136
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:259
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)
Definition: vpServo.cpp:522
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:122
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:299
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
static bool intersection(const vpMeLine &line1, const vpMeLine &line2, vpImagePoint &ip)
Definition: vpMeLine.cpp:1097
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:386
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
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:140
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:155
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
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:133