ViSP
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) || defined(VISP_HAVE_OPENCV))
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/vpDisplayOpenCV.h>
71 #include <visp/vpFeatureBuilder.h>
72 #include <visp/vpFeaturePoint.h>
73 #include <visp/vpHomogeneousMatrix.h>
74 #include <visp/vpImage.h>
75 #include <visp/vpMath.h>
76 #include <visp/vpParseArgv.h>
77 #include <visp/vpProjectionDisplay.h>
78 #include <visp/vpServo.h>
79 #include <visp/vpServoDisplay.h>
80 #include <visp/vpSimulatorCamera.h>
81 
82 // List of allowed command line options
83 #define GETOPTARGS "cdh"
84 
85 void usage(const char *name, const char *badparam);
86 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
87 
96 void usage(const char *name, const char *badparam)
97 {
98  fprintf(stdout, "\n\
99 Tests a control law with the following characteristics:\n\
100 - eye-in-hand control\n\
101 - articular velocity are computed\n\
102 - servo on 4 points,\n\
103 - internal and external camera view displays.\n\
104  \n\
105 SYNOPSIS\n\
106  %s [-c] [-d] [-h]\n", name);
107 
108  fprintf(stdout, "\n\
109 OPTIONS: Default\n\
110  -c\n\
111  Disable the mouse click. Useful to automaze the \n\
112  execution of this program without humain intervention.\n\
113  \n\
114  -d \n\
115  Turn off the display.\n\
116  \n\
117  -h\n\
118  Print the help.\n");
119 
120  if (badparam)
121  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
122 }
135 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
136 {
137  const char *optarg_;
138  int c;
139  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
140 
141  switch (c) {
142  case 'c': click_allowed = false; break;
143  case 'd': display = false; break;
144  case 'h': usage(argv[0], NULL); return false; break;
145 
146  default:
147  usage(argv[0], optarg_);
148  return false; break;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], NULL);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161 }
162 
163 int
164 main(int argc, const char ** argv)
165 {
166  try {
167  bool opt_click_allowed = true;
168  bool opt_display = true;
169 
170  // Read the command line options
171  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
172  exit (-1);
173  }
174 
175  // We open two displays, one for the internal camera view, the other one for
176  // the external view, using either X11, GTK or GDI.
177 #if defined VISP_HAVE_X11
178  vpDisplayX displayInt;
179  vpDisplayX displayExt;
180 #elif defined VISP_HAVE_GTK
181  vpDisplayGTK displayInt;
182  vpDisplayGTK displayExt;
183 #elif defined VISP_HAVE_GDI
184  vpDisplayGDI displayInt;
185  vpDisplayGDI displayExt;
186 #elif defined VISP_HAVE_OPENCV
187  vpDisplayOpenCV displayInt;
188  vpDisplayOpenCV displayExt;
189 #endif
190 
191  // open a display for the visualization
192 
193  vpImage<unsigned char> Iint(300, 300, 0) ;
194  vpImage<unsigned char> Iext(300, 300, 0) ;
195 
196  if (opt_display) {
197  displayInt.init(Iint,0,0, "Internal view") ;
198  displayExt.init(Iext,330,000, "External view") ;
199 
200  }
201  vpProjectionDisplay externalview ;
202 
203  double px, py ; px = py = 500 ;
204  double u0, v0 ; u0 = 150, v0 = 160 ;
205 
206  vpCameraParameters cam(px,py,u0,v0);
207 
208  int i ;
209  vpServo task ;
210  vpSimulatorCamera robot ;
211 
212  std::cout << std::endl ;
213  std::cout << "----------------------------------------------" << std::endl ;
214  std::cout << " Test program for vpServo " <<std::endl ;
215  std::cout << " Eye-in-hand task control, articular velocity are computed"
216  << std::endl ;
217  std::cout << " Simulation " << std::endl ;
218  std::cout << " task : servo 4 points " << std::endl ;
219  std::cout << "----------------------------------------------" << std::endl ;
220  std::cout << std::endl ;
221 
222  // sets the initial camera location
223  vpHomogeneousMatrix cMo(-0.1,-0.1,1,
224  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
225 
226  // Compute the position of the object in the world frame
227  vpHomogeneousMatrix wMc, wMo;
228  robot.getPosition(wMc) ;
229  wMo = wMc * cMo;
230 
231  vpHomogeneousMatrix cextMo(0,0,2,
232  0,0,0) ;//vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
233 
234  // sets the point coordinates in the object frame
235  vpPoint point[4] ;
236  point[0].setWorldCoordinates(-0.1,-0.1,0) ;
237  point[1].setWorldCoordinates(0.1,-0.1,0) ;
238  point[2].setWorldCoordinates(0.1,0.1,0) ;
239  point[3].setWorldCoordinates(-0.1,0.1,0) ;
240 
241  for (i = 0 ; i < 4 ; i++)
242  externalview.insert(point[i]) ;
243 
244  // computes the point coordinates in the camera frame and its 2D coordinates
245  for (i = 0 ; i < 4 ; i++)
246  point[i].track(cMo) ;
247 
248  // sets the desired position of the point
249  vpFeaturePoint p[4] ;
250  for (i = 0 ; i < 4 ; i++)
251  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
252 
253  // sets the desired position of the feature point s*
254  vpFeaturePoint pd[4] ;
255 
256  pd[0].buildFrom(-0.1,-0.1, 1) ;
257  pd[1].buildFrom( 0.1,-0.1, 1) ;
258  pd[2].buildFrom( 0.1, 0.1, 1) ;
259  pd[3].buildFrom(-0.1, 0.1, 1) ;
260 
261  // define the task
262  // - we want an eye-in-hand control law
263  // - articular velocity are computed
266 
267  // Set the position of the camera in the end-effector frame ") ;
268  vpHomogeneousMatrix cMe ;
269  vpVelocityTwistMatrix cVe(cMe) ;
270  task.set_cVe(cVe) ;
271 
272  // Set the Jacobian (expressed in the end-effector frame)
273  vpMatrix eJe ;
274  robot.get_eJe(eJe) ;
275  task.set_eJe(eJe) ;
276 
277  // we want to see a point on a point
278  for (i = 0 ; i < 4 ; i++)
279  task.addFeature(p[i],pd[i]) ;
280 
281  // set the gain
282  task.setLambda(1) ;
283 
284  // Display task information " ) ;
285  task.print() ;
286 
287  unsigned int iter=0 ;
288  // loop
289  while(iter++<200)
290  {
291  std::cout << "---------------------------------------------" << iter <<std::endl ;
292  vpColVector v ;
293 
294  // Set the Jacobian (expressed in the end-effector frame)
295  // since q is modified eJe is modified
296  robot.get_eJe(eJe) ;
297  task.set_eJe(eJe) ;
298 
299  // get the robot position
300  robot.getPosition(wMc) ;
301  // Compute the position of the camera wrt the object frame
302  cMo = wMc.inverse() * wMo;
303 
304  // update new point position and corresponding features
305  for (i = 0 ; i < 4 ; i++)
306  {
307  point[i].track(cMo) ;
308  //retrieve x,y and Z of the vpPoint structure
309  vpFeatureBuilder::create(p[i],point[i]) ;
310  }
311  // since vpServo::MEAN interaction matrix is used, we need also to update the desired features at each iteration
312  pd[0].buildFrom(-0.1,-0.1, 1) ;
313  pd[1].buildFrom( 0.1,-0.1, 1) ;
314  pd[2].buildFrom( 0.1, 0.1, 1) ;
315  pd[3].buildFrom(-0.1, 0.1, 1) ;
316 
317  if (opt_display) {
318  vpDisplay::display(Iint) ;
319  vpDisplay::display(Iext) ;
320  vpServoDisplay::display(task,cam,Iint) ;
321  externalview.display(Iext,cextMo, cMo, cam, vpColor::green) ;
322  vpDisplay::flush(Iint);
323  vpDisplay::flush(Iext);
324  }
325 
326  // compute the control law
327  v = task.computeControlLaw() ;
328 
329  // send the camera velocity to the controller
331 
332  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
333  }
334 
335  // Display task information
336  task.print() ;
337  task.kill();
338 
339  std::cout <<"Final robot position with respect to the object frame:\n";
340  cMo.print();
341 
342  if (opt_display && opt_click_allowed) {
343  // suppressed for automate test
344  std::cout << "\n\nClick in the internal view window to end..." << std::endl;
345  vpDisplay::getClick(Iint) ;
346  }
347  return 0;
348  }
349  catch(vpException e) {
350  std::cout << "Catch a ViSP exception: " << e << std::endl;
351  return 1;
352  }
353 }
354 #else
355 #include <iostream>
356 
357 int main()
358 {
359  std::cout << "You do not have X11, GTK, GDI or OpenCV display functionalities..." << std::endl;
360 }
361 
362 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
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:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
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:210
The vpDisplayOpenCV allows to display image using the opencv library.
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 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