ViSP  2.6.2
servoSimuCylinder2DCamVelocityDisplaySecondaryTask.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuCylinder2DCamVelocityDisplaySecondaryTask.cpp 2457 2010-01-07 10:41:18Z nmelchio $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 on a cylinder.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
59 #include <visp/vpDebug.h>
60 #include <visp/vpConfig.h>
61 
62 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
63 
64 #include <stdlib.h>
65 #include <stdio.h>
66 
67 #include <visp/vpMath.h>
68 #include <visp/vpHomogeneousMatrix.h>
69 #include <visp/vpFeatureLine.h>
70 #include <visp/vpCylinder.h>
71 #include <visp/vpServo.h>
72 #include <visp/vpRobotCamera.h>
73 #include <visp/vpFeatureBuilder.h>
74 
75 
76 // Exception
77 #include <visp/vpException.h>
78 #include <visp/vpMatrixException.h>
79 
80 // Debug trace
81 #include <visp/vpDebug.h>
82 
83 
84 #include <visp/vpServoDisplay.h>
85 #include <visp/vpProjectionDisplay.h>
86 
87 #include <visp/vpImage.h>
88 #include <visp/vpDisplayX.h>
89 #include <visp/vpDisplayGTK.h>
90 #include <visp/vpDisplayGDI.h>
91 #include <visp/vpCameraParameters.h>
92 #include <visp/vpParseArgv.h>
93 
94 // List of allowed command line options
95 #define GETOPTARGS "cdh"
96 
97 
106 void usage(const char *name, const char *badparam)
107 {
108  fprintf(stdout, "\n\
109 Simulation of a 2D visual servoing on a cylinder:\n\
110 - eye-in-hand control law,\n\
111 - velocity computed in the camera frame,\n\
112 - display the camera view.\n\
113  \n\
114 SYNOPSIS\n\
115  %s [-c] [-d] [-h]\n", name);
116 
117  fprintf(stdout, "\n\
118 OPTIONS: Default\n\
119  \n\
120  -c\n\
121  Disable the mouse click. Useful to automaze the \n\
122  execution of this program without humain intervention.\n\
123  \n\
124  -d \n\
125  Turn off the display.\n\
126  \n\
127  -h\n\
128  Print the help.\n");
129 
130  if (badparam)
131  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
132 }
133 
145 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
146 {
147  const char *optarg;
148  int c;
149  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
150 
151  switch (c) {
152  case 'c': click_allowed = false; break;
153  case 'd': display = false; break;
154  case 'h': usage(argv[0], NULL); return false; break;
155 
156  default:
157  usage(argv[0], optarg);
158  return false; break;
159  }
160  }
161 
162  if ((c == 1) || (c == -1)) {
163  // standalone param or error
164  usage(argv[0], NULL);
165  std::cerr << "ERROR: " << std::endl;
166  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
167  return false;
168  }
169 
170  return true;
171 }
172 
173 
174 int
175 main(int argc, const char ** argv)
176 {
177  bool opt_display = true;
178  bool opt_click_allowed = true;
179 
180  // Read the command line options
181  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
182  exit (-1);
183  }
184 
185  vpImage<unsigned char> Iint(512,512,0) ;
186  vpImage<unsigned char> Iext(512,512,0) ;
187 
188  // We open a window using either X11, GTK or GDI.
189 #if defined VISP_HAVE_X11
190  vpDisplayX displayInt;
191  vpDisplayX displayExt;
192 #elif defined VISP_HAVE_GTK
193  vpDisplayGTK displayInt;
194  vpDisplayGTK displayExt;
195 #elif defined VISP_HAVE_GDI
196  vpDisplayGDI displayInt;
197  vpDisplayGDI displayExt;
198 #endif
199 
200  if (opt_display) {
201  try{
202  // Display size is automatically defined by the image (Iint) and (Iext) size
203  displayInt.init(Iint, 100, 100,"Internal view") ;
204  displayExt.init(Iext,330,000, "External view") ;
205  // Display the image
206  // The image class has a member that specify a pointer toward
207  // the display that has been initialized in the display declaration
208  // therefore is is no longuer necessary to make a reference to the
209  // display variable.
210  vpDisplay::display(Iint) ;
211  vpDisplay::display(Iext) ;
212  vpDisplay::flush(Iint) ;
213  vpDisplay::flush(Iext) ;
214  }
215  catch(...)
216  {
217  vpERROR_TRACE("Error while displaying the image") ;
218  exit(-1);
219  }
220  }
221 
222  vpProjectionDisplay externalview ;
223 
224  //Set the camera parameters
225  double px, py ; px = py = 600 ;
226  double u0, v0 ; u0 = v0 = 256 ;
227 
228  vpCameraParameters cam(px,py,u0,v0);
229 
230  vpServo task ;
231  vpRobotCamera robot ;
232 
233  vpTRACE("sets the initial camera location " ) ;
234  vpHomogeneousMatrix cMo(-0.2,0.1,2,
235  vpMath::rad(5), vpMath::rad(5), vpMath::rad(20));
236 
237  robot.setPosition(cMo) ;
238 
239  vpTRACE("sets the final camera location (for simulation purpose)" ) ;
240  vpHomogeneousMatrix cMod(0,0,1,
241  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
242 
243 
244 
245  vpTRACE("sets the cylinder coordinates in the world frame " ) ;
246  vpCylinder cylinder(0,1,0, // direction
247  0,0,0, // point of the axis
248  0.1) ; // radius
249 
250  externalview.insert(cylinder) ;
251 
252  vpTRACE("sets the desired position of the visual feature ") ;
253  cylinder.track(cMod) ;
254  cylinder.print() ;
255 
256  //Build the desired line features thanks to the cylinder and especially its paramaters in the image frame
257  vpFeatureLine ld[2] ;
258  int i ;
259  for(i=0 ; i < 2 ; i++)
260  vpFeatureBuilder::create(ld[i],cylinder,i) ;
261 
262 
263  vpTRACE("project : computes the cylinder coordinates in the camera frame and its 2D coordinates" ) ;
264  vpTRACE("sets the current position of the visual feature ") ;
265  cylinder.track(cMo) ;
266  cylinder.print() ;
267 
268  //Build the current line features thanks to the cylinder and especially its paramaters in the image frame
269  vpFeatureLine l[2] ;
270  for(i=0 ; i < 2 ; i++)
271  {
272  vpFeatureBuilder::create(l[i],cylinder,i) ;
273  l[i].print() ;
274  }
275 
276  vpTRACE("define the task") ;
277  vpTRACE("\t we want an eye-in-hand control law") ;
278  vpTRACE("\t robot is controlled in the camera frame") ;
281  // it can also be interesting to test these possibilities
282  // task.setInteractionMatrixType(vpServo::CURRENT,vpServo::PSEUDO_INVERSE) ;
283  // task.setInteractionMatrixType(vpServo::MEAN, vpServo::PSEUDO_INVERSE) ;
284  // task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE) ;
285  // task.setInteractionMatrixType(vpServo::DESIRED, vpServo::TRANSPOSE) ;
286  // task.setInteractionMatrixType(vpServo::CURRENT, vpServo::TRANSPOSE) ;
287 
288  vpTRACE("\t we want to see 2 lines on 2 lines.") ;
289 
290  task.addFeature(l[0],ld[0]) ;
291  task.addFeature(l[1],ld[1]) ;
292 
293  // Set the point of view of the external view
294  vpHomogeneousMatrix cextMo(0,0,6,
295  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
296 
297  // Display the initial scene
298  vpServoDisplay::display(task,cam,Iint) ;
299  externalview.display(Iext,cextMo, cMo, cam, vpColor::red);
300  vpDisplay::flush(Iint) ;
301  vpDisplay::flush(Iext) ;
302 
303  vpTRACE("Display task information " ) ;
304  task.print() ;
305 
306  if (opt_display && opt_click_allowed) {
307  std::cout << "\n\nClick in the camera view window to start..." << std::endl;
308  vpDisplay::getClick(Iint) ;
309  }
310 
311  vpTRACE("\t set the gain") ;
312  task.setLambda(1) ;
313 
314 
315  vpTRACE("Display task information " ) ;
316  task.print() ;
317 
318  unsigned int iter=0 ;
319  vpTRACE("\t loop") ;
320  //The first loop is needed to reach the desired position
321  do
322  {
323  std::cout << "---------------------------------------------" << iter++ <<std::endl ;
324  vpColVector v ;
325 
326  if (iter==1) vpTRACE("\t\t get the robot position ") ;
327  robot.getPosition(cMo) ;
328  if (iter==1) vpTRACE("\t\t new line position ") ;
329  //retrieve x,y and Z of the vpLine structure
330 
331  // Compute the parameters of the cylinder in the camera frame and in the image frame
332  cylinder.track(cMo) ;
333 
334  //Build the current line features thanks to the cylinder and especially its paramaters in the image frame
335  for(i=0 ; i < 2 ; i++)
336  {
337  vpFeatureBuilder::create(l[i],cylinder,i) ;
338  }
339 
340  // Display the current scene
341  if (opt_display) {
342  vpDisplay::display(Iint) ;
343  vpDisplay::display(Iext) ;
344  vpServoDisplay::display(task,cam,Iint) ;
345  externalview.display(Iext,cextMo, cMo, cam, vpColor::red);
346  vpDisplay::flush(Iint);
347  vpDisplay::flush(Iext);
348  }
349 
350  if (iter==1) vpTRACE("\t\t compute the control law ") ;
351  v = task.computeControlLaw() ;
352 
353  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
355 
356  vpTRACE("\t\t || s - s* || ") ;
357  std::cout << ( task.getError() ).sumSquare() <<std::endl ;
358  }
359  while(( task.getError() ).sumSquare() > 1e-9) ;
360 
361 
362  // Second loop is to compute the control law while taking into account the secondary task.
363  // In this example the secondary task is cut in four steps.
364  // The first one consists in impose a movement of the robot along the x axis of the object frame with a velocity of 0.5.
365  // The second one consists in impose a movement of the robot along the y axis of the object frame with a velocity of 0.5.
366  // The third one consists in impose a movement of the robot along the x axis of the object frame with a velocity of -0.5.
367  // The last one consists in impose a movement of the robot along the y axis of the object frame with a velocity of -0.5.
368  // Each steps is made during 200 iterations.
369  vpColVector e1(6) ; e1 = 0 ;
370  vpColVector e2(6) ; e2 = 0 ;
371  vpColVector proj_e1 ;
372  vpColVector proj_e2 ;
373  iter = 0;
374  double rapport = 0;
375  double vitesse = 0.5;
376  unsigned int tempo = 800;
377 
378  while(iter < tempo)
379  {
380  vpColVector v ;
381 
382  robot.getPosition(cMo) ;
383 
384  cylinder.track(cMo) ;
385 
386  for(i=0 ; i < 2 ; i++)
387  {
388  vpFeatureBuilder::create(l[i],cylinder,i) ;
389  }
390 
391  if (opt_display)
392  {
393  vpDisplay::display(Iint) ;
394  vpDisplay::display(Iext) ;
395  vpServoDisplay::display(task,cam,Iint) ;
396  externalview.display(Iext,cextMo, cMo, cam, vpColor::red);
397  vpDisplay::flush(Iint);
398  vpDisplay::flush(Iext);
399  }
400 
401  v = task.computeControlLaw() ;
402 
403  if ( iter%tempo < 200 /*&& iter%tempo >= 0*/)
404  {
405  e2 = 0;
406  e1[0] = fabs(vitesse) ;
407  proj_e1 = task.secondaryTask(e1);
408  rapport = vitesse/proj_e1[0];
409  proj_e1 *= rapport ;
410  v += proj_e1 ;
411  }
412 
413  if ( iter%tempo < 400 && iter%tempo >= 200)
414  {
415  e1 = 0;
416  e2[1] = fabs(vitesse) ;
417  proj_e2 = task.secondaryTask(e2);
418  rapport = vitesse/proj_e2[1];
419  proj_e2 *= rapport ;
420  v += proj_e2 ;
421  }
422 
423  if ( iter%tempo < 600 && iter%tempo >= 400)
424  {
425  e2 = 0;
426  e1[0] = -fabs(vitesse) ;
427  proj_e1 = task.secondaryTask(e1);
428  rapport = -vitesse/proj_e1[0];
429  proj_e1 *= rapport ;
430  v += proj_e1 ;
431  }
432 
433  if ( iter%tempo < 800 && iter%tempo >= 600)
434  {
435  e1 = 0;
436  e2[1] = -fabs(vitesse) ;
437  proj_e2 = task.secondaryTask(e2);
438  rapport = -vitesse/proj_e2[1];
439  proj_e2 *= rapport ;
440  v += proj_e2 ;
441  }
442 
444 
445  iter++;
446  }
447 
448 
449  if (opt_display && opt_click_allowed) {
450  std::cout << "\nClick in the camera view window to end..." << std::endl;
451  vpDisplay::getClick(Iint) ;
452  }
453 
454  vpTRACE("Display task information " ) ;
455  task.print() ;
456  task.kill();
457 }
458 
459 #else
460 int
461 main()
462 {
463  vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities...");
464 }
465 
466 #endif
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 print(const unsigned int select=FEATURE_ALL) const
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:379
#define vpTRACE
Definition: vpDebug.h:401
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
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:250
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
vpColVector secondaryTask(vpColVector &de2dt)
Add a secondary task.
Definition: vpServo.cpp:1055
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1964
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static const vpColor red
Definition: vpColor.h:165
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:298
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
Class that defines the simplest robot: a free flying camera.
Definition: vpRobotCamera.h:65
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:186
Generic class defining intrinsic camera parameters.
Class that defines a 2D line visual feature which is composed by two parameters that are and ...
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void insert(vpForwardProjection &fp)
void getPosition(vpColVector &q)
void setPosition(const vpRobot::vpControlFrameType, const vpColVector &)
Set a displacement (frame has to be specified) in position control.
void display(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cextMo, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor color, const bool &displayTraj=false)
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
Class that defines what is a cylinder.
Definition: vpCylinder.h:97
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 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.
Definition: vpServo.h:150
interface with the image for feature display
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214