Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
servoSimuSphere2DCamVelocityDisplaySecondaryTask.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 
50 #include <stdio.h>
51 #include <stdlib.h>
52 
53 #include <visp3/core/vpHomogeneousMatrix.h>
54 #include <visp3/core/vpMath.h>
55 #include <visp3/core/vpSphere.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayGTK.h>
58 #include <visp3/gui/vpDisplayOpenCV.h>
59 #include <visp3/gui/vpDisplayX.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/robot/vpSimulatorCamera.h>
62 #include <visp3/visual_features/vpFeatureBuilder.h>
63 #include <visp3/visual_features/vpFeatureEllipse.h>
64 #include <visp3/vs/vpServo.h>
65 #include <visp3/vs/vpServoDisplay.h>
66 
67 // List of allowed command line options
68 #define GETOPTARGS "cdh"
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 - a secondary task is the added.\n\
89  \n\
90 SYNOPSIS\n\
91  %s [-c] [-d] [-h]\n", name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  \n\
96  -c\n\
97  Disable the mouse click. Useful to automaze the \n\
98  execution of this program without humain 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], NULL);
137  return false;
138  break;
139 
140  default:
141  usage(argv[0], optarg_);
142  return false;
143  break;
144  }
145  }
146 
147  if ((c == 1) || (c == -1)) {
148  // standalone param or error
149  usage(argv[0], NULL);
150  std::cerr << "ERROR: " << std::endl;
151  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
152  return false;
153  }
154 
155  return true;
156 }
157 
158 int main(int argc, const char **argv)
159 {
160  try {
161  bool opt_display = true;
162  bool opt_click_allowed = true;
163 
164  // Read the command line options
165  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
166  return(EXIT_FAILURE);
167  }
168 
169  vpImage<unsigned char> I(512, 512, 0);
170 
171 // We open a window using either X11, GTK or GDI.
172 #if defined VISP_HAVE_X11
173  vpDisplayX display;
174 #elif defined VISP_HAVE_GTK
175  vpDisplayGTK display;
176 #elif defined VISP_HAVE_GDI
177  vpDisplayGDI display;
178 #elif defined VISP_HAVE_OPENCV
179  vpDisplayOpenCV display;
180 #endif
181 
182  if (opt_display) {
183 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
184  // Display size is automatically defined by the image (I) size
185  display.init(I, 100, 100, "Camera view...");
186 #endif
187  // Display the image
188  // The image class has a member that specify a pointer toward
189  // the display that has been initialized in the display declaration
190  // therefore is is no longuer necessary to make a reference to the
191  // display variable.
193  vpDisplay::flush(I);
194  }
195 
196  double px = 600, py = 600;
197  double u0 = I.getWidth()/2., v0 = I.getHeight() / 2.;
198 
199  vpCameraParameters cam(px, py, u0, v0);
200 
201  vpServo task;
202  vpSimulatorCamera robot;
203 
204  // sets the initial camera location
206  cMo[0][3] = 0.1;
207  cMo[1][3] = 0.2;
208  cMo[2][3] = 2;
209  // Compute the position of the object in the world frame
210  vpHomogeneousMatrix wMc, wMo;
211  robot.getPosition(wMc);
212  wMo = wMc * cMo;
213 
214  vpHomogeneousMatrix cMod;
215  cMod[0][3] = 0;
216  cMod[1][3] = 0;
217  cMod[2][3] = 1;
218 
219  // sets the sphere coordinates in the world frame
220  vpSphere sphere;
221  sphere.setWorldCoordinates(0, 0, 0, 0.1);
222 
223  // sets the desired position of the visual feature
224  vpFeatureEllipse pd;
225  sphere.track(cMod);
226  vpFeatureBuilder::create(pd, sphere);
227 
228  // computes the sphere coordinates in the camera frame and its 2D
229  // coordinates sets the current position of the visual feature
231  sphere.track(cMo);
232  vpFeatureBuilder::create(p, sphere);
233 
234  // define the task
235  // - we want an eye-in-hand control law
236  // - robot is controlled in the camera frame
238 
239  // we want to see a sphere on a sphere
240  std::cout << std::endl;
241  task.addFeature(p, pd);
242 
243  // set the gain
244  task.setLambda(1);
245 
246  // Display task information
247  task.print();
248  // exit(1) ;
249  unsigned int iter = 0;
250  // loop
251  while (iter++ < 200) {
252  std::cout << "---------------------------------------------" << iter << std::endl;
253  vpColVector v;
254 
255  // get the robot position
256  robot.getPosition(wMc);
257  // Compute the position of the camera wrt the object frame
258  cMo = wMc.inverse() * wMo;
259 
260  // new sphere position: retrieve x,y and Z of the vpSphere structure
261  sphere.track(cMo);
262  vpFeatureBuilder::create(p, sphere);
263 
264  vpColVector de2dt(6);
265  de2dt[2] = 1; // should be zero in (I-WpW)de2dt
266  de2dt[5] = 0.01; // should be ok
267  de2dt[0] = 0.01; // should generate a motion on (I-WpW)de2dt[4]
268 
269  if (opt_display) {
271  vpServoDisplay::display(task, cam, I);
272  vpDisplay::flush(I);
273  }
274 
275  // compute the control law
276  v = task.computeControlLaw();
277 
278  std::cout << "de2dt :" << de2dt.t() << std::endl;
279  vpColVector sec;
280  sec = task.secondaryTask(de2dt);
281  std::cout << "(I-WpW)de2dt :" << sec.t() << std::endl;
282 
283  if (iter > 20)
284  v += sec;
285 
286  // send the camera velocity to the controller
288 
289  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
290  }
291 
292  // Display task information
293  task.print();
294  task.kill();
295  return EXIT_SUCCESS;
296  } catch (const vpException &e) {
297  std::cout << "Catch a ViSP exception: " << e << std::endl;
298  return EXIT_FAILURE;
299  }
300 }
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
unsigned int getWidth() const
Definition: vpImage.h:239
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:497
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 what is a sphere.
Definition: vpSphere.h:60
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:51
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 secondaryTask(const vpColVector &de2dt, const bool &useLargeProjectionOperator=false)
Definition: vpServo.cpp:1485
void kill()
Definition: vpServo.cpp:192
vpColVector getError() const
Definition: vpServo.h:282
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...
vpRowVector t() const
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:138
vpHomogeneousMatrix getPosition() const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:313
unsigned int getHeight() const
Definition: vpImage.h:178
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
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)