ViSP  2.8.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 - 2013 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 
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 
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 #endif
183 
184  // open a display for the visualization
185 
186  vpImage<unsigned char> Iint(300, 300, 0) ;
187  vpImage<unsigned char> Iext(300, 300, 0) ;
188 
189  if (opt_display) {
190  displayInt.init(Iint,0,0, "Internal view") ;
191  displayExt.init(Iext,330,000, "External view") ;
192 
193  }
194  vpProjectionDisplay externalview ;
195 
196  double px, py ; px = py = 500 ;
197  double u0, v0 ; u0 = 150, v0 = 160 ;
198 
199  vpCameraParameters cam(px,py,u0,v0);
200 
201  int i ;
202  vpServo task ;
203  vpSimulatorCamera robot ;
204 
205  std::cout << std::endl ;
206  std::cout << "----------------------------------------------" << std::endl ;
207  std::cout << " Test program for vpServo " <<std::endl ;
208  std::cout << " Eye-in-hand task control, articular velocity are computed"
209  << std::endl ;
210  std::cout << " Simulation " << std::endl ;
211  std::cout << " task : servo 4 points " << std::endl ;
212  std::cout << "----------------------------------------------" << std::endl ;
213  std::cout << std::endl ;
214 
215  // sets the initial camera location
216  vpHomogeneousMatrix cMo(-0.1,-0.1,1,
217  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
218 
219  // Compute the position of the object in the world frame
220  vpHomogeneousMatrix wMc, wMo;
221  robot.getPosition(wMc) ;
222  wMo = wMc * cMo;
223 
224  vpHomogeneousMatrix cextMo(0,0,2,
225  0,0,0) ;//vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
226 
227  // sets the point coordinates in the object frame
228  vpPoint point[4] ;
229  point[0].setWorldCoordinates(-0.1,-0.1,0) ;
230  point[1].setWorldCoordinates(0.1,-0.1,0) ;
231  point[2].setWorldCoordinates(0.1,0.1,0) ;
232  point[3].setWorldCoordinates(-0.1,0.1,0) ;
233 
234  for (i = 0 ; i < 4 ; i++)
235  externalview.insert(point[i]) ;
236 
237  // computes the point coordinates in the camera frame and its 2D coordinates
238  for (i = 0 ; i < 4 ; i++)
239  point[i].track(cMo) ;
240 
241  // sets the desired position of the point
242  vpFeaturePoint p[4] ;
243  for (i = 0 ; i < 4 ; i++)
244  vpFeatureBuilder::create(p[i],point[i]) ; //retrieve x,y and Z of the vpPoint structure
245 
246  // sets the desired position of the feature point s*
247  vpFeaturePoint pd[4] ;
248 
249  pd[0].buildFrom(-0.1,-0.1, 1) ;
250  pd[1].buildFrom( 0.1,-0.1, 1) ;
251  pd[2].buildFrom( 0.1, 0.1, 1) ;
252  pd[3].buildFrom(-0.1, 0.1, 1) ;
253 
254  // define the task
255  // - we want an eye-in-hand control law
256  // - articular velocity are computed
259 
260  // Set the position of the camera in the end-effector frame ") ;
261  vpHomogeneousMatrix cMe ;
262  vpVelocityTwistMatrix cVe(cMe) ;
263  task.set_cVe(cVe) ;
264 
265  // Set the Jacobian (expressed in the end-effector frame)
266  vpMatrix eJe ;
267  robot.get_eJe(eJe) ;
268  task.set_eJe(eJe) ;
269 
270  // we want to see a point on a point
271  for (i = 0 ; i < 4 ; i++)
272  task.addFeature(p[i],pd[i]) ;
273 
274  // set the gain
275  task.setLambda(1) ;
276 
277  // Display task information " ) ;
278  task.print() ;
279 
280  unsigned int iter=0 ;
281  // loop
282  while(iter++<200)
283  {
284  std::cout << "---------------------------------------------" << iter <<std::endl ;
285  vpColVector v ;
286 
287  // Set the Jacobian (expressed in the end-effector frame)
288  // since q is modified eJe is modified
289  robot.get_eJe(eJe) ;
290  task.set_eJe(eJe) ;
291 
292  // get the robot position
293  robot.getPosition(wMc) ;
294  // Compute the position of the camera wrt the object frame
295  cMo = wMc.inverse() * wMo;
296 
297  // update new point position and corresponding features
298  for (i = 0 ; i < 4 ; i++)
299  {
300  point[i].track(cMo) ;
301  //retrieve x,y and Z of the vpPoint structure
302  vpFeatureBuilder::create(p[i],point[i]) ;
303  }
304  // since vpServo::MEAN interaction matrix is used, we need also to update the desired features at each iteration
305  pd[0].buildFrom(-0.1,-0.1, 1) ;
306  pd[1].buildFrom( 0.1,-0.1, 1) ;
307  pd[2].buildFrom( 0.1, 0.1, 1) ;
308  pd[3].buildFrom(-0.1, 0.1, 1) ;
309 
310  if (opt_display) {
311  vpDisplay::display(Iint) ;
312  vpDisplay::display(Iext) ;
313  vpServoDisplay::display(task,cam,Iint) ;
314  externalview.display(Iext,cextMo, cMo, cam, vpColor::green) ;
315  vpDisplay::flush(Iint);
316  vpDisplay::flush(Iext);
317  }
318 
319  // compute the control law
320  v = task.computeControlLaw() ;
321 
322  // send the camera velocity to the controller
324 
325  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
326  }
327 
328  // Display task information
329  task.print() ;
330  task.kill();
331 
332  std::cout <<"Final robot position with respect to the object frame:\n";
333  cMo.print();
334 
335  if (opt_display && opt_click_allowed) {
336  // suppressed for automate test
337  std::cout << "\n\nClick in the internal view window to end..." << std::endl;
338  vpDisplay::getClick(Iint) ;
339  }
340 }
341 #else
342 #include <iostream>
343 
344 int main()
345 {
346  std::cout << "You do not have X11, GTK or GDI display functionalities..." << std::endl;
347 }
348 
349 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
static void display(vpServo &s, const vpCameraParameters &cam, vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
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:133
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)
create a new ste of two visual features
Definition: vpServo.cpp:444
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:253
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
void set_cVe(vpVelocityTwistMatrix &_cVe)
Definition: vpServo.h:230
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
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()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
void set_eJe(vpMatrix &_eJe)
Definition: vpServo.h:238
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
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)
Set the type of the interaction matrix (current, mean, desired, user).
Definition: vpServo.cpp:509
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
vpHomogeneousMatrix inverse() const
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
virtual bool getClick(bool blocking=true)=0
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law descbribed in and .
Definition: vpServo.h:153
void get_eJe(vpMatrix &eJe)
interface with the image for feature display
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
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