ViSP  3.0.0
servoSimuAfma6FourPoints2DCamVelocity.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 cartesian
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/vpSimulatorAfma6.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  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  vpServo task;
195 
196  std::cout << std::endl ;
197  std::cout << "----------------------------------------------" << std::endl ;
198  std::cout << " Test program for vpServo " <<std::endl ;
199  std::cout << " Eye-in-hand task control, articular velocity are computed"
200  << std::endl ;
201  std::cout << " Simulation " << std::endl ;
202  std::cout << " task : servo 4 points " << std::endl ;
203  std::cout << "----------------------------------------------" << std::endl ;
204  std::cout << std::endl ;
205 
206  // sets the initial camera location
207  vpHomogeneousMatrix cMo(-0.05,-0.05,0.7,
208  vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
209 
210  // sets the point coordinates in the object frame
211  vpPoint point[4] ;
212  point[0].setWorldCoordinates(-0.045,-0.045,0) ;
213  point[3].setWorldCoordinates(-0.045,0.045,0) ;
214  point[2].setWorldCoordinates(0.045,0.045,0) ;
215  point[1].setWorldCoordinates(0.045,-0.045,0) ;
216 
217  // computes the point coordinates in the camera frame and its 2D coordinates
218  for (unsigned int i = 0 ; i < 4 ; i++)
219  point[i].track(cMo) ;
220 
221  // sets the desired position of the point
222  vpFeaturePoint p[4] ;
223  for (unsigned int i = 0 ; i < 4 ; i++)
224  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
225 
226  // sets the desired position of the feature point s*
227  vpFeaturePoint pd[4] ;
228 
229  // Desired pose
231 
232  // Projection of the points
233  for (unsigned int i = 0 ; i < 4 ; i++)
234  point[i].track(cdMo);
235 
236  for (unsigned int i = 0 ; i < 4 ; i++)
237  vpFeatureBuilder::create(pd[i], point[i]);
238 
239  // define the task
240  // - we want an eye-in-hand control law
241  // - articular velocity are computed
244 
245  // we want to see a point on a point
246  for (unsigned int i = 0 ; i < 4 ; i++)
247  task.addFeature(p[i],pd[i]) ;
248 
249  // set the gain
250  task.setLambda(0.8) ;
251 
252  // Declaration of the robot
253  vpSimulatorAfma6 robot(opt_display);
254 
255  // Initialise the robot and especially the camera
258 
259  // Initialise the object for the display part*/
261 
262  // Initialise the position of the object relative to the pose of the robot's camera
263  robot.initialiseObjectRelativeToCamera(cMo);
264 
265  // Set the desired position (for the displaypart)
266  robot.setDesiredCameraPosition(cdMo);
267 
268  // Get the internal robot's camera parameters
269  vpCameraParameters cam;
270  robot.getCameraParameters(cam,Iint);
271 
272  if (opt_display)
273  {
274  //Get the internal view
275  vpDisplay::display(Iint);
276  robot.getInternalView(Iint);
277  vpDisplay::flush(Iint);
278  }
279 
280  // Display task information
281  task.print() ;
282 
283  unsigned int iter=0 ;
284  vpTRACE("\t loop") ;
285  while(iter++<500)
286  {
287  std::cout << "---------------------------------------------" << iter <<std::endl ;
288  vpColVector v ;
289 
290  // Get the Time at the beginning of the loop
291  double t = vpTime::measureTimeMs();
292 
293  // Get the current pose of the camera
294  cMo = robot.get_cMo();
295 
296  if (iter==1) {
297  std::cout <<"Initial robot position with respect to the object frame:\n";
298  cMo.print();
299  }
300 
301  // new point position
302  for (unsigned int i = 0 ; i < 4 ; i++)
303  {
304  point[i].track(cMo) ;
305  // retrieve x,y and Z of the vpPoint structure
306  vpFeatureBuilder::create(p[i],point[i]) ;
307  }
308 
309  if (opt_display)
310  {
311  // Get the internal view and display it
312  vpDisplay::display(Iint) ;
313  robot.getInternalView(Iint);
314  vpDisplay::flush(Iint);
315  }
316 
317  if (opt_display && opt_click_allowed && iter == 1)
318  {
319  // suppressed for automate test
320  std::cout << "Click in the internal view window to continue..." << std::endl;
321  vpDisplay::getClick(Iint) ;
322  }
323 
324  // compute the control law
325  v = task.computeControlLaw() ;
326 
327  // send the camera velocity to the controller
329 
330  std::cout << "|| s - s* || " << ( task.getError() ).sumSquare() <<std::endl ;
331 
332  // The main loop has a duration of 10 ms at minimum
333  vpTime::wait(t,10);
334  }
335 
336  // Display task information
337  task.print() ;
338  task.kill();
339 
340  std::cout <<"Final robot position with respect to the object frame:\n";
341  cMo.print();
342 
343  if (opt_display && opt_click_allowed)
344  {
345  // suppressed for automate test
346  std::cout << "Click in the internal view window to end..." << std::endl;
347  vpDisplay::getClick(Iint) ;
348  }
349  return 0;
350  }
351  catch(vpException e) {
352  std::cout << "Catch a ViSP exception: " << e << std::endl;
353  return 1;
354  }
355 }
356 #else
357 int
358 main()
359 {
360  vpERROR_TRACE("You do not have X11, OpenCV or GDI display functionalities or threading capabilities...");
361 }
362 
363 #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
#define vpTRACE
Definition: vpDebug.h:414
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
Simulator of Irisa&#39;s gantry robot named Afma6.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:519
static double rad(double deg)
Definition: vpMath.h:104
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