Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimuPoint2DhalfCamVelocity2.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
45 #include <stdio.h>
46 #include <stdlib.h>
47 
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/core/vpHomogeneousMatrix.h>
50 #include <visp3/core/vpMath.h>
51 #include <visp3/core/vpPoint.h>
52 #include <visp3/io/vpParseArgv.h>
53 #include <visp3/robot/vpSimulatorCamera.h>
54 #include <visp3/visual_features/vpFeatureBuilder.h>
55 #include <visp3/visual_features/vpFeaturePoint.h>
56 #include <visp3/visual_features/vpFeatureThetaU.h>
57 #include <visp3/visual_features/vpGenericFeature.h>
58 #include <visp3/vs/vpServo.h>
59 
60 // List of allowed command line options
61 #define GETOPTARGS "h"
62 
63 #ifdef ENABLE_VISP_NAMESPACE
64 using namespace VISP_NAMESPACE_NAME;
65 #endif
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,log Z, 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",
88  name);
89 
90  fprintf(stdout, "\n\
91 OPTIONS: Default\n\
92  \n\
93  -h\n\
94  Print the help.\n");
95 
96  if (badparam)
97  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
98 }
99 
109 bool getOptions(int argc, const char **argv)
110 {
111  const char *optarg_;
112  int c;
113  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
114 
115  switch (c) {
116  case 'h':
117  usage(argv[0], nullptr);
118  return false;
119 
120  default:
121  usage(argv[0], optarg_);
122  return false;
123  }
124  }
125 
126  if ((c == 1) || (c == -1)) {
127  // standalone param or error
128  usage(argv[0], nullptr);
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 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
140  try {
141  // Read the command line options
142  if (getOptions(argc, argv) == false) {
143  return EXIT_FAILURE;
144  }
145 
146  std::cout << std::endl;
147  std::cout << "-------------------------------------------------------" << std::endl;
148  std::cout << " simulation of a 2 1/2 D visual servoing " << std::endl;
149  std::cout << "-------------------------------------------------------" << std::endl;
150  std::cout << std::endl;
151 
152  // In this example we will simulate a visual servoing task.
153  // In simulation, we have to define the scene frane Ro and the
154  // camera frame Rc.
155  // The camera location is given by an homogenous matrix cMo that
156  // describes the position of the scene or object frame in the camera frame.
157 
158  vpServo task;
159 
160  // sets the initial camera location
161  // we give the camera location as a size 6 vector (3 translations in meter
162  // and 3 rotation (theta U representation)
163  vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
164 
165  // this pose vector is then transformed in a 4x4 homogeneous matrix
166  vpHomogeneousMatrix cMo(c_r_o);
167 
168  // We define a robot
169  // The vpSimulatorCamera implements a simple moving that is juste defined
170  // by its location cMo
171  vpSimulatorCamera robot;
172 
173  // Compute the position of the object in the world frame
174  vpHomogeneousMatrix wMc, wMo;
175  robot.getPosition(wMc);
176  wMo = wMc * cMo;
177 
178  // Now that the current camera position has been defined,
179  // let us defined the defined camera location.
180  // It is defined by cdMo
181  // sets the desired camera location
182  vpPoseVector cd_r_o(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
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  // since we simulate we have to define a 3D point that will
195  // forward-projected to define the current position x,y of the
196  // reference point
197 
198  //------------------------------------------------------------------
199  // First feature (x,y)
200  //------------------------------------------------------------------
201  // Let oP be this ... point,
202  // a vpPoint class has three main member
203  // .oP : 3D coordinates in scene frame
204  // .cP : 3D coordinates in camera frame
205  // .p : 2D
206 
207  //------------------------------------------------------------------
208  // sets the point coordinates in the world frame
209  vpPoint point(0, 0, 0);
210  // computes the point coordinates in the camera frame and its
211  // 2D coordinates cP and then p
212  // computes the point coordinates in the camera frame and its 2D
213  // coordinates" ) ;
214  point.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 pointd(0, 0, 0);
219  pointd.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
234  vpFeatureBuilder::create(p, point);
235  vpFeatureBuilder::create(pd, pointd);
236 
237  //------------------------------------------------------------------
238  // Second feature log (Z/Zd)
239  // not necessary to project twice (reuse p)
240 
241  // This case in intersting since this visual feature has not
242  // been predefined in VisP
243  // In such case we have a generic feature class vpGenericFeature
244  // We will have to defined
245  // the vector s : .set_s(...)
246  // the interaction matrix Ls : .setInteractionMatrix(...)
247 
248  // log(Z/Zd) is then a size 1 vector logZ
249  vpGenericFeature logZ(1);
250  // initialized to s = log(Z/Zd)
251  // Let us note that here we use the point P and Pd, it's not necessary
252  // to forward project twice (it's already done)
253  logZ.set_s(log(point.get_Z() / pointd.get_Z()));
254 
255  // This visual has to be regulated to zero
256 
257  //------------------------------------------------------------------
258  // 3rd feature ThetaU
259  // The thetaU feature is defined, tu represents the rotation that the
260  // camera has to realized. the complete displacement is then defined by:
261  //------------------------------------------------------------------
262  vpHomogeneousMatrix cdMc;
263  // compute the rotation that the camera has to achieve
264  cdMc = cdMo * cMo.inverse();
265 
266  // from this displacement, we extract the rotation cdRc represented by
267  // the angle theta and the rotation axis u
269  tu.buildFrom(cdMc);
270  // This visual has to be regulated to zero
271 
272  // sets the desired rotation (always zero !)
273  // since s is the rotation that the camera has to realize
274 
275  //------------------------------------------------------------------
276  // Let us now the task itself
277  //------------------------------------------------------------------
278 
279  // define the task
280  // - we want an eye-in-hand control law
281  // - robot is controlled in the camera frame
282  // we choose to control the robot in the camera frame
284  // Interaction matrix is computed with the current value of s
286 
287  // we build the task by "stacking" the visual feature
288  // previously defined
289  task.addFeature(p, pd);
290  task.addFeature(logZ);
291  task.addFeature(tu);
292  // addFeature(X,Xd) means X should be regulated to Xd
293  // addFeature(X) means that X should be regulated to 0
294  // some features such as vpFeatureThetaU MUST be regulated to zero
295  // (otherwise, it will results in an error at exectution level)
296 
297  // set the gain
298  task.setLambda(1);
299 
300  // Display task information
301  task.print();
302  //------------------------------------------------------------------
303  // An now the closed loop
304 
305  unsigned int iter = 0;
306  // loop
307  while (iter++ < 200) {
308  std::cout << "---------------------------------------------" << iter << std::endl;
309  vpColVector v;
310 
311  // get the robot position
312  robot.getPosition(wMc);
313  // Compute the position of the object frame in the camera frame
314  cMo = wMc.inverse() * wMo;
315 
316  // update the feature
317  point.track(cMo);
318  vpFeatureBuilder::create(p, point);
319 
320  cdMc = cdMo * cMo.inverse();
321  tu.buildFrom(cdMc);
322 
323  // there is no feature for logZ, we explicitly build
324  // the related interaction matrix") ;
325  logZ.set_s(log(point.get_Z() / pointd.get_Z()));
326  vpMatrix LlogZ(1, 6);
327  LlogZ[0][0] = LlogZ[0][1] = LlogZ[0][5] = 0;
328  LlogZ[0][2] = -1 / p.get_Z();
329  LlogZ[0][3] = -p.get_y();
330  LlogZ[0][4] = p.get_x();
331 
332  logZ.setInteractionMatrix(LlogZ);
333 
334  // compute the control law
335  v = task.computeControlLaw();
336 
337  // send the camera velocity to the controller ") ;
339 
340  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
341  }
342 
343  // Display task information
344  task.print();
345  // Final camera location
346  std::cout << cMo << std::endl;
347  return EXIT_SUCCESS;
348  }
349  catch (const vpException &e) {
350  std::cout << "Catch a ViSP exception: " << e << std::endl;
351  return EXIT_FAILURE;
352  }
353 #else
354  (void)argc;
355  (void)argv;
356  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
357  return EXIT_SUCCESS;
358 #endif
359 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
double get_y() const
double get_x() const
double get_Z() const
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
Class that enables to define a feature or a set of features which are not implemented in ViSP as a sp...
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:986
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
Class that defines the simplest robot: a free flying camera.