ViSP  2.6.2
servoSimuSphere2DCamVelocitySecondaryTask.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuSphere2DCamVelocitySecondaryTask.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 sphere.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
65 #include <visp/vpMath.h>
66 #include <visp/vpHomogeneousMatrix.h>
67 #include <visp/vpFeatureEllipse.h>
68 #include <visp/vpSphere.h>
69 #include <visp/vpServo.h>
70 #include <visp/vpRobotCamera.h>
71 #include <visp/vpFeatureBuilder.h>
72 
73 
74 // Exception
75 #include <visp/vpException.h>
76 #include <visp/vpMatrixException.h>
77 
78 // Debug trace
79 #include <visp/vpDebug.h>
80 #include <visp/vpParseArgv.h>
81 
82 #include <stdlib.h>
83 #include <stdio.h>
84 // List of allowed command line options
85 #define GETOPTARGS "h"
86 
95 void usage(const char *name, const char *badparam)
96 {
97  fprintf(stdout, "\n\
98 Simulation of a 2D visual servoing on a sphere:\n\
99 - eye-in-hand control law,\n\
100 - velocity computed in the camera frame,\n\
101 - without display,\n\
102 - a secondary task is the added.\n\
103  \n\
104 SYNOPSIS\n\
105  %s [-h]\n", name);
106 
107  fprintf(stdout, "\n\
108 OPTIONS: Default\n\
109  \n\
110  -h\n\
111  Print the help.\n");
112 
113  if (badparam)
114  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
115 }
116 
127 bool getOptions(int argc, const char **argv)
128 {
129  const char *optarg;
130  int c;
131  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
132 
133  switch (c) {
134  case 'h': usage(argv[0], NULL); return false; break;
135 
136  default:
137  usage(argv[0], optarg);
138  return false; break;
139  }
140  }
141 
142  if ((c == 1) || (c == -1)) {
143  // standalone param or error
144  usage(argv[0], NULL);
145  std::cerr << "ERROR: " << std::endl;
146  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
147  return false;
148  }
149 
150  return true;
151 }
152 
153 int
154 main(int argc, const char ** argv)
155 {
156  // Read the command line options
157  if (getOptions(argc, argv) == false) {
158  exit (-1);
159  }
160 
161  vpServo task ;
162  vpRobotCamera robot ;
163 
164  std::cout << std::endl ;
165  std::cout << "-------------------------------------------------------" << std::endl ;
166  std::cout << " Test program for vpServo " <<std::endl ;
167  std::cout << " Simulation " << std::endl ;
168  std::cout << " task : servo a sphere with a secondary task" << std::endl ;
169  std::cout << "-------------------------------------------------------" << std::endl ;
170  std::cout << std::endl ;
171 
172 
173  vpTRACE("sets the initial camera location " ) ;
174  vpHomogeneousMatrix cMo ;
175  cMo[0][3] = 0.1 ;
176  cMo[1][3] = 0.2 ;
177  cMo[2][3] = 2 ;
178  robot.setPosition(cMo) ;
179 
180  vpHomogeneousMatrix cMod ;
181  cMod[0][3] = 0 ;
182  cMod[1][3] = 0 ;
183  cMod[2][3] = 1 ;
184 
185 
186 
187  vpTRACE("sets the sphere coordinates in the world frame " ) ;
188  vpSphere sphere ;
189  sphere.setWorldCoordinates(0,0,0,0.1) ;
190 
191  vpTRACE("sets the desired position of the visual feature ") ;
192  vpFeatureEllipse pd ;
193  sphere.track(cMod) ;
194  vpFeatureBuilder::create(pd,sphere) ;
195 
196  vpTRACE("project : computes the sphere coordinates in the camera frame and its 2D coordinates" ) ;
197 
198  vpTRACE("sets the current position of the visual feature ") ;
199  vpFeatureEllipse p ;
200  sphere.track(cMo) ;
201  vpFeatureBuilder::create(p,sphere) ;
202 
203  vpTRACE("define the task") ;
204  vpTRACE("\t we want an eye-in-hand control law") ;
205  vpTRACE("\t robot is controlled in the camera frame") ;
207 
208  vpTRACE("\t we want to see a sphere on a sphere..") ;
209  std::cout << std::endl ;
210  task.addFeature(p,pd) ;
211 
212  vpTRACE("\t set the gain") ;
213  task.setLambda(1) ;
214 
215 
216  vpTRACE("Display task information " ) ;
217  task.print() ;
218  // exit(1) ;
219  unsigned int iter=0 ;
220  vpTRACE("\t loop") ;
221  while(iter++ < 500)
222  {
223  std::cout << "---------------------------------------------" << iter <<std::endl ;
224  vpColVector v ;
225 
226  if (iter==1) vpTRACE("\t\t get the robot position ") ;
227  robot.getPosition(cMo) ;
228  if (iter==1) vpTRACE("\t\t new sphere position ") ;
229  //retrieve x,y and Z of the vpSphere structure
230 
231  sphere.track(cMo) ;
232  vpFeatureBuilder::create(p,sphere);
233 
234  vpColVector de2dt(6) ;
235  de2dt[2] = 1 ; // should be zero in (I-WpW)de2dt
236  de2dt[5] = 0.01 ; // should be ok
237  de2dt[0] = 0.01 ; // should generate a motion on (I-WpW)de2dt[4]
238 
239  if (iter==1) vpTRACE("\t\t compute the control law ") ;
240 
241  v = task.computeControlLaw() ;
242 
243  std::cout << "de2dt :"<< de2dt.t() ;
244  vpColVector sec ;
245  sec = task.secondaryTask(de2dt) ;
246  std::cout << " (I-WpW)de2dt :"<< sec.t() ;
247 
248  if (iter>20) v += sec ;
249 
250  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
252 
253  std::cout << "\t || s - s* || " ;
254  std::cout << ( task.getError() ).sumSquare() <<std::endl ;
255 
256 
257  }
258 
259  vpTRACE("Display task information " ) ;
260  task.print() ;
261  task.kill();
262 }
263 
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpTRACE
Definition: vpDebug.h:401
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 track(const vpHomogeneousMatrix &cMo)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Class that defines what is a sphere.
Definition: vpSphere.h:64
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:58
vpColVector secondaryTask(vpColVector &de2dt)
Add a secondary task.
Definition: vpServo.cpp:1055
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
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
vpRowVector t() const
transpose of Vector
void getPosition(vpColVector &q)
void setPosition(const vpRobot::vpControlFrameType, const vpColVector &)
Set a displacement (frame has to be specified) in position control.
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
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214