Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimuViper850FourPoints2DCamVelocity.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
36 
53 #include <visp3/core/vpConfig.h>
54 #include <visp3/core/vpDebug.h>
55 
56 #if defined(VISP_HAVE_THREADS) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GDI)) \
57  && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
58 
59 // We need to use threading capabilities. Thus on Unix-like
60 // platforms, the libpthread third-party library need to be
61 // installed. On Windows, we use the native threading capabilities.
62 
63 #include <stdio.h>
64 #include <stdlib.h>
65 
66 #include <visp3/core/vpCameraParameters.h>
67 #include <visp3/core/vpHomogeneousMatrix.h>
68 #include <visp3/core/vpImage.h>
69 #include <visp3/core/vpImagePoint.h>
70 #include <visp3/core/vpIoTools.h>
71 #include <visp3/core/vpMath.h>
72 #include <visp3/core/vpMeterPixelConversion.h>
73 #include <visp3/gui/vpDisplayGDI.h>
74 #include <visp3/gui/vpDisplayGTK.h>
75 #include <visp3/gui/vpDisplayX.h>
76 #include <visp3/io/vpParseArgv.h>
77 #include <visp3/robot/vpSimulatorViper850.h>
78 #include <visp3/visual_features/vpFeatureBuilder.h>
79 #include <visp3/visual_features/vpFeaturePoint.h>
80 #include <visp3/vs/vpServo.h>
81 
82 // List of allowed command line options
83 #define GETOPTARGS "cdh"
84 
85 #ifdef ENABLE_VISP_NAMESPACE
86 using namespace VISP_NAMESPACE_NAME;
87 #endif
88 
89 void usage(const char *name, const char *badparam);
90 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
91 
100 void usage(const char *name, const char *badparam)
101 {
102  fprintf(stdout, "\n\
103 Tests a control law with the following characteristics:\n\
104 - eye-in-hand control\n\
105 - articular velocity are computed\n\
106 - servo on 4 points,\n\
107 - internal and external camera view displays.\n\
108  \n\
109 SYNOPSIS\n\
110  %s [-c] [-d] [-h]\n",
111  name);
112 
113  fprintf(stdout, "\n\
114 OPTIONS: Default\n\
115  -c\n\
116  Disable the mouse click. Useful to automate the \n\
117  execution of this program without human intervention.\n\
118  \n\
119  -d \n\
120  Turn off the display.\n\
121  \n\
122  -h\n\
123  Print the help.\n");
124 
125  if (badparam)
126  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
127 }
140 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
141 {
142  const char *optarg_;
143  int c;
144  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
145 
146  switch (c) {
147  case 'c':
148  click_allowed = false;
149  break;
150  case 'd':
151  display = false;
152  break;
153  case 'h':
154  usage(argv[0], nullptr);
155  return false;
156  break;
157 
158  default:
159  usage(argv[0], optarg_);
160  return false;
161  break;
162  }
163  }
164 
165  if ((c == 1) || (c == -1)) {
166  // standalone param or error
167  usage(argv[0], nullptr);
168  std::cerr << "ERROR: " << std::endl;
169  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
170  return false;
171  }
172 
173  return true;
174 }
175 
176 int main(int argc, const char **argv)
177 {
178  try {
179  bool opt_click_allowed = true;
180  bool opt_display = true;
181 
182  // Read the command line options
183  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
184  return EXIT_FAILURE;
185  }
186 
187  // We open two displays, one for the internal camera view, the other one for
188  // the external view, using either X11, GTK or GDI.
189 #if defined(VISP_HAVE_X11)
190  vpDisplayX displayInt;
191 #elif defined(VISP_HAVE_GDI)
192  vpDisplayGDI displayInt;
193 #elif defined(HAVE_OPENCV_HIGHGUI)
194  vpDisplayOpenCV displayInt;
195 #endif
196 
197  // open a display for the visualization
198 
199  vpImage<unsigned char> Iint(480, 640, 255);
200 
201  if (opt_display) {
202  displayInt.init(Iint, 700, 0, "Internal view");
203  }
204 
205  vpServo task;
206 
207  std::cout << std::endl;
208  std::cout << "----------------------------------------------" << std::endl;
209  std::cout << " Test program for vpServo " << std::endl;
210  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
211  std::cout << " Simulation " << std::endl;
212  std::cout << " task : servo 4 points " << std::endl;
213  std::cout << "----------------------------------------------" << std::endl;
214  std::cout << std::endl;
215 
216  // sets the initial camera location
217  vpHomogeneousMatrix cMo(-0.05, -0.05, 0.7, vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
218 
219  // sets the point coordinates in the object frame
220  vpPoint point[4];
221  point[0].setWorldCoordinates(-0.045, -0.045, 0);
222  point[3].setWorldCoordinates(-0.045, 0.045, 0);
223  point[2].setWorldCoordinates(0.045, 0.045, 0);
224  point[1].setWorldCoordinates(0.045, -0.045, 0);
225 
226  // computes the point coordinates in the camera frame and its 2D
227  // coordinates
228  for (unsigned int i = 0; i < 4; i++)
229  point[i].track(cMo);
230 
231  // sets the desired position of the point
232  vpFeaturePoint p[4];
233  for (unsigned int i = 0; i < 4; i++)
234  vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
235 
236  // sets the desired position of the feature point s*
237  vpFeaturePoint pd[4];
238 
239  // Desired pose
241 
242  // Projection of the points
243  for (unsigned int i = 0; i < 4; i++)
244  point[i].track(cdMo);
245 
246  for (unsigned int i = 0; i < 4; i++)
247  vpFeatureBuilder::create(pd[i], point[i]);
248 
249  // define the task
250  // - we want an eye-in-hand control law
251  // - articular velocity are computed
254 
255  // - we want to see a point on a point
256  for (unsigned int i = 0; i < 4; i++)
257  task.addFeature(p[i], pd[i]);
258 
259  // set the gain
260  task.setLambda(0.8);
261 
262  // Declaration of the robot
263  vpSimulatorViper850 robot(opt_display);
264 
265  // Initialise the robot and especially the camera
268 
269  // Initialise the object for the display part
271 
272  // Initialise the position of the object relative to the pose of the
273  // robot's camera
274  robot.initialiseObjectRelativeToCamera(cMo);
275 
276  // Set the desired position (for the display part)
277  robot.setDesiredCameraPosition(cdMo);
278 
279  // Get the internal robot's camera parameters
280  vpCameraParameters cam;
281  robot.getCameraParameters(cam, Iint);
282 
283  if (opt_display) {
284  // Get the internal view
285  vpDisplay::display(Iint);
286  robot.getInternalView(Iint);
287  vpDisplay::flush(Iint);
288  }
289 
290  // Display task information
291  task.print();
292 
293  unsigned int iter = 0;
294  // loop
295  while (iter++ < 500) {
296  std::cout << "---------------------------------------------" << iter << std::endl;
297  vpColVector v;
298 
299  // Get the Time at the beginning of the loop
300  double t = vpTime::measureTimeMs();
301 
302  // Get the current pose of the camera
303  cMo = robot.get_cMo();
304 
305  if (iter == 1) {
306  std::cout << "Initial robot position with respect to the object frame:\n";
307  cMo.print();
308  }
309 
310  // new point position
311  for (unsigned int i = 0; i < 4; i++) {
312  point[i].track(cMo);
313  // retrieve x,y and Z of the vpPoint structure
314  vpFeatureBuilder::create(p[i], point[i]);
315  }
316 
317  if (opt_display) {
318  // Get the internal view and display it
319  vpDisplay::display(Iint);
320  robot.getInternalView(Iint);
321  vpDisplay::flush(Iint);
322  }
323 
324  if (opt_display && opt_click_allowed && iter == 1) {
325  // suppressed for automate test
326  std::cout << "Click in the internal view window to continue..." << std::endl;
327  vpDisplay::getClick(Iint);
328  }
329 
330  // compute the control law
331  v = task.computeControlLaw();
332 
333  // send the camera velocity to the controller
335 
336  std::cout << "|| s - s* || " << (task.getError()).sumSquare() << std::endl;
337 
338  // The main loop has a duration of 10 ms at minimum
339  vpTime::wait(t, 10);
340  }
341 
342  // Display task information
343  task.print();
344 
345  std::cout << "Final robot position with respect to the object frame:\n";
346  cMo.print();
347 
348  if (opt_display && opt_click_allowed) {
349  // suppressed for automate test
350  std::cout << "Click in the internal view window to end..." << std::endl;
351  vpDisplay::getClick(Iint);
352  }
353  return EXIT_SUCCESS;
354  }
355  catch (const vpException &e) {
356  std::cout << "Catch a ViSP exception: " << e << std::endl;
357  return EXIT_FAILURE;
358  }
359 }
360 #elif !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
361 int main()
362 {
363  std::cout << "You do not have X11, or GDI (Graphical Device Interface) of OpenCV functionalities to display images..."
364  << std::endl;
365  std::cout << "Tip if you are on a unix-like system:" << std::endl;
366  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
367  std::cout << "Tip if you are on a windows-like system:" << std::endl;
368  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
369  return EXIT_SUCCESS;
370 }
371 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
372 int main()
373 {
374  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
375  return EXIT_SUCCESS;
376 }
377 #else
378 int main()
379 {
380  std::cout << "You do not have threading capabilities" << std::endl;
381  std::cout << "Tip:" << std::endl;
382  std::cout << "- Install pthread, configure again ViSP using cmake and build again this example" << std::endl;
383  return EXIT_SUCCESS;
384 }
385 #endif
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
static double rad(double deg)
Definition: vpMath.h:129
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:111
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:67
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:202
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:986
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ DESIRED
Definition: vpServo.h:208
@ TOOL_PTGREY_FLEA2_CAMERA
Definition: vpViper850.h:122
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()