ViSP  2.9.0
servoSimuPoint2DhalfCamVelocity3.cpp
1 
2 /****************************************************************************
3  *
4  * $Id: servoSimuPoint2DhalfCamVelocity3.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 2 1/2 D visual servoing using theta U visual features.
37  *
38  * Authors:
39  * Eric Marchand
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 
55 #include <stdlib.h>
56 #include <stdio.h>
57 
58 #include <visp/vpFeatureBuilder.h>
59 #include <visp/vpFeaturePoint.h>
60 #include <visp/vpFeatureThetaU.h>
61 #include <visp/vpGenericFeature.h>
62 #include <visp/vpHomogeneousMatrix.h>
63 #include <visp/vpMath.h>
64 #include <visp/vpParseArgv.h>
65 #include <visp/vpPoint.h>
66 #include <visp/vpServo.h>
67 #include <visp/vpSimulatorCamera.h>
68 
69 // List of allowed command line options
70 #define GETOPTARGS "h"
71 
72 void usage(const char *name, const char *badparam);
73 bool getOptions(int argc, const char **argv);
74 
83 void usage(const char *name, const char *badparam)
84 {
85  fprintf(stdout, "\n\
86 Simulation of a 2 1/2 D visual servoing (x,y,logZ, theta U):\n\
87 - eye-in-hand control law,\n\
88 - velocity computed in the camera frame,\n\
89 - without display.\n\
90  \n\
91 SYNOPSIS\n\
92  %s [-h]\n", name);
93 
94  fprintf(stdout, "\n\
95 OPTIONS: Default\n\
96  \n\
97  -h\n\
98  Print the help.\n");
99 
100  if (badparam)
101  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
102 }
103 
113 bool getOptions(int argc, const char **argv)
114 {
115  const char *optarg_;
116  int c;
117  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118 
119  switch (c) {
120  case 'h': usage(argv[0], NULL); return false; break;
121 
122  default:
123  usage(argv[0], optarg_);
124  return false; break;
125  }
126  }
127 
128  if ((c == 1) || (c == -1)) {
129  // standalone param or error
130  usage(argv[0], NULL);
131  std::cerr << "ERROR: " << std::endl;
132  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
133  return false;
134  }
135 
136  return true;
137 }
138 
139 int
140 main(int argc, const char ** argv)
141 {
142  try {
143  // Read the command line options
144  if (getOptions(argc, argv) == false) {
145  exit (-1);
146  }
147 
148  std::cout << std::endl ;
149  std::cout << "-------------------------------------------------------" << std::endl ;
150  std::cout << " simulation of a 2 1/2 D visual servoing " << std::endl ;
151  std::cout << "-------------------------------------------------------" << std::endl ;
152  std::cout << std::endl ;
153 
154  // In this example we will simulate a visual servoing task.
155  // In simulation, we have to define the scene frane Ro and the
156  // camera frame Rc.
157  // The camera location is given by an homogenous matrix cMo that
158  // describes the position of the camera in the scene frame.
159 
160  vpServo task ;
161 
162  // sets the initial camera location
163  // we give the camera location as a size 6 vector (3 translations in meter
164  // and 3 rotation (theta U representation)
165  vpPoseVector c_r_o(0.1,0.2,2,
166  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
167  ) ;
168 
169  // this pose vector is then transformed in a 4x4 homogeneous matrix
170  vpHomogeneousMatrix cMo(c_r_o) ;
171 
172  // We define a robot
173  // The vpSimulatorCamera implements a simple moving that is juste defined
174  // by its location cMo
175  vpSimulatorCamera robot ;
176 
177  // Compute the position of the object in the world frame
178  vpHomogeneousMatrix wMc, wMo;
179  robot.getPosition(wMc) ;
180  wMo = wMc * cMo;
181 
182  // Now that the current camera position has been defined,
183  // let us defined the defined camera location.
184  // It is defined by cdMo
185  // sets the desired camera location " ) ;
186  vpPoseVector cd_r_o(0,0,1,
188  vpHomogeneousMatrix cdMo(cd_r_o) ;
189 
190  //----------------------------------------------------------------------
191  // A 2 1/2 D visual servoing can be defined by
192  // - the position of a point x,y
193  // - the difference between this point depth and a desire depth
194  // modeled by log Z/Zd to be regulated to 0
195  // - the rotation that the camera has to realized cdMc
196 
197  // Let us now defined the current value of these features
198 
199 
200  // since we simulate we have to define a 3D point that will
201  // forward-projected to define the current position x,y of the
202  // reference point
203 
204  //------------------------------------------------------------------
205  // First feature (x,y)
206  //------------------------------------------------------------------
207  // Let oP be this ... point,
208  // a vpPoint class has three main member
209  // .oP : 3D coordinates in scene frame
210  // .cP : 3D coordinates in camera frame
211  // .p : 2D
212 
213  //------------------------------------------------------------------
214  // sets the point coordinates in the world frame
215  vpPoint P ;
216  // defined point coordinates in the scene frame : oP
217  P.setWorldCoordinates(0,0,0) ;
218  // computes the P coordinates in the camera frame and its
219  // 2D coordinates cP and then p
220  // computes the point coordinates in the camera frame and its 2D coordinates
221  P.track(cMo) ;
222 
223  // We also defined (again by forward projection) the desired position
224  // of this point according to the desired camera position
225  vpPoint Pd ;
226  Pd.setWorldCoordinates(0,0,0) ;
227  Pd.track(cdMo) ;
228 
229  // Nevertheless, a vpPoint is not a feature, this is just a "tracker"
230  // from which the feature are built
231  // a feature is juste defined by a vector s, a way to compute the
232  // interaction matrix and the error, and if required a (or a vector of)
233  // 3D information
234 
235  // for a point (x,y) Visp implements the vpFeaturePoint class.
236  // we no defined a feature for x,y (and for (x*,y*))
237  vpFeaturePoint p,pd ;
238 
239  // and we initialized the vector s=(x,y) of p from the tracker P
240  // Z coordinates in p is also initialized, it will be used to compute
241  // the interaction matrix
243  vpFeatureBuilder::create(pd,Pd) ;
244 
245  // This visual has to be regulated to zero
246 
247  //------------------------------------------------------------------
248  // 2nd feature ThetaUz and 3rd feature t
249  // The thetaU feature is defined, tu represents the rotation that the camera
250  // has to realized. t the translation.
251  // the complete displacement is then defined by:
252  //------------------------------------------------------------------
253  vpHomogeneousMatrix cdMc ;
254  // compute the rotation that the camera has to achieve
255  cdMc = cdMo*cMo.inverse() ;
256 
257  // from this displacement, we extract the rotation cdRc represented by
258  // the angle theta and the rotation axis u
260  tuz.buildFrom(cdMc) ;
261  // And the translations
263  t.buildFrom(cdMc) ;
264 
265  // This visual has to be regulated to zero
266 
267  // sets the desired rotation (always zero !)
268  // since s is the rotation that the camera has to achieve
269 
270  //------------------------------------------------------------------
271  // Let us now the task itself
272  //------------------------------------------------------------------
273 
274  // define the task
275  // - we want an eye-in-hand control law
276  // - robot is controlled in the camera frame
277  // we choose to control the robot in the camera frame
279  // Interaction matrix is computed with the current value of s
281 
282  // we build the task by "stacking" the visual feature
283  // previously defined
284  task.addFeature(t) ;
285  task.addFeature(p,pd) ;
286  task.addFeature(tuz,vpFeatureThetaU::TUz) ; //selection of TUz
287 
288  // addFeature(X,Xd) means X should be regulated to Xd
289  // addFeature(X) means that X should be regulated to 0
290  // some features such as vpFeatureThetaU MUST be regulated to zero
291  // (otherwise, it will results in an error at exectution level)
292 
293  // set the gain
294  task.setLambda(1) ;
295 
296  // Display task information " ) ;
297  task.print() ;
298  //------------------------------------------------------------------
299  // An now the closed loop
300 
301  unsigned int iter=0 ;
302  // loop
303  while(iter++<200)
304  {
305  std::cout << "---------------------------------------------" << iter <<std::endl ;
306  vpColVector v ;
307 
308  // get the robot position
309  robot.getPosition(wMc) ;
310  // Compute the position of the camera wrt the object frame
311  cMo = wMc.inverse() * wMo;
312 
313  // update the feature
314  P.track(cMo) ;
316 
317  cdMc = cdMo*cMo.inverse() ;
318  tuz.buildFrom(cdMc) ;
319  t.buildFrom(cdMc) ;
320 
321  // compute the control law: v = -lambda L^+(s-sd)
322  v = task.computeControlLaw() ;
323 
324  // send the camera velocity to the controller
326 
327  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
328  }
329 
330  // Display task information
331  task.print() ;
332  task.kill();
333  // Final camera location
334  std::cout << "Final camera location: \n" << cMo << std::endl ;
335  return 0;
336  }
337  catch(vpException e) {
338  std::cout << "Catch a ViSP exception: " << e << std::endl;
339  return 1;
340  }
341 }
342 
Class that defines the translation visual feature .
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 a 2D point visual feature which is composed by two parameters that are the cartes...
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
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
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
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:251
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:220
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