ViSP  2.6.2
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 - 2012 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  *****************************************************************************/
64 #include <visp/vpMath.h>
65 #include <visp/vpHomogeneousMatrix.h>
66 #include <visp/vpFeatureEllipse.h>
67 #include <visp/vpCircle.h>
68 #include <visp/vpServo.h>
69 #include <visp/vpRobotCamera.h>
70 #include <visp/vpFeatureBuilder.h>
71 
72 
73 // Exception
74 #include <visp/vpException.h>
75 #include <visp/vpMatrixException.h>
76 
77 // Debug trace
78 #include <visp/vpDebug.h>
79 #include <visp/vpParseArgv.h>
80 
81 #include <stdlib.h>
82 #include <stdio.h>
83 // List of allowed command line options
84 #define GETOPTARGS "h"
85 
94 void usage(const char *name, const char *badparam)
95 {
96  fprintf(stdout, "\n\
97 Simulation of a 2D visual servoing on a circle:\n\
98 - eye-in-hand control law,\n\
99 - velocity computed in the camera frame,\n\
100 - without display.\n\
101  \n\
102 SYNOPSIS\n\
103  %s [-h]\n", name);
104 
105  fprintf(stdout, "\n\
106 OPTIONS: Default\n\
107  \n\
108  -h\n\
109  Print the help.\n");
110 
111  if (badparam)
112  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
113 }
114 
124 bool getOptions(int argc, const char **argv)
125 {
126  const char *optarg;
127  int c;
128  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
129 
130  switch (c) {
131  case 'h': usage(argv[0], NULL); return false; break;
132 
133  default:
134  usage(argv[0], optarg);
135  return false; break;
136  }
137  }
138 
139  if ((c == 1) || (c == -1)) {
140  // standalone param or error
141  usage(argv[0], NULL);
142  std::cerr << "ERROR: " << std::endl;
143  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
144  return false;
145  }
146 
147  return true;
148 }
149 
150 int
151 main(int argc, const char ** argv)
152 {
153  // Read the command line options
154  if (getOptions(argc, argv) == false) {
155  exit (-1);
156  }
157 
158  vpServo task ;
159  vpRobotCamera robot ;
160 
161  std::cout << std::endl ;
162  std::cout << "-------------------------------------------------------" << std::endl ;
163  std::cout << " Test program for vpServo " <<std::endl ;
164  std::cout << " Simulation " << std::endl ;
165  std::cout << " task : servo a circle " << std::endl ;
166  std::cout << "-------------------------------------------------------" << std::endl ;
167  std::cout << std::endl ;
168 
169 
170  vpTRACE("sets the initial camera location " ) ;
171  vpHomogeneousMatrix cMo ;
172  cMo[0][3] = 0.1 ;
173  cMo[1][3] = 0.2 ;
174  cMo[2][3] = 2 ;
175  robot.setPosition(cMo) ;
176 
177  vpHomogeneousMatrix cMod ;
178  cMod[0][3] = 0 ;
179  cMod[1][3] = 0 ;
180  cMod[2][3] = 1 ;
181 
182 
183 
184  vpTRACE("sets the circle coordinates in the world frame " ) ;
185  vpCircle circle ;
186  circle.setWorldCoordinates(0,0,1,0,0,0,0.1) ;
187 
188  vpTRACE("sets the desired position of the visual feature ") ;
189  vpFeatureEllipse pd ;
190  circle.track(cMod) ;
191  vpFeatureBuilder::create(pd,circle) ;
192 
193  vpTRACE("project : computes the circle coordinates in the camera frame and its 2D coordinates" ) ;
194 
195  vpTRACE("sets the current position of the visual feature ") ;
196  vpFeatureEllipse p ;
197  circle.track(cMo) ;
198  vpFeatureBuilder::create(p,circle) ;
199 
200  vpTRACE("define the task") ;
201  vpTRACE("\t we want an eye-in-hand control law") ;
202  vpTRACE("\t robot is controlled in the camera frame") ;
204 
205  vpTRACE("\t we want to see a circle on a circle..") ;
206  std::cout << std::endl ;
207  task.addFeature(p,pd) ;
208 
209  vpTRACE("\t set the gain") ;
210  task.setLambda(1) ;
211 
212 
213  vpTRACE("Display task information " ) ;
214  task.print() ;
215 
216  unsigned int iter=0 ;
217  vpTRACE("\t loop") ;
218  while(iter++ < 500)
219  {
220  std::cout << "---------------------------------------------" << iter <<std::endl ;
221  vpColVector v ;
222 
223  if (iter==1) vpTRACE("\t\t get the robot position ") ;
224  robot.getPosition(cMo) ;
225  if (iter==1) vpTRACE("\t\t new circle position ") ;
226  //retrieve x,y and Z of the vpCircle structure
227 
228  circle.track(cMo) ;
229  vpFeatureBuilder::create(p,circle);
230 
231  if (iter==1) vpTRACE("\t\t compute the control law ") ;
232  v = task.computeControlLaw() ;
233  // vpTRACE("computeControlLaw" ) ;
234  std::cout << task.rankJ1 <<std::endl ;
235  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
237 
238 
239  // vpTRACE("\t\t || s - s* || ") ;
240  // std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
241  }
242 
243  vpTRACE("Display task information " ) ;
244  task.print() ;
245  task.kill();
246 }
247 
unsigned int rankJ1
Rank of the task Jacobian.
Definition: vpServo.h:448
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpTRACE
Definition: vpDebug.h:401
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:250
void track(const vpHomogeneousMatrix &cMo)
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
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 computeControlLaw()
compute the desired control law
Definition: vpServo.cpp:883
Class that defines the simplest robot: a free flying camera.
Definition: vpRobotCamera.h:65
void getPosition(vpColVector &q)
void setPosition(const vpRobot::vpControlFrameType, const vpColVector &)
Set a displacement (frame has to be specified) in position control.
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
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.
Definition: vpServo.h:150
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