Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimu3D_cdMc_CamVelocity.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 3D visual servoing.
33  *
34 *****************************************************************************/
55 #include <stdio.h>
56 #include <stdlib.h>
57 
58 #include <visp3/core/vpConfig.h>
59 #include <visp3/core/vpHomogeneousMatrix.h>
60 #include <visp3/core/vpIoTools.h>
61 #include <visp3/core/vpMath.h>
62 #include <visp3/io/vpParseArgv.h>
63 #include <visp3/robot/vpSimulatorCamera.h>
64 #include <visp3/visual_features/vpFeatureThetaU.h>
65 #include <visp3/visual_features/vpFeatureTranslation.h>
66 #include <visp3/vs/vpServo.h>
67 
68 // List of allowed command line options
69 #define GETOPTARGS "h"
70 
71 #ifdef ENABLE_VISP_NAMESPACE
72 using namespace VISP_NAMESPACE_NAME;
73 #endif
74 
75 void usage(const char *name, const char *badparam);
76 bool getOptions(int argc, const char **argv);
77 
86 void usage(const char *name, const char *badparam)
87 {
88  fprintf(stdout, "\n\
89 Simulation of a 3D visual servoing:\n\
90  - eye-in-hand control law,\n\
91  - velocity computed in the camera frame,\n\
92  - without display.\n\
93  \n\
94 SYNOPSIS\n\
95  %s [-h]\n",
96  name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  \n\
101  -h\n\
102  Print the help.\n");
103 
104  if (badparam)
105  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
106 }
107 
117 bool getOptions(int argc, const char **argv)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'h':
125  usage(argv[0], nullptr);
126  return false;
127 
128  default:
129  usage(argv[0], optarg_);
130  return false;
131  }
132  }
133 
134  if ((c == 1) || (c == -1)) {
135  // standalone param or error
136  usage(argv[0], nullptr);
137  std::cerr << "ERROR: " << std::endl;
138  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
139  return false;
140  }
141 
142  return true;
143 }
144 
145 int main(int argc, const char **argv)
146 {
147 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
148  try {
149  // Read the command line options
150  if (getOptions(argc, argv) == false) {
151  return EXIT_FAILURE;
152  }
153 
154  // Log file creation in /tmp/$USERNAME/log.dat
155  // This file contains by line:
156  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
157  // - the 6 values of s - s*
158  std::string username;
159  // Get the user login name
160  vpIoTools::getUserName(username);
161 
162  // Create a log filename to save velocities...
163  std::string logdirname;
164 #if defined(_WIN32)
165  logdirname = "C:/temp/" + username;
166 #else
167  logdirname = "/tmp/" + username;
168 #endif
169  // Test if the output path exist. If no try to create it
170  if (vpIoTools::checkDirectory(logdirname) == false) {
171  try {
172  // Create the dirname
173  vpIoTools::makeDirectory(logdirname);
174  }
175  catch (...) {
176  std::cerr << std::endl << "ERROR:" << std::endl;
177  std::cerr << " Cannot create " << logdirname << std::endl;
178  return EXIT_FAILURE;
179  }
180  }
181  std::string logfilename;
182  logfilename = logdirname + "/log.dat";
183 
184  // Open the log file name
185  std::ofstream flog(logfilename.c_str());
186 
187  vpServo task;
188  vpSimulatorCamera robot;
189 
190  std::cout << std::endl;
191  std::cout << "-------------------------------------------------------" << std::endl;
192  std::cout << " Test program for vpServo " << std::endl;
193  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
194  std::cout << " Simulation " << std::endl;
195  std::cout << " task : 3D visual servoing " << std::endl;
196  std::cout << "-------------------------------------------------------" << std::endl;
197  std::cout << std::endl;
198 
199  // Sets the initial camera location
200  vpPoseVector c_r_o( // Translation tx,ty,tz
201  0.1, 0.2, 2,
202  // ThetaU rotation
203  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
204 
205  // From the camera pose build the corresponding homogeneous matrix
206  vpHomogeneousMatrix cMo(c_r_o);
207 
208  // Set the robot initial position
209  vpHomogeneousMatrix wMc, wMo;
210  robot.getPosition(wMc);
211  wMo = wMc * cMo; // Compute the position of the object in the world frame
212 
213  // Sets the desired camera location
214  vpPoseVector cd_r_o( // Translation tx,ty,tz
215  0, 0, 1,
216  // ThetaU rotation
217  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
218  // From the camera desired pose build the corresponding homogeneous matrix
219  vpHomogeneousMatrix cdMo(cd_r_o);
220 
221  // Compute the homogeneous transformation from the desired camera position
222  // to the initial one
223  vpHomogeneousMatrix cdMc;
224  cdMc = cdMo * cMo.inverse();
225 
226  // Build the current visual features s = (c*tc, thetaU_c*Rc)^T
228  vpFeatureThetaU tu(vpFeatureThetaU::cdRc); // current feature
229  t.buildFrom(cdMc);
230  tu.buildFrom(cdMc);
231 
232  // Sets the desired rotation (always zero !) since s is the
233  // rotation that the camera has to achieve. Here s* = (0, 0)^T
235  vpFeatureThetaU tud(vpFeatureThetaU::cdRc); // desired feature
236 
237  // Define the task
238  // - we want an eye-in-hand control law
239  // - the robot is controlled in the camera frame
241  // - we use here the interaction matrix computed with the
242  // current features
244 
245  // Add the current and desired visual features
246  task.addFeature(t, td); // 3D translation
247  task.addFeature(tu, tud); // 3D rotation
248 
249  // - set the constant gain to 1.0
250  task.setLambda(1);
251 
252  // Display task information
253  task.print();
254 
255  unsigned int iter = 0;
256  // Start the visual servoing loop. We stop the servo after 200 iterations
257  while (iter++ < 200) {
258  std::cout << "-----------------------------------" << iter << std::endl;
259  vpColVector v;
260 
261  // get the robot position
262  robot.getPosition(wMc);
263  // Compute the position of the object frame in the camera frame
264  cMo = wMc.inverse() * wMo;
265 
266  // new displacement to achieve
267  cdMc = cdMo * cMo.inverse();
268 
269  // Update the current visual features
270  t.buildFrom(cdMc);
271  tu.buildFrom(cdMc);
272 
273  // Compute the control law
274  v = task.computeControlLaw();
275 
276  // Display task information
277  if (iter == 1)
278  task.print();
279 
280  // Send the camera velocity to the controller
282 
283  // Retrieve the error
284  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
285 
286  // Save log
287  flog << v.t() << " " << (task.getError()).t() << std::endl;
288  }
289  // Display task information
290  task.print();
291 
292  // Close the log file
293  flog.close();
294  return EXIT_SUCCESS;
295  }
296  catch (const vpException &e) {
297  std::cout << "Catch a ViSP exception: " << e << std::endl;
298  return EXIT_FAILURE;
299  }
300 #else
301  (void)argc;
302  (void)argv;
303  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
304  return EXIT_SUCCESS;
305 #endif
306 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
Class that defines the translation visual feature .
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix inverse() const
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string getUserName()
Definition: vpIoTools.cpp:285
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
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
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.