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