ViSP  2.6.2
servoAfma6SquareLines2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6SquareLines2DCamVelocity.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  * Eric Marchand
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/vpImageIo.h>
73 #include <visp/vpDisplay.h>
74 #include <visp/vpDisplayX.h>
75 
76 #include <visp/vpMath.h>
77 #include <visp/vpHomogeneousMatrix.h>
78 #include <visp/vpFeatureLine.h>
79 #include <visp/vpLine.h>
80 #include <visp/vpMeLine.h>
81 #include <visp/vpServo.h>
82 #include <visp/vpFeatureBuilder.h>
83 
84 #include <visp/vpRobotAfma6.h>
85 
86 // Exception
87 #include <visp/vpException.h>
88 #include <visp/vpMatrixException.h>
89 #include <visp/vpServoDisplay.h>
90 
91 #include <visp/vpDot.h>
92 
93 int
94 main()
95 {
96  try
97  {
99 
103  g.open(I) ;
104 
105  g.acquire(I) ;
106 
107 
108  vpDisplayX display(I,100,100,"test SquareLines.cpp ") ;
109  vpTRACE(" ") ;
110 
111  vpDisplay::display(I) ;
112  vpDisplay::flush(I);
113 
114  vpServo task ;
115 
116 
117  std::cout << std::endl ;
118  std::cout << "-------------------------------------------------------" << std::endl ;
119  std::cout << " Test program for vpServo " <<std::endl ;
120  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
121  std::cout << " Simulation " << std::endl ;
122  std::cout << " task : servo a line " << std::endl ;
123  std::cout << "-------------------------------------------------------" << std::endl ;
124  std::cout << std::endl ;
125 
126  int i ;
127  int nbline =4 ;
128 
129  vpMeLine line[nbline] ;
130 
131  vpMe me ;
132  me.setRange(10) ;
133  me.setPointsToTrack(100) ;
134  me.setThreshold(50000) ;
135  me.setSampleStep(10);
136 
137  //Initialize the tracking. Define the four lines to track.
138  for (i=0 ; i < nbline ; i++)
139  {
141  line[i].setMe(&me) ;
142 
143  line[i].initTracking(I) ;
144  line[i].track(I) ;
145  }
146 
147  vpRobotAfma6 robot ;
148  //robot.move("zero.pos") ;
149 
150  vpCameraParameters cam ;
151  // Update camera parameters
152  robot.getCameraParameters (cam, I);
153 
154  vpTRACE("sets the current position of the visual feature ") ;
155  vpFeatureLine p[nbline] ;
156  for (i=0 ; i < nbline ; i++)
157  vpFeatureBuilder::create(p[i],cam, line[i]) ;
158 
159  vpTRACE("sets the desired position of the visual feature ") ;
160  vpLine lined[nbline];
161  lined[0].setWorldCoordinates(1,0,0,0.05,0,0,1,0);
162  lined[1].setWorldCoordinates(0,1,0,0.05,0,0,1,0);
163  lined[2].setWorldCoordinates(1,0,0,-0.05,0,0,1,0);
164  lined[3].setWorldCoordinates(0,1,0,-0.05,0,0,1,0);
165 
166  vpHomogeneousMatrix cMo(0,0,0.5,0,0,vpMath::rad(0));
167 
168  lined[0].project(cMo);
169  lined[1].project(cMo);
170  lined[2].project(cMo);
171  lined[3].project(cMo);
172 
173  //Those lines are needed to keep the conventions define in vpMeLine (Those in vpLine are less restrictive)
174  //Another way to have the coordinates of the desired features is to learn them before executing the program.
175  lined[0].setRho(-fabs(lined[0].getRho()));
176  lined[0].setTheta(0);
177  lined[1].setRho(-fabs(lined[1].getRho()));
178  lined[1].setTheta(M_PI/2);
179  lined[2].setRho(-fabs(lined[2].getRho()));
180  lined[2].setTheta(M_PI);
181  lined[3].setRho(-fabs(lined[3].getRho()));
182  lined[3].setTheta(-M_PI/2);
183 
184  vpFeatureLine pd[nbline] ;
185 
186  vpFeatureBuilder::create(pd[0],lined[0]);
187  vpFeatureBuilder::create(pd[1],lined[1]);
188  vpFeatureBuilder::create(pd[2],lined[2]);
189  vpFeatureBuilder::create(pd[3],lined[3]);
190 
191  vpTRACE("define the task") ;
192  vpTRACE("\t we want an eye-in-hand control law") ;
193  vpTRACE("\t robot is controlled in the camera frame") ;
196 
197  vpTRACE("\t we want to see a point on a point..") ;
198  std::cout << std::endl ;
199  for (i=0 ; i < nbline ; i++)
200  task.addFeature(p[i],pd[i]) ;
201 
202  vpTRACE("\t set the gain") ;
203  task.setLambda(0.2) ;
204 
205 
206  vpTRACE("Display task information " ) ;
207  task.print() ;
208 
210 
211  unsigned int iter=0 ;
212  vpTRACE("\t loop") ;
213  vpColVector v ;
214  vpImage<vpRGBa> Ic ;
215  double lambda_av =0.05;
216  double alpha = 0.05 ;
217  double beta =3 ;
218 
219  for ( ; ; )
220  {
221  std::cout << "---------------------------------------------" << iter <<std::endl ;
222 
223  try {
224  g.acquire(I) ;
225  vpDisplay::display(I) ;
226 
227  //Track the lines and update the features
228  for (i=0 ; i < nbline ; i++)
229  {
230  line[i].track(I) ;
231  line[i].display(I, vpColor::red) ;
232 
233  vpFeatureBuilder::create(p[i],cam,line[i]);
234 
235  p[i].display(cam, I, vpColor::red) ;
236  pd[i].display(cam, I, vpColor::green) ;
237  }
238 
239  double gain ;
240  {
241  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
242  gain = lambda_av ;
243  else
244  {
245  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av ;
246  }
247  }
248 
249  task.setLambda(gain) ;
250 
251  v = task.computeControlLaw() ;
252 
253  vpDisplay::flush(I) ;
254 
255  if (iter==0) vpDisplay::getClick(I) ;
256  if (v.sumSquare() > 0.5)
257  {
258  v =0 ;
260  robot.stopMotion() ;
262  }
263 
265 
266  }
267  catch(...)
268  {
269  v =0 ;
271  robot.stopMotion() ;
272  exit(1) ;
273  }
274 
275  vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
276  iter++;
277  }
278 
279  vpTRACE("Display task information " ) ;
280  task.print() ;
281  task.kill();
282  }
283  catch (...)
284  {
285  vpERROR_TRACE(" Test failed") ;
286  return 0;
287  }
288 }
289 
290 #else
291 int
292 main()
293 {
294  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
295 }
296 
297 #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 setWorldCoordinates(const double &A1, const double &B1, const double &C1, const double &D1, const double &A2, const double &B2, const double &C2, const double &D2)
Definition: vpLine.cpp:98
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
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
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:250
void track(const vpImage< unsigned char > &Im)
Definition: vpMeLine.cpp:784
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
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
void display(const vpImage< unsigned char > &I, vpColor col)
Definition: vpMeLine.cpp:229
void open(vpImage< unsigned char > &I)
Class that defines a line in the object frame, the camera frame and the image plane. All the parameters must be set in meter.
Definition: vpLine.h:124
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
void display(const vpCameraParameters &cam, const vpImage< unsigned char > &I, const vpColor &color=vpColor::green, unsigned int thickness=1) const
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:108
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
Generic class defining intrinsic camera parameters.
Class that defines a 2D line visual feature which is composed by two parameters that are and ...
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:243
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
void setRho(const double rho)
Definition: vpLine.h:144
void setTheta(const double theta)
Definition: vpLine.h:154
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
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
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &velocity)
virtual bool getClick(bool blocking=true)=0
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 setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
void setMe(vpMe *me)
Definition: vpMeTracker.h:140