ViSP  2.8.0
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 - 2013 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 
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/vpDisplayX.h>
63 #include <visp/vpDisplayGTK.h>
64 #include <visp/vpDisplayGDI.h>
65 #include <visp/vpFeatureBuilder.h>
66 #include <visp/vpFeatureLine.h>
67 #include <visp/vpHomogeneousMatrix.h>
68 #include <visp/vpImage.h>
69 #include <visp/vpLine.h>
70 #include <visp/vpMath.h>
71 #include <visp/vpParseArgv.h>
72 #include <visp/vpRobotCamera.h>
73 #include <visp/vpServo.h>
74 #include <visp/vpServoDisplay.h>
75 #include <visp/vpSimulatorCamera.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 2D a visual servoing on a line:\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 
128 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
129 {
130  const char *optarg;
131  int c;
132  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
133 
134  switch (c) {
135  case 'c': click_allowed = false; break;
136  case 'd': display = false; break;
137  case 'h': usage(argv[0], NULL); return false; break;
138 
139  default:
140  usage(argv[0], optarg);
141  return false; break;
142  }
143  }
144 
145  if ((c == 1) || (c == -1)) {
146  // standalone param or error
147  usage(argv[0], NULL);
148  std::cerr << "ERROR: " << std::endl;
149  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
150  return false;
151  }
152 
153  return true;
154 }
155 
156 
157 int
158 main(int argc, const char ** argv)
159 {
160  bool opt_display = true;
161  bool opt_click_allowed = true;
162 
163  // Read the command line options
164  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
165  exit (-1);
166  }
167 
168  vpImage<unsigned char> I(512,512,0) ;
169 
170  // We open a window using either X11, GTK or GDI.
171 #if defined VISP_HAVE_X11
172  vpDisplayX display;
173 #elif defined VISP_HAVE_GTK
174  vpDisplayGTK display;
175 #elif defined VISP_HAVE_GDI
176  vpDisplayGDI display;
177 #endif
178 
179  if (opt_display) {
180  try{
181  // Display size is automatically defined by the image (I) size
182  display.init(I, 100, 100,"Camera view...") ;
183  // Display the image
184  // The image class has a member that specify a pointer toward
185  // the display that has been initialized in the display declaration
186  // therefore is is no longuer necessary to make a reference to the
187  // display variable.
188  vpDisplay::display(I) ;
189  vpDisplay::flush(I) ;
190  }
191  catch(...)
192  {
193  vpERROR_TRACE("Error while displaying the image") ;
194  exit(-1);
195  }
196  }
197 
198  // Set the camera parameters
199  double px, py ; px = py = 600 ;
200  double u0, v0 ; u0 = v0 = 256 ;
201 
202  vpCameraParameters cam(px,py,u0,v0);
203 
204  vpServo task ;
205  vpSimulatorCamera robot ;
206 
207  // sets the initial camera location
208  vpHomogeneousMatrix cMo(0.2,0.2,1,
209  vpMath::rad(45), vpMath::rad(45), vpMath::rad(125));
210 
211  // Compute the position of the object in the world frame
212  vpHomogeneousMatrix wMc, wMo;
213  robot.getPosition(wMc) ;
214  wMo = wMc * cMo;
215 
216  // sets the final camera location (for simulation purpose)
217  vpHomogeneousMatrix cMod(0,0,1,
218  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
219 
220 
221  int nbline = 4;
222 
223  // sets the line coordinates (2 planes) in the world frame
224  vpLine line[4] ;
225  line[0].setWorldCoordinates(1,0,0,0.05,0,0,1,0);
226  line[1].setWorldCoordinates(0,1,0,0.05,0,0,1,0);
227  line[2].setWorldCoordinates(1,0,0,-0.05,0,0,1,0);
228  line[3].setWorldCoordinates(0,1,0,-0.05,0,0,1,0);
229 
230  vpFeatureLine ld[4] ;
231  vpFeatureLine l[4] ;
232 
233  // sets the desired position of the visual feature
234  for(int i = 0; i < nbline; i++)
235  {
236  line[i].track(cMod) ;
237  line[i].print() ;
238 
239  vpFeatureBuilder::create(ld[i],line[i]) ;
240  }
241 
242  // computes the line coordinates in the camera frame and its 2D coordinates
243  // sets the current position of the visual feature
244  for(int i = 0; i < nbline; i++)
245  {
246  line[i].track(cMo) ;
247  line[i].print() ;
248 
249  vpFeatureBuilder::create(l[i],line[i]) ;
250  l[i].print() ;
251  }
252 
253  // define the task
254  // - we want an eye-in-hand control law
255  // - robot is controlled in the camera frame
258  //It could be also interesting to test the following tasks
259  //task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
260  //task.setInteractionMatrixType(vpServo::MEAN, vpServo::PSEUDO_INVERSE);
261 
262  // we want to see a four lines on four lines
263  for(int i = 0; i < nbline; i++)
264  task.addFeature(l[i],ld[i]) ;
265 
266  vpDisplay::display(I) ;
267  vpServoDisplay::display(task,cam,I) ;
268  vpDisplay::flush(I) ;
269 
270  // set the gain
271  task.setLambda(1) ;
272 
273  // Display task information
274  task.print() ;
275 
276  if (opt_display && opt_click_allowed) {
277  std::cout << "\n\nClick in the camera view window to start..." << std::endl;
279  }
280 
281  unsigned int iter=0 ;
282  // loop
283  while(iter++<200)
284  {
285  std::cout << "---------------------------------------------" << iter <<std::endl ;
286  vpColVector v ;
287 
288  // get the robot position
289  robot.getPosition(wMc) ;
290  // Compute the position of the camera wrt the object frame
291  cMo = wMc.inverse() * wMo;
292 
293  // new line position: retrieve x,y and Z of the vpLine structure
294  for(int i = 0; i < nbline; i++)
295  {
296  line[i].track(cMo) ;
297  vpFeatureBuilder::create(l[i],line[i]);
298  }
299 
300  if (opt_display) {
301  vpDisplay::display(I) ;
302  vpServoDisplay::display(task,cam,I) ;
303  vpDisplay::flush(I) ;
304  }
305 
306  // compute the control law
307  v = task.computeControlLaw() ;
308 
309  // send the camera velocity to the controller
311 
312  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ; ;
313 
314  }
315 
316  if (opt_display && opt_click_allowed) {
317  std::cout << "\nClick in the camera view window to end..." << std::endl;
319  }
320 
321  // Display task information
322  task.print() ;
323  task.kill();
324 }
325 
326 #else
327 int
328 main()
329 {
330  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
331 }
332 
333 #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.
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: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
void track(const vpHomogeneousMatrix &cMo)
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
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:301
virtual void print() const
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 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