ViSP  3.0.0
servoSimuViper850FourPoints2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Simulation of a 2D visual servoing using 4 points with polar
32  * coordinates as visual feature.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
56 #include <visp3/core/vpDebug.h>
57 #include <visp3/core/vpConfig.h>
58 
59 #if (defined(_WIN32) || defined(VISP_HAVE_PTHREAD)) && (defined (VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GDI))
60 
61 // We need to use threading capabilities. Thus on Unix-like
62 // platforms, the libpthread third-party library need to be
63 // installed. On Windows, we use the native threading capabilities.
64 
65 #include <stdlib.h>
66 #include <stdio.h>
67 
68 #include <visp3/core/vpCameraParameters.h>
69 #include <visp3/gui/vpDisplayX.h>
70 #include <visp3/gui/vpDisplayGTK.h>
71 #include <visp3/gui/vpDisplayGDI.h>
72 #include <visp3/visual_features/vpFeatureBuilder.h>
73 #include <visp3/visual_features/vpFeaturePoint.h>
74 #include <visp3/core/vpHomogeneousMatrix.h>
75 #include <visp3/core/vpImage.h>
76 #include <visp3/core/vpImagePoint.h>
77 #include <visp3/core/vpIoTools.h>
78 #include <visp3/core/vpMath.h>
79 #include <visp3/core/vpMeterPixelConversion.h>
80 #include <visp3/io/vpParseArgv.h>
81 #include <visp3/vs/vpServo.h>
82 #include <visp3/robot/vpSimulatorViper850.h>
83 
84 // List of allowed command line options
85 #define GETOPTARGS "cdh"
86 
87 void usage(const char *name, const char *badparam);
88 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
89 
98 void usage(const char *name, const char *badparam)
99 {
100  fprintf(stdout, "\n\
101 Tests a control law with the following characteristics:\n\
102 - eye-in-hand control\n\
103 - articular velocity are computed\n\
104 - servo on 4 points,\n\
105 - internal and external camera view displays.\n\
106  \n\
107 SYNOPSIS\n\
108  %s [-c] [-d] [-h]\n", name);
109 
110  fprintf(stdout, "\n\
111 OPTIONS: Default\n\
112  -c\n\
113  Disable the mouse click. Useful to automaze the \n\
114  execution of this program without humain intervention.\n\
115  \n\
116  -d \n\
117  Turn off the display.\n\
118  \n\
119  -h\n\
120  Print the help.\n");
121 
122  if (badparam)
123  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
124 }
137 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
138 {
139  const char *optarg_;
140  int c;
141  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
142 
143  switch (c) {
144  case 'c': click_allowed = false; break;
145  case 'd': display = false; break;
146  case 'h': usage(argv[0], NULL); return false; break;
147 
148  default:
149  usage(argv[0], optarg_);
150  return false; break;
151  }
152  }
153 
154  if ((c == 1) || (c == -1)) {
155  // standalone param or error
156  usage(argv[0], NULL);
157  std::cerr << "ERROR: " << std::endl;
158  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
159  return false;
160  }
161 
162  return true;
163 }
164 
165 int
166 main(int argc, const char ** argv)
167 {
168  try {
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  // open a display for the visualization
188 
189  vpImage<unsigned char> Iint(480, 640, 255);
190 
191  if (opt_display) {
192  displayInt.init(Iint,700,0, "Internal view") ;
193  }
194 
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 
212  // sets the point coordinates in the object frame
213  vpPoint point[4] ;
214  point[0].setWorldCoordinates(-0.045,-0.045,0) ;
215  point[3].setWorldCoordinates(-0.045,0.045,0) ;
216  point[2].setWorldCoordinates(0.045,0.045,0) ;
217  point[1].setWorldCoordinates(0.045,-0.045,0) ;
218 
219  // computes the point coordinates in the camera frame and its 2D coordinates
220  for (unsigned int i = 0 ; i < 4 ; i++)
221  point[i].track(cMo) ;
222 
223  // sets the desired position of the point
224  vpFeaturePoint p[4] ;
225  for (unsigned int i = 0 ; i < 4 ; i++)
226  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
227 
228  // sets the desired position of the feature point s*
229  vpFeaturePoint pd[4] ;
230 
231  //Desired pose
233 
234  // Projection of the points
235  for (unsigned int i = 0 ; i < 4 ; i++)
236  point[i].track(cdMo);
237 
238  for (unsigned int i = 0 ; i < 4 ; i++)
239  vpFeatureBuilder::create(pd[i], point[i]);
240 
241  // define the task
242  // - we want an eye-in-hand control law
243  // - articular velocity are computed
246 
247  // - we want to see a point on a point
248  for (unsigned int i = 0 ; i < 4 ; i++)
249  task.addFeature(p[i],pd[i]) ;
250 
251  // set the gain
252  task.setLambda(0.8) ;
253 
254  // Declaration of the robot
255  vpSimulatorViper850 robot(opt_display);
256 
257  // Initialise the robot and especially the camera
260 
261  // Initialise the object for the display part
263 
264  // Initialise the position of the object relative to the pose of the robot's camera
265  robot.initialiseObjectRelativeToCamera(cMo);
266 
267  // Set the desired position (for the display part)
268  robot.setDesiredCameraPosition(cdMo);
269 
270  // Get the internal robot's camera parameters
271  vpCameraParameters cam;
272  robot.getCameraParameters(cam,Iint);
273 
274  if (opt_display)
275  {
276  //Get the internal view
277  vpDisplay::display(Iint);
278  robot.getInternalView(Iint);
279  vpDisplay::flush(Iint);
280  }
281 
282  // Display task information
283  task.print() ;
284 
285  unsigned int iter=0 ;
286  // loop
287  while(iter++<500)
288  {
289  std::cout << "---------------------------------------------" << iter <<std::endl ;
290  vpColVector v ;
291 
292  //Get the Time at the beginning of the loop
293  double t = vpTime::measureTimeMs();
294 
295  //Get the current pose of the camera
296  cMo = robot.get_cMo();
297 
298  if (iter==1) {
299  std::cout <<"Initial robot position with respect to the object frame:\n";
300  cMo.print();
301  }
302 
303  // new point position
304  for (unsigned int i = 0 ; i < 4 ; i++)
305  {
306  point[i].track(cMo) ;
307  //retrieve x,y and Z of the vpPoint structure
308  vpFeatureBuilder::create(p[i],point[i]) ;
309  }
310 
311  if (opt_display)
312  {
313  // Get the internal view and display it
314  vpDisplay::display(Iint) ;
315  robot.getInternalView(Iint);
316  vpDisplay::flush(Iint);
317  }
318 
319  if (opt_display && opt_click_allowed && iter == 1)
320  {
321  // suppressed for automate test
322  std::cout << "Click in the internal view window to continue..." << std::endl;
323  vpDisplay::getClick(Iint) ;
324  }
325 
326  // compute the control law
327  v = task.computeControlLaw() ;
328 
329  // send the camera velocity to the controller
331 
332  std::cout << "|| s - s* || " << ( task.getError() ).sumSquare() <<std::endl ;
333 
334  // The main loop has a duration of 10 ms at minimum
335  vpTime::wait(t,10);
336  }
337 
338  // Display task information
339  task.print() ;
340  task.kill();
341 
342  std::cout <<"Final robot position with respect to the object frame:\n";
343  cMo.print();
344 
345  if (opt_display && opt_click_allowed)
346  {
347  // suppressed for automate test
348  std::cout << "Click in the internal view window to end..." << std::endl;
349  vpDisplay::getClick(Iint) ;
350  }
351  return 0;
352  }
353  catch(vpException e) {
354  std::cout << "Catch a ViSP exception: " << e << std::endl;
355  return 1;
356  }
357 }
358 #else
359 int
360 main()
361 {
362  vpERROR_TRACE("You do not have X11, OpenCV or GDI display functionalities or threading capabilities...");
363 }
364 
365 #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)
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Perspective projection without distortion model.
Implementation of an homogeneous matrix and operations on such kind of matrices.
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.
#define vpERROR_TRACE
Definition: vpDebug.h:391
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
error that can be emited by ViSP classes.
Definition: vpException.h:73
void track(const vpHomogeneousMatrix &cMo)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
Class that defines what is a point.
Definition: vpPoint.h:59
void kill()
Definition: vpServo.cpp:186
Initialize the velocity controller.
Definition: vpRobot.h:68
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
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:390
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:519
static double rad(double deg)
Definition: vpMath.h:104
Simulator of Irisa&#39;s Viper S850 robot named Viper850.
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:248
virtual bool getClick(bool blocking=true)=0
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void print() const
Print the matrix as a pose vector .
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:217