Visual Servoing Platform  version 3.4.0
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", name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  \n\
94  -c\n\
95  Disable the mouse click. Useful to automaze the \n\
96  execution of this program without humain intervention.\n\
97  \n\
98  -d \n\
99  Turn off the display.\n\
100  \n\
101  -h\n\
102  Print the help.\n");
103 
104  if (badparam)
105  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
106 }
107 
120 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
121 {
122  const char *optarg_;
123  int c;
124  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
125 
126  switch (c) {
127  case 'c':
128  click_allowed = false;
129  break;
130  case 'd':
131  display = false;
132  break;
133  case 'h':
134  usage(argv[0], NULL);
135  return false;
136 
137  default:
138  usage(argv[0], optarg_);
139  return false;
140  }
141  }
142 
143  if ((c == 1) || (c == -1)) {
144  // standalone param or error
145  usage(argv[0], NULL);
146  std::cerr << "ERROR: " << std::endl;
147  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
148  return false;
149  }
150 
151  return true;
152 }
153 
154 int main(int argc, const char **argv)
155 {
156 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
157  try {
158  bool opt_display = true;
159  bool opt_click_allowed = true;
160 
161  // Read the command line options
162  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
163  return(EXIT_FAILURE);
164  }
165 
166  vpImage<unsigned char> I(512, 512, 0);
167 
168 // We open a window using either X11, GTK or GDI.
169 #if defined VISP_HAVE_X11
170  vpDisplayX display;
171 #elif defined VISP_HAVE_GTK
172  vpDisplayGTK display;
173 #elif defined VISP_HAVE_GDI
174  vpDisplayGDI display;
175 #elif defined VISP_HAVE_OPENCV
176  vpDisplayOpenCV display;
177 #endif
178 
179  if (opt_display) {
180 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
181  // Display size is automatically defined by the image (I) size
182  display.init(I, 100, 100, "Camera view...");
183 #endif
184  // Display the image
185  // The image class has a member that specify a pointer toward
186  // the display that has been initialized in the display declaration
187  // therefore is is no longuer necessary to make a reference to the
188  // display variable.
190  vpDisplay::flush(I);
191  }
192 
193  double px = 600, py = 600;
194  double u0 = I.getWidth()/2., v0 = I.getHeight() / 2.;
195 
196  vpCameraParameters cam(px, py, u0, v0);
197 
198  vpServo task;
199  vpSimulatorCamera robot;
200 
201  // sets the initial camera location
203  cMo[0][3] = 0.1;
204  cMo[1][3] = 0.2;
205  cMo[2][3] = 2;
206  // Compute the position of the object in the world frame
207  vpHomogeneousMatrix wMc, wMo;
208  robot.getPosition(wMc);
209  wMo = wMc * cMo;
210 
211  vpHomogeneousMatrix cMod;
212  cMod[0][3] = 0;
213  cMod[1][3] = 0;
214  cMod[2][3] = 1;
215 
216  // sets the sphere coordinates in the world frame
217  vpSphere sphere;
218  sphere.setWorldCoordinates(0, 0, 0, 0.1);
219 
220  // sets the desired position of the visual feature
221  vpFeatureEllipse pd;
222  sphere.track(cMod);
223  vpFeatureBuilder::create(pd, sphere);
224 
225  // computes the sphere coordinates in the camera frame and its 2D
226  // coordinates sets the current position of the visual feature
228  sphere.track(cMo);
229  vpFeatureBuilder::create(p, sphere);
230 
231  // define the task
232  // - we want an eye-in-hand control law
233  // - robot is controlled in the camera frame
235 
236  // we want to see a sphere on a sphere
237  task.addFeature(p, pd);
238 
239  // set the gain
240  task.setLambda(1);
241 
242  // Display task information
243  task.print();
244 
245  unsigned int iter = 0;
246  // loop
247  while (iter++ < 200) {
248  std::cout << "---------------------------------------------" << iter << std::endl;
249  vpColVector v;
250 
251  // get the robot position
252  robot.getPosition(wMc);
253  // Compute the position of the object frame in the camera frame
254  cMo = wMc.inverse() * wMo;
255 
256  // new sphere position: retrieve x,y and Z of the vpSphere structure
257  sphere.track(cMo);
258  vpFeatureBuilder::create(p, sphere);
259 
260  if (opt_display) {
262  vpServoDisplay::display(task, cam, I);
263  vpDisplay::flush(I);
264  }
265 
266  // compute the control law
267  v = task.computeControlLaw();
268 
269  std::cout << "Task rank: " << task.getTaskRank() << std::endl;
270  // send the camera velocity to the controller
272 
273  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
274  }
275 
276  if (opt_display && opt_click_allowed) {
277  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::white);
278  vpDisplay::flush(I);
280  }
281 
282  // Display task information
283  task.print();
284  return EXIT_SUCCESS;
285  } catch (const vpException &e) {
286  std::cout << "Catch a ViSP exception: " << e << std::endl;
287  return EXIT_FAILURE;
288  }
289 #else
290  (void)argc;
291  (void)argv;
292  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
293  return EXIT_SUCCESS;
294 #endif
295 }
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
unsigned int getWidth() const
Definition: vpImage.h:246
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
error that can be emited by ViSP classes.
Definition: vpException.h:71
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void track(const vpHomogeneousMatrix &cMo)
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
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
vpColVector getError() const
Definition: vpServo.h:278
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:404
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
vpHomogeneousMatrix getPosition() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:306
unsigned int getHeight() const
Definition: vpImage.h:188
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
static const vpColor white
Definition: vpColor.h:212
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
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)
unsigned int getTaskRank() const
Definition: vpServo.cpp:1786