Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimuPoint2DhalfCamVelocity3.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Simulation of a 2 1/2 D visual servoing using theta U visual features.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
50 #include <stdlib.h>
51 #include <stdio.h>
52 
53 #include <visp3/visual_features/vpFeatureBuilder.h>
54 #include <visp3/visual_features/vpFeaturePoint.h>
55 #include <visp3/visual_features/vpFeatureThetaU.h>
56 #include <visp3/visual_features/vpGenericFeature.h>
57 #include <visp3/core/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpMath.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/core/vpPoint.h>
61 #include <visp3/vs/vpServo.h>
62 #include <visp3/robot/vpSimulatorCamera.h>
63 
64 // List of allowed command line options
65 #define GETOPTARGS "h"
66 
67 void usage(const char *name, const char *badparam);
68 bool getOptions(int argc, const char **argv);
69 
78 void usage(const char *name, const char *badparam)
79 {
80  fprintf(stdout, "\n\
81 Simulation of a 2 1/2 D visual servoing (x,y,logZ, theta U):\n\
82 - eye-in-hand control law,\n\
83 - velocity computed in the camera frame,\n\
84 - without display.\n\
85  \n\
86 SYNOPSIS\n\
87  %s [-h]\n", name);
88 
89  fprintf(stdout, "\n\
90 OPTIONS: Default\n\
91  \n\
92  -h\n\
93  Print the help.\n");
94 
95  if (badparam)
96  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
97 }
98 
108 bool getOptions(int argc, const char **argv)
109 {
110  const char *optarg_;
111  int c;
112  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
113 
114  switch (c) {
115  case 'h': usage(argv[0], NULL); return false; break;
116 
117  default:
118  usage(argv[0], optarg_);
119  return false; break;
120  }
121  }
122 
123  if ((c == 1) || (c == -1)) {
124  // standalone param or error
125  usage(argv[0], NULL);
126  std::cerr << "ERROR: " << std::endl;
127  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
128  return false;
129  }
130 
131  return true;
132 }
133 
134 int
135 main(int argc, const char ** argv)
136 {
137  try {
138  // Read the command line options
139  if (getOptions(argc, argv) == false) {
140  exit (-1);
141  }
142 
143  std::cout << std::endl ;
144  std::cout << "-------------------------------------------------------" << std::endl ;
145  std::cout << " simulation of a 2 1/2 D visual servoing " << std::endl ;
146  std::cout << "-------------------------------------------------------" << std::endl ;
147  std::cout << std::endl ;
148 
149  // In this example we will simulate a visual servoing task.
150  // In simulation, we have to define the scene frane Ro and the
151  // camera frame Rc.
152  // The camera location is given by an homogenous matrix cMo that
153  // describes the position of the camera in the scene frame.
154 
155  vpServo task ;
156 
157  // sets the initial camera location
158  // we give the camera location as a size 6 vector (3 translations in meter
159  // and 3 rotation (theta U representation)
160  vpPoseVector c_r_o(0.1,0.2,2,
161  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50)
162  ) ;
163 
164  // this pose vector is then transformed in a 4x4 homogeneous matrix
165  vpHomogeneousMatrix cMo(c_r_o) ;
166 
167  // We define a robot
168  // The vpSimulatorCamera implements a simple moving that is juste defined
169  // by its location cMo
170  vpSimulatorCamera robot ;
171 
172  // Compute the position of the object in the world frame
173  vpHomogeneousMatrix wMc, wMo;
174  robot.getPosition(wMc) ;
175  wMo = wMc * cMo;
176 
177  // Now that the current camera position has been defined,
178  // let us defined the defined camera location.
179  // It is defined by cdMo
180  // sets the desired camera location " ) ;
181  vpPoseVector cd_r_o(0,0,1,
183  vpHomogeneousMatrix cdMo(cd_r_o) ;
184 
185  //----------------------------------------------------------------------
186  // A 2 1/2 D visual servoing can be defined by
187  // - the position of a point x,y
188  // - the difference between this point depth and a desire depth
189  // modeled by log Z/Zd to be regulated to 0
190  // - the rotation that the camera has to realized cdMc
191 
192  // Let us now defined the current value of these features
193 
194 
195  // since we simulate we have to define a 3D point that will
196  // forward-projected to define the current position x,y of the
197  // reference point
198 
199  //------------------------------------------------------------------
200  // First feature (x,y)
201  //------------------------------------------------------------------
202  // Let oP be this ... point,
203  // a vpPoint class has three main member
204  // .oP : 3D coordinates in scene frame
205  // .cP : 3D coordinates in camera frame
206  // .p : 2D
207 
208  //------------------------------------------------------------------
209  // sets the point coordinates in the world frame
210  vpPoint P(0, 0, 0) ;
211  // computes the P coordinates in the camera frame and its
212  // 2D coordinates cP and then p
213  // computes the point coordinates in the camera frame and its 2D coordinates
214  P.track(cMo) ;
215 
216  // We also defined (again by forward projection) the desired position
217  // of this point according to the desired camera position
218  vpPoint Pd(0, 0, 0) ;
219  Pd.track(cdMo) ;
220 
221  // Nevertheless, a vpPoint is not a feature, this is just a "tracker"
222  // from which the feature are built
223  // a feature is juste defined by a vector s, a way to compute the
224  // interaction matrix and the error, and if required a (or a vector of)
225  // 3D information
226 
227  // for a point (x,y) Visp implements the vpFeaturePoint class.
228  // we no defined a feature for x,y (and for (x*,y*))
229  vpFeaturePoint p,pd ;
230 
231  // and we initialized the vector s=(x,y) of p from the tracker P
232  // Z coordinates in p is also initialized, it will be used to compute
233  // the interaction matrix
235  vpFeatureBuilder::create(pd,Pd) ;
236 
237  // This visual has to be regulated to zero
238 
239  //------------------------------------------------------------------
240  // 2nd feature ThetaUz and 3rd feature t
241  // The thetaU feature is defined, tu represents the rotation that the camera
242  // has to realized. t the translation.
243  // the complete displacement is then defined by:
244  //------------------------------------------------------------------
245  vpHomogeneousMatrix cdMc ;
246  // compute the rotation that the camera has to achieve
247  cdMc = cdMo*cMo.inverse() ;
248 
249  // from this displacement, we extract the rotation cdRc represented by
250  // the angle theta and the rotation axis u
252  tuz.buildFrom(cdMc) ;
253  // And the translations
255  t.buildFrom(cdMc) ;
256 
257  // This visual has to be regulated to zero
258 
259  // sets the desired rotation (always zero !)
260  // since s is the rotation that the camera has to achieve
261 
262  //------------------------------------------------------------------
263  // Let us now the task itself
264  //------------------------------------------------------------------
265 
266  // define the task
267  // - we want an eye-in-hand control law
268  // - robot is controlled in the camera frame
269  // we choose to control the robot in the camera frame
271  // Interaction matrix is computed with the current value of s
273 
274  // we build the task by "stacking" the visual feature
275  // previously defined
276  task.addFeature(t) ;
277  task.addFeature(p,pd) ;
278  task.addFeature(tuz,vpFeatureThetaU::TUz) ; //selection of TUz
279 
280  // addFeature(X,Xd) means X should be regulated to Xd
281  // addFeature(X) means that X should be regulated to 0
282  // some features such as vpFeatureThetaU MUST be regulated to zero
283  // (otherwise, it will results in an error at exectution level)
284 
285  // set the gain
286  task.setLambda(1) ;
287 
288  // Display task information " ) ;
289  task.print() ;
290  //------------------------------------------------------------------
291  // An now the closed loop
292 
293  unsigned int iter=0 ;
294  // loop
295  while(iter++<200)
296  {
297  std::cout << "---------------------------------------------" << iter <<std::endl ;
298  vpColVector v ;
299 
300  // get the robot position
301  robot.getPosition(wMc) ;
302  // Compute the position of the camera wrt the object frame
303  cMo = wMc.inverse() * wMo;
304 
305  // update the feature
306  P.track(cMo) ;
308 
309  cdMc = cdMo*cMo.inverse() ;
310  tuz.buildFrom(cdMc) ;
311  t.buildFrom(cdMc) ;
312 
313  // compute the control law: v = -lambda L^+(s-sd)
314  v = task.computeControlLaw() ;
315 
316  // send the camera velocity to the controller
318 
319  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
320  }
321 
322  // Display task information
323  task.print() ;
324  task.kill();
325  // Final camera location
326  std::cout << "Final camera location: \n" << cMo << std::endl ;
327  return 0;
328  }
329  catch(vpException &e) {
330  std::cout << "Catch a ViSP exception: " << e << std::endl;
331  return 1;
332  }
333 }
334 
Class that defines the translation visual feature .
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Implementation of an homogeneous matrix and operations on such kind of matrices.
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:512
error that can be emited by ViSP classes.
Definition: vpException.h:73
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:76
Class that defines what is a point.
Definition: vpPoint.h:59
void kill()
Definition: vpServo.cpp:191
vpColVector getError() const
Definition: vpServo.h:271
vpColVector computeControlLaw()
Definition: vpServo.cpp:954
void setLambda(double c)
Definition: vpServo.h:391
vpHomogeneousMatrix getPosition() const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:585
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
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:314
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:222