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