ViSP  2.6.2
servoSimuAfma6FourPoints2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuFourPoints2DPolarCamVelocityDisplay.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 with cartesian
36  * coordinates as visual feature.
37  *
38  * Authors:
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
60 #include <visp/vpDebug.h>
61 #include <visp/vpConfig.h>
62 
63 #if (defined(WIN32) || defined(VISP_HAVE_PTHREAD)) && (defined (VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GDI))
64 
65 // We need to use threading capabilities. Thus on Unix-like
66 // platforms, the libpthread third-party library need to be
67 // installed. On Windows, we use the native threading capabilities.
68 
69 #include <stdlib.h>
70 #include <stdio.h>
71 
72 #include <visp/vpMath.h>
73 #include <visp/vpHomogeneousMatrix.h>
74 #include <visp/vpServo.h>
75 #include <visp/vpDebug.h>
76 #include <visp/vpFeatureBuilder.h>
77 #include <visp/vpFeaturePoint.h>
78 #include <visp/vpSimulatorAfma6.h>
79 
80 #include <visp/vpMeterPixelConversion.h>
81 
82 #include <visp/vpImage.h>
83 #include <visp/vpImagePoint.h>
84 #include <visp/vpDisplayX.h>
85 #include <visp/vpDisplayGTK.h>
86 #include <visp/vpDisplayGDI.h>
87 #include <visp/vpCameraParameters.h>
88 #include <visp/vpParseArgv.h>
89 #include <visp/vpIoTools.h>
90 
91 // List of allowed command line options
92 #define GETOPTARGS "cdh"
93 
102 void usage(const char *name, const char *badparam)
103 {
104  fprintf(stdout, "\n\
105 Tests a control law with the following characteristics:\n\
106  - eye-in-hand control\n\
107  - articular velocity are computed\n\
108  - servo on 4 points,\n\
109  - internal and external camera view displays.\n\
110  \n\
111 SYNOPSIS\n\
112  %s [-c] [-d] [-h]\n", name);
113 
114  fprintf(stdout, "\n\
115 OPTIONS: Default\n\
116  -c\n\
117  Disable the mouse click. Useful to automaze the \n\
118  execution of this program without humain intervention.\n\
119  \n\
120  -d \n\
121  Turn off the display.\n\
122  \n\
123  -h\n\
124  Print the help.\n");
125 
126  if (badparam)
127  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
128 }
141 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
142 {
143  const char *optarg;
144  int c;
145  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
146 
147  switch (c) {
148  case 'c': click_allowed = false; break;
149  case 'd': display = false; break;
150  case 'h': usage(argv[0], NULL); return false; break;
151 
152  default:
153  usage(argv[0], optarg);
154  return false; break;
155  }
156  }
157 
158  if ((c == 1) || (c == -1)) {
159  // standalone param or error
160  usage(argv[0], NULL);
161  std::cerr << "ERROR: " << std::endl;
162  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
163  return false;
164  }
165 
166  return true;
167 }
168 
169 int
170 main(int argc, const char ** argv)
171 {
172  bool opt_click_allowed = true;
173  bool opt_display = true;
174 
175  // Read the command line options
176  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
177  exit (-1);
178  }
179 
180  // We open two displays, one for the internal camera view, the other one for
181  // the external view, using either X11, GTK or GDI.
182 #if defined VISP_HAVE_X11
183  vpDisplayX displayInt;
184 #elif defined VISP_HAVE_GDI
185  vpDisplayGDI displayInt;
186 #elif defined VISP_HAVE_OPENCV
187  vpDisplayOpenCV displayInt;
188 #endif
189 
190  // open a display for the visualization
191 
192  vpImage<unsigned char> Iint(480, 640, 255);
193 
194  if (opt_display) {
195  displayInt.init(Iint,700,0, "Internal view") ;
196  }
197 
198  int i;
199  vpServo task;
200 
201 
202  std::cout << std::endl ;
203  std::cout << "----------------------------------------------" << std::endl ;
204  std::cout << " Test program for vpServo " <<std::endl ;
205  std::cout << " Eye-in-hand task control, articular velocity are computed"
206  << std::endl ;
207  std::cout << " Simulation " << std::endl ;
208  std::cout << " task : servo 4 points " << std::endl ;
209  std::cout << "----------------------------------------------" << std::endl ;
210  std::cout << std::endl ;
211 
212 
213  vpTRACE("sets the initial camera location " ) ;
214  vpHomogeneousMatrix cMo(-0.05,-0.05,0.7,
215  vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
216 
217 
218  vpTRACE("sets the point coordinates in the object frame " ) ;
219  vpPoint point[4] ;
220  point[0].setWorldCoordinates(-0.045,-0.045,0) ;
221  point[3].setWorldCoordinates(-0.045,0.045,0) ;
222  point[2].setWorldCoordinates(0.045,0.045,0) ;
223  point[1].setWorldCoordinates(0.045,-0.045,0) ;
224 
225  vpTRACE("project : computes the point coordinates in the camera frame and its 2D coordinates" ) ;
226  for (i = 0 ; i < 4 ; i++)
227  point[i].track(cMo) ;
228 
229  vpTRACE("sets the desired position of the point ") ;
230  vpFeaturePoint p[4] ;
231  for (i = 0 ; i < 4 ; i++)
232  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
233 
234  vpTRACE("sets the desired position of the feature point s*") ;
235  vpFeaturePoint pd[4] ;
236 
237  //Desired pose
239 
240  // Projection of the points
241  for (int i = 0 ; i < 4 ; i++)
242  point[i].track(cdMo);
243 
244  for (int i = 0 ; i < 4 ; i++)
245  vpFeatureBuilder::create(pd[i], point[i]);
246 
247  vpTRACE("define the task") ;
248  vpTRACE("\t we want an eye-in-hand control law") ;
249  vpTRACE("\t articular velocity are computed") ;
252 
253  vpTRACE("\t we want to see a point on a point..") ;
254  for (i = 0 ; i < 4 ; i++)
255  task.addFeature(p[i],pd[i]) ;
256 
257  vpTRACE("\t set the gain") ;
258  task.setLambda(0.8) ;
259 
260  /*Declaration of the robot*/
261  vpSimulatorAfma6 robot(opt_display);
262 
263  /*Initialise the robot and especially the camera*/
266 
267  /*Initialise the object for the display part*/
269 
270  /*Initialise the position of the object relative to the pose of the robot's camera*/
271  robot.initialiseObjectRelativeToCamera(cMo);
272 
273  /*Set the desired position (for the displaypart)*/
274  robot.setDesiredCameraPosition(cdMo);
275 
276  /*Get the internal robot's camera parameters*/
277  vpCameraParameters cam;
278  robot.getCameraParameters(cam,Iint);
279 
280  if (opt_display)
281  {
282  //Get the internal view
283  vpDisplay::display(Iint);
284  robot.getInternalView(Iint);
285  vpDisplay::flush(Iint);
286  }
287 
288 
289  vpTRACE("Display task information " ) ;
290  task.print() ;
291 
292  unsigned int iter=0 ;
293  vpTRACE("\t loop") ;
294  while(iter++<500)
295  {
296  std::cout << "---------------------------------------------" << iter <<std::endl ;
297  vpColVector v ;
298 
299  //Get the Time at the beginning of the loop
300  double t = vpTime::measureTimeMs();
301 
302  //Get the current pose of the camera
303  cMo = robot.get_cMo();
304 
305  if (iter==1) {
306  std::cout <<"Initial robot position with respect to the object frame:\n";
307  cMo.print();
308  }
309 
310  if (iter==1) vpTRACE("\t new point position ") ;
311  for (i = 0 ; i < 4 ; i++)
312  {
313  point[i].track(cMo) ;
314  //retrieve x,y and Z of the vpPoint structure
315  try {
316  vpFeatureBuilder::create(p[i],point[i]) ;
317  }
318  catch(...)
319  {
320  break;
321  }
322  }
323 
324  if (opt_display)
325  {
326  /*Get the internal view and display it*/
327  vpDisplay::display(Iint) ;
328  robot.getInternalView(Iint);
329  vpDisplay::flush(Iint);
330  }
331 
332  if (opt_display && opt_click_allowed && iter == 1)
333  {
334  // suppressed for automate test
335  vpTRACE("\n\nClick in the internal view window to continue...");
336  vpDisplay::getClick(Iint) ;
337  }
338 
339  if (iter==1) vpTRACE("\t\t compute the control law ") ;
340  v = task.computeControlLaw() ;
341 
342  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
344 
345  vpTRACE("\t\t || s - s* || ") ;
346  std::cout << ( task.getError() ).sumSquare() <<std::endl ;
347 
348  /* The main loop as a duration of 10 ms at minimum*/
349  vpTime::wait(t,10);
350  }
351 
352  vpTRACE("Display task information " ) ;
353  task.print() ;
354  task.kill();
355 
356 
357  std::cout <<"Final robot position with respect to the object frame:\n";
358  cMo.print();
359 
360  if (opt_display && opt_click_allowed)
361  {
362  // suppressed for automate test
363  vpTRACE("\n\nClick in the internal view window to end...");
364  vpDisplay::getClick(Iint) ;
365  }
366 }
367 #else
368 int
369 main()
370 {
371  vpERROR_TRACE("You do not have X11, OpenCV or GDI display functionalities or threading capabilities...");
372 }
373 
374 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Perspective projection without distortion model.
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 double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
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
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:152
Class that defines what is a point.
Definition: vpPoint.h:65
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
Initialize the velocity controller.
Definition: vpRobot.h:70
vpColVector getError() const
Definition: vpServo.h:298
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
The vpDisplayOpenCV allows to display image using the opencv library.
Generic class defining intrinsic camera parameters.
Simulator of Irisa's gantry robot named Afma6.
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
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
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