ViSP  2.9.0
servoSimuFourPoints2DCamVelocityDisplay.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuFourPoints2DCamVelocityDisplay.cpp 2503 2010-02-16 18:55:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Simulation of a 2D visual servoing using 4 points as visual feature.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
59 #include <visp/vpConfig.h>
60 
61 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
62 
63 #include <stdlib.h>
64 #include <stdio.h>
65 
66 #include <visp/vpCameraParameters.h>
67 #include <visp/vpDisplayX.h>
68 #include <visp/vpDisplayGTK.h>
69 #include <visp/vpDisplayGDI.h>
70 #include <visp/vpFeatureBuilder.h>
71 #include <visp/vpFeaturePoint.h>
72 #include <visp/vpHomogeneousMatrix.h>
73 #include <visp/vpImage.h>
74 #include <visp/vpMath.h>
75 #include <visp/vpParseArgv.h>
76 #include <visp/vpProjectionDisplay.h>
77 #include <visp/vpServo.h>
78 #include <visp/vpServoDisplay.h>
79 #include <visp/vpSimulatorCamera.h>
80 
81 // List of allowed command line options
82 #define GETOPTARGS "cdh"
83 
84 void usage(const char *name, const char *badparam);
85 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
86 
95 void usage(const char *name, const char *badparam)
96 {
97  fprintf(stdout, "\n\
98 Tests a control law with the following characteristics:\n\
99 - eye-in-hand control\n\
100 - articular velocity are computed\n\
101 - servo on 4 points,\n\
102 - internal and external camera view displays.\n\
103  \n\
104 SYNOPSIS\n\
105  %s [-c] [-d] [-h]\n", name);
106 
107  fprintf(stdout, "\n\
108 OPTIONS: Default\n\
109  -c\n\
110  Disable the mouse click. Useful to automaze the \n\
111  execution of this program without humain intervention.\n\
112  \n\
113  -d \n\
114  Turn off the display.\n\
115  \n\
116  -h\n\
117  Print the help.\n");
118 
119  if (badparam)
120  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
121 }
134 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
135 {
136  const char *optarg_;
137  int c;
138  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
139 
140  switch (c) {
141  case 'c': click_allowed = false; break;
142  case 'd': display = false; break;
143  case 'h': usage(argv[0], NULL); return false; break;
144 
145  default:
146  usage(argv[0], optarg_);
147  return false; break;
148  }
149  }
150 
151  if ((c == 1) || (c == -1)) {
152  // standalone param or error
153  usage(argv[0], NULL);
154  std::cerr << "ERROR: " << std::endl;
155  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
156  return false;
157  }
158 
159  return true;
160 }
161 
162 int
163 main(int argc, const char ** argv)
164 {
165  try {
166  bool opt_click_allowed = true;
167  bool opt_display = true;
168 
169  // Read the command line options
170  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
171  exit (-1);
172  }
173 
174  // We open two displays, one for the internal camera view, the other one for
175  // the external view, using either X11, GTK or GDI.
176 #if defined VISP_HAVE_X11
177  vpDisplayX displayInt;
178  vpDisplayX displayExt;
179 #elif defined VISP_HAVE_GTK
180  vpDisplayGTK displayInt;
181  vpDisplayGTK displayExt;
182 #elif defined VISP_HAVE_GDI
183  vpDisplayGDI displayInt;
184  vpDisplayGDI 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 or GDI display functionalities..." << std::endl;
356 }
357 
358 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
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)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Class that defines the simplest robot: a free flying camera.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:439
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
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:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
Class that defines what is a point.
Definition: vpPoint.h:65
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void insert(vpForwardProjection &fp)
Class that consider the particular case of twist transformation matrix that allows to transform a vel...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
void buildFrom(const double x, const double y, const double Z)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:414
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:251
virtual bool getClick(bool blocking=true)=0
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:220
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)
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74