ViSP  2.6.2
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 - 2012 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  *****************************************************************************/
89 #include <visp/vpMath.h>
90 #include <visp/vpHomogeneousMatrix.h>
91 #include <visp/vpFeatureThetaU.h>
92 #include <visp/vpFeatureTranslation.h>
93 #include <visp/vpServo.h>
94 #include <visp/vpRobotCamera.h>
95 #include <visp/vpDebug.h>
96 #include <visp/vpParseArgv.h>
97 #include <visp/vpIoTools.h>
98 #include <stdlib.h>
99 #include <stdio.h>
100 // List of allowed command line options
101 #define GETOPTARGS "h"
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  // Read the command line options
171  if (getOptions(argc, argv) == false) {
172  exit (-1);
173  }
174 
175  // Log file creation in /tmp/$USERNAME/log.dat
176  // This file contains by line:
177  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
178  // - the 6 values of s - s*
179  std::string username;
180  // Get the user login name
181  vpIoTools::getUserName(username);
182 
183  // Create a log filename to save velocities...
184  std::string logdirname;
185 #ifdef WIN32
186  logdirname ="C:/temp/" + username;
187 #else
188  logdirname ="/tmp/" + username;
189 #endif
190  // Test if the output path exist. If no try to create it
191  if (vpIoTools::checkDirectory(logdirname) == false) {
192  try {
193  // Create the dirname
194  vpIoTools::makeDirectory(logdirname);
195  }
196  catch (...) {
197  std::cerr << std::endl
198  << "ERROR:" << std::endl;
199  std::cerr << " Cannot create " << logdirname << std::endl;
200  exit(-1);
201  }
202  }
203  std::string logfilename;
204  logfilename = logdirname + "/log.dat";
205 
206  // Open the log file name
207  std::ofstream flog(logfilename.c_str());
208 
209  vpRobotCamera robot ;
210 
211  std::cout << std::endl ;
212  std::cout << "-------------------------------------------------------" << std::endl ;
213  std::cout << " Test program for vpServo " <<std::endl ;
214  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
215  std::cout << " Simulation " << std::endl ;
216  std::cout << " task : 3D visual servoing " << std::endl ;
217  std::cout << "-------------------------------------------------------" << std::endl ;
218  std::cout << std::endl ;
219 
220  // Sets the initial camera location
221  vpPoseVector c_r_o(// Translation tx,ty,tz
222  0.1, 0.2, 2,
223  // ThetaU rotation
224  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50) ) ;
225 
226  // From the camera pose build the corresponding homogeneous matrix
227  vpHomogeneousMatrix cMo(c_r_o) ;
228 
229  // Set the robot initial position
230  robot.setPosition(cMo) ;
231 
232  // Sets the desired camera location
233  vpPoseVector cd_r_o(// Translation tx,ty,tz
234  0, 0, 1,
235  // ThetaU rotation
237 
238  // From the camera desired pose build the corresponding homogeneous matrix
239  vpHomogeneousMatrix cdMo(cd_r_o) ;
240 
241  vpHomogeneousMatrix cMcd; // Transformation between current and desired camera frame
242  vpRotationMatrix cRcd; // Rotation between current and desired camera frame
243 
244  // Set the constant gain of the servo
245  double lambda = 1;
246 
247  unsigned int iter=0 ;
248  // Start the visual servoing loop. We stop the servo after 200 iterations
249  while(iter++ < 200) {
250  std::cout << "------------------------------------" << iter <<std::endl ;
251 
252  // get the robot position
253  robot.getPosition(cMo) ;
254 
255  // new displacement to achieve
256  cMcd = cMo*cdMo.inverse() ;
257 
258  // Extract the translation vector ctc* which is the current
259  // translational visual feature.
260  vpTranslationVector ctcd;
261  cMcd.extract(ctcd);
262  // Compute the current theta U visual feature
263  vpThetaUVector tu_cRcd(cMcd);
264 
265  // Create the identity matrix
266  vpMatrix I(3,3);
267  I.setIdentity();
268 
269  // Compute the camera translational velocity
270  vpColVector v(3);
271  v = lambda * ( I - vpColVector::skew(tu_cRcd) ) * ctcd;
272  // Compute the camera rotational velocity
273  vpColVector w(3);
274  w = lambda * tu_cRcd;
275 
276  // Update the complete camera velocity vector
277  vpColVector velocity(6);
278  for (unsigned int i=0; i<3; i++) {
279  velocity[i] = v[i]; // Translational velocity
280  velocity[i+3] = w[i]; // Rotational velocity
281  }
282 
283  // Send the camera velocity to the controller
284  robot.setVelocity(vpRobot::CAMERA_FRAME, velocity) ;
285 
286  // Retrieve the error (s-s*)
287  std::cout << ctcd.t() << " " << tu_cRcd.t() << std::endl;
288 
289  // Save log
290  flog << velocity.t() << " " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
291  }
292 
293  // Close the log file
294  flog.close();
295 }
296 
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:289
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
The vpRotationMatrix considers the particular case of a rotation matrix.
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
vpColVector t() const
Transpose the vector.
Class that defines the simplest robot: a free flying camera.
Definition: vpRobotCamera.h:65
vpRowVector t() const
transpose of Vector
static std::string getUserName()
Definition: vpIoTools.cpp:136
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.