Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
servoSimu3D_cMcd_CamVelocityWithoutVpServo.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 *****************************************************************************/
78 #include <stdio.h>
79 #include <stdlib.h>
80 
81 #include <visp3/core/vpConfig.h>
82 #include <visp3/core/vpHomogeneousMatrix.h>
83 #include <visp3/core/vpIoTools.h>
84 #include <visp3/core/vpMath.h>
85 #include <visp3/io/vpParseArgv.h>
86 #include <visp3/robot/vpSimulatorCamera.h>
87 #include <visp3/visual_features/vpFeatureThetaU.h>
88 #include <visp3/visual_features/vpFeatureTranslation.h>
89 #include <visp3/vs/vpServo.h>
90 
91 // List of allowed command line options
92 #define GETOPTARGS "h"
93 
94 #ifdef ENABLE_VISP_NAMESPACE
95 using namespace VISP_NAMESPACE_NAME;
96 #endif
97 
98 void usage(const char *name, const char *badparam);
99 bool getOptions(int argc, const char **argv);
100 
109 void usage(const char *name, const char *badparam)
110 {
111  fprintf(stdout, "\n\
112 Simulation of a 3D visual servoing:\n\
113 - eye-in-hand control law,\n\
114 - velocity computed in the camera frame,\n\
115 - without display.\n\
116 \n\
117 SYNOPSIS\n\
118  %s [-h]\n",
119  name);
120 
121  fprintf(stdout, "\n\
122 OPTIONS: Default\n\
123 \n\
124  -h\n\
125  Print the help.\n");
126 
127  if (badparam)
128  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
129 }
130 
140 bool getOptions(int argc, const char **argv)
141 {
142  const char *optarg_;
143  int c;
144  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
145 
146  switch (c) {
147  case 'h':
148  usage(argv[0], nullptr);
149  return false;
150 
151  default:
152  usage(argv[0], optarg_);
153  return false;
154  }
155  }
156 
157  if ((c == 1) || (c == -1)) {
158  // standalone param or error
159  usage(argv[0], nullptr);
160  std::cerr << "ERROR: " << std::endl;
161  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
162  return false;
163  }
164 
165  return true;
166 }
167 
168 int main(int argc, const char **argv)
169 {
170  try {
171  // Read the command line options
172  if (getOptions(argc, argv) == false) {
173  return EXIT_FAILURE;
174  }
175 
176  // Log file creation in /tmp/$USERNAME/log.dat
177  // This file contains by line:
178  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
179  // - the 6 values of s - s*
180  std::string username;
181  // Get the user login name
182  vpIoTools::getUserName(username);
183 
184  // Create a log filename to save velocities...
185  std::string logdirname;
186 #if defined(_WIN32)
187  logdirname = "C:/temp/" + username;
188 #else
189  logdirname = "/tmp/" + username;
190 #endif
191  // Test if the output path exist. If no try to create it
192  if (vpIoTools::checkDirectory(logdirname) == false) {
193  try {
194  // Create the dirname
195  vpIoTools::makeDirectory(logdirname);
196  }
197  catch (...) {
198  std::cerr << std::endl << "ERROR:" << std::endl;
199  std::cerr << " Cannot create " << logdirname << std::endl;
200  return EXIT_FAILURE;
201  }
202  }
203  std::string logfilename;
204  logfilename = logdirname + "/log.dat";
205 
206  // Open the log file name
207  std::ofstream flog(logfilename.c_str());
208 
209  vpSimulatorCamera robot;
210 
211  std::cout << std::endl;
212  std::cout << "-------------------------------------------------------" << std::endl;
213  std::cout << " Test program for vpServo " << std::endl;
214  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
215  std::cout << " Simulation " << std::endl;
216  std::cout << " task : 3D visual servoing " << std::endl;
217  std::cout << "-------------------------------------------------------" << std::endl;
218  std::cout << std::endl;
219 
220  // Sets the initial camera location
221  vpPoseVector c_r_o( // Translation tx,ty,tz
222  0.1, 0.2, 2,
223  // ThetaU rotation
224  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
225 
226  // From the camera pose build the corresponding homogeneous matrix
227  vpHomogeneousMatrix cMo(c_r_o);
228 
229  // Set the robot initial position
230  vpHomogeneousMatrix wMc, wMo;
231  robot.getPosition(wMc);
232  wMo = wMc * cMo; // Compute the position of the object in the world frame
233 
234  // Sets the desired camera location
235  vpPoseVector cd_r_o( // Translation tx,ty,tz
236  0, 0, 1,
237  // ThetaU rotation
238  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
239 
240  // From the camera desired pose build the corresponding homogeneous matrix
241  vpHomogeneousMatrix cdMo(cd_r_o);
242 
243  vpHomogeneousMatrix cMcd; // Transformation between current and desired camera frame
244  vpRotationMatrix cRcd; // Rotation between current and desired camera frame
245 
246  // Set the constant gain of the servo
247  double lambda = 1;
248 
249  unsigned int iter = 0;
250  // Start the visual servoing loop. We stop the servo after 200 iterations
251  while (iter++ < 200) {
252  std::cout << "------------------------------------" << iter << std::endl;
253 
254  // get the robot position
255  robot.getPosition(wMc);
256  // Compute the position of the object frame in the camera frame
257  cMo = wMc.inverse() * wMo;
258 
259  // new displacement to achieve
260  cMcd = cMo * cdMo.inverse();
261 
262  // Extract the translation vector ctc* which is the current
263  // translational visual feature.
264  vpTranslationVector ctcd;
265  cMcd.extract(ctcd);
266  // Compute the current theta U visual feature
267  vpThetaUVector tu_cRcd(cMcd);
268 
269  // Create the identity matrix
270  vpMatrix I(3, 3);
271  I.eye();
272 
273  // Compute the camera translational velocity
274  vpColVector v(3);
275  v = lambda * (I - vpColVector::skew(vpColVector(tu_cRcd))) * ctcd;
276  // Compute the camera rotational velocity
277  vpColVector w(3);
278  w = lambda * tu_cRcd;
279 
280  // Update the complete camera velocity vector
281  vpColVector velocity(6);
282  for (unsigned int i = 0; i < 3; i++) {
283  velocity[i] = v[i]; // Translational velocity
284  velocity[i + 3] = w[i]; // Rotational velocity
285  }
286 
287  // Send the camera velocity to the controller
288  robot.setVelocity(vpRobot::CAMERA_FRAME, velocity);
289 
290  // Retrieve the error (s-s*)
291  std::cout << "|| s - s* || = " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
292 
293  // Save log
294  flog << velocity.t() << " " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
295  }
296 
297  // Close the log file
298  flog.close();
299  return EXIT_SUCCESS;
300  }
301  catch (const vpException &e) {
302  std::cout << "Catch a ViSP exception: " << e << std::endl;
303  return EXIT_FAILURE;
304  }
305 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static vpMatrix skew(const vpColVector &v)
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
void extract(vpRotationMatrix &R) 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
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
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
Implementation of a rotation matrix and operations on such kind of matrices.
vpColVector t() const
Class that defines the simplest robot: a free flying camera.
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
vpRowVector t() const