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