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