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