ViSP  2.8.0
servoSimuCylinder2DCamVelocityDisplay.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuCylinder2DCamVelocityDisplay.cpp 2457 2010-01-07 10:41:18Z nmelchio $
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  * Simulation of a 2D visual servoing on a cylinder.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
53 #include <visp/vpDebug.h>
54 #include <visp/vpConfig.h>
55 
56 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
57 
58 #include <stdlib.h>
59 #include <stdio.h>
60 
61 #include <visp/vpCameraParameters.h>
62 #include <visp/vpCylinder.h>
63 #include <visp/vpDisplayX.h>
64 #include <visp/vpDisplayGTK.h>
65 #include <visp/vpDisplayGDI.h>
66 #include <visp/vpFeatureBuilder.h>
67 #include <visp/vpFeatureLine.h>
68 #include <visp/vpHomogeneousMatrix.h>
69 #include <visp/vpImage.h>
70 #include <visp/vpMath.h>
71 #include <visp/vpParseArgv.h>
72 #include <visp/vpProjectionDisplay.h>
73 #include <visp/vpServo.h>
74 #include <visp/vpSimulatorCamera.h>
75 #include <visp/vpServoDisplay.h>
76 
77 // List of allowed command line options
78 #define GETOPTARGS "cdh"
79 
88 void usage(const char *name, const char *badparam)
89 {
90  fprintf(stdout, "\n\
91 Simulation of a 2D visual servoing on a cylinder:\n\
92 - eye-in-hand control law,\n\
93 - velocity computed in the camera frame,\n\
94 - display the camera view.\n\
95  \n\
96 SYNOPSIS\n\
97  %s [-c] [-d] [-h]\n", name);
98 
99  fprintf(stdout, "\n\
100 OPTIONS: Default\n\
101  \n\
102  -c\n\
103  Disable the mouse click. Useful to automaze the \n\
104  execution of this program without humain intervention.\n\
105  \n\
106  -d \n\
107  Turn off the display.\n\
108  \n\
109  -h\n\
110  Print the help.\n");
111 
112  if (badparam)
113  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
114 }
115 
127 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
128 {
129  const char *optarg;
130  int c;
131  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
132 
133  switch (c) {
134  case 'c': click_allowed = false; break;
135  case 'd': display = false; break;
136  case 'h': usage(argv[0], NULL); return false; break;
137 
138  default:
139  usage(argv[0], optarg);
140  return false; break;
141  }
142  }
143 
144  if ((c == 1) || (c == -1)) {
145  // standalone param or error
146  usage(argv[0], NULL);
147  std::cerr << "ERROR: " << std::endl;
148  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
149  return false;
150  }
151 
152  return true;
153 }
154 
155 
156 int
157 main(int argc, const char ** argv)
158 {
159  bool opt_display = true;
160  bool opt_click_allowed = true;
161 
162  // Read the command line options
163  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
164  exit (-1);
165  }
166 
167  vpImage<unsigned char> I(512,512,255) ;
168 
169  // We open a window using either X11, GTK or GDI.
170 #if defined VISP_HAVE_X11
171  vpDisplayX display;
172 #elif defined VISP_HAVE_GTK
173  vpDisplayGTK display;
174 #elif defined VISP_HAVE_GDI
175  vpDisplayGDI display;
176 #endif
177 
178  if (opt_display) {
179  try{
180  // Display size is automatically defined by the image (I) size
181  display.init(I, 100, 100,"Camera view...") ;
182  // Display the image
183  // The image class has a member that specify a pointer toward
184  // the display that has been initialized in the display declaration
185  // therefore is is no longuer necessary to make a reference to the
186  // display variable.
187  vpDisplay::display(I) ;
188  vpDisplay::flush(I) ;
189  }
190  catch(...)
191  {
192  vpERROR_TRACE("Error while displaying the image") ;
193  exit(-1);
194  }
195  }
196 
197  double px, py ; px = py = 600 ;
198  double u0, v0 ; u0 = v0 = 256 ;
199 
200  vpCameraParameters cam(px,py,u0,v0);
201 
202  vpServo task ;
203  vpSimulatorCamera robot ;
204 
205  // sets the initial camera location
206  vpHomogeneousMatrix cMo(-0.2,0.1,2,
207  vpMath::rad(5), vpMath::rad(5), vpMath::rad(20));
208 
209  vpHomogeneousMatrix wMc, wMo;
210  robot.getPosition(wMc) ;
211  wMo = wMc * cMo; // Compute the position of the object in the world frame
212 
213  // sets the final camera location (for simulation purpose)
214  vpHomogeneousMatrix cMod(0,0,1,
215  vpMath::rad(-60), vpMath::rad(0), vpMath::rad(0));
216 
217  // sets the cylinder coordinates in the world frame
218  vpCylinder cylinder(0,1,0, // direction
219  0,0,0, // point of the axis
220  0.1) ; // radius
221 
222  // sets the desired position of the visual feature
223  cylinder.track(cMod) ;
224  cylinder.print() ;
225 
226  vpFeatureLine ld[2] ;
227  int i ;
228  for(i=0 ; i < 2 ; i++)
229  vpFeatureBuilder::create(ld[i],cylinder,i) ;
230 
231  // computes the cylinder coordinates in the camera frame and its 2D coordinates
232  // sets the current position of the visual feature
233  cylinder.track(cMo) ;
234  cylinder.print() ;
235 
236  vpFeatureLine l[2] ;
237  for(i=0 ; i < 2 ; i++)
238  {
239  vpFeatureBuilder::create(l[i],cylinder,i) ;
240  l[i].print() ;
241  }
242 
243  // define the task
244  // - we want an eye-in-hand control law
245  // - robot is controlled in the camera frame
247  // task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE) ;
248  // it can also be interesting to test these possibilities
249  // task.setInteractionMatrixType(vpServo::MEAN, vpServo::PSEUDO_INVERSE) ;
251  //task.setInteractionMatrixType(vpServo::DESIRED, vpServo::TRANSPOSE) ;
252  // task.setInteractionMatrixType(vpServo::CURRENT, vpServo::TRANSPOSE) ;
253 
254  // - we want to see 2 lines on 2 lines
255  task.addFeature(l[0],ld[0]) ;
256  task.addFeature(l[1],ld[1]) ;
257 
258  vpServoDisplay::display(task,cam,I) ;
259  vpDisplay::flush(I) ;
260 
261  // Display task information
262  task.print() ;
263 
264  if (opt_display && opt_click_allowed) {
265  std::cout << "\n\nClick in the camera view window to start..." << std::endl;
267  }
268 
269  // - set the gain
270  task.setLambda(1) ;
271 
272  // Display task information
273  task.print() ;
274 
275  unsigned int iter=0 ;
276  // loop
277  do
278  {
279  std::cout << "---------------------------------------------" << iter++ <<std::endl ;
280  vpColVector v ;
281 
282  // get the robot position
283  robot.getPosition(wMc) ;
284  // Compute the position of the camera wrt the object frame
285  cMo = wMc.inverse() * wMo;
286 
287  // new line position
288  // retrieve x,y and Z of the vpLine structure
289  cylinder.track(cMo) ;
290  // cylinder.print() ;
291  for(i=0 ; i < 2 ; i++)
292  {
293  vpFeatureBuilder::create(l[i],cylinder,i) ;
294  // l[i].print() ;
295  }
296 
297  if (opt_display) {
298  vpDisplay::display(I) ;
299  vpServoDisplay::display(task,cam,I) ;
300  vpDisplay::flush(I) ;
301  }
302 
303  // compute the control law
304  v = task.computeControlLaw() ;
305 
306  // send the camera velocity to the controller
308 
309  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ; ;
310 
311  // vpDisplay::getClick(I) ;
312  }
313  while(( task.getError() ).sumSquare() > 1e-9) ;
314 
315  if (opt_display && opt_click_allowed) {
316  std::cout << "\nClick in the camera view window to end..." << std::endl;
318  }
319 
320  // Display task information
321  task.print() ;
322  task.kill();
323 }
324 
325 #else
326 int
327 main()
328 {
329  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
330 }
331 
332 #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 setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void print(const unsigned int select=FEATURE_ALL) const
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
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
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:253
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
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
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
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
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
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
virtual bool getClick(bool blocking=true)=0
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law descbribed in and .
Definition: vpServo.h:153
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214