Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuCircle2DCamVelocityDisplay.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Simulation of a 2D visual servoing on a circle.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
49 #include <visp3/core/vpDebug.h>
50 #include <visp3/core/vpConfig.h>
51 
52 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
53 
54 #include <stdlib.h>
55 #include <stdio.h>
56 
57 #include <visp3/core/vpCameraParameters.h>
58 #include <visp3/core/vpCircle.h>
59 #include <visp3/gui/vpDisplayX.h>
60 #include <visp3/gui/vpDisplayGTK.h>
61 #include <visp3/gui/vpDisplayGDI.h>
62 #include <visp3/gui/vpDisplayOpenCV.h>
63 #include <visp3/visual_features/vpFeatureBuilder.h>
64 #include <visp3/visual_features/vpFeatureLine.h>
65 #include <visp3/core/vpHomogeneousMatrix.h>
66 #include <visp3/core/vpImage.h>
67 #include <visp3/core/vpMath.h>
68 #include <visp3/io/vpParseArgv.h>
69 #include <visp3/vs/vpServo.h>
70 #include <visp3/robot/vpSimulatorCamera.h>
71 #include <visp3/vs/vpServoDisplay.h>
72 
73 // List of allowed command line options
74 #define GETOPTARGS "cdh"
75 
76 void usage(const char *name, const char *badparam);
77 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
78 
87 void usage(const char *name, const char *badparam)
88 {
89  fprintf(stdout, "\n\
90 Simulation of a 2D visual servoing on a circle:\n\
91 - eye-in-hand control law,\n\
92 - velocity computed in the camera frame,\n\
93 - display the camera view.\n\
94  \n\
95 SYNOPSIS\n\
96  %s [-c] [-d] [-h]\n", name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  \n\
101  -c\n\
102  Disable the mouse click. Useful to automaze the \n\
103  execution of this program without humain intervention.\n\
104  \n\
105  -d \n\
106  Turn off the display.\n\
107  \n\
108  -h\n\
109  Print the help.\n");
110 
111  if (badparam) {
112  fprintf(stderr, "ERROR: \n" );
113  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
114  }
115 }
116 
129 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
130 {
131  const char *optarg_;
132  int c;
133  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
134 
135  switch (c) {
136  case 'c': click_allowed = false; break;
137  case 'd': display = false; break;
138  case 'h': usage(argv[0], NULL); return false; break;
139 
140  default:
141  usage(argv[0], optarg_);
142  return false; break;
143  }
144  }
145 
146  if ((c == 1) || (c == -1)) {
147  // standalone param or error
148  usage(argv[0], NULL);
149  std::cerr << "ERROR: " << std::endl;
150  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
151  return false;
152  }
153 
154  return true;
155 }
156 
157 
158 int
159 main(int argc, const char ** argv)
160 {
161  try {
162  bool opt_display = true;
163  bool opt_click_allowed = true;
164 
165  // Read the command line options
166  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
167  exit (-1);
168  }
169 
170  vpImage<unsigned char> I(512,512,0) ;
171 
172  // We open a window using either X11, GTK or GDI.
173 #if defined VISP_HAVE_X11
174  vpDisplayX display;
175 #elif defined VISP_HAVE_GTK
176  vpDisplayGTK display;
177 #elif defined VISP_HAVE_GDI
178  vpDisplayGDI display;
179 #elif defined VISP_HAVE_OPENCV
180  vpDisplayOpenCV display;
181 #endif
182 
183  if (opt_display) {
184  try{
185  // Display size is automatically defined by the image (I) size
186  display.init(I, 100, 100,"Camera view...") ;
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.
192  vpDisplay::display(I) ;
193  vpDisplay::flush(I) ;
194  }
195  catch(...)
196  {
197  vpERROR_TRACE("Error while displaying the image") ;
198  exit(-1);
199  }
200  }
201 
202  double px, py ; px = py = 600 ;
203  double u0, v0 ; u0 = v0 = 256 ;
204 
205  vpCameraParameters cam(px,py,u0,v0);
206 
207  vpServo task ;
208  vpSimulatorCamera robot ;
209 
210  // sets the initial camera location
211  vpHomogeneousMatrix cMo(0,0,1,
212  vpMath::rad(0), vpMath::rad(80), vpMath::rad(30)) ;
213  vpHomogeneousMatrix wMc, wMo;
214  robot.getPosition(wMc) ;
215  wMo = wMc * cMo; // Compute the position of the object in the world frame
216 
217  vpHomogeneousMatrix cMod(-0.1,-0.1,0.7,
218  vpMath::rad(40), vpMath::rad(10), vpMath::rad(30)) ;
219 
220  // sets the circle coordinates in the world frame
221  vpCircle circle ;
222  circle.setWorldCoordinates(0,0,1,
223  0,0,0,
224  0.1) ;
225 
226  // sets the desired position of the visual feature
227  vpFeatureEllipse pd ;
228  circle.track(cMod) ;
229  vpFeatureBuilder::create(pd,circle) ;
230 
231  // project : computes the circle coordinates in the camera frame and its 2D coordinates
232  // sets the current position of the visual feature
233  vpFeatureEllipse p ;
234  circle.track(cMo) ;
235  vpFeatureBuilder::create(p,circle) ;
236 
237  // define the task
238  // - we want an eye-in-hand control law
239  // - robot is controlled in the camera frame
242  // - we want to see a circle on a circle
243  task.addFeature(p,pd) ;
244  // - set the gain
245  task.setLambda(1) ;
246 
247  // Display task information
248  task.print() ;
249 
250  unsigned int iter=0 ;
251  // loop
252  while(iter++ < 200)
253  {
254  std::cout << "---------------------------------------------" << iter <<std::endl ;
255  vpColVector v ;
256 
257  // get the robot position
258  robot.getPosition(wMc) ;
259  // Compute the position of the camera wrt the object frame
260  cMo = wMc.inverse() * wMo;
261 
262  // new circle position
263  // retrieve x,y and Z of the vpCircle structure
264  circle.track(cMo) ;
265  vpFeatureBuilder::create(p,circle);
266  circle.print() ;
267  p.print() ;
268 
269  if (opt_display) {
270  vpDisplay::display(I) ;
271  vpServoDisplay::display(task,cam,I) ;
272  vpDisplay::flush(I) ;
273  }
274 
275  // compute the control law
276  v = task.computeControlLaw() ;
277  std::cout << "task rank: " << task.getTaskRank() <<std::endl ;
278  // send the camera velocity to the controller
280 
281  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
282  }
283 
284  // Display task information
285  task.print() ;
286  task.kill();
287 
288  if (opt_display && opt_click_allowed) {
289  std::cout << "Click in the camera view window to end..." << std::endl;
291  }
292  return 0;
293  }
294  catch(vpException &e) {
295  std::cout << "Catch a ViSP exception: " << e << std::endl;
296  return 1;
297  }
298 }
299 #else
300 int
301 main()
302 {
303  vpERROR_TRACE("You do not have X11, GTK, GDI or OpenCV display functionalities...");
304 }
305 
306 #endif
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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.
#define vpERROR_TRACE
Definition: vpDebug.h:391
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:153
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:512
error that can be emited by ViSP classes.
Definition: vpException.h:73
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:76
void kill()
Definition: vpServo.cpp:191
vpColVector getError() const
Definition: vpServo.h:271
virtual void print() const
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
void print(const unsigned int select=FEATURE_ALL) const
print the name of the feature
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:391
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
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
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:314
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines what is a circle.
Definition: vpCircle.h:57
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222
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:1849
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:62