ViSP  2.8.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 - 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 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 
78 void usage(const char *name, const char *badparam)
79 {
80  fprintf(stdout, "\n\
81 Simulation of a 2D visual servoing on a sphere:\n\
82 - eye-in-hand control law,\n\
83 - velocity computed in the camera frame,\n\
84 - without display,\n\
85 - a secondary task is the added.\n\
86  \n\
87 SYNOPSIS\n\
88  %s [-h]\n", name);
89 
90  fprintf(stdout, "\n\
91 OPTIONS: Default\n\
92  \n\
93  -h\n\
94  Print the help.\n");
95 
96  if (badparam)
97  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
98 }
99 
110 bool getOptions(int argc, const char **argv)
111 {
112  const char *optarg;
113  int c;
114  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
115 
116  switch (c) {
117  case 'h': usage(argv[0], NULL); return false; break;
118 
119  default:
120  usage(argv[0], optarg);
121  return false; break;
122  }
123  }
124 
125  if ((c == 1) || (c == -1)) {
126  // standalone param or error
127  usage(argv[0], NULL);
128  std::cerr << "ERROR: " << std::endl;
129  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
130  return false;
131  }
132 
133  return true;
134 }
135 
136 int
137 main(int argc, const char ** argv)
138 {
139  // Read the command line options
140  if (getOptions(argc, argv) == false) {
141  exit (-1);
142  }
143 
144  vpServo task ;
145  vpSimulatorCamera robot ;
146 
147  std::cout << std::endl ;
148  std::cout << "-------------------------------------------------------" << std::endl ;
149  std::cout << " Test program for vpServo " <<std::endl ;
150  std::cout << " Simulation " << std::endl ;
151  std::cout << " task : servo a sphere with a secondary task" << std::endl ;
152  std::cout << "-------------------------------------------------------" << std::endl ;
153  std::cout << std::endl ;
154 
155 
156  // sets the initial camera location
157  vpHomogeneousMatrix cMo ;
158  cMo[0][3] = 0.1 ;
159  cMo[1][3] = 0.2 ;
160  cMo[2][3] = 2 ;
161  // Compute the position of the object in the world frame
162  vpHomogeneousMatrix wMc, wMo;
163  robot.getPosition(wMc) ;
164  wMo = wMc * cMo;
165 
166  vpHomogeneousMatrix cMod ;
167  cMod[0][3] = 0 ;
168  cMod[1][3] = 0 ;
169  cMod[2][3] = 1 ;
170 
171  // sets the sphere coordinates in the world frame
172  vpSphere sphere ;
173  sphere.setWorldCoordinates(0,0,0,0.1) ;
174 
175  // sets the desired position of the visual feature
176  vpFeatureEllipse pd ;
177  sphere.track(cMod) ;
178  vpFeatureBuilder::create(pd,sphere) ;
179 
180  // computes the sphere coordinates in the camera frame and its 2D coordinates
181  // sets the current position of the visual feature
182  vpFeatureEllipse p ;
183  sphere.track(cMo) ;
184  vpFeatureBuilder::create(p,sphere) ;
185 
186  // define the task
187  // - we want an eye-in-hand control law
188  // - robot is controlled in the camera frame
190 
191  // we want to see a sphere on a sphere
192  std::cout << std::endl ;
193  task.addFeature(p,pd) ;
194 
195  // set the gain
196  task.setLambda(1) ;
197 
198  // Display task information
199  task.print() ;
200  // exit(1) ;
201  unsigned int iter=0 ;
202  // loop
203  while(iter++ < 500)
204  {
205  std::cout << "---------------------------------------------" << iter <<std::endl ;
206  vpColVector v ;
207 
208  // get the robot position
209  robot.getPosition(wMc) ;
210  // Compute the position of the camera wrt the object frame
211  cMo = wMc.inverse() * wMo;
212 
213  // new sphere position: retrieve x,y and Z of the vpSphere structure
214  sphere.track(cMo) ;
215  vpFeatureBuilder::create(p,sphere);
216 
217  vpColVector de2dt(6) ;
218  de2dt[2] = 1 ; // should be zero in (I-WpW)de2dt
219  de2dt[5] = 0.01 ; // should be ok
220  de2dt[0] = 0.01 ; // should generate a motion on (I-WpW)de2dt[4]
221 
222  // compute the control law
223  v = task.computeControlLaw() ;
224 
225  std::cout << "de2dt :"<< de2dt.t() << std::endl;
226  vpColVector sec ;
227  sec = task.secondaryTask(de2dt) ;
228  std::cout << "(I-WpW)de2dt :"<< sec.t() << std::endl;
229 
230  if (iter>20) v += sec ;
231 
232  // send the camera velocity to the controller
234 
235  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
236  }
237 
238  // Display task information
239  task.print() ;
240  task.kill();
241 }
242 
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)
create a new ste of two visual features
Definition: vpServo.cpp:444
void setLambda(double _lambda)
set the gain lambda
Definition: vpServo.h:253
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
vpColVector secondaryTask(vpColVector &de2dt)
Add a secondary task.
Definition: vpServo.cpp:1053
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:301
vpColVector computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
vpRowVector t() const
transpose of Vector
void getPosition(vpHomogeneousMatrix &wMc) const
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
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 descbribed in and .
Definition: vpServo.h:153
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214