ViSP  2.6.2
servoSimuLine2DCamVelocityDisplay.cpp
1 
2 /****************************************************************************
3  *
4  * $Id: servoSimuLine2DCamVelocityDisplay.cpp 2457 2010-01-07 10:41:18Z nmelchio $
5  *
6  * This file is part of the ViSP software.
7  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
8  *
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * ("GPL") version 2 as published by the Free Software Foundation.
12  * See the file LICENSE.txt at the root directory of this source
13  * distribution for additional information about the GNU GPL.
14  *
15  * For using ViSP with software that can not be combined with the GNU
16  * GPL, please contact INRIA about acquiring a ViSP Professional
17  * Edition License.
18  *
19  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
20  *
21  * This software was developed at:
22  * INRIA Rennes - Bretagne Atlantique
23  * Campus Universitaire de Beaulieu
24  * 35042 Rennes Cedex
25  * France
26  * http://www.irisa.fr/lagadic
27  *
28  * If you have questions regarding the use of this file, please contact
29  * INRIA at visp@inria.fr
30  *
31  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
32  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
33  *
34  *
35  * Description:
36  * Simulation of a 2D visual servoing on a line.
37  *
38  * Authors:
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
61 #include <visp/vpDebug.h>
62 #include <visp/vpConfig.h>
63 
64 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
65 
66 #include <stdlib.h>
67 #include <stdio.h>
68 
69 #include <visp/vpMath.h>
70 #include <visp/vpHomogeneousMatrix.h>
71 #include <visp/vpFeatureLine.h>
72 #include <visp/vpLine.h>
73 #include <visp/vpServo.h>
74 #include <visp/vpRobotCamera.h>
75 #include <visp/vpFeatureBuilder.h>
76 
77 
78 // Exception
79 #include <visp/vpException.h>
80 #include <visp/vpMatrixException.h>
81 
82 // Debug trace
83 #include <visp/vpDebug.h>
84 
85 
86 #include <visp/vpServoDisplay.h>
87 
88 #include <visp/vpImage.h>
89 #include <visp/vpDisplayX.h>
90 #include <visp/vpDisplayGTK.h>
91 #include <visp/vpDisplayGDI.h>
92 #include <visp/vpCameraParameters.h>
93 #include <visp/vpParseArgv.h>
94 
95 
96 // List of allowed command line options
97 #define GETOPTARGS "cdh"
98 
107 void usage(const char *name, const char *badparam)
108 {
109  fprintf(stdout, "\n\
110 Simulation of 2D a visual servoing on a line:\n\
111 - eye-in-hand control law,\n\
112 - velocity computed in the camera frame,\n\
113 - display the camera view.\n\
114  \n\
115 SYNOPSIS\n\
116  %s [-c] [-d] [-h]\n", name);
117 
118  fprintf(stdout, "\n\
119 OPTIONS: Default\n\
120  \n\
121  -c\n\
122  Disable the mouse click. Useful to automaze the \n\
123  execution of this program without humain intervention.\n\
124  \n\
125  -d \n\
126  Turn off the display.\n\
127  \n\
128  -h\n\
129  Print the help.\n");
130 
131  if (badparam)
132  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
133 }
134 
147 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
148 {
149  const char *optarg;
150  int c;
151  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
152 
153  switch (c) {
154  case 'c': click_allowed = false; break;
155  case 'd': display = false; break;
156  case 'h': usage(argv[0], NULL); return false; break;
157 
158  default:
159  usage(argv[0], optarg);
160  return false; break;
161  }
162  }
163 
164  if ((c == 1) || (c == -1)) {
165  // standalone param or error
166  usage(argv[0], NULL);
167  std::cerr << "ERROR: " << std::endl;
168  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
169  return false;
170  }
171 
172  return true;
173 }
174 
175 
176 int
177 main(int argc, const char ** argv)
178 {
179  bool opt_display = true;
180  bool opt_click_allowed = true;
181 
182  // Read the command line options
183  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
184  exit (-1);
185  }
186 
187  vpImage<unsigned char> I(512,512,0) ;
188 
189  // We open a window using either X11, GTK or GDI.
190 #if defined VISP_HAVE_X11
191  vpDisplayX display;
192 #elif defined VISP_HAVE_GTK
193  vpDisplayGTK display;
194 #elif defined VISP_HAVE_GDI
195  vpDisplayGDI display;
196 #endif
197 
198  if (opt_display) {
199  try{
200  // Display size is automatically defined by the image (I) size
201  display.init(I, 100, 100,"Camera view...") ;
202  // Display the image
203  // The image class has a member that specify a pointer toward
204  // the display that has been initialized in the display declaration
205  // therefore is is no longuer necessary to make a reference to the
206  // display variable.
207  vpDisplay::display(I) ;
208  vpDisplay::flush(I) ;
209  }
210  catch(...)
211  {
212  vpERROR_TRACE("Error while displaying the image") ;
213  exit(-1);
214  }
215  }
216 
217  double px, py ; px = py = 600 ;
218  double u0, v0 ; u0 = v0 = 256 ;
219 
220  vpCameraParameters cam(px,py,u0,v0);
221 
222  vpServo task ;
223  vpRobotCamera robot ;
224 
225  vpTRACE("sets the initial camera location " ) ;
226  vpHomogeneousMatrix cMo(-0.2,0.1,1,
227  vpMath::rad(5), vpMath::rad(5), vpMath::rad(90));
228 
229  robot.setPosition(cMo) ;
230 
231  vpTRACE("sets the final camera location (for simulation purpose)" ) ;
232  vpHomogeneousMatrix cMod(0,0,1,
233  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
234 
235 
236 
237  vpTRACE("sets the line coordinates (2 planes) in the world frame " ) ;
238 
239  vpColVector plane1(4) ;
240  vpColVector plane2(4) ;
241  plane1[0] = 0; // z = 0
242  plane1[1] = 0;
243  plane1[2] = 1;
244  plane1[3] = 0;
245  plane2[0] = 0; // y =0
246  plane2[1] = 1;
247  plane2[2] = 0;
248  plane2[3] = 0;
249 
250 
251  vpLine line ;
252  line.setWorldCoordinates(plane1, plane2) ;
253 
254  vpTRACE("sets the desired position of the visual feature ") ;
255  line.track(cMod) ;
256  line.print() ;
257 
258  vpFeatureLine ld ;
259  vpFeatureBuilder::create(ld,line) ;
260 
261 
262  vpTRACE("project : computes the line coordinates in the camera frame and its 2D coordinates" ) ;
263  vpTRACE("sets the current position of the visual feature ") ;
264  line.track(cMo) ;
265  line.print() ;
266 
267  vpFeatureLine l ;
268  vpFeatureBuilder::create(l,line) ;
269  l.print() ;
270 
271  vpTRACE("define the task") ;
272  vpTRACE("\t we want an eye-in-hand control law") ;
273  vpTRACE("\t robot is controlled in the camera frame") ;
275 
276  vpTRACE("\t we want to see a line on a line..\n") ;
277 
278  task.addFeature(l,ld) ;
279  vpDisplay::display(I) ;
280  vpServoDisplay::display(task,cam,I) ;
281  vpDisplay::flush(I) ;
282 
283  vpTRACE("\t set the gain") ;
284  task.setLambda(1) ;
285 
286 
287  vpTRACE("Display task information " ) ;
288  task.print() ;
289 
290  if (opt_display && opt_click_allowed) {
291  std::cout << "\n\nClick in the camera view window to start..." << std::endl;
293  }
294 
295  unsigned int iter=0 ;
296  vpTRACE("\t loop") ;
297  while(iter++<200)
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  line.track(cMo) ;
308  vpFeatureBuilder::create(l,line);
309 
310  if (opt_display) {
311  vpDisplay::display(I) ;
312  vpServoDisplay::display(task,cam,I) ;
313  vpDisplay::flush(I) ;
314  }
315 
316  if (iter==1) vpTRACE("\t\t compute the control law ") ;
317  v = task.computeControlLaw() ;
318 
319  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
321 
322  vpTRACE("\t\t || s - s* || ") ;
323  std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
324 
325  }
326 
327  if (opt_display && opt_click_allowed) {
328  std::cout << "\nClick in the camera view window to end..." << std::endl;
330  }
331 
332  vpTRACE("Display task information " ) ;
333  task.print() ;
334  task.kill();
335 }
336 
337 #else
338 int
339 main()
340 {
341  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
342 }
343 
344 #endif
345 
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
void setWorldCoordinates(const double &A1, const double &B1, const double &C1, const double &D1, const double &A2, const double &B2, const double &C2, const double &D2)
Definition: vpLine.cpp:98
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 track(const vpHomogeneousMatrix &cMo)
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
Class that defines a line in the object frame, the camera frame and the image plane. All the parameters must be set in meter.
Definition: vpLine.h:124
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:298
virtual void print() const
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)
static double rad(double deg)
Definition: vpMath.h:100
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