ViSP  3.0.0
servoAfma6Ellipse2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * tests the control law
32  * eye-in-hand control
33  * velocity computed in the camera frame
34  *
35  * Authors:
36  * Eric Marchand
37  *
38  *****************************************************************************/
39 
40 
61 #include <visp3/core/vpConfig.h>
62 #include <visp3/core/vpDebug.h> // Debug trace
63 #include <stdlib.h>
64 #include <cmath> // std::fabs
65 #include <limits> // numeric_limits
66 #if (defined (VISP_HAVE_AFMA6) && defined (VISP_HAVE_DC1394))
67 
68 #include <visp3/sensor/vp1394TwoGrabber.h>
69 #include <visp3/core/vpImage.h>
70 #include <visp3/core/vpDisplay.h>
71 #include <visp3/gui/vpDisplayX.h>
72 #include <visp3/gui/vpDisplayOpenCV.h>
73 #include <visp3/gui/vpDisplayGTK.h>
74 
75 #include <visp3/core/vpMath.h>
76 #include <visp3/core/vpHomogeneousMatrix.h>
77 #include <visp3/visual_features/vpFeatureEllipse.h>
78 #include <visp3/vs/vpServo.h>
79 #include <visp3/visual_features/vpFeatureBuilder.h>
80 
81 #include <visp3/robot/vpRobotAfma6.h>
82 
83 // Exception
84 #include <visp3/core/vpException.h>
85 #include <visp3/vs/vpServoDisplay.h>
86 
87 #include <visp3/blob/vpDot.h>
88 
89 
90 
91 int
92 main()
93 {
94  try
95  {
96  vpServo task ;
97 
102  g.open(I) ;
103  g.acquire(I) ;
104 
105 #ifdef VISP_HAVE_X11
106  vpDisplayX display(I,100,100,"Current image") ;
107 #elif defined(VISP_HAVE_OPENCV)
108  vpDisplayOpenCV display(I,100,100,"Current image") ;
109 #elif defined(VISP_HAVE_GTK)
110  vpDisplayGTK display(I,100,100,"Current image") ;
111 #endif
112 
113  vpDisplay::display(I) ;
114  vpDisplay::flush(I) ;
115 
116  std::cout << std::endl ;
117  std::cout << "-------------------------------------------------------" << std::endl ;
118  std::cout << " Test program for vpServo " <<std::endl ;
119  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
120  std::cout << " Simulation " << std::endl ;
121  std::cout << " task : servo a point " << std::endl ;
122  std::cout << "-------------------------------------------------------" << std::endl ;
123  std::cout << std::endl ;
124 
125 
126  vpDot dot ;
127 
128  dot.setMaxDotSize(0.30) ; // Max dot size is 30 % of the image size
129  // dot.setGraphics(true) ;
130  dot.setComputeMoments(true) ;
131  std::cout << "Click on an ellipse..." << std::endl;
132  dot.initTracking(I) ;
133  vpImagePoint cog = dot.getCog();
135  vpDisplay::flush(I);
136 
137  dot.track(I) ;
138 
139  vpCameraParameters cam ;
140 
141  vpRobotAfma6 robot ;
142 
143  // Update camera parameters
144  robot.getCameraParameters (cam, I);
145 
146  vpTRACE("sets the current position of the visual feature ") ;
147  vpFeatureEllipse c ;
148  vpFeatureBuilder::create(c, cam, dot) ;
149 
150 
151  std::cout << " Learning 0/1 " <<std::endl ;
152  int learning ;
153  std::cin >> learning ;
154  char name[FILENAME_MAX] ;
155  sprintf(name,"dat/ellipse.dat") ;
156  if (learning ==1)
157  {
158  // save the object position
159  vpTRACE("Save the location of the object in a file dat/ellipse.dat") ;
160  std::ofstream f(name) ;
161  f << c.get_s().t() ;
162  f.close() ;
163  exit(1) ;
164  }
165 
166 
167  vpTRACE("sets the desired position of the visual feature ") ;
168  vpFeatureEllipse cd ;
169  std::ifstream f("dat/ellipse.dat") ;
170  double x,y,mu20,mu11,mu02 ;
171  f >> x ; f >> y ; f >> mu20 ; f >> mu11 ; f >> mu02 ;
172  f.close() ;
173  cd.buildFrom(x,y,mu20,mu11,mu02) ;
174  cd.setABC(0,0,10) ;
175 
178 
179  task.addFeature(c,cd) ;
180 
181  task.setLambda(0.01) ;
182 
184  unsigned int iter=0 ;
185  double lambda_av =0.01;
186  double alpha = 0.1 ; //1 ;
187  double beta =3 ; //3 ;
188 
189  std::cout << "alpha 0.7" << std::endl;
190  std::cin >> alpha ;
191  std::cout << "beta 5" << std::endl;
192  std::cin >> beta ;
193  for ( ; ; )
194  {
195  std::cout << "---------------------------------------------" << iter++ <<std::endl ;
196 
197  g.acquire(I) ;
198  vpDisplay::display(I) ;
199 
200  dot.track(I) ;
201 
202  // Get the dot cog
203  cog = dot.getCog();
204 
206 
207  vpFeatureBuilder::create(c,cam, dot);
208  // Compute the adaptative gain (speed up the convergence)
209  double gain ;
210  if (iter>2)
211  {
212  if (std::fabs(alpha) <= std::numeric_limits<double>::epsilon())
213  gain = lambda_av ;
214  else
215  {
216  gain = alpha * exp (-beta * ( task.getError() ).sumSquare() ) + lambda_av;
217  }
218  }
219  else gain = lambda_av ;
220 
221 
222  vpTRACE("%f %f", ( task.getError() ).sumSquare(), gain) ;
223  task.setLambda(gain) ;
224  vpColVector v ;
225  v = task.computeControlLaw() ;
226  std::cout <<"rank " << task.getTaskRank() << std::endl ;
227  vpServoDisplay::display(task,cam,I) ;
228  std::cout << v.t() ;
230 
231  vpDisplay::flush(I) ;
232  vpTRACE("\t\t || s - s* || = %f ", ( task.getError() ).sumSquare()) ;
233  }
234 
235  vpTRACE("Display task information " ) ;
236  task.print() ;
237  task.kill();
238  }
239  catch (...)
240  {
241  vpERROR_TRACE(" Test failed") ;
242  return 0;
243  }
244 }
245 
246 
247 #else
248 int
249 main()
250 {
251  vpERROR_TRACE("You do not have an afma6 robot or a firewire framegrabber connected to your computer...");
252 }
253 #endif
void getCameraParameters(vpCameraParameters &cam, const unsigned int &image_width, const unsigned int &image_height) const
Definition: vpAfma6.cpp:1244
void setMaxDotSize(double percentage)
Definition: vpDot.cpp:617
#define vpERROR_TRACE
Definition: vpDebug.h:391
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
void track(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:800
static const vpColor green
Definition: vpColor.h:166
void acquire(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
Control of Irisa&#39;s gantry robot named Afma6.
Definition: vpRobotAfma6.h:210
void open(vpImage< unsigned char > &I)
vpImagePoint getCog() const
Definition: vpDot.h:224
void kill()
Definition: vpServo.cpp:186
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
#define vpTRACE
Definition: vpDebug.h:414
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
vpRowVector t() const
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:390
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
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)
Definition: vpServo.cpp:519
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setFramerate(vp1394TwoFramerateType fps)
void setVideoMode(vp1394TwoVideoModeType videomode)
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:248
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage...
Definition: vpDot.h:115
void setComputeMoments(const bool activate)
Definition: vpDot.h:323
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:88
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
vpColVector get_s(unsigned int select=FEATURE_ALL) const
Get the feature vector .
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:217
void initTracking(const vpImage< unsigned char > &I)
Definition: vpDot.cpp:654
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
unsigned int getTaskRank() const
Definition: vpServo.cpp:1794
static const vpColor blue
Definition: vpColor.h:169