Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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  break;
137 
138  default:
139  usage(argv[0], optarg_);
140  return false;
141  break;
142  }
143  }
144 
145  if ((c == 1) || (c == -1)) {
146  // standalone param or error
147  usage(argv[0], NULL);
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  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  // Display task information
278  task.print();
279  task.kill();
280  return EXIT_SUCCESS;
281  } catch (const vpException &e) {
282  std::cout << "Catch a ViSP exception: " << e << std::endl;
283  return EXIT_FAILURE;
284  }
285 }
unsigned int getTaskRank() const
Definition: vpServo.cpp:1821
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
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:497
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
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)
vpHomogeneousMatrix inverse() const
Class that defines what is a sphere.
Definition: vpSphere.h:60
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:51
vpHomogeneousMatrix getPosition() const
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
void kill()
Definition: vpServo.cpp:192
vpColVector computeControlLaw()
Definition: vpServo.cpp:935
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:406
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
unsigned int getHeight() const
Definition: vpImage.h:186
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
vpColVector getError() const
Definition: vpServo.h:282
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
unsigned int getWidth() const
Definition: vpImage.h:244
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:223
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)