ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
servoAfma6Points2DCamVelocityEyeToHand.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6Points2DCamVelocityEyeToHand.cpp 4323 2013-07-18 09:24:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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-to-hand control
37  * velocity computed in the camera frame
38  *
39  * Authors:
40  * Eric Marchand
41  *
42  *****************************************************************************/
64 #include <visp/vpConfig.h>
65 #include <visp/vpDebug.h> // Debug trace
66 #include <stdlib.h>
67 #include <cmath> // std::fabs
68 #include <limits> // numeric_limits
69 #include <list>
70 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
71 
72 #define SAVE 0
73 
74 #include <visp/vp1394TwoGrabber.h>
75 #include <visp/vpImage.h>
76 #include <visp/vpImagePoint.h>
77 #include <visp/vpMath.h>
78 #include <visp/vpHomogeneousMatrix.h>
79 #include <visp/vpFeaturePoint.h>
80 #include <visp/vpPoint.h>
81 #include <visp/vpServo.h>
82 #include <visp/vpFeatureBuilder.h>
83 #include <visp/vpRobotAfma6.h>
84 #include <visp/vpException.h>
85 #include <visp/vpMatrixException.h>
86 #include <visp/vpServoDisplay.h>
87 #include <visp/vpDot.h>
88 #include <visp/vpPose.h>
89 #include <visp/vpImageIo.h>
90 #include <visp/vpDisplay.h>
91 #include <visp/vpDisplayX.h>
92 #include <visp/vpDisplayOpenCV.h>
93 #include <visp/vpDisplayGTK.h>
94 
95 #define L 0.006
96 #define D 0
97 
98 int main()
99 {
100  try
101  {
102  vpServo task ;
103 
104  vpCameraParameters cam ;
106  int i ;
107 
111  g.open(I) ;
112 
113  g.acquire(I) ;
114 
115 #ifdef VISP_HAVE_X11
116  vpDisplayX display(I,100,100,"Current image") ;
117 #elif defined(VISP_HAVE_OPENCV)
118  vpDisplayOpenCV display(I,100,100,"Current image") ;
119 #elif defined(VISP_HAVE_GTK)
120  vpDisplayGTK display(I,100,100,"Current image") ;
121 #endif
122 
123  vpDisplay::display(I) ;
124  vpDisplay::flush(I) ;
125 
126  std::cout << std::endl ;
127  std::cout << "-------------------------------------------------------" << std::endl ;
128  std::cout << " Test program for vpServo " <<std::endl ;
129  std::cout << " Eye-to-hand task control" << std::endl ;
130  std::cout << " Simulation " << std::endl ;
131  std::cout << " task : servo a point " << std::endl ;
132  std::cout << "-------------------------------------------------------" << std::endl ;
133  std::cout << std::endl ;
134 
135  int nbPoint =7 ;
136 
137  vpDot dot[nbPoint] ;
138  vpImagePoint cog;
139 
140  for (i=0 ; i < nbPoint ; i++)
141  {
142  dot[i].initTracking(I) ;
143  dot[i].setGraphics(true) ;
144  dot[i].track(I) ;
145  vpDisplay::flush(I) ;
146  dot[i].setGraphics(false) ;
147  }
148 
149  // Compute the pose 3D model
150  vpPoint point[nbPoint] ;
151  point[0].setWorldCoordinates(-2*L,D, -3*L) ;
152  point[1].setWorldCoordinates(0,D, -3*L) ;
153  point[2].setWorldCoordinates(2*L,D, -3*L) ;
154 
155  point[3].setWorldCoordinates(-L,D,-L) ;
156  point[4].setWorldCoordinates(L,D, -L) ;
157  point[5].setWorldCoordinates(L,D, L) ;
158  point[6].setWorldCoordinates(-L,D, L) ;
159 
160  vpRobotAfma6 robot ;
161  // Update camera parameters
162  robot.getCameraParameters (cam, I);
163 
164  vpHomogeneousMatrix cMo, cdMo ;
165  vpPose pose ;
166  pose.clearPoint() ;
167  for (i=0 ; i < nbPoint ; i++)
168  {
169  cog = dot[i].getCog();
170  double x=0, y=0;
171  vpPixelMeterConversion::convertPoint(cam, cog, x, y) ;
172  point[i].set_x(x) ;
173  point[i].set_y(y) ;
174  pose.addPoint(point[i]) ;
175  }
176 
177  // compute the initial pose using Dementhon method followed by a non linear
178  // minimisation method
180 
181 
182  std::cout << cMo << std::endl ;
183  cMo.print() ;
184 
185  /*------------------------------------------------------------------
186  -- Learning the desired position
187  -- or reading the desired position
188  ------------------------------------------------------------------
189  */
190  std::cout << " Learning 0/1 " <<std::endl ;
191  char name[FILENAME_MAX] ;
192  sprintf(name,"cdMo.dat") ;
193  int learning ;
194  std::cin >> learning ;
195  if (learning ==1)
196  {
197  // save the object position
198  vpTRACE("Save the location of the object in a file cdMo.dat") ;
199  std::ofstream f(name) ;
200  cMo.save(f) ;
201  f.close() ;
202  exit(1) ;
203  }
204 
205 
206  {
207  vpTRACE("Loading desired location from cdMo.dat") ;
208  std::ifstream f("cdMo.dat") ;
209  cdMo.load(f) ;
210  f.close() ;
211  }
212 
213  vpFeaturePoint p[nbPoint], pd[nbPoint] ;
214 
215  // set the desired position of the point by forward projection using
216  // the pose cdMo
217  for (i=0 ; i < nbPoint ; i++)
218  {
219  vpColVector cP, p ;
220  point[i].changeFrame(cdMo, cP) ;
221  point[i].projection(cP, p) ;
222 
223  pd[i].set_x(p[0]) ;
224  pd[i].set_y(p[1]) ;
225  }
226 
227 
228 
229  //------------------------------------------------------------------
230 
231  vpTRACE("define the task") ;
232  vpTRACE("\t we want an eye-in-hand control law") ;
233  vpTRACE("\t robot is controlled in the camera frame") ;
236 
237 
238  for (i=0 ; i < nbPoint ; i++)
239  {
240  task.addFeature(p[i],pd[i]) ;
241  }
242 
243 
244  vpTRACE("Display task information " ) ;
245  task.print() ;
246 
247 
248  //------------------------------------------------------------------
249 
250 
251  double convergence_threshold = 0.00; //025 ;
253 
254  //-------------------------------------------------------------
255  double error =1 ;
256  unsigned int iter=0 ;
257  vpTRACE("\t loop") ;
259  vpColVector v ; // computed robot velocity
260 
261 
262  // position of the object in the effector frame
263  vpHomogeneousMatrix oMcamrobot ;
264  oMcamrobot[0][3] = -0.05 ;
265 
266  vpImage<vpRGBa> Ic ;
267  int it = 0 ;
268 
269  double lambda_av =0.1;
270  double alpha = 1 ; //1 ;
271  double beta =3 ; //3 ;
272 
273  std::cout << "alpha 0.7" << std::endl;
274  std::cin >> alpha ;
275  std::cout << "beta 5" << std::endl;
276  std::cin >> beta ;
277  std::list<vpImagePoint> Lcog ;
278  vpImagePoint ip;
279  while(error > convergence_threshold)
280  {
281  std::cout << "---------------------------------------------" << iter++ <<std::endl ;
282 
283  g.acquire(I) ;
284  vpDisplay::display(I) ;
285  ip.set_i( 265 );
286  ip.set_j( 150 );
288  "Eye-To-Hand Visual Servoing",
289  vpColor::green) ;
290  ip.set_i( 280 );
291  ip.set_j( 150 );
293  "IRISA-INRIA Rennes, Lagadic project",
294  vpColor::green) ;
295  try
296  {
297  for (i=0 ; i < nbPoint ; i++)
298  {
299  dot[i].track(I) ;
300  Lcog.push_back( dot[i].getCog() );
301  }
302  }
303  catch(...)
304  {
305  vpTRACE("Error detected while tracking visual features") ;
306  robot.stopMotion() ;
307  exit(1) ;
308  }
309 
310  // compute the initial pose using a non linear minimisation method
311  pose.clearPoint() ;
312 
313  for (i=0 ; i < nbPoint ; i++)
314  {
315  double x=0, y=0;
316  cog = dot[i].getCog();
317  vpPixelMeterConversion::convertPoint(cam, cog, x, y) ;
318  point[i].set_x(x) ;
319  point[i].set_y(y) ;
320 
321  vpColVector cP ;
322  point[i].changeFrame(cdMo, cP) ;
323 
324  p[i].set_x(x) ;
325  p[i].set_y(y) ;
326  p[i].set_Z(cP[2]) ;
327 
328  pose.addPoint(point[i]) ;
329 
330  point[i].display(I,cMo,cam, vpColor::green) ;
331  point[i].display(I,cdMo,cam, vpColor::blue) ;
332  }
333  pose.computePose(vpPose::LOWE, cMo) ;
334  vpDisplay::flush(I) ;
335 
337  vpHomogeneousMatrix cMe, camrobotMe ;
338  robot.get_cMe(camrobotMe) ;
339  cMe = cMo *oMcamrobot * camrobotMe ;
340 
341 
342  task.set_cVe(cMe) ;
343 
344  vpMatrix eJe ;
345  robot.get_eJe(eJe) ;
346  task.set_eJe(eJe) ;
347 
348 
349  // Compute the adaptative gain (speed up the convergence)
350  double gain ;
351  if (iter>2)
352  {
353  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
354  gain = lambda_av ;
355  else
356  {
357  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av;
358  }
359  }
360  else gain = lambda_av ;
361  if (SAVE==1)
362  gain = gain/5 ;
363 
364  vpTRACE("%f %f %f %f %f",alpha, beta, lambda_av, ( task.getError() ).sumSquare(), gain) ;
365  task.setLambda(gain) ;
366 
367 
368  v = task.computeControlLaw() ;
369 
370  // display points trajectory
371  for (std::list<vpImagePoint>::const_iterator it_cog = Lcog.begin(); it_cog != Lcog.end(); ++it_cog)
372  {
374  }
375  vpServoDisplay::display(task,cam,I) ;
377 
378  error = ( task.getError() ).sumSquare() ;
379  std::cout << "|| s - s* || = "<< error<<std::endl ;
380 
381  if (error>7)
382  {
383  vpTRACE("Error detected while tracking visual features") ;
384  robot.stopMotion() ;
385  exit(1) ;
386  }
387 
388  // display the pose
389  // pose.display(I,cMo,cam, 0.04, vpColor::red) ;
390  // display the pose
391  // pose.display(I,cdMo,cam, 0.04, vpColor::blue) ;
392  if ((SAVE==1) && (iter %3==0))
393  {
394 
395  vpDisplay::getImage(I,Ic) ;
396  sprintf(name,"/tmp/marchand/image.%04d.ppm",it++) ;
397  vpImageIo::write(Ic,name) ;
398  }
399  }
400  v = 0 ;
403  task.kill();
404  }
405  catch (...)
406  {
407  vpERROR_TRACE(" Test failed") ;
408  return 0;
409  }
410 }
411 
412 #else
413 int
414 main()
415 {
416  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
417 }
418 
419 #endif