ViSP  2.9.0
servoAfma6Cylinder2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6Cylinder2DCamVelocity.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  *****************************************************************************/
43 
59 #include <visp/vpConfig.h>
60 #include <visp/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_2))
65 
66 #include <visp/vp1394TwoGrabber.h>
67 #include <visp/vpImage.h>
68 #include <visp/vpImageIo.h>
69 #include <visp/vpDisplay.h>
70 #include <visp/vpDisplayX.h>
71 #include <visp/vpDisplayOpenCV.h>
72 #include <visp/vpDisplayGTK.h>
73 
74 #include <visp/vpMath.h>
75 #include <visp/vpHomogeneousMatrix.h>
76 #include <visp/vpFeatureLine.h>
77 #include <visp/vpMeLine.h>
78 #include <visp/vpCylinder.h>
79 #include <visp/vpServo.h>
80 #include <visp/vpFeatureBuilder.h>
81 
82 #include <visp/vpRobotAfma6.h>
83 
84 // Exception
85 #include <visp/vpException.h>
86 #include <visp/vpMatrixException.h>
87 #include <visp/vpServoDisplay.h>
88 
89 int
90 main()
91 {
92  try
93  {
95 
99  g.open(I) ;
100 
101  g.acquire(I) ;
102 
103 #ifdef VISP_HAVE_X11
104  vpDisplayX display(I,100,100,"Current image") ;
105 #elif defined(VISP_HAVE_OPENCV)
106  vpDisplayOpenCV display(I,100,100,"Current image") ;
107 #elif defined(VISP_HAVE_GTK)
108  vpDisplayGTK display(I,100,100,"Current image") ;
109 #endif
110  vpDisplay::display(I) ;
111  vpDisplay::flush(I) ;
112 
113  vpServo task ;
114 
115  std::cout << std::endl ;
116  std::cout << "-------------------------------------------------------" << std::endl ;
117  std::cout << " Test program for vpServo " <<std::endl ;
118  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
119  std::cout << " Simulation " << std::endl ;
120  std::cout << " task : servo a point " << std::endl ;
121  std::cout << "-------------------------------------------------------" << std::endl ;
122  std::cout << std::endl ;
123 
124 
125  int i ;
126  int nbline =2 ;
127  vpMeLine line[nbline] ;
128 
129  vpMe me ;
130  me.setRange(10) ;
131  me.setPointsToTrack(100) ;
132  me.setThreshold(30000) ;
133  me.setSampleStep(10);
134 
135  //Initialize the tracking of the two edges of the cylinder
136  for (i=0 ; i < nbline ; i++)
137  {
139  line[i].setMe(&me) ;
140 
141  line[i].initTracking(I) ;
142  line[i].track(I) ;
143  }
144 
145  vpRobotAfma6 robot ;
146  //robot.move("zero.pos") ;
147 
148  vpCameraParameters cam ;
149  // Update camera parameters
150  robot.getCameraParameters (cam, I);
151 
152  vpTRACE("sets the current position of the visual feature ") ;
153  vpFeatureLine p[nbline] ;
154  for (i=0 ; i < nbline ; i++)
155  vpFeatureBuilder::create(p[i],cam, line[i]) ;
156 
157  vpTRACE("sets the desired position of the visual feature ") ;
158  vpCylinder cyld(0,1,0,0,0,0,0.04);
159 
160  vpHomogeneousMatrix cMo(0,0,0.4,0,0,vpMath::rad(0));
161 
162  cyld.project(cMo);
163 
164  vpFeatureLine pd[nbline] ;
167 
168  //Those lines are needed to keep the conventions define in vpMeLine (Those in vpLine are less restrictive)
169  //Another way to have the coordinates of the desired features is to learn them before executing the program.
170  pd[0].setRhoTheta(-fabs(pd[0].getRho()),0);
171  pd[1].setRhoTheta(-fabs(pd[1].getRho()),M_PI);
172 
173  vpTRACE("define the task") ;
174  vpTRACE("\t we want an eye-in-hand control law") ;
175  vpTRACE("\t robot is controlled in the camera frame") ;
178 
179  vpTRACE("\t we want to see a two lines on two lines..") ;
180  std::cout << std::endl ;
181  for (i=0 ; i < nbline ; i++)
182  task.addFeature(p[i],pd[i]) ;
183 
184  vpTRACE("\t set the gain") ;
185  task.setLambda(0.2) ;
186 
187 
188  vpTRACE("Display task information " ) ;
189  task.print() ;
190 
191 
193 
194  unsigned int iter=0 ;
195  vpTRACE("\t loop") ;
196  vpColVector v ;
197  vpImage<vpRGBa> Ic ;
198  double lambda_av =0.05;
199  double alpha = 0.2 ;
200  double beta =3 ;
201  for ( ; ; )
202  {
203  std::cout << "---------------------------------------------" << iter <<std::endl ;
204 
205  try {
206  g.acquire(I) ;
207  vpDisplay::display(I) ;
208 
209  //Track the two edges and update the features
210  for (i=0 ; i < nbline ; i++)
211  {
212  line[i].track(I) ;
213  line[i].display(I, vpColor::red) ;
214 
215  vpFeatureBuilder::create(p[i],cam,line[i]);
216  vpTRACE("%f %f ",line[i].getRho(), line[i].getTheta()) ;
217 
218  p[i].display(cam, I, vpColor::red) ;
219  pd[i].display(cam, I, vpColor::green) ;
220  }
221 
222  vpDisplay::flush(I) ;
223 
224  //Adaptative gain
225  double gain ;
226  {
227  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
228  gain = lambda_av ;
229  else
230  {
231  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av ;
232  }
233  }
234  task.setLambda(gain) ;
235 
236  v = task.computeControlLaw() ;
237 
238  if (iter==0) vpDisplay::getClick(I) ;
240  }
241  catch(...)
242  {
243  v =0 ;
245  robot.stopMotion() ;
246  exit(1) ;
247  }
248 
249  vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
250  iter++;
251  }
252 
253  vpTRACE("Display task information " ) ;
254  task.print() ;
255  task.kill();
256  }
257  catch (...)
258  {
259  vpERROR_TRACE(" Test failed") ;
260  return 0;
261  }
262 }
263 
264 #else
265 int
266 main()
267 {
268  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
269 }
270 
271 #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
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
void track(const vpImage< unsigned char > &Im)
Definition: vpMeLine.cpp:816
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
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
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
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: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.
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
Class that defines a 2D line visual feature which is composed by two parameters that are and ...
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:259
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
Class that defines what is a cylinder.
Definition: vpCylinder.h:97
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:299
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
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
void setMe(vpMe *p_me)
Definition: vpMeTracker.h:140
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void setRhoTheta(const double rho, const double theta)