Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
servoSimuAfma6FourPoints2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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  try {
177  bool opt_click_allowed = true;
178  bool opt_display = true;
179 
180  // Read the command line options
181  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
182  exit(-1);
183  }
184 
185  // We open two displays, one for the internal camera view, the other one for
186  // the external view, using either X11, GTK or GDI.
187 #if defined VISP_HAVE_X11
188  vpDisplayX displayInt;
189 #elif defined VISP_HAVE_GDI
190  vpDisplayGDI displayInt;
191 #elif defined VISP_HAVE_OPENCV
192  vpDisplayOpenCV displayInt;
193 #endif
194 
195  vpImage<unsigned char> Iint(480, 640, 255);
196 
197  if (opt_display) {
198  // open a display for the visualization
199  displayInt.init(Iint, 700, 0, "Internal view");
200  }
201 
202  vpServo task;
203 
204  std::cout << std::endl;
205  std::cout << "----------------------------------------------" << std::endl;
206  std::cout << " Test program for vpServo " << std::endl;
207  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
208  std::cout << " Simulation " << std::endl;
209  std::cout << " task : servo 4 points " << std::endl;
210  std::cout << "----------------------------------------------" << std::endl;
211  std::cout << std::endl;
212 
213  // sets the initial camera location
214  vpHomogeneousMatrix cMo(-0.05, -0.05, 0.7, vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
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
224  // coordinates
225  for (unsigned int i = 0; i < 4; i++)
226  point[i].track(cMo);
227 
228  // sets the desired position of the point
229  vpFeaturePoint p[4];
230  for (unsigned int i = 0; i < 4; i++)
231  vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
232 
233  // sets the desired position of the feature point s*
234  vpFeaturePoint pd[4];
235 
236  // Desired pose
238 
239  // Projection of the points
240  for (unsigned int i = 0; i < 4; i++)
241  point[i].track(cdMo);
242 
243  for (unsigned int i = 0; i < 4; i++)
244  vpFeatureBuilder::create(pd[i], point[i]);
245 
246  // define the task
247  // - we want an eye-in-hand control law
248  // - articular velocity are computed
251 
252  // we want to see a point on a point
253  for (unsigned int i = 0; i < 4; i++)
254  task.addFeature(p[i], pd[i]);
255 
256  // set the gain
257  task.setLambda(0.8);
258 
259  // Declaration of the robot
260  vpSimulatorAfma6 robot(opt_display);
261 
262  // Initialise the robot and especially the camera
265 
266  // Initialise the object for the display part*/
268 
269  // Initialise the position of the object relative to the pose of the
270  // robot's camera
271  robot.initialiseObjectRelativeToCamera(cMo);
272 
273  // Set the desired position (for the displaypart)
274  robot.setDesiredCameraPosition(cdMo);
275 
276  // Get the internal robot's camera parameters
277  vpCameraParameters cam;
278  robot.getCameraParameters(cam, Iint);
279 
280  if (opt_display) {
281  // Get the internal view
282  vpDisplay::display(Iint);
283  robot.getInternalView(Iint);
284  vpDisplay::flush(Iint);
285  }
286 
287  // Display task information
288  task.print();
289 
290  unsigned int iter = 0;
291  vpTRACE("\t loop");
292  while (iter++ < 500) {
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  point[i].track(cMo);
310  // retrieve x,y and Z of the vpPoint structure
311  vpFeatureBuilder::create(p[i], point[i]);
312  }
313 
314  if (opt_display) {
315  // Get the internal view and display it
316  vpDisplay::display(Iint);
317  robot.getInternalView(Iint);
318  vpDisplay::flush(Iint);
319  }
320 
321  if (opt_display && opt_click_allowed && iter == 1) {
322  // suppressed for automate test
323  std::cout << "Click in the internal view window to continue..." << std::endl;
324  vpDisplay::getClick(Iint);
325  }
326 
327  // compute the control law
328  v = task.computeControlLaw();
329 
330  // send the camera velocity to the controller
332 
333  std::cout << "|| s - s* || " << (task.getError()).sumSquare() << std::endl;
334 
335  // The main loop has a duration of 10 ms at minimum
336  vpTime::wait(t, 10);
337  }
338 
339  // Display task information
340  task.print();
341  task.kill();
342 
343  std::cout << "Final robot position with respect to the object frame:\n";
344  cMo.print();
345 
346  if (opt_display && opt_click_allowed) {
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 EXIT_SUCCESS;
352  }
353  catch (const vpException &e) {
354  std::cout << "Catch a ViSP exception: " << e << std::endl;
355  return EXIT_FAILURE;
356  }
357  return EXIT_SUCCESS;
358 }
359 #else
360 int main()
361 {
362 #if (!(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)))
363  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..." << std::endl;
364  std::cout << "Tip if you are on a unix-like system:" << std::endl;
365  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
366  std::cout << "Tip if you are on a windows-like system:" << std::endl;
367  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
368 #else
369  std::cout << "You do not have threading capabilities" << std::endl;
370  std::cout << "Tip:" << std::endl;
371  std::cout << "- Install pthread, configure again ViSP using cmake and build again this example" << std::endl;
372 #endif
373  return EXIT_SUCCESS;
374 }
375 
376 #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.
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
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 getError() const
Definition: vpServo.h:282
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
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:223