Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimuPoint2DhalfCamVelocity1.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.
33  *
34 *****************************************************************************/
35 
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpHomogeneousMatrix.h>
51 #include <visp3/core/vpMath.h>
52 #include <visp3/core/vpPoint.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/robot/vpSimulatorCamera.h>
55 #include <visp3/visual_features/vpFeatureBuilder.h>
56 #include <visp3/visual_features/vpFeaturePoint.h>
57 #include <visp3/visual_features/vpFeatureThetaU.h>
58 #include <visp3/visual_features/vpGenericFeature.h>
59 #include <visp3/vs/vpServo.h>
60 
61 // List of allowed command line options
62 #define GETOPTARGS "h"
63 
64 #ifdef ENABLE_VISP_NAMESPACE
65 using namespace VISP_NAMESPACE_NAME;
66 #endif
67 
68 void usage(const char *name, const char *badparam);
69 bool getOptions(int argc, const char **argv);
70 
79 void usage(const char *name, const char *badparam)
80 {
81  fprintf(stdout, "\n\
82 Simulation of a 2 1/2 D visual servoing (x,y,Z,theta U):\n\
83 - eye-in-hand control law,\n\
84 - velocity computed in the camera frame,\n\
85 - without display.\n\
86  \n\
87 SYNOPSIS\n\
88  %s [-h]\n",
89  name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  \n\
94  -h\n\
95  Print the help.\n");
96 
97  if (badparam) {
98  fprintf(stderr, "ERROR: \n");
99  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
100  }
101 }
102 
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':
121  usage(argv[0], nullptr);
122  return false;
123 
124  default:
125  usage(argv[0], optarg_);
126  return false;
127  }
128  }
129 
130  if ((c == 1) || (c == -1)) {
131  // standalone param or error
132  usage(argv[0], nullptr);
133  std::cerr << "ERROR: " << std::endl;
134  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
135  return false;
136  }
137 
138  return true;
139 }
140 
141 int main(int argc, const char **argv)
142 {
143 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
144  try {
145  // Read the command line options
146  if (getOptions(argc, argv) == false) {
147  return EXIT_FAILURE;
148  }
149 
150  vpServo task;
151  vpSimulatorCamera robot;
152 
153  std::cout << std::endl;
154  std::cout << "-------------------------------------------------------" << std::endl;
155  std::cout << " Test program for vpServo " << std::endl;
156  std::cout << " task : 2 1/2 D visual servoing " << std::endl;
157  std::cout << "-------------------------------------------------------" << std::endl;
158  std::cout << std::endl;
159 
160  // sets the initial camera location
161  vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
162 
163  vpHomogeneousMatrix cMo(c_r_o);
164  // Compute the position of the object in the world frame
165  vpHomogeneousMatrix wMc, wMo;
166  robot.getPosition(wMc);
167  wMo = wMc * cMo;
168 
169  // sets the desired camera location
170  vpPoseVector cd_r_o(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
171  vpHomogeneousMatrix cdMo(cd_r_o);
172 
173  // sets the point coordinates in the world frame
174  vpPoint point(0, 0, 0);
175  // computes the point coordinates in the camera frame and its 2D
176  // coordinates
177  point.track(cMo);
178 
179  vpPoint pointd(0, 0, 0);
180  pointd.track(cdMo);
181  //------------------------------------------------------------------
182  // 1st feature (x,y)
183  // want to it at (0,0)
184  vpFeaturePoint p;
185  vpFeatureBuilder::create(p, point);
186 
187  vpFeaturePoint pd;
188  vpFeatureBuilder::create(pd, pointd);
189 
190  //------------------------------------------------------------------
191  // 2nd feature (Z)
192  // not necessary to project twice (reuse p)
194  vpFeatureBuilder::create(Z, point); // retrieve x,y and Z of the vpPoint structure
195 
196  // want to see it one meter away (here again use pd)
197  vpFeaturePoint3D Zd;
198  vpFeatureBuilder::create(Zd, pointd); // retrieve x,y and Z of the vpPoint structure
199 
200  //------------------------------------------------------------------
201  // 3rd feature ThetaU
202  // compute the rotation that the camera has to achieve
203  vpHomogeneousMatrix cdMc;
204  cdMc = cdMo * cMo.inverse();
205 
207  tu.buildFrom(cdMc);
208 
209  // sets the desired rotation (always zero !)
210  // since s is the rotation that the camera has to achieve
211 
212  //------------------------------------------------------------------
213  // define the task
214  // - we want an eye-in-hand control law
215  // - robot is controlled in the camera frame
217 
218  task.addFeature(p, pd);
219  task.addFeature(Z, Zd, vpFeaturePoint3D::selectZ());
220  task.addFeature(tu);
221 
222  // set the gain
223  task.setLambda(1);
224 
225  // Display task information
226  task.print();
227 
228  unsigned int iter = 0;
229  // loop
230  while (iter++ < 200) {
231  std::cout << "---------------------------------------------" << iter << std::endl;
232  vpColVector v;
233 
234  // get the robot position
235  robot.getPosition(wMc);
236  // Compute the position of the object frame in the camera frame
237  cMo = wMc.inverse() * wMo;
238 
239  // update the feature
240  point.track(cMo);
241  vpFeatureBuilder::create(p, point);
242  vpFeatureBuilder::create(Z, point);
243 
244  cdMc = cdMo * cMo.inverse();
245  tu.buildFrom(cdMc);
246 
247  // compute the control law
248  v = task.computeControlLaw();
249  // send the camera velocity to the controller ") ;
251 
252  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
253  }
254 
255  // Display task information
256  task.print();
257  std::cout << "Final camera location:\n " << cMo << std::endl;
258  return EXIT_SUCCESS;
259  }
260  catch (const vpException &e) {
261  std::cout << "Catch a ViSP exception: " << e << std::endl;
262  return EXIT_SUCCESS;
263  }
264 #else
265  (void)argc;
266  (void)argv;
267  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
268  return EXIT_SUCCESS;
269 #endif
270 }
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 the 3D point visual feature.
static unsigned int selectZ()
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
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
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
@ 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
Class that defines the simplest robot: a free flying camera.