Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
servoSimuSphere2DCamVelocityDisplay.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 sphere.
33  *
34 *****************************************************************************/
35 
45 #include <stdio.h>
46 #include <stdlib.h>
47 
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/core/vpHomogeneousMatrix.h>
50 #include <visp3/core/vpMath.h>
51 #include <visp3/core/vpSphere.h>
52 #include <visp3/gui/vpDisplayGDI.h>
53 #include <visp3/gui/vpDisplayGTK.h>
54 #include <visp3/gui/vpDisplayOpenCV.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/robot/vpSimulatorCamera.h>
58 #include <visp3/visual_features/vpFeatureBuilder.h>
59 #include <visp3/visual_features/vpFeatureEllipse.h>
60 #include <visp3/vs/vpServo.h>
61 #include <visp3/vs/vpServoDisplay.h>
62 
63 // List of allowed command line options
64 #define GETOPTARGS "cdh"
65 
66 #ifdef ENABLE_VISP_NAMESPACE
67 using namespace VISP_NAMESPACE_NAME;
68 #endif
69 
70 void usage(const char *name, const char *badparam);
71 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
72 
81 void usage(const char *name, const char *badparam)
82 {
83  fprintf(stdout, "\n\
84 Simulation of a 2D visual servoing on a sphere:\n\
85 - eye-in-hand control law,\n\
86 - velocity computed in the camera frame,\n\
87 - display the camera view.\n\
88  \n\
89 SYNOPSIS\n\
90  %s [-c] [-d] [-h]\n",
91  name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  \n\
96  -c\n\
97  Disable the mouse click. Useful to automate the \n\
98  execution of this program without human intervention.\n\
99  \n\
100  -d \n\
101  Turn off the display.\n\
102  \n\
103  -h\n\
104  Print the help.\n");
105 
106  if (badparam)
107  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
108 }
109 
122 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
123 {
124  const char *optarg_;
125  int c;
126  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
127 
128  switch (c) {
129  case 'c':
130  click_allowed = false;
131  break;
132  case 'd':
133  display = false;
134  break;
135  case 'h':
136  usage(argv[0], nullptr);
137  return false;
138 
139  default:
140  usage(argv[0], optarg_);
141  return false;
142  }
143  }
144 
145  if ((c == 1) || (c == -1)) {
146  // standalone param or error
147  usage(argv[0], nullptr);
148  std::cerr << "ERROR: " << std::endl;
149  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
150  return false;
151  }
152 
153  return true;
154 }
155 
156 int main(int argc, const char **argv)
157 {
158 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
159  try {
160  bool opt_display = true;
161  bool opt_click_allowed = true;
162 
163  // Read the command line options
164  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
165  return (EXIT_FAILURE);
166  }
167 
168  vpImage<unsigned char> I(512, 512, 0);
169 
170 // We open a window using either X11, GTK or GDI.
171 #if defined(VISP_HAVE_X11)
172  vpDisplayX display;
173 #elif defined(VISP_HAVE_GTK)
174  vpDisplayGTK display;
175 #elif defined(VISP_HAVE_GDI)
176  vpDisplayGDI display;
177 #elif defined(HAVE_OPENCV_HIGHGUI)
178  vpDisplayOpenCV display;
179 #endif
180 
181  if (opt_display) {
182 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
183  // Display size is automatically defined by the image (I) size
184  display.init(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 
195  double px = 600, py = 600;
196  double u0 = I.getWidth() / 2., v0 = I.getHeight() / 2.;
197 
198  vpCameraParameters cam(px, py, u0, v0);
199 
200  vpServo task;
201  vpSimulatorCamera robot;
202 
203  // sets the initial camera location
205  cMo[0][3] = 0.1;
206  cMo[1][3] = 0.2;
207  cMo[2][3] = 2;
208  // Compute the position of the object in the world frame
209  vpHomogeneousMatrix wMc, wMo;
210  robot.getPosition(wMc);
211  wMo = wMc * cMo;
212 
213  vpHomogeneousMatrix cMod;
214  cMod[0][3] = 0;
215  cMod[1][3] = 0;
216  cMod[2][3] = 1;
217 
218  // sets the sphere coordinates in the world frame
219  vpSphere sphere;
220  sphere.setWorldCoordinates(0, 0, 0, 0.1);
221 
222  // sets the desired position of the visual feature
223  vpFeatureEllipse pd;
224  sphere.track(cMod);
225  vpFeatureBuilder::create(pd, sphere);
226 
227  // computes the sphere coordinates in the camera frame and its 2D
228  // coordinates sets the current position of the visual feature
230  sphere.track(cMo);
231  vpFeatureBuilder::create(p, sphere);
232 
233  // define the task
234  // - we want an eye-in-hand control law
235  // - robot is controlled in the camera frame
237 
238  // we want to see a sphere on a sphere
239  task.addFeature(p, pd);
240 
241  // set the gain
242  task.setLambda(1);
243 
244  // Display task information
245  task.print();
246 
247  unsigned int iter = 0;
248  // loop
249  while (iter++ < 200) {
250  std::cout << "---------------------------------------------" << iter << std::endl;
251  vpColVector v;
252 
253  // get the robot position
254  robot.getPosition(wMc);
255  // Compute the position of the object frame in the camera frame
256  cMo = wMc.inverse() * wMo;
257 
258  // new sphere position: retrieve x,y and Z of the vpSphere structure
259  sphere.track(cMo);
260  vpFeatureBuilder::create(p, sphere);
261 
262  if (opt_display) {
264  vpServoDisplay::display(task, cam, I);
265  vpDisplay::flush(I);
266  }
267 
268  // compute the control law
269  v = task.computeControlLaw();
270 
271  std::cout << "Task rank: " << task.getTaskRank() << std::endl;
272  // send the camera velocity to the controller
274 
275  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
276  }
277 
278  if (opt_display && opt_click_allowed) {
279  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::white);
280  vpDisplay::flush(I);
282  }
283 
284  // Display task information
285  task.print();
286  return EXIT_SUCCESS;
287  }
288  catch (const vpException &e) {
289  std::cout << "Catch a ViSP exception: " << e << std::endl;
290  return EXIT_FAILURE;
291  }
292 #else
293  (void)argc;
294  (void)argv;
295  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
296  return EXIT_SUCCESS;
297 #endif
298  }
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 2D ellipse visual feature.
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 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)
@ 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:606
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
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
Class that defines the simplest robot: a free flying camera.
Class that defines a 3D sphere in the object frame and allows forward projection of a 3D sphere in th...
Definition: vpSphere.h:80
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition: vpSphere.cpp:59