Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
servoSimuSquareLine2DCamVelocityDisplay.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 on a line.
33  *
34 *****************************************************************************/
35 
45 #include <visp3/core/vpConfig.h>
46 #include <visp3/core/vpDebug.h>
47 
48 #if defined(VISP_HAVE_DISPLAY) && \
49  (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
50 
51 #include <stdio.h>
52 #include <stdlib.h>
53 
54 #include <visp3/core/vpCameraParameters.h>
55 #include <visp3/core/vpHomogeneousMatrix.h>
56 #include <visp3/core/vpImage.h>
57 #include <visp3/core/vpLine.h>
58 #include <visp3/core/vpMath.h>
59 #include <visp3/gui/vpDisplayFactory.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/robot/vpSimulatorCamera.h>
62 #include <visp3/visual_features/vpFeatureBuilder.h>
63 #include <visp3/visual_features/vpFeatureLine.h>
64 #include <visp3/vs/vpServo.h>
65 #include <visp3/vs/vpServoDisplay.h>
66 
67 // List of allowed command line options
68 #define GETOPTARGS "cdh"
69 
70 #ifdef ENABLE_VISP_NAMESPACE
71 using namespace VISP_NAMESPACE_NAME;
72 #endif
73 
74 void usage(const char *name, const char *badparam);
75 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
76 
85 void usage(const char *name, const char *badparam)
86 {
87  fprintf(stdout, "\n\
88 Simulation of 2D a visual servoing on a line:\n\
89 - eye-in-hand control law,\n\
90 - velocity computed in the camera frame,\n\
91 - display the camera view.\n\
92  \n\
93 SYNOPSIS\n\
94  %s [-c] [-d] [-h]\n",
95  name);
96 
97  fprintf(stdout, "\n\
98 OPTIONS: Default\n\
99  \n\
100  -c\n\
101  Disable the mouse click. Useful to automate the \n\
102  execution of this program without human intervention.\n\
103  \n\
104  -d \n\
105  Turn off the display.\n\
106  \n\
107  -h\n\
108  Print the help.\n");
109 
110  if (badparam)
111  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
112 }
113 
126 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
127 {
128  const char *optarg_;
129  int c;
130  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
131 
132  switch (c) {
133  case 'c':
134  click_allowed = false;
135  break;
136  case 'd':
137  display = false;
138  break;
139  case 'h':
140  usage(argv[0], nullptr);
141  return false;
142 
143  default:
144  usage(argv[0], optarg_);
145  return false;
146  }
147  }
148 
149  if ((c == 1) || (c == -1)) {
150  // standalone param or error
151  usage(argv[0], nullptr);
152  std::cerr << "ERROR: " << std::endl;
153  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
154  return false;
155  }
156 
157  return true;
158 }
159 
160 int main(int argc, const char **argv)
161 {
162 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
163  std::shared_ptr<vpDisplay> display;
164 #else
165  vpDisplay *display = nullptr;
166 #endif
167  try {
168  bool opt_display = true;
169  bool opt_click_allowed = true;
170 
171  // Read the command line options
172  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
173  return EXIT_FAILURE;
174  }
175 
176  vpImage<unsigned char> I(512, 512, 0);
177 
178  if (opt_display) {
179  try {
180  // Display size is automatically defined by the image (I) size
181 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
182  display = vpDisplayFactory::createDisplay(I, 100, 100, "Camera view...");
183 #else
184  display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Camera view...");
185 #endif
186  // Display the image
187  // The image class has a member that specify a pointer toward
188  // the display that has been initialized in the display declaration
189  // therefore is is no longer necessary to make a reference to the
190  // display variable.
192  vpDisplay::flush(I);
193  }
194  catch (...) {
195  vpERROR_TRACE("Error while displaying the image");
196  return EXIT_FAILURE;
197  }
198  }
199 
200  // Set the camera parameters
201  double px, py;
202  px = py = 600;
203  double u0, v0;
204  u0 = v0 = 256;
205 
206  vpCameraParameters cam(px, py, u0, v0);
207 
208  vpServo task;
209  vpSimulatorCamera robot;
210 
211  // sets the initial camera location
212  vpHomogeneousMatrix cMo(0.2, 0.2, 1, vpMath::rad(45), vpMath::rad(45), vpMath::rad(125));
213 
214  // Compute the position of the object in the world frame
215  vpHomogeneousMatrix wMc, wMo;
216  robot.getPosition(wMc);
217  wMo = wMc * cMo;
218 
219  // sets the final camera location (for simulation purpose)
220  vpHomogeneousMatrix cMod(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
221 
222  int nbline = 4;
223 
224  // sets the line coordinates (2 planes) in the world frame
225  vpLine line[4];
226  line[0].setWorldCoordinates(1, 0, 0, 0.05, 0, 0, 1, 0);
227  line[1].setWorldCoordinates(0, 1, 0, 0.05, 0, 0, 1, 0);
228  line[2].setWorldCoordinates(1, 0, 0, -0.05, 0, 0, 1, 0);
229  line[3].setWorldCoordinates(0, 1, 0, -0.05, 0, 0, 1, 0);
230 
231  vpFeatureLine ld[4];
232  vpFeatureLine l[4];
233 
234  // sets the desired position of the visual feature
235  for (int i = 0; i < nbline; i++) {
236  line[i].track(cMod);
237  line[i].print();
238 
239  vpFeatureBuilder::create(ld[i], line[i]);
240  }
241 
242  // computes the line coordinates in the camera frame and its 2D
243  // coordinates sets the current position of the visual feature
244  for (int i = 0; i < nbline; i++) {
245  line[i].track(cMo);
246  line[i].print();
247 
248  vpFeatureBuilder::create(l[i], line[i]);
249  l[i].print();
250  }
251 
252  // define the task
253  // - we want an eye-in-hand control law
254  // - robot is controlled in the camera frame
257  // It could be also interesting to test the following tasks
258  // task.setInteractionMatrixType(vpServo::DESIRED,
259  // vpServo::PSEUDO_INVERSE); task.setInteractionMatrixType(vpServo::MEAN,
260  // vpServo::PSEUDO_INVERSE);
261 
262  // we want to see a four lines on four lines
263  for (int i = 0; i < nbline; i++)
264  task.addFeature(l[i], ld[i]);
265 
267  vpServoDisplay::display(task, cam, I);
268  vpDisplay::flush(I);
269 
270  // set the gain
271  task.setLambda(1);
272 
273  // Display task information
274  task.print();
275 
276  if (opt_display && opt_click_allowed) {
277  std::cout << "\n\nClick in the camera view window to start..." << std::endl;
279  }
280 
281  unsigned int iter = 0;
282  // loop
283  while (iter++ < 200) {
284  std::cout << "---------------------------------------------" << iter << std::endl;
285  vpColVector v;
286 
287  // get the robot position
288  robot.getPosition(wMc);
289  // Compute the position of the object frame in the camera frame
290  cMo = wMc.inverse() * wMo;
291 
292  // new line position: retrieve x,y and Z of the vpLine structure
293  for (int i = 0; i < nbline; i++) {
294  line[i].track(cMo);
295  vpFeatureBuilder::create(l[i], line[i]);
296  }
297 
298  if (opt_display) {
300  vpServoDisplay::display(task, cam, I);
301  vpDisplay::flush(I);
302  }
303 
304  // compute the control law
305  v = task.computeControlLaw();
306 
307  // send the camera velocity to the controller
309 
310  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
311  }
312 
313  if (opt_display && opt_click_allowed) {
314  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::white);
315  vpDisplay::flush(I);
317  }
318 
319  // Display task information
320  task.print();
321 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
322  if (display != nullptr) {
323  delete display;
324  }
325 #endif
326  return EXIT_SUCCESS;
327  }
328  catch (const vpException &e) {
329  std::cout << "Catch a ViSP exception: " << e << std::endl;
330 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
331  if (display != nullptr) {
332  delete display;
333  }
334 #endif
335  return EXIT_FAILURE;
336  }
337 }
338 
339 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
340 int main()
341 {
342  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
343  return EXIT_SUCCESS;
344 }
345 #else
346 int main()
347 {
348  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) or OpenCV functionalities to display "
349  "images..."
350  << std::endl;
351  std::cout << "Tip if you are on a unix-like system:" << std::endl;
352  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
353  std::cout << "Tip if you are on a windows-like system:" << std::endl;
354  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
355  return EXIT_SUCCESS;
356 }
357 #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
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 line visual feature which is composed by two parameters that are and ,...
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
virtual void print() const
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
Class that defines a 3D line in the object frame and allows forward projection of the line in the cam...
Definition: vpLine.h:103
void setWorldCoordinates(const double &oA1, const double &oB1, const double &oC1, const double &oD1, const double &oA2, const double &oB2, const double &oC2, const double &oD2)
Definition: vpLine.cpp:83
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
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_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:991
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:515
@ PSEUDO_INVERSE
Definition: vpServo.h:235
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
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.