ViSP  2.6.2
servoAfma6Ellipse2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoAfma6Ellipse2DCamVelocity.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  *****************************************************************************/
43 
44 
65 #include <visp/vpConfig.h>
66 #include <visp/vpDebug.h> // Debug trace
67 #include <stdlib.h>
68 #include <cmath> // std::fabs
69 #include <limits> // numeric_limits
70 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394_2))
71 
72 #include <visp/vp1394TwoGrabber.h>
73 #include <visp/vpImage.h>
74 #include <visp/vpDisplay.h>
75 #include <visp/vpDisplayX.h>
76 
77 #include <visp/vpMath.h>
78 #include <visp/vpHomogeneousMatrix.h>
79 #include <visp/vpFeatureEllipse.h>
80 #include <visp/vpServo.h>
81 #include <visp/vpFeatureBuilder.h>
82 
83 #include <visp/vpRobotAfma6.h>
84 
85 // Exception
86 #include <visp/vpException.h>
87 #include <visp/vpMatrixException.h>
88 #include <visp/vpServoDisplay.h>
89 
90 #include <visp/vpDot.h>
91 
92 
93 
94 int
95 main()
96 {
97  try
98  {
99  vpServo task ;
100 
105  g.open(I) ;
106  g.acquire(I) ;
107 
108  vpDisplayX display(I,0,0,"testServoEllipse.cpp ") ;
109  vpTRACE(" ") ;
110  vpDisplay::display(I) ;
111  vpDisplay::flush(I) ;
112 
113  std::cout << std::endl ;
114  std::cout << "-------------------------------------------------------" << std::endl ;
115  std::cout << " Test program for vpServo " <<std::endl ;
116  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
117  std::cout << " Simulation " << std::endl ;
118  std::cout << " task : servo a point " << std::endl ;
119  std::cout << "-------------------------------------------------------" << std::endl ;
120  std::cout << std::endl ;
121 
122 
123  vpDot dot ;
124 
125  dot.setMaxDotSize(0.30) ; // Max dot size is 30 % of the image size
126  // dot.setGraphics(true) ;
127  dot.setComputeMoments(true) ;
128  std::cout << "Click on an ellipse..." << std::endl;
129  dot.initTracking(I) ;
130  vpImagePoint cog = dot.getCog();
132  vpDisplay::flush(I);
133 
134  dot.track(I) ;
135 
136  vpCameraParameters cam ;
137 
138  vpRobotAfma6 robot ;
139 
140  // Update camera parameters
141  robot.getCameraParameters (cam, I);
142 
143  vpTRACE("sets the current position of the visual feature ") ;
144  vpFeatureEllipse c ;
145  vpFeatureBuilder::create(c, cam, dot) ;
146 
147 
148  std::cout << " Learning 0/1 " <<std::endl ;
149  int learning ;
150  std::cin >> learning ;
151  char name[FILENAME_MAX] ;
152  sprintf(name,"dat/ellipse.dat") ;
153  if (learning ==1)
154  {
155  // save the object position
156  vpTRACE("Save the location of the object in a file dat/ellipse.dat") ;
157  std::ofstream f(name) ;
158  f << c.get_s().t() ;
159  f.close() ;
160  exit(1) ;
161  }
162 
163 
164  vpTRACE("sets the desired position of the visual feature ") ;
165  vpFeatureEllipse cd ;
166  std::ifstream f("dat/ellipse.dat") ;
167  double x,y,mu20,mu11,mu02 ;
168  f >> x ; f >> y ; f >> mu20 ; f >> mu11 ; f >> mu02 ;
169  f.close() ;
170  cd.buildFrom(x,y,mu20,mu11,mu02) ;
171  cd.setABC(0,0,10) ;
172 
175 
176  task.addFeature(c,cd) ;
177 
178  task.setLambda(0.01) ;
179 
181  unsigned int iter=0 ;
182  double lambda_av =0.01;
183  double alpha = 0.1 ; //1 ;
184  double beta =3 ; //3 ;
185 
186  std::cout << "alpha 0.7" << std::endl;
187  std::cin >> alpha ;
188  std::cout << "beta 5" << std::endl;
189  std::cin >> beta ;
190  for ( ; ; )
191  {
192  std::cout << "---------------------------------------------" << iter++ <<std::endl ;
193 
194  g.acquire(I) ;
195  vpDisplay::display(I) ;
196 
197  dot.track(I) ;
198 
199  // Get the dot cog
200  cog = dot.getCog();
201 
203 
204  vpFeatureBuilder::create(c,cam, dot);
205  // Compute the adaptative gain (speed up the convergence)
206  double gain ;
207  if (iter>2)
208  {
209  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
210  gain = lambda_av ;
211  else
212  {
213  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av;
214  }
215  }
216  else gain = lambda_av ;
217 
218 
219  vpTRACE("%f %f", ( task.getError() ).sumSquare(), gain) ;
220  task.setLambda(gain) ;
221  vpColVector v ;
222  v = task.computeControlLaw() ;
223  std::cout <<"rank " << task.getTaskRank() << std::endl ;
224  vpServoDisplay::display(task,cam,I) ;
225  std::cout << v.t() ;
227 
228  vpDisplay::flush(I) ;
229  vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
230  }
231 
232  vpTRACE("Display task information " ) ;
233  task.print() ;
234  task.kill();
235  }
236  catch (...)
237  {
238  vpERROR_TRACE(" Test failed") ;
239  return 0;
240  }
241 }
242 
243 
244 #else
245 int
246 main()
247 {
248  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
249 }
250 #endif
static void display(vpServo &s, const vpCameraParameters &cam, vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
void setMaxDotSize(double percentage)
Definition: vpDot.cpp:601
#define vpERROR_TRACE
Definition: vpDebug.h:379
#define vpTRACE
Definition: vpDebug.h:401
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height)
Definition: vpAfma6.cpp:1226
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 > &I)
Definition: vpDot.cpp:787
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
void open(vpImage< unsigned char > &I)
vpImagePoint getCog() const
Definition: vpDot.h:249
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
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
vpRowVector t() const
transpose of Vector
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void setABC(const double A, const double B, const double C)
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
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)
double getTaskRank() const
Definition: vpServo.h:386
void setVideoMode(vp1394TwoVideoModeType videomode)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
This tracker is meant to track a dot (connex pixels with same gray level) on a vpImage.
Definition: vpDot.h:80
void setComputeMoments(const bool activate)
Definition: vpDot.h:200
void buildFrom(const double x, const double y, const double mu20, const double mu11, const double mu02)
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:92
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:638
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
static const vpColor blue
Definition: vpColor.h:171