ViSP  2.9.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 - 2014 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 
69 void usage(const char *name, const char *badparam);
70 bool getOptions(int argc, const char **argv);
71 
80 void usage(const char *name, const char *badparam)
81 {
82  fprintf(stdout, "\n\
83 Simulation of a 2D visual servoing on a sphere:\n\
84 - eye-in-hand control law,\n\
85 - velocity computed in the camera frame,\n\
86 - without display.\n\
87  \n\
88 SYNOPSIS\n\
89  %s [-h]\n", name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  \n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam)
98  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99 }
100 
111 bool getOptions(int argc, const char **argv)
112 {
113  const char *optarg_;
114  int c;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117  switch (c) {
118  case 'h': usage(argv[0], NULL); return false; break;
119 
120  default:
121  usage(argv[0], optarg_);
122  return false; break;
123  }
124  }
125 
126  if ((c == 1) || (c == -1)) {
127  // standalone param or error
128  usage(argv[0], NULL);
129  std::cerr << "ERROR: " << std::endl;
130  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
131  return false;
132  }
133 
134  return true;
135 }
136 
137 int
138 main(int argc, const char ** argv)
139 {
140  try {
141  // Read the command line options
142  if (getOptions(argc, argv) == false) {
143  exit (-1);
144  }
145 
146  vpServo task ;
147  vpSimulatorCamera robot ;
148 
149  std::cout << std::endl ;
150  std::cout << "-------------------------------------------------------" << std::endl ;
151  std::cout << " Test program for vpServo " <<std::endl ;
152  std::cout << " Simulation " << std::endl ;
153  std::cout << " task : servo a sphere " << std::endl ;
154  std::cout << "-------------------------------------------------------" << std::endl ;
155  std::cout << std::endl ;
156 
157  // sets the initial camera location
158  vpHomogeneousMatrix cMo ;
159  cMo[0][3] = 0.1 ;
160  cMo[1][3] = 0.2 ;
161  cMo[2][3] = 2 ;
162  // Compute the position of the object in the world frame
163  vpHomogeneousMatrix wMc, wMo;
164  robot.getPosition(wMc) ;
165  wMo = wMc * cMo;
166 
167  vpHomogeneousMatrix cMod ;
168  cMod[0][3] = 0 ;
169  cMod[1][3] = 0 ;
170  cMod[2][3] = 1 ;
171 
172  // sets the sphere coordinates in the world frame
173  vpSphere sphere ;
174  sphere.setWorldCoordinates(0,0,0,0.1) ;
175 
176  // sets the desired position of the visual feature
177  vpFeatureEllipse pd ;
178  sphere.track(cMod) ;
179  vpFeatureBuilder::create(pd,sphere) ;
180 
181  // computes the sphere coordinates in the camera frame and its 2D coordinates
182  // sets the current position of the visual feature
183  vpFeatureEllipse p ;
184  sphere.track(cMo) ;
185  vpFeatureBuilder::create(p,sphere) ;
186 
187  // define the task
188  // - we want an eye-in-hand control law
189  // - robot is controlled in the camera frame
191 
192  // we want to see a sphere on a sphere
193  task.addFeature(p,pd) ;
194 
195  // set the gain
196  task.setLambda(1) ;
197 
198  // Display task information
199  task.print() ;
200 
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  // compute the control law
218  v = task.computeControlLaw() ;
219 
220  std::cout << "Task rank: " << task.getTaskRank() << std::endl ;
221  // send the camera velocity to the controller
223 
224  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
225  }
226 
227  // Display task information
228  task.print() ;
229  task.kill();
230  return 0;
231  }
232  catch(vpException e) {
233  std::cout << "Catch a ViSP exception: " << e << std::endl;
234  return 1;
235  }
236 }
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
void kill()
Definition: vpServo.cpp:189
vpColVector getError() const
Definition: vpServo.h:257
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
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
unsigned int getTaskRank() const
Definition: vpServo.cpp:1567