Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
servoSimu3D_cMcd_CamVelocityWithoutVpServo.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Simulation of a 3D visual servoing.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
81 #include <stdlib.h>
82 #include <stdio.h>
83 
84 #include <visp3/core/vpMath.h>
85 #include <visp3/visual_features/vpFeatureThetaU.h>
86 #include <visp3/visual_features/vpFeatureTranslation.h>
87 #include <visp3/core/vpHomogeneousMatrix.h>
88 #include <visp3/core/vpIoTools.h>
89 #include <visp3/io/vpParseArgv.h>
90 #include <visp3/vs/vpServo.h>
91 #include <visp3/robot/vpSimulatorCamera.h>
92 
93 // List of allowed command line options
94 #define GETOPTARGS "h"
95 
96 void usage(const char *name, const char *badparam);
97 bool getOptions(int argc, const char **argv);
98 
107 void usage(const char *name, const char *badparam)
108 {
109  fprintf(stdout, "\n\
110 Simulation of a 3D visual servoing:\n\
111 - eye-in-hand control law,\n\
112 - velocity computed in the camera frame,\n\
113 - without display.\n\
114 \n\
115 SYNOPSIS\n\
116  %s [-h]\n", name);
117 
118  fprintf(stdout, "\n\
119 OPTIONS: Default\n\
120 \n\
121  -h\n\
122  Print the help.\n");
123 
124  if (badparam)
125  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
126 }
127 
137 bool getOptions(int argc, const char **argv)
138 {
139  const char *optarg_;
140  int c;
141  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
142 
143  switch (c) {
144  case 'h': usage(argv[0], NULL); return false; break;
145 
146  default:
147  usage(argv[0], optarg_);
148  return false; break;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], NULL);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161 }
162 
163 int
164 main(int argc, const char ** argv)
165 {
166  try {
167  // Read the command line options
168  if (getOptions(argc, argv) == false) {
169  exit (-1);
170  }
171 
172  // Log file creation in /tmp/$USERNAME/log.dat
173  // This file contains by line:
174  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
175  // - the 6 values of s - s*
176  std::string username;
177  // Get the user login name
178  vpIoTools::getUserName(username);
179 
180  // Create a log filename to save velocities...
181  std::string logdirname;
182 #if defined(_WIN32)
183  logdirname ="C:/temp/" + username;
184 #else
185  logdirname ="/tmp/" + username;
186 #endif
187  // Test if the output path exist. If no try to create it
188  if (vpIoTools::checkDirectory(logdirname) == false) {
189  try {
190  // Create the dirname
191  vpIoTools::makeDirectory(logdirname);
192  }
193  catch (...) {
194  std::cerr << std::endl
195  << "ERROR:" << std::endl;
196  std::cerr << " Cannot create " << logdirname << std::endl;
197  exit(-1);
198  }
199  }
200  std::string logfilename;
201  logfilename = logdirname + "/log.dat";
202 
203  // Open the log file name
204  std::ofstream flog(logfilename.c_str());
205 
206  vpSimulatorCamera robot ;
207 
208  std::cout << std::endl ;
209  std::cout << "-------------------------------------------------------" << std::endl ;
210  std::cout << " Test program for vpServo " <<std::endl ;
211  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
212  std::cout << " Simulation " << std::endl ;
213  std::cout << " task : 3D visual servoing " << std::endl ;
214  std::cout << "-------------------------------------------------------" << std::endl ;
215  std::cout << std::endl ;
216 
217  // Sets the initial camera location
218  vpPoseVector c_r_o(// Translation tx,ty,tz
219  0.1, 0.2, 2,
220  // ThetaU rotation
221  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50) ) ;
222 
223  // From the camera pose build the corresponding homogeneous matrix
224  vpHomogeneousMatrix cMo(c_r_o) ;
225 
226  // Set the robot initial position
227  vpHomogeneousMatrix wMc, wMo;
228  robot.getPosition(wMc) ;
229  wMo = wMc * cMo; // Compute the position of the object in the world frame
230 
231  // Sets the desired camera location
232  vpPoseVector cd_r_o(// Translation tx,ty,tz
233  0, 0, 1,
234  // ThetaU rotation
236 
237  // From the camera desired pose build the corresponding homogeneous matrix
238  vpHomogeneousMatrix cdMo(cd_r_o) ;
239 
240  vpHomogeneousMatrix cMcd; // Transformation between current and desired camera frame
241  vpRotationMatrix cRcd; // Rotation between current and desired camera frame
242 
243  // Set the constant gain of the servo
244  double lambda = 1;
245 
246  unsigned int iter=0 ;
247  // Start the visual servoing loop. We stop the servo after 200 iterations
248  while(iter++ < 200) {
249  std::cout << "------------------------------------" << iter <<std::endl ;
250 
251  // get the robot position
252  robot.getPosition(wMc) ;
253  // Compute the position of the camera wrt the object frame
254  cMo = wMc.inverse() * wMo;
255 
256  // new displacement to achieve
257  cMcd = cMo*cdMo.inverse() ;
258 
259  // Extract the translation vector ctc* which is the current
260  // translational visual feature.
261  vpTranslationVector ctcd;
262  cMcd.extract(ctcd);
263  // Compute the current theta U visual feature
264  vpThetaUVector tu_cRcd(cMcd);
265 
266  // Create the identity matrix
267  vpMatrix I(3,3);
268  I.eye();
269 
270  // Compute the camera translational velocity
271  vpColVector v(3);
272  v = lambda * ( I - vpColVector::skew(tu_cRcd) ) * ctcd;
273  // Compute the camera rotational velocity
274  vpColVector w(3);
275  w = lambda * tu_cRcd;
276 
277  // Update the complete camera velocity vector
278  vpColVector velocity(6);
279  for (unsigned int i=0; i<3; i++) {
280  velocity[i] = v[i]; // Translational velocity
281  velocity[i+3] = w[i]; // Rotational velocity
282  }
283 
284  // Send the camera velocity to the controller
285  robot.setVelocity(vpRobot::CAMERA_FRAME, velocity) ;
286 
287  // Retrieve the error (s-s*)
288  std::cout << "|| s - s* || = " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
289 
290  // Save log
291  flog << velocity.t() << " " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
292  }
293 
294  // Close the log file
295  flog.close();
296  return 0;
297  }
298  catch(vpException &e) {
299  std::cout << "Catch a ViSP exception: " << e << std::endl;
300  return 1;
301  }
302 }
303 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
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:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
Implementation of a rotation matrix and operations on such kind of matrices.
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
vpColVector t() const
vpRowVector t() const
static std::string getUserName()
Definition: vpIoTools.cpp:177
void extract(vpRotationMatrix &R) const
vpRowVector t() const
static double rad(double deg)
Definition: vpMath.h:104
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
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.