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