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