Visual Servoing Platform  version 3.5.1 under development (2022-12-01)
servoSimuSphere2DCamVelocityDisplay.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
49 #include <stdio.h>
50 #include <stdlib.h>
51 
52 #include <visp3/core/vpHomogeneousMatrix.h>
53 #include <visp3/core/vpMath.h>
54 #include <visp3/core/vpSphere.h>
55 #include <visp3/gui/vpDisplayGDI.h>
56 #include <visp3/gui/vpDisplayGTK.h>
57 #include <visp3/gui/vpDisplayOpenCV.h>
58 #include <visp3/gui/vpDisplayX.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/robot/vpSimulatorCamera.h>
61 #include <visp3/visual_features/vpFeatureBuilder.h>
62 #include <visp3/visual_features/vpFeatureEllipse.h>
63 #include <visp3/vs/vpServo.h>
64 #include <visp3/vs/vpServoDisplay.h>
65 
66 // List of allowed command line options
67 #define GETOPTARGS "cdh"
68 
69 void usage(const char *name, const char *badparam);
70 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
71 
80 void usage(const char *name, const char *badparam)
81 {
82  fprintf(stdout, "\n\
83 Simulation of a 2D visual servoing on a sphere:\n\
84 - eye-in-hand control law,\n\
85 - velocity computed in the camera frame,\n\
86 - display the camera view.\n\
87  \n\
88 SYNOPSIS\n\
89  %s [-c] [-d] [-h]\n",
90  name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  \n\
95  -c\n\
96  Disable the mouse click. Useful to automaze the \n\
97  execution of this program without humain intervention.\n\
98  \n\
99  -d \n\
100  Turn off the display.\n\
101  \n\
102  -h\n\
103  Print the help.\n");
104 
105  if (badparam)
106  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
107 }
108 
121 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
122 {
123  const char *optarg_;
124  int c;
125  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
126 
127  switch (c) {
128  case 'c':
129  click_allowed = false;
130  break;
131  case 'd':
132  display = false;
133  break;
134  case 'h':
135  usage(argv[0], NULL);
136  return false;
137 
138  default:
139  usage(argv[0], optarg_);
140  return false;
141  }
142  }
143 
144  if ((c == 1) || (c == -1)) {
145  // standalone param or error
146  usage(argv[0], NULL);
147  std::cerr << "ERROR: " << std::endl;
148  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
149  return false;
150  }
151 
152  return true;
153 }
154 
155 int main(int argc, const char **argv)
156 {
157 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
158  try {
159  bool opt_display = true;
160  bool opt_click_allowed = true;
161 
162  // Read the command line options
163  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
164  return (EXIT_FAILURE);
165  }
166 
167  vpImage<unsigned char> I(512, 512, 0);
168 
169 // We open a window using either X11, GTK or GDI.
170 #if defined VISP_HAVE_X11
171  vpDisplayX display;
172 #elif defined VISP_HAVE_GTK
173  vpDisplayGTK display;
174 #elif defined VISP_HAVE_GDI
175  vpDisplayGDI display;
176 #elif defined VISP_HAVE_OPENCV
177  vpDisplayOpenCV display;
178 #endif
179 
180  if (opt_display) {
181 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
182  // Display size is automatically defined by the image (I) size
183  display.init(I, 100, 100, "Camera view...");
184 #endif
185  // Display the image
186  // The image class has a member that specify a pointer toward
187  // the display that has been initialized in the display declaration
188  // therefore is is no longuer necessary to make a reference to the
189  // display variable.
191  vpDisplay::flush(I);
192  }
193 
194  double px = 600, py = 600;
195  double u0 = I.getWidth() / 2., v0 = I.getHeight() / 2.;
196 
197  vpCameraParameters cam(px, py, u0, v0);
198 
199  vpServo task;
200  vpSimulatorCamera robot;
201 
202  // sets the initial camera location
204  cMo[0][3] = 0.1;
205  cMo[1][3] = 0.2;
206  cMo[2][3] = 2;
207  // Compute the position of the object in the world frame
208  vpHomogeneousMatrix wMc, wMo;
209  robot.getPosition(wMc);
210  wMo = wMc * cMo;
211 
212  vpHomogeneousMatrix cMod;
213  cMod[0][3] = 0;
214  cMod[1][3] = 0;
215  cMod[2][3] = 1;
216 
217  // sets the sphere coordinates in the world frame
218  vpSphere sphere;
219  sphere.setWorldCoordinates(0, 0, 0, 0.1);
220 
221  // sets the desired position of the visual feature
222  vpFeatureEllipse pd;
223  sphere.track(cMod);
224  vpFeatureBuilder::create(pd, sphere);
225 
226  // computes the sphere coordinates in the camera frame and its 2D
227  // coordinates sets the current position of the visual feature
229  sphere.track(cMo);
230  vpFeatureBuilder::create(p, sphere);
231 
232  // define the task
233  // - we want an eye-in-hand control law
234  // - robot is controlled in the camera frame
236 
237  // we want to see a sphere on a sphere
238  task.addFeature(p, pd);
239 
240  // set the gain
241  task.setLambda(1);
242 
243  // Display task information
244  task.print();
245 
246  unsigned int iter = 0;
247  // loop
248  while (iter++ < 200) {
249  std::cout << "---------------------------------------------" << iter << std::endl;
250  vpColVector v;
251 
252  // get the robot position
253  robot.getPosition(wMc);
254  // Compute the position of the object frame in the camera frame
255  cMo = wMc.inverse() * wMo;
256 
257  // new sphere position: retrieve x,y and Z of the vpSphere structure
258  sphere.track(cMo);
259  vpFeatureBuilder::create(p, sphere);
260 
261  if (opt_display) {
263  vpServoDisplay::display(task, cam, I);
264  vpDisplay::flush(I);
265  }
266 
267  // compute the control law
268  v = task.computeControlLaw();
269 
270  std::cout << "Task rank: " << task.getTaskRank() << std::endl;
271  // send the camera velocity to the controller
273 
274  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
275  }
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 
283  // Display task information
284  task.print();
285  return EXIT_SUCCESS;
286  } catch (const vpException &e) {
287  std::cout << "Catch a ViSP exception: " << e << std::endl;
288  return EXIT_FAILURE;
289  }
290 #else
291  (void)argc;
292  (void)argv;
293  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
294  return EXIT_SUCCESS;
295 #endif
296 }
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
static const vpColor white
Definition: vpColor.h:212
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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 emited by ViSP classes.
Definition: vpException.h:72
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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:246
unsigned int getHeight() const
Definition: vpImage.h:188
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
@ CAMERA_FRAME
Definition: vpRobot.h:83
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:155
unsigned int getTaskRank() const
Definition: vpServo.cpp:1783
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:303
void setLambda(double c)
Definition: vpServo.h:404
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:215
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:926
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:487
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:83
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:62