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