ViSP  2.6.2
servoSimuPoint2DhalfCamVelocity1.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimuPoint2DhalfCamVelocity1.cpp 2457 2010-01-07 10:41:18Z nmelchio $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Simulation of a 2 1/2 D visual servoing.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
66 #include <visp/vpMath.h>
67 #include <visp/vpHomogeneousMatrix.h>
68 #include <visp/vpPoint.h>
69 #include <visp/vpFeaturePoint.h>
70 #include <visp/vpFeatureThetaU.h>
71 #include <visp/vpFeaturePoint3D.h>
72 #include <visp/vpServo.h>
73 #include <visp/vpRobotCamera.h>
74 #include <visp/vpDebug.h>
75 #include <visp/vpFeatureBuilder.h>
76 #include <visp/vpParseArgv.h>
77 #include <stdlib.h>
78 #include <stdio.h>
79 // List of allowed command line options
80 #define GETOPTARGS "h"
81 
90 void usage(const char *name, const char *badparam)
91 {
92  fprintf(stdout, "\n\
93 Simulation of a 2 1/2 D visual servoing (x,y,Z,theta U):\n\
94 - eye-in-hand control law,\n\
95 - velocity computed in the camera frame,\n\
96 - without display.\n\
97  \n\
98 SYNOPSIS\n\
99  %s [-h]\n", name);
100 
101  fprintf(stdout, "\n\
102 OPTIONS: Default\n\
103  \n\
104  -h\n\
105  Print the help.\n");
106 
107  if (badparam) {
108  fprintf(stderr, "ERROR: \n" );
109  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
110  }
111 }
112 
123 bool getOptions(int argc, const char **argv)
124 {
125  const char *optarg;
126  int c;
127  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
128 
129  switch (c) {
130  case 'h': usage(argv[0], NULL); return false; break;
131 
132  default:
133  usage(argv[0], optarg);
134  return false; break;
135  }
136  }
137 
138  if ((c == 1) || (c == -1)) {
139  // standalone param or error
140  usage(argv[0], NULL);
141  std::cerr << "ERROR: " << std::endl;
142  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
143  return false;
144  }
145 
146  return true;
147 }
148 
149 int
150 main(int argc, const char ** argv)
151 {
152  // Read the command line options
153  if (getOptions(argc, argv) == false) {
154  exit (-1);
155  }
156 
157  vpServo task ;
158  vpRobotCamera robot ;
159 
160  std::cout << std::endl ;
161  std::cout << "-------------------------------------------------------" << std::endl ;
162  std::cout << " Test program for vpServo " <<std::endl ;
163  std::cout << " task : 2 1/2 D visual servoing " << std::endl ;
164  std::cout << "-------------------------------------------------------" << std::endl ;
165  std::cout << std::endl ;
166 
167 
168  vpTRACE("sets the initial camera location " ) ;
169  vpPoseVector c_r_o(0.1,0.2,2,
170  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
171  ) ;
172 
173  vpCTRACE ; std::cout << std::endl ;
174  vpHomogeneousMatrix cMo(c_r_o) ;
175  vpCTRACE ; std::cout << std::endl ;
176  robot.setPosition(cMo) ;
177  vpCTRACE ; std::cout << std::endl ;
178 
179  vpTRACE("sets the desired camera location " ) ;
180  vpPoseVector cd_r_o(0,0,1,
182  vpHomogeneousMatrix cdMo(cd_r_o) ;
183 
184 
185  vpTRACE("\tsets the point coordinates in the world frame " ) ;
186  vpPoint point ;
187  point.setWorldCoordinates(0,0,0) ;
188  vpTRACE("\tproject : computes the point coordinates in the camera frame and its 2D coordinates" ) ;
189  point.track(cMo) ;
190 
191  vpPoint pointd ;
192  pointd.setWorldCoordinates(0,0,0) ;
193  pointd.track(cdMo) ;
194  //------------------------------------------------------------------
195  vpTRACE("1st feature (x,y)");
196 
197 
198  vpTRACE("\t want to it at (0,0)") ;
199  vpFeaturePoint p ;
200  vpFeatureBuilder::create(p,point) ;
201 
202 
203  vpFeaturePoint pd ;
204  vpFeatureBuilder::create(pd,pointd) ;
205 
206 
207  //------------------------------------------------------------------
208  vpTRACE("2nd feature (Z)") ;
209  vpTRACE("\tnot necessary to project twice (reuse p)") ;
210  vpFeaturePoint3D Z ;
211  vpFeatureBuilder::create(Z,point) ; //retrieve x,y and Z of the vpPoint structure
212 
213  vpTRACE("\twant to see it one meter away (here again use pd)") ;
214  vpFeaturePoint3D Zd ;
215  vpFeatureBuilder::create(Zd,pointd) ; //retrieve x,y and Z of the vpPoint structure
216 
217 
218  //------------------------------------------------------------------
219  vpTRACE("3rd feature ThetaU") ;
220  vpTRACE("\tcompute the rotation that the camera has to realize " ) ;
221  vpHomogeneousMatrix cdMc ;
222  cdMc = cdMo*cMo.inverse() ;
223 
225  tu.buildFrom(cdMc) ;
226 
227  vpTRACE("\tsets the desired rotation (always zero !) ") ;
228  vpTRACE("\tsince s is the rotation that the camera has to realize ") ;
229 
230 
231  //------------------------------------------------------------------
232 
233  vpTRACE("define the task") ;
234  vpTRACE("\t we want an eye-in-hand control law") ;
235  vpTRACE("\t robot is controlled in the camera frame") ;
237 
238  task.addFeature(p,pd) ;
240  task.addFeature(tu) ;
241 
242  vpTRACE("\t set the gain") ;
243  task.setLambda(1) ;
244 
245 
246  vpTRACE("Display task information " ) ;
247  task.print() ;
248 
249  unsigned int iter=0 ;
250  vpTRACE("\t loop") ;
251  while(iter++<200)
252  {
253  std::cout << "---------------------------------------------" << iter <<std::endl ;
254  vpColVector v ;
255 
256  if (iter==1) vpTRACE("\t\t get the robot position ") ;
257  robot.getPosition(cMo) ;
258 
259  if (iter==1) vpTRACE("\t\t update the feature ") ;
260  point.track(cMo) ;
261  vpFeatureBuilder::create(p,point) ;
262  vpFeatureBuilder::create(Z,point) ;
263 
264  cdMc = cdMo*cMo.inverse() ;
265  tu.buildFrom(cdMc) ;
266 
267 
268  if (iter==1) vpTRACE("\t\t compute the control law ") ;
269  v = task.computeControlLaw() ;
270  if (iter==1) task.print() ;
271 
272  if (iter==1) vpTRACE("\t\t send the camera velocity to the controller ") ;
274 
275 
276  std::cout << ( task.getError() ).sumSquare() <<std::endl ; ;
277  }
278 
279  vpTRACE("Display task information " ) ;
280  task.print() ;
281  task.kill();
282  vpTRACE("Final camera location " ) ;
283  std::cout << cMo << std::endl ;
284 }
285 
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)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
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
Class that defines what is a point.
Definition: vpPoint.h:65
#define vpCTRACE
Definition: vpDebug.h:327
void kill()
destruction (memory deallocation if required)
Definition: vpServo.cpp:177
vpColVector getError() const
Definition: vpServo.h:298
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
Class that defines the 3D point visual feature.
static unsigned int selectZ()
void getPosition(vpColVector &q)
void setPosition(const vpRobot::vpControlFrameType, const vpColVector &)
Set a displacement (frame has to be specified) in position control.
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
vpHomogeneousMatrix inverse() const
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:258
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class required to compute the visual servoing control law.
Definition: vpServo.h:150
void setServo(vpServoType _servo_type)
Choice of the visual servoing control law.
Definition: vpServo.cpp:214
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74