ViSP
servoSimuCircle2DCamVelocityDisplay.cpp
1 
2 /****************************************************************************
3  *
4  * $Id: servoSimuCircle2DCamVelocityDisplay.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 circle.
37  *
38  * Authors:
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
54 #include <visp/vpDebug.h>
55 #include <visp/vpConfig.h>
56 
57 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
58 
59 #include <stdlib.h>
60 #include <stdio.h>
61 
62 #include <visp/vpCameraParameters.h>
63 #include <visp/vpCircle.h>
64 #include <visp/vpDisplayX.h>
65 #include <visp/vpDisplayGTK.h>
66 #include <visp/vpDisplayGDI.h>
67 #include <visp/vpDisplayOpenCV.h>
68 #include <visp/vpFeatureBuilder.h>
69 #include <visp/vpFeatureLine.h>
70 #include <visp/vpHomogeneousMatrix.h>
71 #include <visp/vpImage.h>
72 #include <visp/vpMath.h>
73 #include <visp/vpParseArgv.h>
74 #include <visp/vpProjectionDisplay.h>
75 #include <visp/vpServo.h>
76 #include <visp/vpSimulatorCamera.h>
77 #include <visp/vpServoDisplay.h>
78 
79 // List of allowed command line options
80 #define GETOPTARGS "cdh"
81 
82 void usage(const char *name, const char *badparam);
83 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
84 
93 void usage(const char *name, const char *badparam)
94 {
95  fprintf(stdout, "\n\
96 Simulation of a 2D visual servoing on a circle:\n\
97 - eye-in-hand control law,\n\
98 - velocity computed in the camera frame,\n\
99 - display the camera view.\n\
100  \n\
101 SYNOPSIS\n\
102  %s [-c] [-d] [-h]\n", name);
103 
104  fprintf(stdout, "\n\
105 OPTIONS: Default\n\
106  \n\
107  -c\n\
108  Disable the mouse click. Useful to automaze the \n\
109  execution of this program without humain intervention.\n\
110  \n\
111  -d \n\
112  Turn off the display.\n\
113  \n\
114  -h\n\
115  Print the help.\n");
116 
117  if (badparam) {
118  fprintf(stderr, "ERROR: \n" );
119  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
120  }
121 }
122 
135 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
136 {
137  const char *optarg_;
138  int c;
139  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
140 
141  switch (c) {
142  case 'c': click_allowed = false; break;
143  case 'd': display = false; break;
144  case 'h': usage(argv[0], NULL); return false; break;
145 
146  default:
147  usage(argv[0], optarg_);
148  return false; break;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], NULL);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161 }
162 
163 
164 int
165 main(int argc, const char ** argv)
166 {
167  try {
168  bool opt_display = true;
169  bool opt_click_allowed = true;
170 
171  // Read the command line options
172  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
173  exit (-1);
174  }
175 
176  vpImage<unsigned char> I(512,512,0) ;
177 
178  // We open a window using either X11, GTK or GDI.
179 #if defined VISP_HAVE_X11
180  vpDisplayX display;
181 #elif defined VISP_HAVE_GTK
182  vpDisplayGTK display;
183 #elif defined VISP_HAVE_GDI
184  vpDisplayGDI display;
185 #elif defined VISP_HAVE_OPENCV
186  vpDisplayOpenCV display;
187 #endif
188 
189  if (opt_display) {
190  try{
191  // Display size is automatically defined by the image (I) size
192  display.init(I, 100, 100,"Camera view...") ;
193  // Display the image
194  // The image class has a member that specify a pointer toward
195  // the display that has been initialized in the display declaration
196  // therefore is is no longuer necessary to make a reference to the
197  // display variable.
198  vpDisplay::display(I) ;
199  vpDisplay::flush(I) ;
200  }
201  catch(...)
202  {
203  vpERROR_TRACE("Error while displaying the image") ;
204  exit(-1);
205  }
206  }
207 
208  double px, py ; px = py = 600 ;
209  double u0, v0 ; u0 = v0 = 256 ;
210 
211  vpCameraParameters cam(px,py,u0,v0);
212 
213  vpServo task ;
214  vpSimulatorCamera robot ;
215 
216  // sets the initial camera location
217  vpHomogeneousMatrix cMo(0,0,1,
218  vpMath::rad(0), vpMath::rad(80), vpMath::rad(30)) ;
219  vpHomogeneousMatrix wMc, wMo;
220  robot.getPosition(wMc) ;
221  wMo = wMc * cMo; // Compute the position of the object in the world frame
222 
223  vpHomogeneousMatrix cMod(-0.1,-0.1,0.7,
224  vpMath::rad(40), vpMath::rad(10), vpMath::rad(30)) ;
225 
226  // sets the circle coordinates in the world frame
227  vpCircle circle ;
228  circle.setWorldCoordinates(0,0,1,
229  0,0,0,
230  0.1) ;
231 
232  // sets the desired position of the visual feature
233  vpFeatureEllipse pd ;
234  circle.track(cMod) ;
235  vpFeatureBuilder::create(pd,circle) ;
236 
237  // project : computes the circle coordinates in the camera frame and its 2D coordinates
238  // sets the current position of the visual feature
239  vpFeatureEllipse p ;
240  circle.track(cMo) ;
241  vpFeatureBuilder::create(p,circle) ;
242 
243  // define the task
244  // - we want an eye-in-hand control law
245  // - robot is controlled in the camera frame
248  // - we want to see a circle on a circle
249  task.addFeature(p,pd) ;
250  // - set the gain
251  task.setLambda(1) ;
252 
253  // Display task information
254  task.print() ;
255 
256  unsigned int iter=0 ;
257  // loop
258  while(iter++ < 200)
259  {
260  std::cout << "---------------------------------------------" << iter <<std::endl ;
261  vpColVector v ;
262 
263  // get the robot position
264  robot.getPosition(wMc) ;
265  // Compute the position of the camera wrt the object frame
266  cMo = wMc.inverse() * wMo;
267 
268  // new circle position
269  // retrieve x,y and Z of the vpCircle structure
270  circle.track(cMo) ;
271  vpFeatureBuilder::create(p,circle);
272  circle.print() ;
273  p.print() ;
274 
275  if (opt_display) {
276  vpDisplay::display(I) ;
277  vpServoDisplay::display(task,cam,I) ;
278  vpDisplay::flush(I) ;
279  }
280 
281  // compute the control law
282  v = task.computeControlLaw() ;
283  std::cout << "task rank: " << task.getTaskRank() <<std::endl ;
284  // send the camera velocity to the controller
286 
287  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
288  }
289 
290  // Display task information
291  task.print() ;
292  task.kill();
293 
294  if (opt_display && opt_click_allowed) {
295  std::cout << "Click in the camera view window to end..." << std::endl;
297  }
298  return 0;
299  }
300  catch(vpException e) {
301  std::cout << "Catch a ViSP exception: " << e << std::endl;
302  return 1;
303  }
304 }
305 #else
306 int
307 main()
308 {
309  vpERROR_TRACE("You do not have X11, GTK, GDI or OpenCV display functionalities...");
310 }
311 
312 #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)
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.
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
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
virtual void print() const
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
void print(const unsigned int select=FEATURE_ALL) const
print the name of the feature
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
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
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines what is a circle.
Definition: vpCircle.h:61
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)
unsigned int getTaskRank() const
Definition: vpServo.cpp:1572
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:66