ViSP
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 - 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 on a cylinder.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
57 #include <visp/vpConfig.h>
58 
59 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
60 
61 #include <stdlib.h>
62 #include <stdio.h>
63 
64 #include <visp/vpCameraParameters.h>
65 #include <visp/vpCylinder.h>
66 #include <visp/vpDisplayX.h>
67 #include <visp/vpDisplayGTK.h>
68 #include <visp/vpDisplayGDI.h>
69 #include <visp/vpDisplayOpenCV.h>
70 #include <visp/vpFeatureBuilder.h>
71 #include <visp/vpFeatureLine.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/vpSimulatorCamera.h>
79 #include <visp/vpServoDisplay.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 Simulation of a 2D visual servoing on a cylinder:\n\
99 - eye-in-hand control law,\n\
100 - velocity computed in the camera frame,\n\
101 - display the camera view.\n\
102  \n\
103 SYNOPSIS\n\
104  %s [-c] [-d] [-h]\n", name);
105 
106  fprintf(stdout, "\n\
107 OPTIONS: Default\n\
108  \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 }
122 
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 
163 int
164 main(int argc, const char ** argv)
165 {
166  try {
167  bool opt_display = true;
168  bool opt_click_allowed = 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  vpImage<unsigned char> Iint(512,512,0) ;
176  vpImage<unsigned char> Iext(512,512,0) ;
177 
178  // We open a window using either X11, GTK or GDI.
179 #if defined VISP_HAVE_X11
180  vpDisplayX displayInt;
181  vpDisplayX displayExt;
182 #elif defined VISP_HAVE_GTK
183  vpDisplayGTK displayInt;
184  vpDisplayGTK displayExt;
185 #elif defined VISP_HAVE_GDI
186  vpDisplayGDI displayInt;
187  vpDisplayGDI displayExt;
188 #elif defined VISP_HAVE_OPENCV
189  vpDisplayOpenCV displayInt;
190  vpDisplayOpenCV displayExt;
191 #endif
192 
193  if (opt_display) {
194  try{
195  // Display size is automatically defined by the image (Iint) and (Iext) size
196  displayInt.init(Iint, 100, 100,"Internal view") ;
197  displayExt.init(Iext, (int)(130+Iint.getWidth()), 100, "External view") ;
198  // Display the image
199  // The image class has a member that specify a pointer toward
200  // the display that has been initialized in the display declaration
201  // therefore is is no longuer necessary to make a reference to the
202  // display variable.
203  vpDisplay::display(Iint) ;
204  vpDisplay::display(Iext) ;
205  vpDisplay::flush(Iint) ;
206  vpDisplay::flush(Iext) ;
207  }
208  catch(...)
209  {
210  vpERROR_TRACE("Error while displaying the image") ;
211  exit(-1);
212  }
213  }
214 
215  vpProjectionDisplay externalview ;
216 
217  //Set the camera parameters
218  double px, py ; px = py = 600 ;
219  double u0, v0 ; u0 = v0 = 256 ;
220 
221  vpCameraParameters cam(px,py,u0,v0);
222 
223  vpServo task ;
224  vpSimulatorCamera robot ;
225 
226  // sets the initial camera location
227  vpHomogeneousMatrix cMo(-0.2,0.1,2,
228  vpMath::rad(5), vpMath::rad(5), vpMath::rad(20));
229 
230  vpHomogeneousMatrix wMc, wMo;
231  robot.getPosition(wMc) ;
232  wMo = wMc * cMo; // Compute the position of the object in the world frame
233 
234  // sets the final camera location (for simulation purpose)
235  vpHomogeneousMatrix cMod(0,0,1,
236  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
237 
238  // sets the cylinder coordinates in the world frame
239  vpCylinder cylinder(0,1,0, // direction
240  0,0,0, // point of the axis
241  0.1) ; // radius
242 
243  externalview.insert(cylinder) ;
244 
245  // sets the desired position of the visual feature
246  cylinder.track(cMod) ;
247  cylinder.print() ;
248 
249  //Build the desired line features thanks to the cylinder and especially its paramaters in the image frame
250  vpFeatureLine ld[2] ;
251  int i ;
252  for(i=0 ; i < 2 ; i++)
253  vpFeatureBuilder::create(ld[i],cylinder,i) ;
254 
255  // computes the cylinder coordinates in the camera frame and its 2D coordinates
256  // sets the current position of the visual feature
257  cylinder.track(cMo) ;
258  cylinder.print() ;
259 
260  //Build the current line features thanks to the cylinder and especially its paramaters in the image frame
261  vpFeatureLine l[2] ;
262  for(i=0 ; i < 2 ; i++)
263  {
264  vpFeatureBuilder::create(l[i],cylinder,i) ;
265  l[i].print() ;
266  }
267 
268  // define the task
269  // - we want an eye-in-hand control law
270  // - robot is controlled in the camera frame
273  // it can also be interesting to test these possibilities
274  // task.setInteractionMatrixType(vpServo::CURRENT,vpServo::PSEUDO_INVERSE) ;
275  // task.setInteractionMatrixType(vpServo::MEAN, vpServo::PSEUDO_INVERSE) ;
276  // task.setInteractionMatrixType(vpServo::CURRENT, vpServo::PSEUDO_INVERSE) ;
277  // task.setInteractionMatrixType(vpServo::DESIRED, vpServo::TRANSPOSE) ;
278  // task.setInteractionMatrixType(vpServo::CURRENT, vpServo::TRANSPOSE) ;
279 
280  // we want to see 2 lines on 2 lines
281  task.addFeature(l[0],ld[0]) ;
282  task.addFeature(l[1],ld[1]) ;
283 
284  // Set the point of view of the external view
285  vpHomogeneousMatrix cextMo(0,0,6,
286  vpMath::rad(40), vpMath::rad(10), vpMath::rad(60)) ;
287 
288  // Display the initial scene
289  vpServoDisplay::display(task,cam,Iint) ;
290  externalview.display(Iext,cextMo, cMo, cam, vpColor::red);
291  vpDisplay::flush(Iint) ;
292  vpDisplay::flush(Iext) ;
293 
294  // Display task information
295  task.print() ;
296 
297  if (opt_display && opt_click_allowed) {
298  std::cout << "\n\nClick in the internal camera view window to start..." << std::endl;
299  vpDisplay::getClick(Iint) ;
300  }
301 
302  // set the gain
303  task.setLambda(1) ;
304 
305  // Display task information
306  task.print() ;
307 
308  unsigned int iter=0 ;
309  // The first loop is needed to reach the desired position
310  do
311  {
312  std::cout << "---------------------------------------------" << iter++ <<std::endl ;
313  vpColVector v ;
314 
315  // get the robot position
316  robot.getPosition(wMc) ;
317  // Compute the position of the camera wrt the object frame
318  cMo = wMc.inverse() * wMo;
319 
320  // new line position
321  // retrieve x,y and Z of the vpLine structure
322  // Compute the parameters of the cylinder in the camera frame and in the image frame
323  cylinder.track(cMo) ;
324 
325  //Build the current line features thanks to the cylinder and especially its paramaters in the image frame
326  for(i=0 ; i < 2 ; i++)
327  {
328  vpFeatureBuilder::create(l[i],cylinder,i) ;
329  }
330 
331  // Display the current scene
332  if (opt_display) {
333  vpDisplay::display(Iint) ;
334  vpDisplay::display(Iext) ;
335  vpServoDisplay::display(task,cam,Iint) ;
336  externalview.display(Iext,cextMo, cMo, cam, vpColor::red);
337  vpDisplay::flush(Iint);
338  vpDisplay::flush(Iext);
339  }
340 
341  // compute the control law
342  v = task.computeControlLaw() ;
343 
344  // send the camera velocity to the controller
346 
347  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
348  }
349  while(( task.getError() ).sumSquare() > 1e-9) ;
350 
351 
352  // Second loop is to compute the control law while taking into account the secondary task.
353  // In this example the secondary task is cut in four steps.
354  // 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.
355  // 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.
356  // 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.
357  // 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.
358  // Each steps is made during 200 iterations.
359  vpColVector e1(6) ; e1 = 0 ;
360  vpColVector e2(6) ; e2 = 0 ;
361  vpColVector proj_e1 ;
362  vpColVector proj_e2 ;
363  iter = 0;
364  double rapport = 0;
365  double vitesse = 0.5;
366  unsigned int tempo = 800;
367 
368  while(iter < tempo)
369  {
370  vpColVector v ;
371 
372  robot.getPosition(wMc) ;
373  // Compute the position of the camera wrt the object frame
374  cMo = wMc.inverse() * wMo;
375 
376  cylinder.track(cMo) ;
377 
378  for(i=0 ; i < 2 ; i++)
379  {
380  vpFeatureBuilder::create(l[i],cylinder,i) ;
381  }
382 
383  if (opt_display)
384  {
385  vpDisplay::display(Iint) ;
386  vpDisplay::display(Iext) ;
387  vpServoDisplay::display(task,cam,Iint) ;
388  externalview.display(Iext,cextMo, cMo, cam, vpColor::red);
389  vpDisplay::flush(Iint);
390  vpDisplay::flush(Iext);
391  }
392 
393  v = task.computeControlLaw() ;
394 
395  if ( iter%tempo < 200 /*&& iter%tempo >= 0*/)
396  {
397  e2 = 0;
398  e1[0] = fabs(vitesse) ;
399  proj_e1 = task.secondaryTask(e1);
400  rapport = vitesse/proj_e1[0];
401  proj_e1 *= rapport ;
402  v += proj_e1 ;
403  }
404 
405  if ( iter%tempo < 400 && iter%tempo >= 200)
406  {
407  e1 = 0;
408  e2[1] = fabs(vitesse) ;
409  proj_e2 = task.secondaryTask(e2);
410  rapport = vitesse/proj_e2[1];
411  proj_e2 *= rapport ;
412  v += proj_e2 ;
413  }
414 
415  if ( iter%tempo < 600 && iter%tempo >= 400)
416  {
417  e2 = 0;
418  e1[0] = -fabs(vitesse) ;
419  proj_e1 = task.secondaryTask(e1);
420  rapport = -vitesse/proj_e1[0];
421  proj_e1 *= rapport ;
422  v += proj_e1 ;
423  }
424 
425  if ( iter%tempo < 800 && iter%tempo >= 600)
426  {
427  e1 = 0;
428  e2[1] = -fabs(vitesse) ;
429  proj_e2 = task.secondaryTask(e2);
430  rapport = -vitesse/proj_e2[1];
431  proj_e2 *= rapport ;
432  v += proj_e2 ;
433  }
434 
436 
437  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
438 
439  iter++;
440  }
441 
442  if (opt_display && opt_click_allowed) {
443  std::cout << "\nClick in the internal camera view window to end..." << std::endl;
444  vpDisplay::getClick(Iint) ;
445  }
446 
447  // Display task information
448  task.print() ;
449  task.kill();
450  return 0;
451  }
452  catch(vpException e) {
453  std::cout << "Catch a ViSP exception: " << e << std::endl;
454  return 1;
455  }
456 }
457 
458 #else
459 #include <iostream>
460 
461 int main()
462 {
463  std::cout << "You do not have X11, GTK, GDI or OpenCV display functionalities..." << std::endl;
464 }
465 
466 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
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)
unsigned int getWidth() const
Definition: vpImage.h:161
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:395
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
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
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
static const vpColor red
Definition: vpColor.h:167
vpColVector secondaryTask(const vpColVector &de2dt)
Definition: vpServo.cpp:1421
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
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 setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
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
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)
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)