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