ViSP  2.6.2
servoSimuFourPoints2DCamVelocityDisplay.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuFourPoints2DCamVelocityDisplay.cpp 2503 2010-02-16 18:55:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Simulation of a 2D visual servoing using 4 points as visual feature.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
68 #include <visp/vpDebug.h>
69 #include <visp/vpConfig.h>
70 
71 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
72 
73 #include <stdlib.h>
74 #include <stdio.h>
75 
76 #include <visp/vpMath.h>
77 #include <visp/vpHomogeneousMatrix.h>
78 #include <visp/vpFeaturePoint.h>
79 #include <visp/vpServo.h>
80 #include <visp/vpRobotCamera.h>
81 #include <visp/vpDebug.h>
82 #include <visp/vpFeatureBuilder.h>
83 
84 #include <visp/vpServoDisplay.h>
85 #include <visp/vpProjectionDisplay.h>
86 
87 #include <visp/vpImage.h>
88 #include <visp/vpDisplayX.h>
89 #include <visp/vpDisplayGTK.h>
90 #include <visp/vpDisplayGDI.h>
91 #include <visp/vpCameraParameters.h>
92 #include <visp/vpParseArgv.h>
93 
94 // List of allowed command line options
95 #define GETOPTARGS "cdh"
96 
105 void usage(const char *name, const char *badparam)
106 {
107  fprintf(stdout, "\n\
108 Tests a control law with the following characteristics:\n\
109 - eye-in-hand control\n\
110 - articular velocity are computed\n\
111 - servo on 4 points,\n\
112 - internal and external camera view displays.\n\
113  \n\
114 SYNOPSIS\n\
115  %s [-c] [-d] [-h]\n", name);
116 
117  fprintf(stdout, "\n\
118 OPTIONS: Default\n\
119  -c\n\
120  Disable the mouse click. Useful to automaze the \n\
121  execution of this program without humain intervention.\n\
122  \n\
123  -d \n\
124  Turn off the display.\n\
125  \n\
126  -h\n\
127  Print the help.\n");
128 
129  if (badparam)
130  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
131 }
144 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
145 {
146  const char *optarg;
147  int c;
148  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
149 
150  switch (c) {
151  case 'c': click_allowed = false; break;
152  case 'd': display = false; break;
153  case 'h': usage(argv[0], NULL); return false; break;
154 
155  default:
156  usage(argv[0], optarg);
157  return false; break;
158  }
159  }
160 
161  if ((c == 1) || (c == -1)) {
162  // standalone param or error
163  usage(argv[0], NULL);
164  std::cerr << "ERROR: " << std::endl;
165  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
166  return false;
167  }
168 
169  return true;
170 }
171 
172 int
173 main(int argc, const char ** argv)
174 {
175 
176  bool opt_click_allowed = true;
177  bool opt_display = true;
178 
179  // Read the command line options
180  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
181  exit (-1);
182  }
183 
184  // We open two displays, one for the internal camera view, the other one for
185  // the external view, using either X11, GTK or GDI.
186 #if defined VISP_HAVE_X11
187  vpDisplayX displayInt;
188  vpDisplayX displayExt;
189 #elif defined VISP_HAVE_GTK
190  vpDisplayGTK displayInt;
191  vpDisplayGTK displayExt;
192 #elif defined VISP_HAVE_GDI
193  vpDisplayGDI displayInt;
194  vpDisplayGDI displayExt;
195 #endif
196 
197  // open a display for the visualization
198 
199  vpImage<unsigned char> Iint(300, 300, 0) ;
200  vpImage<unsigned char> Iext(300, 300, 0) ;
201 
202  if (opt_display) {
203  displayInt.init(Iint,0,0, "Internal view") ;
204  displayExt.init(Iext,330,000, "External view") ;
205 
206  }
207  vpProjectionDisplay externalview ;
208 
209  double px, py ; px = py = 500 ;
210  double u0, v0 ; u0 = 150, v0 = 160 ;
211 
212  vpCameraParameters cam(px,py,u0,v0);
213 
214  int i ;
215  vpServo task ;
216  vpRobotCamera robot ;
217 
218 
219  std::cout << std::endl ;
220  std::cout << "----------------------------------------------" << std::endl ;
221  std::cout << " Test program for vpServo " <<std::endl ;
222  std::cout << " Eye-in-hand task control, articular velocity are computed"
223  << std::endl ;
224  std::cout << " Simulation " << std::endl ;
225  std::cout << " task : servo 4 points " << std::endl ;
226  std::cout << "----------------------------------------------" << std::endl ;
227  std::cout << std::endl ;
228 
229 
230  vpTRACE("sets the initial camera location " ) ;
231  vpHomogeneousMatrix cMo(-0.1,-0.1,1,
232  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
233 
234  robot.setPosition(cMo) ;
235 
236  vpHomogeneousMatrix cextMo(0,0,2,
237  0,0,0) ;//vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
238 
239 
240  vpTRACE("sets the point coordinates in the object frame " ) ;
241  vpPoint point[4] ;
242  point[0].setWorldCoordinates(-0.1,-0.1,0) ;
243  point[1].setWorldCoordinates(0.1,-0.1,0) ;
244  point[2].setWorldCoordinates(0.1,0.1,0) ;
245  point[3].setWorldCoordinates(-0.1,0.1,0) ;
246 
247 
248  for (i = 0 ; i < 4 ; i++)
249  externalview.insert(point[i]) ;
250 
251  vpTRACE("project : computes the point coordinates in the camera frame and its 2D coordinates" ) ;
252  for (i = 0 ; i < 4 ; i++)
253  point[i].track(cMo) ;
254 
255  vpTRACE("sets the desired position of the point ") ;
256  vpFeaturePoint p[4] ;
257  for (i = 0 ; i < 4 ; i++)
258  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
259 
260 
261  vpTRACE("sets the desired position of the feature point s*") ;
262  vpFeaturePoint pd[4] ;
263 
264  pd[0].buildFrom(-0.1,-0.1,1) ;
265  pd[1].buildFrom(0.1,-0.1,1) ;
266  pd[2].buildFrom(0.1,0.1,1) ;
267  pd[3].buildFrom(-0.1,0.1,1) ;
268 
269  vpTRACE("define the task") ;
270  vpTRACE("\t we want an eye-in-hand control law") ;
271  vpTRACE("\t articular velocity are computed") ;
274 
275 
276  vpTRACE("Set the position of the camera in the end-effector frame ") ;
277  vpHomogeneousMatrix cMe ;
278  vpVelocityTwistMatrix cVe(cMe) ;
279  task.set_cVe(cVe) ;
280 
281  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
282  vpMatrix eJe ;
283  robot.get_eJe(eJe) ;
284  task.set_eJe(eJe) ;
285 
286  vpTRACE("\t we want to see a point on a point..") ;
287  for (i = 0 ; i < 4 ; i++)
288  task.addFeature(p[i],pd[i]) ;
289 
290  vpTRACE("\t set the gain") ;
291  task.setLambda(1) ;
292 
293 
294  vpTRACE("Display task information " ) ;
295  task.print() ;
296 
297  unsigned int iter=0 ;
298  vpTRACE("\t loop") ;
299  while(iter++<200)
300  {
301  std::cout << "---------------------------------------------" << iter <<std::endl ;
302  vpColVector v ;
303 
304 
305  if (iter==1)
306  {
307  vpTRACE("Set the Jacobian (expressed in the end-effector frame)") ;
308  vpTRACE("since q is modified eJe is modified") ;
309  }
310  robot.get_eJe(eJe) ;
311  task.set_eJe(eJe) ;
312 
313  robot.getPosition(cMo) ;
314 
315  if (iter==1) {
316  std::cout <<"Initial robot position with respect to the object frame:\n";
317  cMo.print();
318  }
319 
320  if (iter==1) vpTRACE("\t new point position ") ;
321  for (i = 0 ; i < 4 ; i++)
322  {
323  point[i].track(cMo) ;
324  //retrieve x,y and Z of the vpPoint structure
325  vpFeatureBuilder::create(p[i],point[i]) ;
326 
327  }
328 
329  if (opt_display) {
330  vpDisplay::display(Iint) ;
331  vpDisplay::display(Iext) ;
332  vpServoDisplay::display(task,cam,Iint) ;
333  externalview.display(Iext,cextMo, cMo, cam, vpColor::green) ;
334  vpDisplay::flush(Iint);
335  vpDisplay::flush(Iext);
336  }
337 
338  if (iter==1) vpTRACE("\t\t compute the control law ") ;
339  v = task.computeControlLaw() ;
340 
341  if (iter==1)
342  {
343  vpTRACE("Display task information " ) ;
344  task.print() ;
345  }
346 
347  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
349 
350  vpTRACE("\t\t || s - s* || ") ;
351  std::cout << ( task.getError() ).sumSquare() <<std::endl ;
352  }
353 
354  vpTRACE("Display task information " ) ;
355  task.print() ;
356  task.kill();
357 
358 
359  std::cout <<"Final robot position with respect to the object frame:\n";
360  cMo.print();
361 
362  if (opt_display && opt_click_allowed) {
363  // suppressed for automate test
364  vpTRACE("\n\nClick in the internal view window to end...");
365  vpDisplay::getClick(Iint) ;
366  }
367 }
368 #else
369 int
370 main()
371 {
372  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
373 }
374 
375 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
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 print()
Print the matrix as a vector [T thetaU].
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)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
static const vpColor green
Definition: vpColor.h:168
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:227
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
Class that defines what is a point.
Definition: vpPoint.h:65
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:298
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
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:235
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void insert(vpForwardProjection &fp)
void getPosition(vpColVector &q)
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void setPosition(const vpRobot::vpControlFrameType, const vpColVector &)
Set a displacement (frame has to be specified) in position control.
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor color, const bool &displayTraj=false)
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
void buildFrom(const double x, const double y, const double Z)
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 get_eJe(vpMatrix &_eJe)
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.
Definition: vpServo.h:150
interface with the image for feature display
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74