Visual Servoing Platform  version 3.6.1 under development (2024-04-26)
servoSimuAfma6FourPoints2DCamVelocity.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 cartesian
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/vpSimulatorAfma6.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 void usage(const char *name, const char *badparam);
86 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
87 
96 void usage(const char *name, const char *badparam)
97 {
98  fprintf(stdout, "\n\
99 Tests a control law with the following characteristics:\n\
100  - eye-in-hand control\n\
101  - articular velocity are computed\n\
102  - servo on 4 points,\n\
103  - internal and external camera view displays.\n\
104  \n\
105 SYNOPSIS\n\
106  %s [-c] [-d] [-h]\n",
107  name);
108 
109  fprintf(stdout, "\n\
110 OPTIONS: Default\n\
111  -c\n\
112  Disable the mouse click. Useful to automate the \n\
113  execution of this program without human intervention.\n\
114  \n\
115  -d \n\
116  Turn off the display.\n\
117  \n\
118  -h\n\
119  Print the help.\n");
120 
121  if (badparam)
122  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
123 }
136 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
137 {
138  const char *optarg_;
139  int c;
140  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
141 
142  switch (c) {
143  case 'c':
144  click_allowed = false;
145  break;
146  case 'd':
147  display = false;
148  break;
149  case 'h':
150  usage(argv[0], nullptr);
151  return false;
152  break;
153 
154  default:
155  usage(argv[0], optarg_);
156  return false;
157  break;
158  }
159  }
160 
161  if ((c == 1) || (c == -1)) {
162  // standalone param or error
163  usage(argv[0], nullptr);
164  std::cerr << "ERROR: " << std::endl;
165  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
166  return false;
167  }
168 
169  return true;
170 }
171 
172 int main(int argc, const char **argv)
173 {
174  try {
175  bool opt_click_allowed = true;
176  bool opt_display = true;
177 
178  // Read the command line options
179  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
180  return EXIT_FAILURE;
181  }
182 
183  // We open two displays, one for the internal camera view, the other one for
184  // the external view, using either X11, GTK or GDI.
185 #if defined(VISP_HAVE_X11)
186  vpDisplayX displayInt;
187 #elif defined(VISP_HAVE_GDI)
188  vpDisplayGDI displayInt;
189 #elif defined(HAVE_OPENCV_HIGHGUI)
190  vpDisplayOpenCV displayInt;
191 #endif
192 
193  vpImage<unsigned char> Iint(480, 640, 255);
194 
195  if (opt_display) {
196  // open a display for the visualization
197  displayInt.init(Iint, 700, 0, "Internal view");
198  }
199 
200  vpServo task;
201 
202  std::cout << std::endl;
203  std::cout << "----------------------------------------------" << std::endl;
204  std::cout << " Test program for vpServo " << std::endl;
205  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
206  std::cout << " Simulation " << std::endl;
207  std::cout << " task : servo 4 points " << std::endl;
208  std::cout << "----------------------------------------------" << std::endl;
209  std::cout << std::endl;
210 
211  // sets the initial camera location
212  vpHomogeneousMatrix cMo(-0.05, -0.05, 0.7, vpMath::rad(10), vpMath::rad(10), vpMath::rad(-30));
213 
214  // sets the point coordinates in the object frame
215  vpPoint point[4];
216  point[0].setWorldCoordinates(-0.045, -0.045, 0);
217  point[3].setWorldCoordinates(-0.045, 0.045, 0);
218  point[2].setWorldCoordinates(0.045, 0.045, 0);
219  point[1].setWorldCoordinates(0.045, -0.045, 0);
220 
221  // computes the point coordinates in the camera frame and its 2D
222  // coordinates
223  for (unsigned int i = 0; i < 4; i++)
224  point[i].track(cMo);
225 
226  // sets the desired position of the point
227  vpFeaturePoint p[4];
228  for (unsigned int i = 0; i < 4; i++)
229  vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
230 
231  // sets the desired position of the feature point s*
232  vpFeaturePoint pd[4];
233 
234  // Desired pose
236 
237  // Projection of the points
238  for (unsigned int i = 0; i < 4; i++)
239  point[i].track(cdMo);
240 
241  for (unsigned int i = 0; i < 4; i++)
242  vpFeatureBuilder::create(pd[i], point[i]);
243 
244  // define the task
245  // - we want an eye-in-hand control law
246  // - articular velocity are computed
249 
250  // we want to see a point on a point
251  for (unsigned int i = 0; i < 4; i++)
252  task.addFeature(p[i], pd[i]);
253 
254  // set the gain
255  task.setLambda(0.8);
256 
257  // Declaration of the robot
258  vpSimulatorAfma6 robot(opt_display);
259 
260  // Initialise the robot and especially the camera
263 
264  // Initialise the object for the display part*/
266 
267  // Initialise the position of the object relative to the pose of the
268  // robot's camera
269  robot.initialiseObjectRelativeToCamera(cMo);
270 
271  // Set the desired position (for the displaypart)
272  robot.setDesiredCameraPosition(cdMo);
273 
274  // Get the internal robot's camera parameters
275  vpCameraParameters cam;
276  robot.getCameraParameters(cam, Iint);
277 
278  if (opt_display) {
279  // Get the internal view
280  vpDisplay::display(Iint);
281  robot.getInternalView(Iint);
282  vpDisplay::flush(Iint);
283  }
284 
285  // Display task information
286  task.print();
287 
288  unsigned int iter = 0;
289  vpTRACE("\t loop");
290  while (iter++ < 500) {
291  std::cout << "---------------------------------------------" << iter << std::endl;
292  vpColVector v;
293 
294  // Get the Time at the beginning of the loop
295  double t = vpTime::measureTimeMs();
296 
297  // Get the current pose of the camera
298  cMo = robot.get_cMo();
299 
300  if (iter == 1) {
301  std::cout << "Initial robot position with respect to the object frame:\n";
302  cMo.print();
303  }
304 
305  // new point position
306  for (unsigned int i = 0; i < 4; i++) {
307  point[i].track(cMo);
308  // retrieve x,y and Z of the vpPoint structure
309  vpFeatureBuilder::create(p[i], point[i]);
310  }
311 
312  if (opt_display) {
313  // Get the internal view and display it
314  vpDisplay::display(Iint);
315  robot.getInternalView(Iint);
316  vpDisplay::flush(Iint);
317  }
318 
319  if (opt_display && opt_click_allowed && iter == 1) {
320  // suppressed for automate test
321  std::cout << "Click in the internal view window to continue..." << std::endl;
322  vpDisplay::getClick(Iint);
323  }
324 
325  // compute the control law
326  v = task.computeControlLaw();
327 
328  // send the camera velocity to the controller
330 
331  std::cout << "|| s - s* || " << (task.getError()).sumSquare() << std::endl;
332 
333  // The main loop has a duration of 10 ms at minimum
334  vpTime::wait(t, 10);
335  }
336 
337  // Display task information
338  task.print();
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  // suppressed for automate test
345  std::cout << "Click in the internal view window to end..." << std::endl;
346  vpDisplay::getClick(Iint);
347  }
348  return EXIT_SUCCESS;
349  }
350  catch (const vpException &e) {
351  std::cout << "Catch a ViSP exception: " << e << std::endl;
352  return EXIT_FAILURE;
353  }
354  return EXIT_SUCCESS;
355 }
356 #elif !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
357 int main()
358 {
359  std::cout << "You do not have X11, or GDI (Graphical Device Interface) of OpenCV functionalities to display images..."
360  << std::endl;
361  std::cout << "Tip if you are on a unix-like system:" << std::endl;
362  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
363  std::cout << "Tip if you are on a windows-like system:" << std::endl;
364  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
365  return EXIT_SUCCESS;
366 }
367 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
368 int main()
369 {
370  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
371  return EXIT_SUCCESS;
372 }
373 #else
374 int main()
375 {
376  std::cout << "You do not have threading capabilities" << std::endl;
377  std::cout << "Tip:" << std::endl;
378  std::cout << "- Install pthread, configure again ViSP using cmake and build again this example" << std::endl;
379  return EXIT_SUCCESS;
380 }
381 #endif
@ TOOL_CCMOP
Definition: vpAfma6.h:124
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="") vp_override
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:59
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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:127
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:110
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
@ CAMERA_FRAME
Definition: vpRobot.h:82
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:65
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:198
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:378
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:329
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:169
void setLambda(double c)
Definition: vpServo.h:976
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:132
vpColVector getError() const
Definition: vpServo.h:504
vpColVector computeControlLaw()
Definition: vpServo.cpp:703
@ DESIRED
Definition: vpServo.h:202
Simulator of Irisa's gantry robot named Afma6.
#define vpTRACE
Definition: vpDebug.h:405
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()