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