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