ViSP  2.9.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 - 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 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 
76 void usage(const char *name, const char *badparam);
77 bool getOptions(int argc, const char **argv);
78 
87 void usage(const char *name, const char *badparam)
88 {
89  fprintf(stdout, "\n\
90 Simulation of a 2D visual servoing on a circle:\n\
91 - eye-in-hand control law,\n\
92 - velocity computed in the camera frame,\n\
93 - without display.\n\
94  \n\
95 SYNOPSIS\n\
96  %s [-h]\n", name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  \n\
101  -h\n\
102  Print the help.\n");
103 
104  if (badparam)
105  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
106 }
107 
117 bool getOptions(int argc, const char **argv)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'h': usage(argv[0], NULL); return false; break;
125 
126  default:
127  usage(argv[0], optarg_);
128  return false; break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], NULL);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
143 int
144 main(int argc, const char ** argv)
145 {
146  try {
147  // Read the command line options
148  if (getOptions(argc, argv) == false) {
149  exit (-1);
150  }
151 
152  vpServo task ;
153  vpSimulatorCamera robot ;
154 
155  std::cout << std::endl ;
156  std::cout << "-------------------------------------------------------" << std::endl ;
157  std::cout << " Test program for vpServo " <<std::endl ;
158  std::cout << " Simulation " << std::endl ;
159  std::cout << " task : servo a circle " << std::endl ;
160  std::cout << "-------------------------------------------------------" << std::endl ;
161  std::cout << std::endl ;
162 
163 
164  // sets the initial camera location
165  vpHomogeneousMatrix cMo ;
166  cMo[0][3] = 0.1 ;
167  cMo[1][3] = 0.2 ;
168  cMo[2][3] = 2 ;
169 
170  vpHomogeneousMatrix wMc, wMo;
171  robot.getPosition(wMc) ;
172  wMo = wMc * cMo; // Compute the position of the object in the world frame
173 
174  vpHomogeneousMatrix cMod ;
175  cMod[0][3] = 0 ;
176  cMod[1][3] = 0 ;
177  cMod[2][3] = 1 ;
178 
179  // sets the circle coordinates in the world frame
180  vpCircle circle ;
181  circle.setWorldCoordinates(0,0,1,0,0,0,0.1) ;
182 
183  // sets the desired position of the visual feature
184  vpFeatureEllipse pd ;
185  circle.track(cMod) ;
186  vpFeatureBuilder::create(pd,circle) ;
187 
188  // project : computes the circle coordinates in the camera frame and its 2D coordinates
189 
190  // sets the current position of the visual feature
191  vpFeatureEllipse p ;
192  circle.track(cMo) ;
193  vpFeatureBuilder::create(p,circle) ;
194 
195  // define the task
196  // - we want an eye-in-hand control law
197  // - robot is controlled in the camera frame
199 
200  // - we want to see a circle on a circle
201  std::cout << std::endl ;
202  task.addFeature(p,pd) ;
203 
204  // - set the gain
205  task.setLambda(1) ;
206 
207  // Display task information
208  task.print() ;
209 
210  unsigned int iter=0 ;
211  // loop
212  while(iter++ < 500)
213  {
214  std::cout << "---------------------------------------------" << iter <<std::endl ;
215  vpColVector v ;
216 
217  // get the robot position
218  robot.getPosition(wMc) ;
219  // Compute the position of the camera wrt the object frame
220  cMo = wMc.inverse() * wMo;
221 
222  // new circle position: retrieve x,y and Z of the vpCircle structure
223  circle.track(cMo) ;
224  vpFeatureBuilder::create(p,circle);
225 
226  // compute the control law
227  v = task.computeControlLaw() ;
228  std::cout << "task rank: " << task.getTaskRank() <<std::endl ;
229  // send the camera velocity to the controller
231 
232  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() << std::endl ;
233  }
234 
235  // Display task information
236  task.print() ;
237  task.kill();
238  return 0;
239  }
240  catch(vpException e) {
241  std::cout << "Catch a ViSP exception: " << e << std::endl;
242  return 1;
243  }
244 }
245 
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)
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)
Class that defines what is a circle.
Definition: vpCircle.h:61
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
unsigned int getTaskRank() const
Definition: vpServo.cpp:1567
void setWorldCoordinates(const vpColVector &oP)
Definition: vpCircle.cpp:66