ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
servoAfma6TwoLines2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6TwoLines2DCamVelocity.cpp 4056 2013-01-05 13:04:42Z 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-in-hand control
37  * velocity computed in the camera frame
38  *
39  * Authors:
40  * Eric Marchand
41  *
42  *****************************************************************************/
43 
67 #include <visp/vpConfig.h>
68 #include <visp/vpDebug.h> // Debug trace
69 #include <stdlib.h>
70 #include <cmath> // std::fabs
71 #include <limits> // numeric_limits
72 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
73 
74 #include <visp/vp1394TwoGrabber.h>
75 #include <visp/vpImage.h>
76 #include <visp/vpImageIo.h>
77 #include <visp/vpDisplay.h>
78 #include <visp/vpDisplayX.h>
79 #include <visp/vpDisplayOpenCV.h>
80 #include <visp/vpDisplayGTK.h>
81 
82 #include <visp/vpMath.h>
83 #include <visp/vpHomogeneousMatrix.h>
84 #include <visp/vpFeatureLine.h>
85 #include <visp/vpLine.h>
86 #include <visp/vpMeLine.h>
87 #include <visp/vpServo.h>
88 #include <visp/vpFeatureBuilder.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 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  std::cout << std::endl ;
125  std::cout << "-------------------------------------------------------" << std::endl ;
126  std::cout << " Test program for vpServo " <<std::endl ;
127  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
128  std::cout << " Simulation " << std::endl ;
129  std::cout << " task : servo a point " << std::endl ;
130  std::cout << "-------------------------------------------------------" << std::endl ;
131  std::cout << std::endl ;
132 
133  int i ;
134  int nbline =2 ;
135 
136  vpMeLine line[nbline] ;
137 
138  vpMe me ;
139  me.setRange(10) ;
140  me.setPointsToTrack(100) ;
141  me.setThreshold(50000) ;
142  me.setSampleStep(10);
143 
144  //Initialize the tracking. Define the two lines to track
145  vpTRACE("The two lines to track must be parallels ") ;
146  //vpTRACE("The two lines to track must be perpendicular ") ;
147  for (i=0 ; i < nbline ; i++)
148  {
150  line[i].setMe(&me) ;
151 
152  line[i].initTracking(I) ;
153  line[i].track(I) ;
154  }
155 
156  vpRobotAfma6 robot ;
157  //robot.move("zero.pos") ;
158 
159  vpCameraParameters cam ;
160  // Update camera parameters
161  robot.getCameraParameters (cam, I);
162 
163  vpTRACE("sets the current position of the visual feature ") ;
164  vpFeatureLine p[nbline] ;
165  for (i=0 ; i < nbline ; i++)
166  vpFeatureBuilder::create(p[i],cam, line[i]) ;
167 
168  vpTRACE("sets the desired position of the visual feature ") ;
169  vpLine lined[2];
170  lined[0].setWorldCoordinates(1,0,0,-0.05,0,0,1,0);
171  lined[1].setWorldCoordinates(1,0,0,0.05,0,0,1,0);
172 
173  vpHomogeneousMatrix cMo(0,0,0.5,0,0,vpMath::rad(0));
174 
175  lined[0].project(cMo);
176  lined[1].project(cMo);
177 
178  //Those lines are needed to keep the conventions define in vpMeLine (Those in vpLine are less restrictive)
179  //Another way to have the coordinates of the desired features is to learn them before executing the program.
180  lined[0].setRho(-fabs(lined[0].getRho()));
181  lined[0].setTheta(0);
182  lined[1].setRho(-fabs(lined[1].getRho()));
183  lined[1].setTheta(M_PI);
184 
185  vpFeatureLine pd[nbline] ;
186  vpFeatureBuilder::create(pd[0],lined[0]);
187  vpFeatureBuilder::create(pd[1],lined[1]);
188 
189  vpTRACE("define the task") ;
190  vpTRACE("\t we want an eye-in-hand control law") ;
191  vpTRACE("\t robot is controlled in the camera frame") ;
193 
194  vpTRACE("\t we want to see a point on a point..") ;
195  std::cout << std::endl ;
196  for (i=0 ; i < nbline ; i++)
197  task.addFeature(p[i],pd[i]) ;
198 
199  vpTRACE("\t set the gain") ;
200  task.setLambda(0.2) ;
201 
202 
203  vpTRACE("Display task information " ) ;
204  task.print() ;
205 
206 
208 
209  unsigned int iter=0 ;
210  vpTRACE("\t loop") ;
211  vpColVector v ;
212 
213  vpImage<vpRGBa> Ic ;
214  double lambda_av =0.05;
215  double alpha = 0.2;
216  double beta =3;
217 
218  for ( ; ; )
219  {
220  std::cout << "---------------------------------------------" << iter <<std::endl ;
221 
222  try {
223  g.acquire(I) ;
224  vpDisplay::display(I) ;
225 
226  //Track the lines and update the features
227  for (i=0 ; i < nbline ; i++)
228  {
229  line[i].track(I) ;
230  line[i].display(I, vpColor::red) ;
231 
232  vpFeatureBuilder::create(p[i],cam,line[i]);
233 
234  p[i].display(cam, I, vpColor::red) ;
235  pd[i].display(cam, I, vpColor::green) ;
236  }
237 
238  vpDisplay::flush(I) ;
239 
240  //Adaptative gain
241  double gain ;
242  {
243  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
244  gain = lambda_av ;
245  else
246  {
247  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av ;
248  }
249  }
250  task.setLambda(gain) ;
251 
252  v = task.computeControlLaw() ;
253 
254  if (iter==0) vpDisplay::getClick(I) ;
256  }
257  catch(...)
258  {
259  v =0 ;
261  robot.stopMotion() ;
262  exit(1) ;
263  }
264 
265  vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
266  iter++;
267  }
268 
269  vpTRACE("Display task information " ) ;
270  task.print() ;
271  task.kill();
272  }
273  catch (...)
274  {
275  vpERROR_TRACE(" Test failed") ;
276  return 0;
277  }
278 }
279 
280 #else
281 int
282 main()
283 {
284  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
285 }
286 
287 #endif