ViSP  2.9.0
servoSimuViper850FourPoints2DCamVelocity.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 - 2014 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 polar
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/vpSimulatorViper850.h>
87 
88 // List of allowed command line options
89 #define GETOPTARGS "cdh"
90 
91 void usage(const char *name, const char *badparam);
92 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
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  try {
173  bool opt_click_allowed = true;
174  bool opt_display = true;
175 
176  // Read the command line options
177  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
178  exit (-1);
179  }
180 
181  // We open two displays, one for the internal camera view, the other one for
182  // the external view, using either X11, GTK or GDI.
183 #if defined VISP_HAVE_X11
184  vpDisplayX displayInt;
185 #elif defined VISP_HAVE_GDI
186  vpDisplayGDI displayInt;
187 #elif defined VISP_HAVE_OPENCV
188  vpDisplayOpenCV displayInt;
189 #endif
190 
191  // open a display for the visualization
192 
193  vpImage<unsigned char> Iint(480, 640, 255);
194 
195  if (opt_display) {
196  displayInt.init(Iint,700,0, "Internal view") ;
197  }
198 
199  vpServo task;
200 
201  std::cout << std::endl ;
202  std::cout << "----------------------------------------------" << std::endl ;
203  std::cout << " Test program for vpServo " <<std::endl ;
204  std::cout << " Eye-in-hand task control, articular velocity are computed"
205  << std::endl ;
206  std::cout << " Simulation " << std::endl ;
207  std::cout << " task : servo 4 points " << std::endl ;
208  std::cout << "----------------------------------------------" << std::endl ;
209  std::cout << std::endl ;
210 
211  // sets the initial camera location
212  vpHomogeneousMatrix cMo(-0.05,-0.05,0.7,
213  vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
214 
215 
216  // sets the point coordinates in the object frame
217  vpPoint point[4] ;
218  point[0].setWorldCoordinates(-0.045,-0.045,0) ;
219  point[3].setWorldCoordinates(-0.045,0.045,0) ;
220  point[2].setWorldCoordinates(0.045,0.045,0) ;
221  point[1].setWorldCoordinates(0.045,-0.045,0) ;
222 
223  // computes the point coordinates in the camera frame and its 2D coordinates
224  for (unsigned int i = 0 ; i < 4 ; i++)
225  point[i].track(cMo) ;
226 
227  // sets the desired position of the point
228  vpFeaturePoint p[4] ;
229  for (unsigned int i = 0 ; i < 4 ; i++)
230  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
231 
232  // sets the desired position of the feature point s*
233  vpFeaturePoint pd[4] ;
234 
235  //Desired pose
237 
238  // Projection of the points
239  for (unsigned int i = 0 ; i < 4 ; i++)
240  point[i].track(cdMo);
241 
242  for (unsigned int i = 0 ; i < 4 ; i++)
243  vpFeatureBuilder::create(pd[i], point[i]);
244 
245  // define the task
246  // - we want an eye-in-hand control law
247  // - articular velocity are computed
250 
251  // - we want to see a point on a point
252  for (unsigned int i = 0 ; i < 4 ; i++)
253  task.addFeature(p[i],pd[i]) ;
254 
255  // set the gain
256  task.setLambda(0.8) ;
257 
258  // Declaration of the robot
259  vpSimulatorViper850 robot(opt_display);
260 
261  // Initialise the robot and especially the camera
264 
265  // Initialise the object for the display part
267 
268  // Initialise the position of the object relative to the pose of the robot's camera
269  robot.initialiseObjectRelativeToCamera(cMo);
270 
271  // Set the desired position (for the display part)
272  robot.setDesiredCameraPosition(cdMo);
273 
274  // Get the internal robot's camera parameters
275  vpCameraParameters cam;
276  robot.getCameraParameters(cam,Iint);
277 
278  if (opt_display)
279  {
280  //Get the internal view
281  vpDisplay::display(Iint);
282  robot.getInternalView(Iint);
283  vpDisplay::flush(Iint);
284  }
285 
286  // Display task information
287  task.print() ;
288 
289  unsigned int iter=0 ;
290  // loop
291  while(iter++<500)
292  {
293  std::cout << "---------------------------------------------" << iter <<std::endl ;
294  vpColVector v ;
295 
296  //Get the Time at the beginning of the loop
297  double t = vpTime::measureTimeMs();
298 
299  //Get the current pose of the camera
300  cMo = robot.get_cMo();
301 
302  if (iter==1) {
303  std::cout <<"Initial robot position with respect to the object frame:\n";
304  cMo.print();
305  }
306 
307  // new point position
308  for (unsigned int i = 0 ; i < 4 ; i++)
309  {
310  point[i].track(cMo) ;
311  //retrieve x,y and Z of the vpPoint structure
312  vpFeatureBuilder::create(p[i],point[i]) ;
313  }
314 
315  if (opt_display)
316  {
317  // Get the internal view and display it
318  vpDisplay::display(Iint) ;
319  robot.getInternalView(Iint);
320  vpDisplay::flush(Iint);
321  }
322 
323  if (opt_display && opt_click_allowed && iter == 1)
324  {
325  // suppressed for automate test
326  std::cout << "Click in the internal view window to continue..." << std::endl;
327  vpDisplay::getClick(Iint) ;
328  }
329 
330  // compute the control law
331  v = task.computeControlLaw() ;
332 
333  // send the camera velocity to the controller
335 
336  std::cout << "|| s - s* || " << ( task.getError() ).sumSquare() <<std::endl ;
337 
338  // The main loop has a duration of 10 ms at minimum
339  vpTime::wait(t,10);
340  }
341 
342  // Display task information
343  task.print() ;
344  task.kill();
345 
346  std::cout <<"Final robot position with respect to the object frame:\n";
347  cMo.print();
348 
349  if (opt_display && opt_click_allowed)
350  {
351  // suppressed for automate test
352  std::cout << "Click in the internal view window to end..." << std::endl;
353  vpDisplay::getClick(Iint) ;
354  }
355  return 0;
356  }
357  catch(vpException e) {
358  std::cout << "Catch a ViSP exception: " << e << std::endl;
359  return 1;
360  }
361 }
362 #else
363 int
364 main()
365 {
366  vpERROR_TRACE("You do not have X11, OpenCV or GDI display functionalities or threading capabilities...");
367 }
368 
369 #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:395
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: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)
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:1994
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:190
Class that defines what is a point.
Definition: vpPoint.h:65
void kill()
Definition: vpServo.cpp:189
Initialize the velocity controller.
Definition: vpRobot.h:70
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:370
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
Simulator of Irisa's Viper S850 robot named Viper850.
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:251
virtual bool getClick(bool blocking=true)=0
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &v)
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