Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
servoSimuCircle2DCamVelocityDisplay.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 circle.
33  *
34 *****************************************************************************/
35 
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpDebug.h>
48 
49 #if defined(VISP_HAVE_DISPLAY) && \
50  (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
51 
52 #include <stdio.h>
53 #include <stdlib.h>
54 
55 #include <visp3/core/vpCameraParameters.h>
56 #include <visp3/core/vpCircle.h>
57 #include <visp3/core/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpImage.h>
59 #include <visp3/core/vpMath.h>
60 #include <visp3/gui/vpDisplayFactory.h>
61 #include <visp3/io/vpParseArgv.h>
62 #include <visp3/robot/vpSimulatorCamera.h>
63 #include <visp3/visual_features/vpFeatureBuilder.h>
64 #include <visp3/visual_features/vpFeatureLine.h>
65 #include <visp3/vs/vpServo.h>
66 #include <visp3/vs/vpServoDisplay.h>
67 
68 // List of allowed command line options
69 #define GETOPTARGS "cdh"
70 
71 #ifdef ENABLE_VISP_NAMESPACE
72 using namespace VISP_NAMESPACE_NAME;
73 #endif
74 
75 void usage(const char *name, const char *badparam);
76 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
77 
86 void usage(const char *name, const char *badparam)
87 {
88  fprintf(stdout, "\n\
89 Simulation of a 2D visual servoing on a circle:\n\
90 - eye-in-hand control law,\n\
91 - velocity computed in the camera frame,\n\
92 - display the camera view.\n\
93  \n\
94 SYNOPSIS\n\
95  %s [-c] [-d] [-h]\n",
96  name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  \n\
101  -c\n\
102  Disable the mouse click. Useful to automate the \n\
103  execution of this program without human intervention.\n\
104  \n\
105  -d \n\
106  Turn off the display.\n\
107  \n\
108  -h\n\
109  Print the help.\n");
110 
111  if (badparam) {
112  fprintf(stderr, "ERROR: \n");
113  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
114  }
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 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
166  std::shared_ptr<vpDisplay> display;
167 #else
168  vpDisplay *display = nullptr;
169 #endif
170  try {
171  bool opt_display = true;
172  bool opt_click_allowed = true;
173 
174  // Read the command line options
175  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
176  return (EXIT_FAILURE);
177  }
178 
179  vpImage<unsigned char> I(512, 512, 0);
180 
181  if (opt_display) {
182  // Display size is automatically defined by the image (I) size
183 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
184  display = vpDisplayFactory::createDisplay(I, 100, 100, "Camera view...");
185 #else
186  display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Camera view...");
187 #endif
188  // Display the image
189  // The image class has a member that specify a pointer toward
190  // the display that has been initialized in the display declaration
191  // therefore is is no longer necessary to make a reference to the
192  // display variable.
194  vpDisplay::flush(I);
195  }
196 
197  double px = 600, py = 600;
198  double u0 = I.getWidth() / 2., v0 = I.getHeight() / 2.;
199 
200  vpCameraParameters cam(px, py, u0, v0);
201 
202  vpServo task;
203  vpSimulatorCamera robot;
204 
205  // sets the initial camera location
206  vpHomogeneousMatrix cMo(0, 0, 1, vpMath::rad(0), vpMath::rad(80), vpMath::rad(30));
207  vpHomogeneousMatrix wMc, wMo;
208  robot.getPosition(wMc);
209  wMo = wMc * cMo; // Compute the position of the object in the world frame
210 
211  vpHomogeneousMatrix cMod(-0.1, -0.1, 0.7, vpMath::rad(40), vpMath::rad(10), vpMath::rad(30));
212 
213  // sets the circle coordinates in the world frame
214  vpCircle circle;
215  circle.setWorldCoordinates(0, 0, 1, 0, 0, 0, 0.1);
216 
217  // sets the desired position of the visual feature
218  vpFeatureEllipse pd;
219  circle.track(cMod);
220  vpFeatureBuilder::create(pd, circle);
221 
222  // project : computes the circle coordinates in the camera frame and its
223  // 2D coordinates sets the current position of the visual feature
225  circle.track(cMo);
226  vpFeatureBuilder::create(p, circle);
227 
228  // define the task
229  // - we want an eye-in-hand control law
230  // - robot is controlled in the camera frame
233  // - we want to see a circle on a circle
234  task.addFeature(p, pd);
235  // - set the gain
236  task.setLambda(1);
237 
238  // Display task information
239  task.print();
240 
241  unsigned int iter = 0;
242  // loop
243  while (iter++ < 200) {
244  std::cout << "---------------------------------------------" << iter << std::endl;
245  vpColVector v;
246 
247  // get the robot position
248  robot.getPosition(wMc);
249  // Compute the position of the object frame in the camera frame
250  cMo = wMc.inverse() * wMo;
251 
252  // new circle position
253  // retrieve x,y and Z of the vpCircle structure
254  circle.track(cMo);
255  vpFeatureBuilder::create(p, circle);
256  circle.print();
257  p.print();
258 
259  if (opt_display) {
261  vpServoDisplay::display(task, cam, I);
262  vpDisplay::flush(I);
263  }
264 
265  // compute the control law
266  v = task.computeControlLaw();
267  std::cout << "task rank: " << task.getTaskRank() << std::endl;
268  // send the camera velocity to the controller
270 
271  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
272  }
273 
274  // Display task information
275  task.print();
276 
277  if (opt_display && opt_click_allowed) {
278  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::white);
279  vpDisplay::flush(I);
281  }
282 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
283  if (display != nullptr) {
284  delete display;
285  }
286 #endif
287  return EXIT_SUCCESS;
288  }
289  catch (const vpException &e) {
290  std::cout << "Catch a ViSP exception: " << e << std::endl;
291 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
292  if (display != nullptr) {
293  delete display;
294  }
295 #endif
296  return EXIT_FAILURE;
297  }
298 }
299 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
300 int main()
301 {
302  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
303  return EXIT_SUCCESS;
304 }
305 #else
306 int main()
307 {
308  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) or OpenCV functionalities to display "
309  "images..."
310  << std::endl;
311  std::cout << "Tip if you are on a unix-like system:" << std::endl;
312  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
313  std::cout << "Tip if you are on a windows-like system:" << std::endl;
314  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
315  return EXIT_SUCCESS;
316 }
317 #endif
Generic class defining intrinsic camera parameters.
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition: vpCircle.h:87
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition: vpCircle.cpp:59
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 2D ellipse visual feature.
void print(unsigned int select=FEATURE_ALL) const VP_OVERRIDE
Print the name of the feature.
virtual void print() const
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
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
unsigned int getTaskRank() const
Definition: vpServo.h:611
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
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ DESIRED
Definition: vpServo.h:208
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.