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