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