ViSP  2.8.0
servoSimuSphere2DCamVelocity.cpp
1 
2 /****************************************************************************
3  *
4  * $Id: servoSimuSphere2DCamVelocity.cpp 2457 2010-01-07 10:41:18Z nmelchio $
5  *
6  * This file is part of the ViSP software.
7  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
8  *
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * ("GPL") version 2 as published by the Free Software Foundation.
12  * See the file LICENSE.txt at the root directory of this source
13  * distribution for additional information about the GNU GPL.
14  *
15  * For using ViSP with software that can not be combined with the GNU
16  * GPL, please contact INRIA about acquiring a ViSP Professional
17  * Edition License.
18  *
19  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
20  *
21  * This software was developed at:
22  * INRIA Rennes - Bretagne Atlantique
23  * Campus Universitaire de Beaulieu
24  * 35042 Rennes Cedex
25  * France
26  * http://www.irisa.fr/lagadic
27  *
28  * If you have questions regarding the use of this file, please contact
29  * INRIA at visp@inria.fr
30  *
31  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
32  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
33  *
34  *
35  * Description:
36  * Simulation of a 2D visual servoing on a sphere.
37  *
38  * Authors:
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 
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/vpServo.h>
63 #include <visp/vpSphere.h>
64 #include <visp/vpSimulatorCamera.h>
65 
66 // List of allowed command line options
67 #define GETOPTARGS "h"
68 
77 void usage(const char *name, const char *badparam)
78 {
79  fprintf(stdout, "\n\
80 Simulation of a 2D visual servoing on a sphere:\n\
81 - eye-in-hand control law,\n\
82 - velocity computed in the camera frame,\n\
83 - without display.\n\
84  \n\
85 SYNOPSIS\n\
86  %s [-h]\n", name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  \n\
91  -h\n\
92  Print the help.\n");
93 
94  if (badparam)
95  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
96 }
97 
108 bool getOptions(int argc, const char **argv)
109 {
110  const char *optarg;
111  int c;
112  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
113 
114  switch (c) {
115  case 'h': usage(argv[0], NULL); return false; break;
116 
117  default:
118  usage(argv[0], optarg);
119  return false; break;
120  }
121  }
122 
123  if ((c == 1) || (c == -1)) {
124  // standalone param or error
125  usage(argv[0], NULL);
126  std::cerr << "ERROR: " << std::endl;
127  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
128  return false;
129  }
130 
131  return true;
132 }
133 
134 int
135 main(int argc, const char ** argv)
136 {
137  // Read the command line options
138  if (getOptions(argc, argv) == false) {
139  exit (-1);
140  }
141 
142  vpServo task ;
143  vpSimulatorCamera robot ;
144 
145  std::cout << std::endl ;
146  std::cout << "-------------------------------------------------------" << std::endl ;
147  std::cout << " Test program for vpServo " <<std::endl ;
148  std::cout << " Simulation " << std::endl ;
149  std::cout << " task : servo a sphere " << std::endl ;
150  std::cout << "-------------------------------------------------------" << std::endl ;
151  std::cout << std::endl ;
152 
153  // sets the initial camera location
154  vpHomogeneousMatrix cMo ;
155  cMo[0][3] = 0.1 ;
156  cMo[1][3] = 0.2 ;
157  cMo[2][3] = 2 ;
158  // Compute the position of the object in the world frame
159  vpHomogeneousMatrix wMc, wMo;
160  robot.getPosition(wMc) ;
161  wMo = wMc * cMo;
162 
163  vpHomogeneousMatrix cMod ;
164  cMod[0][3] = 0 ;
165  cMod[1][3] = 0 ;
166  cMod[2][3] = 1 ;
167 
168  // sets the sphere coordinates in the world frame
169  vpSphere sphere ;
170  sphere.setWorldCoordinates(0,0,0,0.1) ;
171 
172  // sets the desired position of the visual feature
173  vpFeatureEllipse pd ;
174  sphere.track(cMod) ;
175  vpFeatureBuilder::create(pd,sphere) ;
176 
177  // computes the sphere coordinates in the camera frame and its 2D coordinates
178  // sets the current position of the visual feature
179  vpFeatureEllipse p ;
180  sphere.track(cMo) ;
181  vpFeatureBuilder::create(p,sphere) ;
182 
183  // define the task
184  // - we want an eye-in-hand control law
185  // - robot is controlled in the camera frame
187 
188  // we want to see a sphere on a sphere
189  task.addFeature(p,pd) ;
190 
191  // set the gain
192  task.setLambda(1) ;
193 
194  // Display task information
195  task.print() ;
196 
197  unsigned int iter=0 ;
198  // loop
199  while(iter++ < 500)
200  {
201  std::cout << "---------------------------------------------" << iter <<std::endl ;
202  vpColVector v ;
203 
204  // get the robot position
205  robot.getPosition(wMc) ;
206  // Compute the position of the camera wrt the object frame
207  cMo = wMc.inverse() * wMo;
208 
209  // new sphere position: retrieve x,y and Z of the vpSphere structure
210  sphere.track(cMo) ;
211  vpFeatureBuilder::create(p,sphere);
212 
213  // compute the control law
214  v = task.computeControlLaw() ;
215 
216  std::cout << "Task rank: " << task.getTaskRank() << std::endl ;
217  // send the camera velocity to the controller
219 
220  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
221  }
222 
223  // Display task information
224  task.print() ;
225  task.kill();
226 }
227 
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
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
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
double getTaskRank() const
Definition: vpServo.h:389
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