Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuFourPoints2DCamVelocityDisplay.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 using 4 points as visual feature.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
55 #include <visp3/core/vpConfig.h>
56 
57 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
58 
59 #include <stdlib.h>
60 #include <stdio.h>
61 
62 #include <visp3/core/vpCameraParameters.h>
63 #include <visp3/gui/vpDisplayX.h>
64 #include <visp3/gui/vpDisplayGTK.h>
65 #include <visp3/gui/vpDisplayGDI.h>
66 #include <visp3/gui/vpDisplayOpenCV.h>
67 #include <visp3/visual_features/vpFeatureBuilder.h>
68 #include <visp3/visual_features/vpFeaturePoint.h>
69 #include <visp3/core/vpHomogeneousMatrix.h>
70 #include <visp3/core/vpImage.h>
71 #include <visp3/core/vpMath.h>
72 #include <visp3/io/vpParseArgv.h>
73 #include <visp3/gui/vpProjectionDisplay.h>
74 #include <visp3/vs/vpServo.h>
75 #include <visp3/vs/vpServoDisplay.h>
76 #include <visp3/robot/vpSimulatorCamera.h>
77 
78 // List of allowed command line options
79 #define GETOPTARGS "cdh"
80 
81 void usage(const char *name, const char *badparam);
82 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
83 
92 void usage(const char *name, const char *badparam)
93 {
94  fprintf(stdout, "\n\
95 Tests a control law with the following characteristics:\n\
96 - eye-in-hand control\n\
97 - articular velocity are computed\n\
98 - servo on 4 points,\n\
99 - internal and external camera view displays.\n\
100  \n\
101 SYNOPSIS\n\
102  %s [-c] [-d] [-h]\n", name);
103 
104  fprintf(stdout, "\n\
105 OPTIONS: Default\n\
106  -c\n\
107  Disable the mouse click. Useful to automaze the \n\
108  execution of this program without humain intervention.\n\
109  \n\
110  -d \n\
111  Turn off the display.\n\
112  \n\
113  -h\n\
114  Print the help.\n");
115 
116  if (badparam)
117  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
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': click_allowed = false; break;
139  case 'd': display = false; break;
140  case 'h': usage(argv[0], NULL); return false; break;
141 
142  default:
143  usage(argv[0], optarg_);
144  return false; break;
145  }
146  }
147 
148  if ((c == 1) || (c == -1)) {
149  // standalone param or error
150  usage(argv[0], NULL);
151  std::cerr << "ERROR: " << std::endl;
152  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
153  return false;
154  }
155 
156  return true;
157 }
158 
159 int
160 main(int argc, const char ** argv)
161 {
162  try {
163  bool opt_click_allowed = true;
164  bool opt_display = true;
165 
166  // Read the command line options
167  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
168  exit (-1);
169  }
170 
171  // We open two displays, one for the internal camera view, the other one for
172  // the external view, using either X11, GTK or GDI.
173 #if defined VISP_HAVE_X11
174  vpDisplayX displayInt;
175  vpDisplayX displayExt;
176 #elif defined VISP_HAVE_GTK
177  vpDisplayGTK displayInt;
178  vpDisplayGTK displayExt;
179 #elif defined VISP_HAVE_GDI
180  vpDisplayGDI displayInt;
181  vpDisplayGDI displayExt;
182 #elif defined VISP_HAVE_OPENCV
183  vpDisplayOpenCV displayInt;
184  vpDisplayOpenCV displayExt;
185 #endif
186 
187  // open a display for the visualization
188 
189  vpImage<unsigned char> Iint(300, 300, 0) ;
190  vpImage<unsigned char> Iext(300, 300, 0) ;
191 
192  if (opt_display) {
193  displayInt.init(Iint,0,0, "Internal view") ;
194  displayExt.init(Iext,330,000, "External view") ;
195 
196  }
197  vpProjectionDisplay externalview ;
198 
199  double px, py ; px = py = 500 ;
200  double u0, v0 ; u0 = 150, v0 = 160 ;
201 
202  vpCameraParameters cam(px,py,u0,v0);
203 
204  int i ;
205  vpServo task ;
206  vpSimulatorCamera robot ;
207 
208  std::cout << std::endl ;
209  std::cout << "----------------------------------------------" << std::endl ;
210  std::cout << " Test program for vpServo " <<std::endl ;
211  std::cout << " Eye-in-hand task control, articular velocity are computed"
212  << std::endl ;
213  std::cout << " Simulation " << std::endl ;
214  std::cout << " task : servo 4 points " << std::endl ;
215  std::cout << "----------------------------------------------" << std::endl ;
216  std::cout << std::endl ;
217 
218  // sets the initial camera location
219  vpHomogeneousMatrix cMo(-0.1,-0.1,1,
220  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
221 
222  // Compute the position of the object in the world frame
223  vpHomogeneousMatrix wMc, wMo;
224  robot.getPosition(wMc) ;
225  wMo = wMc * cMo;
226 
227  vpHomogeneousMatrix cextMo(0,0,2,
228  0,0,0) ;//vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
229 
230  // sets the point coordinates in the object frame
231  vpPoint point[4] ;
232  point[0].setWorldCoordinates(-0.1,-0.1,0) ;
233  point[1].setWorldCoordinates(0.1,-0.1,0) ;
234  point[2].setWorldCoordinates(0.1,0.1,0) ;
235  point[3].setWorldCoordinates(-0.1,0.1,0) ;
236 
237  for (i = 0 ; i < 4 ; i++)
238  externalview.insert(point[i]) ;
239 
240  // computes the point coordinates in the camera frame and its 2D coordinates
241  for (i = 0 ; i < 4 ; i++)
242  point[i].track(cMo) ;
243 
244  // sets the desired position of the point
245  vpFeaturePoint p[4] ;
246  for (i = 0 ; i < 4 ; i++)
247  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
248 
249  // sets the desired position of the feature point s*
250  vpFeaturePoint pd[4] ;
251 
252  pd[0].buildFrom(-0.1,-0.1, 1) ;
253  pd[1].buildFrom( 0.1,-0.1, 1) ;
254  pd[2].buildFrom( 0.1, 0.1, 1) ;
255  pd[3].buildFrom(-0.1, 0.1, 1) ;
256 
257  // define the task
258  // - we want an eye-in-hand control law
259  // - articular velocity are computed
262 
263  // Set the position of the camera in the end-effector frame ") ;
264  vpHomogeneousMatrix cMe ;
265  vpVelocityTwistMatrix cVe(cMe) ;
266  task.set_cVe(cVe) ;
267 
268  // Set the Jacobian (expressed in the end-effector frame)
269  vpMatrix eJe ;
270  robot.get_eJe(eJe) ;
271  task.set_eJe(eJe) ;
272 
273  // we want to see a point on a point
274  for (i = 0 ; i < 4 ; i++)
275  task.addFeature(p[i],pd[i]) ;
276 
277  // set the gain
278  task.setLambda(1) ;
279 
280  // Display task information " ) ;
281  task.print() ;
282 
283  unsigned int iter=0 ;
284  // loop
285  while(iter++<200)
286  {
287  std::cout << "---------------------------------------------" << iter <<std::endl ;
288  vpColVector v ;
289 
290  // Set the Jacobian (expressed in the end-effector frame)
291  // since q is modified eJe is modified
292  robot.get_eJe(eJe) ;
293  task.set_eJe(eJe) ;
294 
295  // get the robot position
296  robot.getPosition(wMc) ;
297  // Compute the position of the camera wrt the object frame
298  cMo = wMc.inverse() * wMo;
299 
300  // update new point position and corresponding features
301  for (i = 0 ; i < 4 ; i++)
302  {
303  point[i].track(cMo) ;
304  //retrieve x,y and Z of the vpPoint structure
305  vpFeatureBuilder::create(p[i],point[i]) ;
306  }
307  // since vpServo::MEAN interaction matrix is used, we need also to update the desired features at each iteration
308  pd[0].buildFrom(-0.1,-0.1, 1) ;
309  pd[1].buildFrom( 0.1,-0.1, 1) ;
310  pd[2].buildFrom( 0.1, 0.1, 1) ;
311  pd[3].buildFrom(-0.1, 0.1, 1) ;
312 
313  if (opt_display) {
314  vpDisplay::display(Iint) ;
315  vpDisplay::display(Iext) ;
316  vpServoDisplay::display(task,cam,Iint) ;
317  externalview.display(Iext,cextMo, cMo, cam, vpColor::green) ;
318  vpDisplay::flush(Iint);
319  vpDisplay::flush(Iext);
320  }
321 
322  // compute the control law
323  v = task.computeControlLaw() ;
324 
325  // send the camera velocity to the controller
327 
328  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
329  }
330 
331  // Display task information
332  task.print() ;
333  task.kill();
334 
335  std::cout <<"Final robot position with respect to the object frame:\n";
336  cMo.print();
337 
338  if (opt_display && opt_click_allowed) {
339  // suppressed for automate test
340  std::cout << "\n\nClick in the internal view window to end..." << std::endl;
341  vpDisplay::getClick(Iint) ;
342  }
343  return 0;
344  }
345  catch(vpException &e) {
346  std::cout << "Catch a ViSP exception: " << e << std::endl;
347  return 1;
348  }
349 }
350 #else
351 #include <iostream>
352 
353 int main()
354 {
355  std::cout << "You do not have X11, GTK, GDI or OpenCV display functionalities..." << std::endl;
356 }
357 
358 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &color, const bool &displayTraj=false, const unsigned int thickness=1)
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:128
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:460
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)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
Class that defines what is a point.
Definition: vpPoint.h:59
void kill()
Definition: vpServo.cpp:191
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
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
void insert(vpForwardProjection &fp)
vpHomogeneousMatrix getPosition() const
Implementation of a velocity twist matrix and operations on such kind of matrices.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
void buildFrom(const double x, const double y, const double Z)
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:435
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:314
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void get_eJe(vpMatrix &eJe)
interface with the image for feature display
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)