ViSP  2.8.0
servoSimuCircle2DCamVelocity.cpp
1 
2 /****************************************************************************
3  *
4  * $Id: servoSimuCircle2DCamVelocity.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 circle.
37  *
38  * Authors:
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
61 #include <stdlib.h>
62 #include <stdio.h>
63 
64 #include <visp/vpCircle.h>
65 #include <visp/vpFeatureEllipse.h>
66 #include <visp/vpFeatureBuilder.h>
67 #include <visp/vpHomogeneousMatrix.h>
68 #include <visp/vpMath.h>
69 #include <visp/vpParseArgv.h>
70 #include <visp/vpServo.h>
71 #include <visp/vpSimulatorCamera.h>
72 
73 // List of allowed command line options
74 #define GETOPTARGS "h"
75 
84 void usage(const char *name, const char *badparam)
85 {
86  fprintf(stdout, "\n\
87 Simulation of a 2D visual servoing on a circle:\n\
88 - eye-in-hand control law,\n\
89 - velocity computed in the camera frame,\n\
90 - without display.\n\
91  \n\
92 SYNOPSIS\n\
93  %s [-h]\n", name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: Default\n\
97  \n\
98  -h\n\
99  Print the help.\n");
100 
101  if (badparam)
102  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
103 }
104 
114 bool getOptions(int argc, const char **argv)
115 {
116  const char *optarg;
117  int c;
118  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
119 
120  switch (c) {
121  case 'h': usage(argv[0], NULL); return false; break;
122 
123  default:
124  usage(argv[0], optarg);
125  return false; break;
126  }
127  }
128 
129  if ((c == 1) || (c == -1)) {
130  // standalone param or error
131  usage(argv[0], NULL);
132  std::cerr << "ERROR: " << std::endl;
133  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
134  return false;
135  }
136 
137  return true;
138 }
139 
140 int
141 main(int argc, const char ** argv)
142 {
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 circle " << 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 
166  vpHomogeneousMatrix wMc, wMo;
167  robot.getPosition(wMc) ;
168  wMo = wMc * cMo; // Compute the position of the object in the world frame
169 
170  vpHomogeneousMatrix cMod ;
171  cMod[0][3] = 0 ;
172  cMod[1][3] = 0 ;
173  cMod[2][3] = 1 ;
174 
175  // sets the circle coordinates in the world frame
176  vpCircle circle ;
177  circle.setWorldCoordinates(0,0,1,0,0,0,0.1) ;
178 
179  // sets the desired position of the visual feature
180  vpFeatureEllipse pd ;
181  circle.track(cMod) ;
182  vpFeatureBuilder::create(pd,circle) ;
183 
184  // project : computes the circle coordinates in the camera frame and its 2D coordinates
185 
186  // sets the current position of the visual feature
187  vpFeatureEllipse p ;
188  circle.track(cMo) ;
189  vpFeatureBuilder::create(p,circle) ;
190 
191  // define the task
192  // - we want an eye-in-hand control law
193  // - robot is controlled in the camera frame
195 
196  // - we want to see a circle on a circle
197  std::cout << std::endl ;
198  task.addFeature(p,pd) ;
199 
200  // - set the gain
201  task.setLambda(1) ;
202 
203  // Display task information
204  task.print() ;
205 
206  unsigned int iter=0 ;
207  // loop
208  while(iter++ < 500)
209  {
210  std::cout << "---------------------------------------------" << iter <<std::endl ;
211  vpColVector v ;
212 
213  // get the robot position
214  robot.getPosition(wMc) ;
215  // Compute the position of the camera wrt the object frame
216  cMo = wMc.inverse() * wMo;
217 
218  // new circle position: retrieve x,y and Z of the vpCircle structure
219  circle.track(cMo) ;
220  vpFeatureBuilder::create(p,circle);
221 
222  // compute the control law
223  v = task.computeControlLaw() ;
224  std::cout << "task rank: " << task.getTaskRank() <<std::endl ;
225  // send the camera velocity to the controller
227 
228  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl ;
229  }
230 
231  // Display task information
232  task.print() ;
233  task.kill();
234 }
235 
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)
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
Class that defines what is a circle.
Definition: vpCircle.h:61
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:66