ViSP  2.8.0
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 - 2013 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/vpCameraParameters.h>
73 #include <visp/vpDisplayX.h>
74 #include <visp/vpDisplayGTK.h>
75 #include <visp/vpDisplayGDI.h>
76 #include <visp/vpFeatureBuilder.h>
77 #include <visp/vpFeaturePoint.h>
78 #include <visp/vpHomogeneousMatrix.h>
79 #include <visp/vpImage.h>
80 #include <visp/vpImagePoint.h>
81 #include <visp/vpIoTools.h>
82 #include <visp/vpMath.h>
83 #include <visp/vpMeterPixelConversion.h>
84 #include <visp/vpParseArgv.h>
85 #include <visp/vpServo.h>
86 #include <visp/vpSimulatorAfma6.h>
87 
88 // List of allowed command line options
89 #define GETOPTARGS "cdh"
90 
99 void usage(const char *name, const char *badparam)
100 {
101  fprintf(stdout, "\n\
102 Tests a control law with the following characteristics:\n\
103  - eye-in-hand control\n\
104  - articular velocity are computed\n\
105  - servo on 4 points,\n\
106  - internal and external camera view displays.\n\
107  \n\
108 SYNOPSIS\n\
109  %s [-c] [-d] [-h]\n", name);
110 
111  fprintf(stdout, "\n\
112 OPTIONS: Default\n\
113  -c\n\
114  Disable the mouse click. Useful to automaze the \n\
115  execution of this program without humain intervention.\n\
116  \n\
117  -d \n\
118  Turn off the display.\n\
119  \n\
120  -h\n\
121  Print the help.\n");
122 
123  if (badparam)
124  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
125 }
138 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
139 {
140  const char *optarg;
141  int c;
142  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
143 
144  switch (c) {
145  case 'c': click_allowed = false; break;
146  case 'd': display = false; break;
147  case 'h': usage(argv[0], NULL); return false; break;
148 
149  default:
150  usage(argv[0], optarg);
151  return false; break;
152  }
153  }
154 
155  if ((c == 1) || (c == -1)) {
156  // standalone param or error
157  usage(argv[0], NULL);
158  std::cerr << "ERROR: " << std::endl;
159  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
160  return false;
161  }
162 
163  return true;
164 }
165 
166 int
167 main(int argc, const char ** argv)
168 {
169  bool opt_click_allowed = true;
170  bool opt_display = true;
171 
172  // Read the command line options
173  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
174  exit (-1);
175  }
176 
177  // We open two displays, one for the internal camera view, the other one for
178  // the external view, using either X11, GTK or GDI.
179 #if defined VISP_HAVE_X11
180  vpDisplayX displayInt;
181 #elif defined VISP_HAVE_GDI
182  vpDisplayGDI displayInt;
183 #elif defined VISP_HAVE_OPENCV
184  vpDisplayOpenCV displayInt;
185 #endif
186 
187  vpImage<unsigned char> Iint(480, 640, 255);
188 
189  if (opt_display) {
190  // open a display for the visualization
191  displayInt.init(Iint,700,0, "Internal view") ;
192  }
193 
194  int i;
195  vpServo task;
196 
197  std::cout << std::endl ;
198  std::cout << "----------------------------------------------" << std::endl ;
199  std::cout << " Test program for vpServo " <<std::endl ;
200  std::cout << " Eye-in-hand task control, articular velocity are computed"
201  << std::endl ;
202  std::cout << " Simulation " << std::endl ;
203  std::cout << " task : servo 4 points " << std::endl ;
204  std::cout << "----------------------------------------------" << std::endl ;
205  std::cout << std::endl ;
206 
207  // sets the initial camera location
208  vpHomogeneousMatrix cMo(-0.05,-0.05,0.7,
209  vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
210 
211  // sets the point coordinates in the object frame
212  vpPoint point[4] ;
213  point[0].setWorldCoordinates(-0.045,-0.045,0) ;
214  point[3].setWorldCoordinates(-0.045,0.045,0) ;
215  point[2].setWorldCoordinates(0.045,0.045,0) ;
216  point[1].setWorldCoordinates(0.045,-0.045,0) ;
217 
218  // computes the point coordinates in the camera frame and its 2D coordinates
219  for (i = 0 ; i < 4 ; i++)
220  point[i].track(cMo) ;
221 
222  // sets the desired position of the point
223  vpFeaturePoint p[4] ;
224  for (i = 0 ; i < 4 ; i++)
225  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
226 
227  // sets the desired position of the feature point s*
228  vpFeaturePoint pd[4] ;
229 
230  // Desired pose
232 
233  // Projection of the points
234  for (int i = 0 ; i < 4 ; i++)
235  point[i].track(cdMo);
236 
237  for (int i = 0 ; i < 4 ; i++)
238  vpFeatureBuilder::create(pd[i], point[i]);
239 
240  // define the task
241  // - we want an eye-in-hand control law
242  // - articular velocity are computed
245 
246  // we want to see a point on a point
247  for (i = 0 ; i < 4 ; i++)
248  task.addFeature(p[i],pd[i]) ;
249 
250  // set the gain
251  task.setLambda(0.8) ;
252 
253  // Declaration of the robot
254  vpSimulatorAfma6 robot(opt_display);
255 
256  // Initialise the robot and especially the camera
259 
260  // Initialise the object for the display part*/
262 
263  // Initialise the position of the object relative to the pose of the robot's camera
264  robot.initialiseObjectRelativeToCamera(cMo);
265 
266  // Set the desired position (for the displaypart)
267  robot.setDesiredCameraPosition(cdMo);
268 
269  // Get the internal robot's camera parameters
270  vpCameraParameters cam;
271  robot.getCameraParameters(cam,Iint);
272 
273  if (opt_display)
274  {
275  //Get the internal view
276  vpDisplay::display(Iint);
277  robot.getInternalView(Iint);
278  vpDisplay::flush(Iint);
279  }
280 
281  // Display task information
282  task.print() ;
283 
284  unsigned int iter=0 ;
285  vpTRACE("\t loop") ;
286  while(iter++<500)
287  {
288  std::cout << "---------------------------------------------" << iter <<std::endl ;
289  vpColVector v ;
290 
291  // Get the Time at the beginning of the loop
292  double t = vpTime::measureTimeMs();
293 
294  // Get the current pose of the camera
295  cMo = robot.get_cMo();
296 
297  if (iter==1) {
298  std::cout <<"Initial robot position with respect to the object frame:\n";
299  cMo.print();
300  }
301 
302  // new point position
303  for (i = 0 ; i < 4 ; i++)
304  {
305  point[i].track(cMo) ;
306  // retrieve x,y and Z of the vpPoint structure
307  try {
308  vpFeatureBuilder::create(p[i],point[i]) ;
309  }
310  catch(...)
311  {
312  break;
313  }
314  }
315 
316  if (opt_display)
317  {
318  // Get the internal view and display it
319  vpDisplay::display(Iint) ;
320  robot.getInternalView(Iint);
321  vpDisplay::flush(Iint);
322  }
323 
324  if (opt_display && opt_click_allowed && iter == 1)
325  {
326  // suppressed for automate test
327  std::cout << "Click in the internal view window to continue..." << std::endl;
328  vpDisplay::getClick(Iint) ;
329  }
330 
331  // compute the control law
332  v = task.computeControlLaw() ;
333 
334  // send the camera velocity to the controller
336 
337  std::cout << "|| s - s* || " << ( task.getError() ).sumSquare() <<std::endl ;
338 
339  // The main loop has a duration of 10 ms at minimum
340  vpTime::wait(t,10);
341  }
342 
343  // Display task information
344  task.print() ;
345  task.kill();
346 
347  std::cout <<"Final robot position with respect to the object frame:\n";
348  cMo.print();
349 
350  if (opt_display && opt_click_allowed)
351  {
352  // suppressed for automate test
353  std::cout << "Click in the internal view window to end..." << std::endl;
354  vpDisplay::getClick(Iint) ;
355  }
356 }
357 #else
358 int
359 main()
360 {
361  vpERROR_TRACE("You do not have X11, OpenCV or GDI display functionalities or threading capabilities...");
362 }
363 
364 #endif
The object displayed at the desired position is the same than the scene object defined in vpSceneObje...
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
A 40cm by 40cm plate with 4 points at coordinates (-0.1,-0.1,0), (0.1,-0.1,0), (0.1,0.1,0), (0.1,0.1,0). Each point is represented by a circle with 2cm radius.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
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:253
void track(const vpHomogeneousMatrix &cMo)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
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:1991
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:154
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:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
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 descbribed in and .
Definition: vpServo.h:153
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
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