Visual Servoing Platform  version 3.1.0
servoSimuViper850FourPoints2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Simulation of a 2D visual servoing using 4 points with polar
33  * coordinates as visual feature.
34  *
35  * Authors:
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
56 #include <visp3/core/vpConfig.h>
57 #include <visp3/core/vpDebug.h>
58 
59 #if ((defined(_WIN32) && !defined(WINRT_8_0)) || defined(VISP_HAVE_PTHREAD)) && \
60  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GDI))
61 
62 // We need to use threading capabilities. Thus on Unix-like
63 // platforms, the libpthread third-party library need to be
64 // installed. On Windows, we use the native threading capabilities.
65 
66 #include <stdio.h>
67 #include <stdlib.h>
68 
69 #include <visp3/core/vpCameraParameters.h>
70 #include <visp3/core/vpHomogeneousMatrix.h>
71 #include <visp3/core/vpImage.h>
72 #include <visp3/core/vpImagePoint.h>
73 #include <visp3/core/vpIoTools.h>
74 #include <visp3/core/vpMath.h>
75 #include <visp3/core/vpMeterPixelConversion.h>
76 #include <visp3/gui/vpDisplayGDI.h>
77 #include <visp3/gui/vpDisplayGTK.h>
78 #include <visp3/gui/vpDisplayX.h>
79 #include <visp3/io/vpParseArgv.h>
80 #include <visp3/robot/vpSimulatorViper850.h>
81 #include <visp3/visual_features/vpFeatureBuilder.h>
82 #include <visp3/visual_features/vpFeaturePoint.h>
83 #include <visp3/vs/vpServo.h>
84 
85 // List of allowed command line options
86 #define GETOPTARGS "cdh"
87 
88 void usage(const char *name, const char *badparam);
89 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
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':
146  click_allowed = false;
147  break;
148  case 'd':
149  display = false;
150  break;
151  case 'h':
152  usage(argv[0], NULL);
153  return false;
154  break;
155 
156  default:
157  usage(argv[0], optarg_);
158  return false;
159  break;
160  }
161  }
162 
163  if ((c == 1) || (c == -1)) {
164  // standalone param or error
165  usage(argv[0], NULL);
166  std::cerr << "ERROR: " << std::endl;
167  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
168  return false;
169  }
170 
171  return true;
172 }
173 
174 int main(int argc, const char **argv)
175 {
176  std::string appveyor_threading = "";
177  try {
178  appveyor_threading = vpIoTools::getenv("APPVEYOR_THREADING");
179  } catch (...) {
180  }
181 
182  if (appveyor_threading == "true") {
183  try {
184  bool opt_click_allowed = true;
185  bool opt_display = true;
186 
187  // Read the command line options
188  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
189  exit(-1);
190  }
191 
192 // We open two displays, one for the internal camera view, the other one for
193 // the external view, using either X11, GTK or GDI.
194 #if defined VISP_HAVE_X11
195  vpDisplayX displayInt;
196 #elif defined VISP_HAVE_GDI
197  vpDisplayGDI displayInt;
198 #elif defined VISP_HAVE_OPENCV
199  vpDisplayOpenCV displayInt;
200 #endif
201 
202  // open a display for the visualization
203 
204  vpImage<unsigned char> Iint(480, 640, 255);
205 
206  if (opt_display) {
207  displayInt.init(Iint, 700, 0, "Internal view");
208  }
209 
210  vpServo task;
211 
212  std::cout << std::endl;
213  std::cout << "----------------------------------------------" << std::endl;
214  std::cout << " Test program for vpServo " << std::endl;
215  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
216  std::cout << " Simulation " << std::endl;
217  std::cout << " task : servo 4 points " << std::endl;
218  std::cout << "----------------------------------------------" << std::endl;
219  std::cout << std::endl;
220 
221  // sets the initial camera location
222  vpHomogeneousMatrix cMo(-0.05, -0.05, 0.7, vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
223 
224  // sets the point coordinates in the object frame
225  vpPoint point[4];
226  point[0].setWorldCoordinates(-0.045, -0.045, 0);
227  point[3].setWorldCoordinates(-0.045, 0.045, 0);
228  point[2].setWorldCoordinates(0.045, 0.045, 0);
229  point[1].setWorldCoordinates(0.045, -0.045, 0);
230 
231  // computes the point coordinates in the camera frame and its 2D
232  // coordinates
233  for (unsigned int i = 0; i < 4; i++)
234  point[i].track(cMo);
235 
236  // sets the desired position of the point
237  vpFeaturePoint p[4];
238  for (unsigned int i = 0; i < 4; i++)
239  vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
240 
241  // sets the desired position of the feature point s*
242  vpFeaturePoint pd[4];
243 
244  // Desired pose
246 
247  // Projection of the points
248  for (unsigned int i = 0; i < 4; i++)
249  point[i].track(cdMo);
250 
251  for (unsigned int i = 0; i < 4; i++)
252  vpFeatureBuilder::create(pd[i], point[i]);
253 
254  // define the task
255  // - we want an eye-in-hand control law
256  // - articular velocity are computed
259 
260  // - we want to see a point on a point
261  for (unsigned int i = 0; i < 4; i++)
262  task.addFeature(p[i], pd[i]);
263 
264  // set the gain
265  task.setLambda(0.8);
266 
267  // Declaration of the robot
268  vpSimulatorViper850 robot(opt_display);
269 
270  // Initialise the robot and especially the camera
273 
274  // Initialise the object for the display part
276 
277  // Initialise the position of the object relative to the pose of the
278  // robot's camera
279  robot.initialiseObjectRelativeToCamera(cMo);
280 
281  // Set the desired position (for the display part)
282  robot.setDesiredCameraPosition(cdMo);
283 
284  // Get the internal robot's camera parameters
285  vpCameraParameters cam;
286  robot.getCameraParameters(cam, Iint);
287 
288  if (opt_display) {
289  // Get the internal view
290  vpDisplay::display(Iint);
291  robot.getInternalView(Iint);
292  vpDisplay::flush(Iint);
293  }
294 
295  // Display task information
296  task.print();
297 
298  unsigned int iter = 0;
299  // loop
300  while (iter++ < 500) {
301  std::cout << "---------------------------------------------" << iter << std::endl;
302  vpColVector v;
303 
304  // Get the Time at the beginning of the loop
305  double t = vpTime::measureTimeMs();
306 
307  // Get the current pose of the camera
308  cMo = robot.get_cMo();
309 
310  if (iter == 1) {
311  std::cout << "Initial robot position with respect to the object frame:\n";
312  cMo.print();
313  }
314 
315  // new point position
316  for (unsigned int i = 0; i < 4; i++) {
317  point[i].track(cMo);
318  // retrieve x,y and Z of the vpPoint structure
319  vpFeatureBuilder::create(p[i], point[i]);
320  }
321 
322  if (opt_display) {
323  // Get the internal view and display it
324  vpDisplay::display(Iint);
325  robot.getInternalView(Iint);
326  vpDisplay::flush(Iint);
327  }
328 
329  if (opt_display && opt_click_allowed && iter == 1) {
330  // suppressed for automate test
331  std::cout << "Click in the internal view window to continue..." << std::endl;
332  vpDisplay::getClick(Iint);
333  }
334 
335  // compute the control law
336  v = task.computeControlLaw();
337 
338  // send the camera velocity to the controller
340 
341  std::cout << "|| s - s* || " << (task.getError()).sumSquare() << std::endl;
342 
343  // The main loop has a duration of 10 ms at minimum
344  vpTime::wait(t, 10);
345  }
346 
347  // Display task information
348  task.print();
349  task.kill();
350 
351  std::cout << "Final robot position with respect to the object frame:\n";
352  cMo.print();
353 
354  if (opt_display && opt_click_allowed) {
355  // suppressed for automate test
356  std::cout << "Click in the internal view window to end..." << std::endl;
357  vpDisplay::getClick(Iint);
358  }
359  return 0;
360  } catch (vpException &e) {
361  std::cout << "Catch a ViSP exception: " << e << std::endl;
362  return 1;
363  }
364  }
365 }
366 #else
367 int main()
368 {
369  vpERROR_TRACE("You do not have X11, OpenCV or GDI display functionalities "
370  "or threading capabilities...");
371 }
372 
373 #endif
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Implementation of an homogeneous matrix and operations on such kind of matrices.
#define vpERROR_TRACE
Definition: vpDebug.h:393
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
static std::string getenv(const char *env)
Definition: vpIoTools.cpp:266
void print() const
Print the matrix as a pose vector .
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
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)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
Class that defines what is a point.
Definition: vpPoint.h:58
void kill()
Definition: vpServo.cpp:192
Initialize the velocity controller.
Definition: vpRobot.h:67
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:406
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:574
static double rad(double deg)
Definition: vpMath.h:102
Simulator of Irisa&#39;s Viper S850 robot named Viper850.
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:113
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:313
vpColVector getError() const
Definition: vpServo.h:282
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223