ViSP  2.9.0
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 - 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 sphere.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
54 #include <stdlib.h>
55 #include <stdio.h>
56 
57 #include <visp/vpFeatureBuilder.h>
58 #include <visp/vpFeatureEllipse.h>
59 #include <visp/vpHomogeneousMatrix.h>
60 #include <visp/vpMath.h>
61 #include <visp/vpParseArgv.h>
62 #include <visp/vpRobotCamera.h>
63 #include <visp/vpServo.h>
64 #include <visp/vpSphere.h>
65 #include <visp/vpSimulatorCamera.h>
66 
67 // List of allowed command line options
68 #define GETOPTARGS "h"
69 
70 void usage(const char *name, const char *badparam);
71 bool getOptions(int argc, const char **argv);
72 
81 void usage(const char *name, const char *badparam)
82 {
83  fprintf(stdout, "\n\
84 Simulation of a 2D visual servoing on a sphere:\n\
85 - eye-in-hand control law,\n\
86 - velocity computed in the camera frame,\n\
87 - without display,\n\
88 - a secondary task is the added.\n\
89  \n\
90 SYNOPSIS\n\
91  %s [-h]\n", name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  \n\
96  -h\n\
97  Print the help.\n");
98 
99  if (badparam)
100  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
101 }
102 
113 bool getOptions(int argc, const char **argv)
114 {
115  const char *optarg_;
116  int c;
117  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118 
119  switch (c) {
120  case 'h': usage(argv[0], NULL); return false; break;
121 
122  default:
123  usage(argv[0], optarg_);
124  return false; break;
125  }
126  }
127 
128  if ((c == 1) || (c == -1)) {
129  // standalone param or error
130  usage(argv[0], NULL);
131  std::cerr << "ERROR: " << std::endl;
132  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
133  return false;
134  }
135 
136  return true;
137 }
138 
139 int
140 main(int argc, const char ** argv)
141 {
142  try {
143  // Read the command line options
144  if (getOptions(argc, argv) == false) {
145  exit (-1);
146  }
147 
148  vpServo task ;
149  vpSimulatorCamera robot ;
150 
151  std::cout << std::endl ;
152  std::cout << "-------------------------------------------------------" << std::endl ;
153  std::cout << " Test program for vpServo " <<std::endl ;
154  std::cout << " Simulation " << std::endl ;
155  std::cout << " task : servo a sphere with a secondary task" << std::endl ;
156  std::cout << "-------------------------------------------------------" << std::endl ;
157  std::cout << std::endl ;
158 
159 
160  // sets the initial camera location
161  vpHomogeneousMatrix cMo ;
162  cMo[0][3] = 0.1 ;
163  cMo[1][3] = 0.2 ;
164  cMo[2][3] = 2 ;
165  // Compute the position of the object in the world frame
166  vpHomogeneousMatrix wMc, wMo;
167  robot.getPosition(wMc) ;
168  wMo = wMc * cMo;
169 
170  vpHomogeneousMatrix cMod ;
171  cMod[0][3] = 0 ;
172  cMod[1][3] = 0 ;
173  cMod[2][3] = 1 ;
174 
175  // sets the sphere coordinates in the world frame
176  vpSphere sphere ;
177  sphere.setWorldCoordinates(0,0,0,0.1) ;
178 
179  // sets the desired position of the visual feature
180  vpFeatureEllipse pd ;
181  sphere.track(cMod) ;
182  vpFeatureBuilder::create(pd,sphere) ;
183 
184  // computes the sphere coordinates in the camera frame and its 2D coordinates
185  // sets the current position of the visual feature
186  vpFeatureEllipse p ;
187  sphere.track(cMo) ;
188  vpFeatureBuilder::create(p,sphere) ;
189 
190  // define the task
191  // - we want an eye-in-hand control law
192  // - robot is controlled in the camera frame
194 
195  // we want to see a sphere on a sphere
196  std::cout << std::endl ;
197  task.addFeature(p,pd) ;
198 
199  // set the gain
200  task.setLambda(1) ;
201 
202  // Display task information
203  task.print() ;
204  // exit(1) ;
205  unsigned int iter=0 ;
206  // loop
207  while(iter++ < 500)
208  {
209  std::cout << "---------------------------------------------" << iter <<std::endl ;
210  vpColVector v ;
211 
212  // get the robot position
213  robot.getPosition(wMc) ;
214  // Compute the position of the camera wrt the object frame
215  cMo = wMc.inverse() * wMo;
216 
217  // new sphere position: retrieve x,y and Z of the vpSphere structure
218  sphere.track(cMo) ;
219  vpFeatureBuilder::create(p,sphere);
220 
221  vpColVector de2dt(6) ;
222  de2dt[2] = 1 ; // should be zero in (I-WpW)de2dt
223  de2dt[5] = 0.01 ; // should be ok
224  de2dt[0] = 0.01 ; // should generate a motion on (I-WpW)de2dt[4]
225 
226  // compute the control law
227  v = task.computeControlLaw() ;
228 
229  std::cout << "de2dt :"<< de2dt.t() << std::endl;
230  vpColVector sec ;
231  sec = task.secondaryTask(de2dt) ;
232  std::cout << "(I-WpW)de2dt :"<< sec.t() << std::endl;
233 
234  if (iter>20) v += sec ;
235 
236  // send the camera velocity to the controller
238 
239  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
240  }
241 
242  // Display task information
243  task.print() ;
244  task.kill();
245  return 0;
246  }
247  catch(vpException e) {
248  std::cout << "Catch a ViSP exception: " << e << std::endl;
249  return 1;
250  }
251 }
252 
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
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.
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
void track(const vpHomogeneousMatrix &cMo)
Class that defines what is a sphere.
Definition: vpSphere.h:64
void setWorldCoordinates(const vpColVector &oP)
Definition: vpSphere.cpp:58
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
vpColVector secondaryTask(const vpColVector &de2dt)
Definition: vpServo.cpp:1416
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
vpRowVector t() const
transpose of Vector
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
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
Class that defines 2D ellipse visual feature.
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220