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