Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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  break;
141 
142  default:
143  usage(argv[0], optarg_);
144  return false;
145  break;
146  }
147  }
148 
149  if ((c == 1) || (c == -1)) {
150  // standalone param or error
151  usage(argv[0], NULL);
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  exit(-1);
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  } catch (...) {
189  std::cerr << std::endl << "ERROR:" << std::endl;
190  std::cerr << " Cannot create " << logdirname << std::endl;
191  exit(-1);
192  }
193  }
194  std::string logfilename;
195  logfilename = logdirname + "/log.dat";
196 
197  // Open the log file name
198  std::ofstream flog(logfilename.c_str());
199 
200  vpSimulatorCamera robot;
201 
202  std::cout << std::endl;
203  std::cout << "-------------------------------------------------------" << std::endl;
204  std::cout << " Test program without vpServo and vpFeature classes " << std::endl;
205  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
206  std::cout << " Simulation " << std::endl;
207  std::cout << " task : 3D visual servoing " << std::endl;
208  std::cout << "-------------------------------------------------------" << std::endl;
209  std::cout << std::endl;
210 
211  // Sets the initial camera location
212  vpPoseVector c_r_o( // Translation tx,ty,tz
213  0.1, 0.2, 2,
214  // ThetaU rotation
215  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
216 
217  // From the camera pose build the corresponding homogeneous matrix
218  vpHomogeneousMatrix cMo(c_r_o);
219 
220  // Set the robot initial position
221  vpHomogeneousMatrix wMc, wMo;
222  robot.getPosition(wMc);
223  wMo = wMc * cMo; // Compute the position of the object in the world frame
224 
225  // Sets the desired camera location
226  vpPoseVector cd_r_o( // Translation tx,ty,tz
227  0, 0, 1,
228  // ThetaU rotation
229  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
230  // From the camera desired pose build the corresponding homogeneous matrix
231  vpHomogeneousMatrix cdMo(cd_r_o);
232 
233  vpHomogeneousMatrix cdMc; // Transformation between desired and current camera frame
234  vpRotationMatrix cdRc; // Rotation between desired and current camera frame
235  vpRotationMatrix cRcd; // Rotation between current and desired camera frame
236 
237  // Set the constant gain of the servo
238  double lambda = 1;
239 
240  unsigned int iter = 0;
241  // Start the visual servoing loop. We stop the servo after 200 iterations
242  while (iter++ < 200) {
243  std::cout << "-----------------------------------" << iter << std::endl;
244 
245  // get the robot position
246  robot.getPosition(wMc);
247  // Compute the position of the object frame in the camera frame
248  cMo = wMc.inverse() * wMo;
249 
250  // new displacement to achieve
251  cdMc = cdMo * cMo.inverse();
252 
253  // Extract the translation vector c*tc which is the current
254  // translational visual feature.
255  vpTranslationVector cdtc;
256  cdMc.extract(cdtc);
257  // Extract the rotation matrix c*Rc
258  cdMc.extract(cdRc);
259  // Compute the inverse rotation cRc* (in fact the transpose of c*Rc)
260  cRcd = cdRc.inverse();
261  // Compute the current theta U visual feature
262  vpThetaUVector tu_cdRc(cdMc);
263  // Compute the camera translational velocity
264  vpColVector v(3);
265  v = -lambda * cRcd * cdtc;
266  // Compute the camera rotational velocity
267  vpColVector w(3);
268  w = -lambda * tu_cdRc;
269 
270  // Update the complete camera velocity vector
271  vpColVector velocity(6);
272  for (unsigned int i = 0; i < 3; i++) {
273  velocity[i] = v[i]; // Translational velocity
274  velocity[i + 3] = w[i]; // Rotational velocity
275  }
276 
277  // Send the camera velocity to the controller
278  robot.setVelocity(vpRobot::CAMERA_FRAME, velocity);
279 
280  // Retrieve the error (s-s*)
281  std::cout << "|| s - s* || = " << cdtc.t() << " " << tu_cdRc.t() << std::endl;
282 
283  // Save log
284  flog << velocity.t() << " " << cdtc.t() << " " << tu_cdRc.t() << std::endl;
285  }
286  // Close the log file
287  flog.close();
288  return EXIT_SUCCESS;
289  } catch (const vpException &e) {
290  std::cout << "Catch a ViSP exception: " << e << std::endl;
291  return EXIT_FAILURE;
292  }
293 }
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:572
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
vpHomogeneousMatrix inverse() const
void extract(vpRotationMatrix &R) const
vpRotationMatrix inverse() const
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:422
static std::string getUserName()
Definition: vpIoTools.cpp:318
static double rad(double deg)
Definition: vpMath.h:108
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
Class that consider the case of a translation vector.
Implementation of a rotation vector as axis-angle minimal representation.