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