Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoAfma62DhalfCamVelocity.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 the camera frame
34  *
35  * Authors:
36  * Nicolas Melchior
37  *
38  *****************************************************************************/
59 #include <visp3/core/vpConfig.h>
60 #include <visp3/core/vpDebug.h> // Debug trace
61 #include <stdlib.h>
62 #include <cmath> // std::fabs
63 #include <limits> // numeric_limits
64 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394))
65 
66 #include <visp3/sensor/vp1394TwoGrabber.h>
67 #include <visp3/core/vpImage.h>
68 #include <visp3/core/vpImagePoint.h>
69 #include <visp3/io/vpImageIo.h>
70 #include <visp3/core/vpDisplay.h>
71 #include <visp3/gui/vpDisplayX.h>
72 #include <visp3/gui/vpDisplayOpenCV.h>
73 #include <visp3/gui/vpDisplayGTK.h>
74 
75 #include <visp3/core/vpMath.h>
76 #include <visp3/core/vpHomogeneousMatrix.h>
77 #include <visp3/visual_features/vpFeatureLine.h>
78 #include <visp3/visual_features/vpFeaturePoint.h>
79 #include <visp3/visual_features/vpFeatureDepth.h>
80 #include <visp3/visual_features/vpGenericFeature.h>
81 #include <visp3/core/vpLine.h>
82 #include <visp3/vs/vpServo.h>
83 #include <visp3/visual_features/vpFeatureBuilder.h>
84 #include <visp3/vision/vpPose.h>
85 
86 #include <visp3/robot/vpRobotAfma6.h>
87 
88 // Exception
89 #include <visp3/core/vpException.h>
90 #include <visp3/vs/vpServoDisplay.h>
91 
92 #include <visp3/blob/vpDot2.h>
93 #include <visp3/core/vpPoint.h>
94 #include <visp3/core/vpHomogeneousMatrix.h>
95 
96 
97 int
98 main()
99 {
100  try
101  {
103 
107  g.open(I) ;
108 
109  g.acquire(I) ;
110 
111 #ifdef VISP_HAVE_X11
112  vpDisplayX display(I,100,100,"Current image") ;
113 #elif defined(VISP_HAVE_OPENCV)
114  vpDisplayOpenCV display(I,100,100,"Current image") ;
115 #elif defined(VISP_HAVE_GTK)
116  vpDisplayGTK display(I,100,100,"Current image") ;
117 #endif
118 
119  vpDisplay::display(I) ;
120  vpDisplay::flush(I);
121 
122  vpServo task ;
123 
124  vpRobotAfma6 robot ;
125  //robot.move("zero.pos") ;
126 
127  vpCameraParameters cam ;
128  // Update camera parameters
129  robot.getCameraParameters (cam, I);
130 
131 
132  std::cout << std::endl ;
133  std::cout << "-------------------------------------------------------" << std::endl ;
134  std::cout << " Test program for vpServo " <<std::endl ;
135  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
136  std::cout << " Simulation " << std::endl ;
137  std::cout << " task : servo a line " << std::endl ;
138  std::cout << "-------------------------------------------------------" << std::endl ;
139  std::cout << std::endl ;
140 
141  int nbline =4 ;
142  int nbpoint =4 ;
143 
144 
145  vpTRACE("sets the desired position of the visual feature ") ;
146  vpPoint pointd[nbpoint]; //position of the fours corners
147  vpPoint pointcd; //position of the center of the square
148  vpFeaturePoint pd;
149 
150  double L=0.05 ;
151  pointd[0].setWorldCoordinates(L,-L, 0 ) ;
152  pointd[1].setWorldCoordinates(L,L, 0 ) ;
153  pointd[2].setWorldCoordinates(-L,L, 0 ) ;
154  pointd[3].setWorldCoordinates(-L,-L, 0 ) ;
155 
156  //The coordinates in the object frame of the point used as a feature ie the center of the square
157  pointcd.setWorldCoordinates(0, 0, 0 ) ;
158 
159  //The desired homogeneous matrix.
160  vpHomogeneousMatrix cMod(0,0,0.4,0,0,vpMath::rad(10));
161 
162  pointd[0].project(cMod);
163  pointd[1].project(cMod);
164  pointd[2].project(cMod);
165  pointd[3].project(cMod);
166 
167  pointcd.project(cMod);
168 
169  vpFeatureBuilder::create(pd,pointcd);
170 
171  vpTRACE("Initialization of the tracking") ;
172  vpMeLine line[nbline] ;
173  vpPoint point[nbpoint];
174  int i ;
175 
176  vpMe me ;
177  me.setRange(10) ;
178  me.setPointsToTrack(100) ;
179  me.setThreshold(50000) ;
180  me.setSampleStep(10);
181 
182  //Initialize the tracking. Define the four lines to track
183  for (i=0 ; i < nbline ; i++)
184  {
185  line[i].setMe(&me) ;
186 
187  line[i].initTracking(I) ;
188  line[i].track(I) ;
189  }
190 
191  // Compute the position of the four corners. The goal is to
192  // compute the pose
193  vpImagePoint ip;
194  for (i=0 ; i < nbline ; i++)
195  {
196  double x=0, y=0;
197 
198  if (!vpMeLine::intersection (line[i%nbline], line[(i+1)%nbline], ip))
199  {
200  exit(-1);
201  }
202 
203  vpPixelMeterConversion::convertPoint(cam, ip, x, y) ;
204 
205  point[i].set_x(x) ;
206  point[i].set_y(y) ;
207  }
208 
209  //Compute the pose cMo
210  vpPose pose ;
211  pose.clearPoint() ;
212  vpHomogeneousMatrix cMo ;
213 
214  point[0].setWorldCoordinates(L,-L, 0 ) ;
215  point[1].setWorldCoordinates(L,L, 0 ) ;
216  point[2].setWorldCoordinates(-L,L, 0 ) ;
217  point[3].setWorldCoordinates(-L,-L, 0 ) ;
218 
219  for (i=0 ; i < nbline ; i++)
220  {
221  pose.addPoint(point[i]) ; // and added to the pose computation point list
222  }
223 
224  pose.computePose(vpPose::LAGRANGE, cMo) ;
225  pose.computePose(vpPose::VIRTUAL_VS, cMo);
226 
227 
228  vpTRACE("sets the current position of the visual feature ") ;
229 
230  //The first features are the position in the camera frame x and y of the square center
231  vpPoint pointc; //The current position of the center of the square
232  double xc = (point[0].get_x()+point[2].get_x())/2;
233  double yc = (point[0].get_y()+point[2].get_y())/2;
234  pointc.set_x(xc);
235  pointc.set_y(yc);
236  vpFeaturePoint p;
237  pointc.project(cMo);
238  vpFeatureBuilder::create(p,pointc);
239 
240  //The second feature is the depth of the current square center relative to the depth of the desired square center.
241  vpFeatureDepth logZ;
242  logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z()/pointcd.get_Z()));
243 
244  //The last three features are the rotations thetau between the current pose and the desired pose.
245  vpHomogeneousMatrix cdMc ;
246  cdMc = cMod*cMo.inverse() ;
248  tu.buildFrom(cdMc) ;
249 
250  vpTRACE("define the task") ;
251  vpTRACE("\t we want an eye-in-hand control law") ;
252  vpTRACE("\t robot is controlled in the camera frame") ;
255 
256  vpTRACE("\t we want to see a point on a point..") ;
257  std::cout << std::endl ;
258  task.addFeature(p,pd) ;
259  task.addFeature(logZ) ;
260  task.addFeature(tu);
261 
262 
263  vpTRACE("\t set the gain") ;
264  task.setLambda(0.2) ;
265 
266 
267  vpTRACE("Display task information " ) ;
268  task.print() ;
269 
271 
272  unsigned int iter=0 ;
273  vpTRACE("\t loop") ;
274  vpColVector v ;
275  vpImage<vpRGBa> Ic ;
276  double lambda_av =0.05;
277  double alpha = 0.05 ;
278  double beta =3 ;
279 
280  for ( ; ; )
281  {
282  std::cout << "---------------------------------------------" << iter <<std::endl ;
283 
284  try {
285  g.acquire(I) ;
286  vpDisplay::display(I) ;
287 
288  pose.clearPoint() ;
289 
290  //Track the lines and find the current position of the corners
291  for (i=0 ; i < nbline ; i++)
292  {
293  line[i].track(I) ;
294 
295  line[i].display(I,vpColor::green);
296 
297  double x=0, y=0;
298 
299  if (!vpMeLine::intersection (line[i%nbline], line[(i+1)%nbline], ip))
300  {
301  exit(-1);
302  }
303 
304  vpPixelMeterConversion::convertPoint(cam, ip, x, y) ;
305 
306  point[i].set_x(x);
307  point[i].set_y(y);
308 
309  pose.addPoint(point[i]) ;
310  }
311 
312  //Compute the pose
313  pose.computePose(vpPose::VIRTUAL_VS, cMo) ;
314 
315  //Update the two first features x and y (position of the square center)
316  xc = (point[0].get_x()+point[2].get_x())/2;
317  yc = (point[0].get_y()+point[2].get_y())/2;
318  pointc.set_x(xc);
319  pointc.set_y(yc);
320  pointc.project(cMo);
321  vpFeatureBuilder::create(p,pointc);
322  //Print the current and the desired position of the center of the square
323  //Print the desired position of the four corners
324  p.display(cam, I, vpColor::green) ;
325  pd.display(cam, I, vpColor::red) ;
326  for (i = 0; i < nbpoint; i++) pointd[i].display(I, cam, vpColor::red);
327 
328  //Update the second feature
329  logZ.buildFrom(pointc.get_x(), pointc.get_y(), pointc.get_Z(), log(pointc.get_Z()/pointcd.get_Z()));
330 
331  //Update the last three features
332  cdMc = cMod*cMo.inverse() ;
333  tu.buildFrom(cdMc) ;
334 
335  //Adaptive gain
336  double gain ;
337  {
338  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
339  gain = lambda_av ;
340  else
341  {
342  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av ;
343  }
344  }
345 
346  task.setLambda(gain) ;
347 
348  v = task.computeControlLaw() ;
349 
350  vpDisplay::flush(I) ;
351  std::cout << v.sumSquare() <<std::endl ;
352  if (iter==0) vpDisplay::getClick(I) ;
353  if (v.sumSquare() > 0.5)
354  {
355  v =0 ;
357  robot.stopMotion() ;
359  }
360 
362 
363  }
364  catch(...)
365  {
366  v =0 ;
368  robot.stopMotion() ;
369  exit(1) ;
370  }
371 
372  vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
373  iter++;
374  }
375 
376  vpTRACE("Display task information " ) ;
377  task.print() ;
378  task.kill();
379  }
380  catch (...)
381  {
382  vpERROR_TRACE(" Test failed") ;
383  return 0;
384  }
385 }
386 
387 #else
388 int
389 main()
390 {
391  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
392 }
393 
394 #endif
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1235
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setPointsToTrack(const int &n)
Definition: vpMe.h:249
Implementation of an homogeneous matrix and operations on such kind of matrices.
#define vpERROR_TRACE
Definition: vpDebug.h:391
void setSampleStep(const double &s)
Definition: vpMe.h:263
void buildFrom(const double x, const double y, const double Z, const double LogZoverZstar)
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
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:812
void set_x(const double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:496
Definition: vpMe.h:59
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.cpp:458
static const vpColor green
Definition: vpColor.h:166
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:210
static const vpColor red
Definition: vpColor.h:163
Class that defines what is a point.
Definition: vpPoint.h:59
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeLine.cpp:241
void open(vpImage< unsigned char > &I)
void kill()
Definition: vpServo.cpp:191
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
#define vpTRACE
Definition: vpDebug.h:414
static void display(const vpImage< unsigned char > &I)
Class that tracks in an image a line moving edges.
Definition: vpMeLine.h:152
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:76
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:391
void set_y(const double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:498
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
Definition: vpPose.cpp:372
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:456
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:255
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.cpp:442
double sumSquare() const
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of column vector and the associated operations.
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:284
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:314
static bool intersection(const vpMeLine &line1, const vpMeLine &line2, vpImagePoint &ip)
Definition: vpMeLine.cpp:1091
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
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 setRange(const unsigned int &r)
Definition: vpMe.h:256
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:135
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:145
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
void clearPoint()
Definition: vpPose.cpp:130