Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimuPoint2DCamVelocity2.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 2D visual servoing on a point.
33  *
34 *****************************************************************************/
35 
52 #include <stdio.h>
53 #include <stdlib.h>
54 
55 #include <visp3/core/vpConfig.h>
56 #include <visp3/core/vpHomogeneousMatrix.h>
57 #include <visp3/core/vpMath.h>
58 #include <visp3/io/vpParseArgv.h>
59 #include <visp3/robot/vpSimulatorCamera.h>
60 #include <visp3/visual_features/vpFeatureBuilder.h>
61 #include <visp3/visual_features/vpFeaturePoint.h>
62 #include <visp3/vs/vpServo.h>
63 
64 // List of allowed command line options
65 #define GETOPTARGS "h"
66 
67 #ifdef ENABLE_VISP_NAMESPACE
68 using namespace VISP_NAMESPACE_NAME;
69 #endif
70 
71 void usage(const char *name, const char *badparam);
72 bool getOptions(int argc, const char **argv);
73 
82 void usage(const char *name, const char *badparam)
83 {
84  fprintf(stdout, "\n\
85 Simulation of a 2D visual servoing on a point:\n\
86 - eye-in-hand control law,\n\
87 - articular velocity are computed,\n\
88 - without display.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-h]\n",
92  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 
114 bool getOptions(int argc, const char **argv)
115 {
116  const char *optarg_;
117  int c;
118  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
119 
120  switch (c) {
121  case 'h':
122  usage(argv[0], nullptr);
123  return false;
124 
125  default:
126  usage(argv[0], optarg_);
127  return false;
128  }
129  }
130 
131  if ((c == 1) || (c == -1)) {
132  // standalone param or error
133  usage(argv[0], nullptr);
134  std::cerr << "ERROR: " << std::endl;
135  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136  return false;
137  }
138 
139  return true;
140 }
141 
142 int main(int argc, const char **argv)
143 {
144 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
145  try {
146  // Read the command line options
147  if (getOptions(argc, argv) == false) {
148  return EXIT_FAILURE;
149  }
150 
151  vpServo task;
152  vpSimulatorCamera robot;
153 
154  std::cout << std::endl;
155  std::cout << "-------------------------------------------------------" << std::endl;
156  std::cout << " Test program for vpServo " << std::endl;
157  std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
158  std::cout << " Simulation " << std::endl;
159  std::cout << " task : servo a point " << std::endl;
160  std::cout << "-------------------------------------------------------" << std::endl;
161  std::cout << std::endl;
162 
163  // sets the initial camera location
165  cMo[0][3] = 0.1;
166  cMo[1][3] = 0.2;
167  cMo[2][3] = 2;
168  // Compute the position of the object in the world frame
169  vpHomogeneousMatrix wMc, wMo;
170  robot.getPosition(wMc);
171  wMo = wMc * cMo;
172 
173  // sets the point coordinates in the world frame
174  vpPoint point(0, 0, 0);
175 
176  // computes the point coordinates in the camera frame and its 2D
177  // coordinates
178  point.track(cMo);
179 
180  // sets the current position of the visual feature
181  vpFeaturePoint p;
182  vpFeatureBuilder::create(p, point); // retrieve x,y and Z of the vpPoint structure
183 
184  // sets the desired position of the visual feature
185  vpFeaturePoint pd;
186  pd.buildFrom(0, 0, 1);
187 
188  // define the task
189  // - we want an eye-in-hand control law
190  // - articular velocity are computed
193 
194  // Set the position of the end-effector frame in the camera frame
196  vpVelocityTwistMatrix cVe(cMe);
197  task.set_cVe(cVe);
198 
199  // Set the Jacobian (expressed in the end-effector frame)
200  vpMatrix eJe;
201  robot.get_eJe(eJe);
202  task.set_eJe(eJe);
203 
204  // we want to see a point on a point
205  task.addFeature(p, pd);
206 
207  // set the gain
208  task.setLambda(1);
209  // Display task information
210  task.print();
211 
212  unsigned int iter = 0;
213  // loop
214  while (iter++ < 100) {
215  std::cout << "---------------------------------------------" << iter << std::endl;
216  vpColVector v;
217 
218  // Set the Jacobian (expressed in the end-effector frame)
219  // since q is modified eJe is modified
220  robot.get_eJe(eJe);
221  task.set_eJe(eJe);
222 
223  // get the robot position
224  robot.getPosition(wMc);
225  // Compute the position of the object frame in the camera frame
226  cMo = wMc.inverse() * wMo;
227 
228  // new point position
229  point.track(cMo);
230  vpFeatureBuilder::create(p, point); // retrieve x,y and Z of the vpPoint structure
231  pd.buildFrom(0, 0, 1); // Since vpServo::MEAN interaction matrix is
232  // used, we need to update the desired feature at
233  // each iteration
234 
235  // compute the control law
236  v = task.computeControlLaw();
237 
238  // send the camera velocity to the controller
240 
241  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
242  }
243 
244  // Display task information
245  task.print();
246  return EXIT_SUCCESS;
247  }
248  catch (const vpException &e) {
249  std::cout << "Catch a ViSP exception: " << e << std::endl;
250  return EXIT_FAILURE;
251  }
252 #else
253  (void)argc;
254  (void)argv;
255  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
256  return EXIT_SUCCESS;
257 #endif
258 }
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...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
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
void get_eJe(vpMatrix &eJe) VP_OVERRIDE
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_L_cVe_eJe
Definition: vpServo.h:168
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:1038
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 set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:1101
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ MEAN
Definition: vpServo.h:214
Class that defines the simplest robot: a free flying camera.