Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
servoSimuFourPoints2DCamVelocityDisplay.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 as visual feature.
33  *
34 *****************************************************************************/
35 
52 #include <iostream>
53 
54 #include <visp3/core/vpConfig.h>
55 
56 #if defined(VISP_HAVE_DISPLAY) && \
57  (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
58 
59 #include <stdio.h>
60 #include <stdlib.h>
61 
62 #include <visp3/core/vpCameraParameters.h>
63 #include <visp3/core/vpHomogeneousMatrix.h>
64 #include <visp3/core/vpImage.h>
65 #include <visp3/core/vpMath.h>
66 #include <visp3/gui/vpDisplayFactory.h>
67 #include <visp3/gui/vpProjectionDisplay.h>
68 #include <visp3/io/vpParseArgv.h>
69 #include <visp3/robot/vpSimulatorCamera.h>
70 #include <visp3/visual_features/vpFeatureBuilder.h>
71 #include <visp3/visual_features/vpFeaturePoint.h>
72 #include <visp3/vs/vpServo.h>
73 #include <visp3/vs/vpServoDisplay.h>
74 
75 // List of allowed command line options
76 #define GETOPTARGS "cdh"
77 
78 #ifdef ENABLE_VISP_NAMESPACE
79 using namespace VISP_NAMESPACE_NAME;
80 #endif
81 
82 void usage(const char *name, const char *badparam);
83 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
84 
93 void usage(const char *name, const char *badparam)
94 {
95  fprintf(stdout, "\n\
96 Tests a control law with the following characteristics:\n\
97 - eye-in-hand control\n\
98 - articular velocity are computed\n\
99 - servo on 4 points,\n\
100 - internal and external camera view displays.\n\
101  \n\
102 SYNOPSIS\n\
103  %s [-c] [-d] [-h]\n",
104  name);
105 
106  fprintf(stdout, "\n\
107 OPTIONS: Default\n\
108  -c\n\
109  Disable the mouse click. Useful to automate the \n\
110  execution of this program without human intervention.\n\
111  \n\
112  -d \n\
113  Turn off the display.\n\
114  \n\
115  -h\n\
116  Print the help.\n");
117 
118  if (badparam)
119  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
120 }
133 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
134 {
135  const char *optarg_;
136  int c;
137  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
138 
139  switch (c) {
140  case 'c':
141  click_allowed = false;
142  break;
143  case 'd':
144  display = false;
145  break;
146  case 'h':
147  usage(argv[0], nullptr);
148  return false;
149 
150  default:
151  usage(argv[0], optarg_);
152  return false;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], nullptr);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 int main(int argc, const char **argv)
168 {
169  // We declare the windows variables to be able to free the memory in the catch sections if needed
170 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
171  std::shared_ptr<vpDisplay> displayInt;
172  std::shared_ptr<vpDisplay> displayExt;
173 #else
174  vpDisplay *displayInt = nullptr;
175  vpDisplay *displayExt = nullptr;
176 #endif
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  // open a display for the visualization
187 
188  vpImage<unsigned char> Iint(300, 300, 0);
189  vpImage<unsigned char> Iext(300, 300, 0);
190 
191  if (opt_display) {
192  // We open two displays, one for the internal camera view, the other one for
193  // the external view
194  // Display size is automatically defined by the image (Iint) and
195  // (Iext) size
196 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
197  displayInt = vpDisplayFactory::createDisplay(Iint, 0, 0, "Internal view");
198  displayExt = vpDisplayFactory::createDisplay(Iext, 330, 000, "External view");
199 #else
200  displayInt = vpDisplayFactory::allocateDisplay(Iint, 0, 0, "Internal view");
201  displayExt = vpDisplayFactory::allocateDisplay(Iext, 330, 000, "External view");
202 #endif
203  }
204  vpProjectionDisplay externalview;
205 
206  double px = 500, py = 500;
207  double u0 = 150, v0 = 160;
208 
209  vpCameraParameters cam(px, py, u0, v0);
210 
211  vpServo task;
212  vpSimulatorCamera robot;
213 
214  std::cout << std::endl;
215  std::cout << "----------------------------------------------" << std::endl;
216  std::cout << " Test program for vpServo " << std::endl;
217  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
218  std::cout << " Simulation " << std::endl;
219  std::cout << " task : servo 4 points " << std::endl;
220  std::cout << "----------------------------------------------" << std::endl;
221  std::cout << std::endl;
222 
223  // sets the initial camera location
224  vpHomogeneousMatrix cMo(-0.1, -0.1, 1, vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
225 
226  // Compute the position of the object in the world frame
227  vpHomogeneousMatrix wMc, wMo;
228  robot.getPosition(wMc);
229  wMo = wMc * cMo;
230 
231  vpHomogeneousMatrix cextMo(0, 0, 2, 0, 0, 0); // vpMath::rad(40), vpMath::rad(10), vpMath::rad(60));
232 
233  // sets the point coordinates in the object frame
234  vpPoint point[4];
235  point[0].setWorldCoordinates(-0.1, -0.1, 0);
236  point[1].setWorldCoordinates(0.1, -0.1, 0);
237  point[2].setWorldCoordinates(0.1, 0.1, 0);
238  point[3].setWorldCoordinates(-0.1, 0.1, 0);
239 
240  for (unsigned i = 0; i < 4; i++)
241  externalview.insert(point[i]);
242 
243  // computes the point coordinates in the camera frame and its 2D
244  // coordinates
245  for (unsigned i = 0; i < 4; i++)
246  point[i].track(cMo);
247 
248  // sets the desired position of the point
249  vpFeaturePoint p[4];
250  for (unsigned i = 0; i < 4; i++)
251  vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
252 
253  // sets the desired position of the feature point s*
254  vpFeaturePoint pd[4];
255 
256  pd[0].buildFrom(-0.1, -0.1, 1);
257  pd[1].buildFrom(0.1, -0.1, 1);
258  pd[2].buildFrom(0.1, 0.1, 1);
259  pd[3].buildFrom(-0.1, 0.1, 1);
260 
261  // define the task
262  // - we want an eye-in-hand control law
263  // - articular velocity are computed
266 
267  // Set the position of the end-effector frame in the camera frame as identity
269  vpVelocityTwistMatrix cVe(cMe);
270  task.set_cVe(cVe);
271 
272  // Set the Jacobian (expressed in the end-effector frame
273  vpMatrix eJe;
274  robot.get_eJe(eJe);
275  task.set_eJe(eJe);
276 
277  // we want to see a point on a point
278  for (unsigned i = 0; i < 4; i++)
279  task.addFeature(p[i], pd[i]);
280 
281  // set the gain
282  task.setLambda(1);
283 
284  // Display task information
285  task.print();
286 
287  unsigned int iter = 0;
288  // loop
289  while (iter++ < 200) {
290  std::cout << "---------------------------------------------" << iter << std::endl;
291  vpColVector v;
292 
293  // Set the Jacobian (expressed in the end-effector frame)
294  // since q is modified eJe is modified
295  robot.get_eJe(eJe);
296  task.set_eJe(eJe);
297 
298  // get the robot position
299  robot.getPosition(wMc);
300  // Compute the position of the object frame in the camera frame
301  cMo = wMc.inverse() * wMo;
302 
303  // update new point position and corresponding features
304  for (unsigned i = 0; i < 4; i++) {
305  point[i].track(cMo);
306  // retrieve x,y and Z of the vpPoint structure
307  vpFeatureBuilder::create(p[i], point[i]);
308  }
309  // since vpServo::MEAN interaction matrix is used, we need also to
310  // update the desired features at each iteration
311  pd[0].buildFrom(-0.1, -0.1, 1);
312  pd[1].buildFrom(0.1, -0.1, 1);
313  pd[2].buildFrom(0.1, 0.1, 1);
314  pd[3].buildFrom(-0.1, 0.1, 1);
315 
316  if (opt_display) {
317  vpDisplay::display(Iint);
318  vpDisplay::display(Iext);
319  vpServoDisplay::display(task, cam, Iint);
320  externalview.display(Iext, cextMo, cMo, cam, vpColor::green);
321  vpDisplay::flush(Iint);
322  vpDisplay::flush(Iext);
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 
334  // Display task information
335  task.print();
336 
337  std::cout << "Final robot position with respect to the object frame:\n";
338  cMo.print();
339 
340  if (opt_display && opt_click_allowed) {
341  vpDisplay::displayText(Iint, 20, 20, "Click to quit...", vpColor::white);
342  vpDisplay::flush(Iint);
343  vpDisplay::getClick(Iint);
344  }
345 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
346  if (displayInt != nullptr) {
347  delete displayInt;
348  }
349  if (displayExt != nullptr) {
350  delete displayExt;
351  }
352 #endif
353  return EXIT_SUCCESS;
354  }
355  catch (const vpException &e) {
356  std::cout << "Catch a ViSP exception: " << e << std::endl;
357 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
358  if (displayInt != nullptr) {
359  delete displayInt;
360  }
361  if (displayExt != nullptr) {
362  delete displayExt;
363  }
364 #endif
365  return EXIT_FAILURE;
366  }
367 }
368 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
369 int main()
370 {
371  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
372  return EXIT_SUCCESS;
373 }
374 #else
375 int main()
376 {
377  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
378  << std::endl;
379  std::cout << "Tip if you are on a unix-like system:" << std::endl;
380  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
381  std::cout << "Tip if you are on a windows-like system:" << std::endl;
382  std::cout << "- Install GDI, 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.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static const vpColor white
Definition: vpColor.h:193
static const vpColor green
Definition: vpColor.h:201
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
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...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
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:113
interface with the image for feature display
void insert(vpForwardProjection &fp)
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &color, const bool &displayTraj=false, unsigned int thickness=1)
void get_eJe(vpMatrix &eJe) VP_OVERRIDE
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_L_cVe_eJe
Definition: vpServo.h:168
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:1043
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:991
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:1106
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:515
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ MEAN
Definition: vpServo.h:214
Class that defines the simplest robot: a free flying camera.
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.