ViSP
servoSimu3D_cMcd_CamVelocityWithoutVpServo.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimu3D_cMcd_CamVelocityWithoutVpServo.cpp 2457 2010-01-07 10:41:18Z nmelchio $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Simulation of a 3D visual servoing.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
85 #include <stdlib.h>
86 #include <stdio.h>
87 
88 #include <visp/vpMath.h>
89 #include <visp/vpFeatureThetaU.h>
90 #include <visp/vpFeatureTranslation.h>
91 #include <visp/vpHomogeneousMatrix.h>
92 #include <visp/vpIoTools.h>
93 #include <visp/vpParseArgv.h>
94 #include <visp/vpServo.h>
95 #include <visp/vpSimulatorCamera.h>
96 
97 // List of allowed command line options
98 #define GETOPTARGS "h"
99 
100 void usage(const char *name, const char *badparam);
101 bool getOptions(int argc, const char **argv);
102 
111 void usage(const char *name, const char *badparam)
112 {
113  fprintf(stdout, "\n\
114 Simulation of a 3D visual servoing:\n\
115 - eye-in-hand control law,\n\
116 - velocity computed in the camera frame,\n\
117 - without display.\n\
118 \n\
119 SYNOPSIS\n\
120  %s [-h]\n", name);
121 
122  fprintf(stdout, "\n\
123 OPTIONS: Default\n\
124 \n\
125  -h\n\
126  Print the help.\n");
127 
128  if (badparam)
129  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
130 }
131 
141 bool getOptions(int argc, const char **argv)
142 {
143  const char *optarg_;
144  int c;
145  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
146 
147  switch (c) {
148  case 'h': usage(argv[0], NULL); return false; break;
149 
150  default:
151  usage(argv[0], optarg_);
152  return false; break;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], NULL);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 int
168 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  }
197  catch (...) {
198  std::cerr << std::endl
199  << "ERROR:" << std::endl;
200  std::cerr << " Cannot create " << logdirname << std::endl;
201  exit(-1);
202  }
203  }
204  std::string logfilename;
205  logfilename = logdirname + "/log.dat";
206 
207  // Open the log file name
208  std::ofstream flog(logfilename.c_str());
209 
210  vpSimulatorCamera robot ;
211 
212  std::cout << std::endl ;
213  std::cout << "-------------------------------------------------------" << std::endl ;
214  std::cout << " Test program for vpServo " <<std::endl ;
215  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
216  std::cout << " Simulation " << std::endl ;
217  std::cout << " task : 3D visual servoing " << std::endl ;
218  std::cout << "-------------------------------------------------------" << std::endl ;
219  std::cout << std::endl ;
220 
221  // Sets the initial camera location
222  vpPoseVector c_r_o(// Translation tx,ty,tz
223  0.1, 0.2, 2,
224  // ThetaU rotation
225  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50) ) ;
226 
227  // From the camera pose build the corresponding homogeneous matrix
228  vpHomogeneousMatrix cMo(c_r_o) ;
229 
230  // Set the robot initial position
231  vpHomogeneousMatrix wMc, wMo;
232  robot.getPosition(wMc) ;
233  wMo = wMc * cMo; // Compute the position of the object in the world frame
234 
235  // Sets the desired camera location
236  vpPoseVector cd_r_o(// Translation tx,ty,tz
237  0, 0, 1,
238  // ThetaU rotation
240 
241  // From the camera desired pose build the corresponding homogeneous matrix
242  vpHomogeneousMatrix cdMo(cd_r_o) ;
243 
244  vpHomogeneousMatrix cMcd; // Transformation between current and desired camera frame
245  vpRotationMatrix cRcd; // Rotation between current and desired camera frame
246 
247  // Set the constant gain of the servo
248  double lambda = 1;
249 
250  unsigned int iter=0 ;
251  // Start the visual servoing loop. We stop the servo after 200 iterations
252  while(iter++ < 200) {
253  std::cout << "------------------------------------" << iter <<std::endl ;
254 
255  // get the robot position
256  robot.getPosition(wMc) ;
257  // Compute the position of the camera wrt the object frame
258  cMo = wMc.inverse() * wMo;
259 
260  // new displacement to achieve
261  cMcd = cMo*cdMo.inverse() ;
262 
263  // Extract the translation vector ctc* which is the current
264  // translational visual feature.
265  vpTranslationVector ctcd;
266  cMcd.extract(ctcd);
267  // Compute the current theta U visual feature
268  vpThetaUVector tu_cRcd(cMcd);
269 
270  // Create the identity matrix
271  vpMatrix I(3,3);
272  I.setIdentity();
273 
274  // Compute the camera translational velocity
275  vpColVector v(3);
276  v = lambda * ( I - vpColVector::skew(tu_cRcd) ) * ctcd;
277  // Compute the camera rotational velocity
278  vpColVector w(3);
279  w = lambda * tu_cRcd;
280 
281  // Update the complete camera velocity vector
282  vpColVector velocity(6);
283  for (unsigned int i=0; i<3; i++) {
284  velocity[i] = v[i]; // Translational velocity
285  velocity[i+3] = w[i]; // Rotational velocity
286  }
287 
288  // Send the camera velocity to the controller
289  robot.setVelocity(vpRobot::CAMERA_FRAME, velocity) ;
290 
291  // Retrieve the error (s-s*)
292  std::cout << "|| s - s* || = " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
293 
294  // Save log
295  flog << velocity.t() << " " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
296  }
297 
298  // Close the log file
299  flog.close();
300  return 0;
301  }
302  catch(vpException e) {
303  std::cout << "Catch a ViSP exception: " << e << std::endl;
304  return 1;
305  }
306 }
307 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:315
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Class that defines the simplest robot: a free flying camera.
error that can be emited by ViSP classes.
Definition: vpException.h:76
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
The vpRotationMatrix considers the particular case of a rotation matrix.
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:384
vpColVector t() const
Transpose the vector.
vpRowVector t() const
Transpose of a vector.
static std::string getUserName()
Definition: vpIoTools.cpp:141
void extract(vpRotationMatrix &R) const
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
vpHomogeneousMatrix inverse() const
static vpMatrix skew(const vpColVector &v)
Class that consider the case of a translation vector.
Class that consider the case of the parameterization for the rotation.