Visual Servoing Platform  version 3.4.0
servoSimuCircle2DCamVelocityDisplay.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 circle.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
50 #include <visp3/core/vpConfig.h>
51 #include <visp3/core/vpDebug.h>
52 
53 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) \
54  && (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
55 
56 #include <stdio.h>
57 #include <stdlib.h>
58 
59 #include <visp3/core/vpCameraParameters.h>
60 #include <visp3/core/vpCircle.h>
61 #include <visp3/core/vpHomogeneousMatrix.h>
62 #include <visp3/core/vpImage.h>
63 #include <visp3/core/vpMath.h>
64 #include <visp3/gui/vpDisplayGDI.h>
65 #include <visp3/gui/vpDisplayGTK.h>
66 #include <visp3/gui/vpDisplayOpenCV.h>
67 #include <visp3/gui/vpDisplayX.h>
68 #include <visp3/io/vpParseArgv.h>
69 #include <visp3/robot/vpSimulatorCamera.h>
70 #include <visp3/visual_features/vpFeatureBuilder.h>
71 #include <visp3/visual_features/vpFeatureLine.h>
72 #include <visp3/vs/vpServo.h>
73 #include <visp3/vs/vpServoDisplay.h>
74 
75 // List of allowed command line options
76 #define GETOPTARGS "cdh"
77 
78 void usage(const char *name, const char *badparam);
79 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
80 
89 void usage(const char *name, const char *badparam)
90 {
91  fprintf(stdout, "\n\
92 Simulation of a 2D visual servoing on a circle:\n\
93 - eye-in-hand control law,\n\
94 - velocity computed in the camera frame,\n\
95 - display the camera view.\n\
96  \n\
97 SYNOPSIS\n\
98  %s [-c] [-d] [-h]\n", name);
99 
100  fprintf(stdout, "\n\
101 OPTIONS: Default\n\
102  \n\
103  -c\n\
104  Disable the mouse click. Useful to automaze the \n\
105  execution of this program without humain intervention.\n\
106  \n\
107  -d \n\
108  Turn off the display.\n\
109  \n\
110  -h\n\
111  Print the help.\n");
112 
113  if (badparam) {
114  fprintf(stderr, "ERROR: \n");
115  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
116  }
117 }
118 
131 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
132 {
133  const char *optarg_;
134  int c;
135  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
136 
137  switch (c) {
138  case 'c':
139  click_allowed = false;
140  break;
141  case 'd':
142  display = false;
143  break;
144  case 'h':
145  usage(argv[0], NULL);
146  return false;
147 
148  default:
149  usage(argv[0], optarg_);
150  return false;
151  }
152  }
153 
154  if ((c == 1) || (c == -1)) {
155  // standalone param or error
156  usage(argv[0], NULL);
157  std::cerr << "ERROR: " << std::endl;
158  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
159  return false;
160  }
161 
162  return true;
163 }
164 
165 int main(int argc, const char **argv)
166 {
167  try {
168  bool opt_display = true;
169  bool opt_click_allowed = true;
170 
171  // Read the command line options
172  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
173  return(EXIT_FAILURE);
174  }
175 
176  vpImage<unsigned char> I(512, 512, 0);
177 
178 // We open a window using either X11, GTK or GDI.
179 #if defined VISP_HAVE_X11
180  vpDisplayX display;
181 #elif defined VISP_HAVE_GTK
182  vpDisplayGTK display;
183 #elif defined VISP_HAVE_GDI
184  vpDisplayGDI display;
185 #elif defined VISP_HAVE_OPENCV
186  vpDisplayOpenCV display;
187 #endif
188 
189  if (opt_display) {
190  // Display size is automatically defined by the image (I) size
191  display.init(I, 100, 100, "Camera view...");
192  // Display the image
193  // The image class has a member that specify a pointer toward
194  // the display that has been initialized in the display declaration
195  // therefore is is no longuer necessary to make a reference to the
196  // display variable.
198  vpDisplay::flush(I);
199  }
200 
201  double px = 600, py = 600;
202  double u0 = I.getWidth()/2., v0 = I.getHeight() / 2.;
203 
204  vpCameraParameters cam(px, py, u0, v0);
205 
206  vpServo task;
207  vpSimulatorCamera robot;
208 
209  // sets the initial camera location
210  vpHomogeneousMatrix cMo(0, 0, 1, vpMath::rad(0), vpMath::rad(80), vpMath::rad(30));
211  vpHomogeneousMatrix wMc, wMo;
212  robot.getPosition(wMc);
213  wMo = wMc * cMo; // Compute the position of the object in the world frame
214 
215  vpHomogeneousMatrix cMod(-0.1, -0.1, 0.7, vpMath::rad(40), vpMath::rad(10), vpMath::rad(30));
216 
217  // sets the circle coordinates in the world frame
218  vpCircle circle;
219  circle.setWorldCoordinates(0, 0, 1, 0, 0, 0, 0.1);
220 
221  // sets the desired position of the visual feature
222  vpFeatureEllipse pd;
223  circle.track(cMod);
224  vpFeatureBuilder::create(pd, circle);
225 
226  // project : computes the circle coordinates in the camera frame and its
227  // 2D coordinates sets the current position of the visual feature
229  circle.track(cMo);
230  vpFeatureBuilder::create(p, circle);
231 
232  // define the task
233  // - we want an eye-in-hand control law
234  // - robot is controlled in the camera frame
237  // - we want to see a circle on a circle
238  task.addFeature(p, pd);
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 circle position
257  // retrieve x,y and Z of the vpCircle structure
258  circle.track(cMo);
259  vpFeatureBuilder::create(p, circle);
260  circle.print();
261  p.print();
262 
263  if (opt_display) {
265  vpServoDisplay::display(task, cam, I);
266  vpDisplay::flush(I);
267  }
268 
269  // compute the control law
270  v = task.computeControlLaw();
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  // Display task information
279  task.print();
280 
281  if (opt_display && opt_click_allowed) {
282  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::white);
283  vpDisplay::flush(I);
285  }
286  return EXIT_SUCCESS;
287  } catch (const vpException &e) {
288  std::cout << "Catch a ViSP exception: " << e << std::endl;
289  return EXIT_FAILURE;
290  }
291 }
292 #elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
293 int main()
294 {
295  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
296  return EXIT_SUCCESS;
297 }
298 #else
299 int main()
300 {
301  std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) or OpenCV functionalities to display images..." << std::endl;
302  std::cout << "Tip if you are on a unix-like system:" << std::endl;
303  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
304  std::cout << "Tip if you are on a windows-like system:" << std::endl;
305  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
306  return EXIT_SUCCESS;
307 }
308 #endif
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
void print(unsigned int select=FEATURE_ALL) const
print the name of the feature
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)
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
virtual void print() const
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
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
static double rad(double deg)
Definition: vpMath.h:110
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)
Class that defines a 3D circle in the object frame and allows forward projection of a 3D circle in th...
Definition: vpCircle.h:91
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
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:60