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