ViSP  2.6.2
servoSimu3D_cdMc_CamVelocityWithoutVpServo.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimu3D_cdMc_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  *****************************************************************************/
80 #include <visp/vpMath.h>
81 #include <visp/vpHomogeneousMatrix.h>
82 #include <visp/vpThetaUVector.h>
83 #include <visp/vpTranslationVector.h>
84 #include <visp/vpRobotCamera.h>
85 #include <visp/vpDebug.h>
86 #include <visp/vpParseArgv.h>
87 #include <visp/vpIoTools.h>
88 #include <stdlib.h>
89 #include <stdio.h>
90 #include <string>
91 // List of allowed command line options
92 #define GETOPTARGS "h"
93 
102 void usage(const char *name, const char *badparam)
103 {
104  fprintf(stdout, "\n\
105 Simulation of a 3D visual servoing:\n\
106 - eye-in-hand control law,\n\
107 - velocity computed in the camera frame,\n\
108 - without display.\n\
109 \n\
110 SYNOPSIS\n\
111  %s [-h]\n", name);
112 
113  fprintf(stdout, "\n\
114 OPTIONS: Default\n\
115 \n\
116  -h\n\
117  Print the help.\n");
118 
119  if (badparam)
120  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
121 }
122 
132 bool getOptions(int argc, const char **argv)
133 {
134  const char *optarg;
135  int c;
136  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
137 
138  switch (c) {
139  case 'h': usage(argv[0], NULL); return false; break;
140 
141  default:
142  usage(argv[0], optarg);
143  return false; break;
144  }
145  }
146 
147  if ((c == 1) || (c == -1)) {
148  // standalone param or error
149  usage(argv[0], NULL);
150  std::cerr << "ERROR: " << std::endl;
151  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
152  return false;
153  }
154 
155  return true;
156 }
157 
158 int
159 main(int argc, const char ** argv)
160 {
161  // Read the command line options
162  if (getOptions(argc, argv) == false) {
163  exit (-1);
164  }
165  // Log file creation in /tmp/$USERNAME/log.dat
166  // This file contains by line:
167  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
168  // - the 6 values of s - s*
169  std::string username;
170  // Get the user login name
171  vpIoTools::getUserName(username);
172 
173  // Create a log filename to save velocities...
174  std::string logdirname;
175 #ifdef WIN32
176  logdirname ="C:/temp/" + username;
177 #else
178  logdirname ="/tmp/" + username;
179 #endif
180 
181  // Test if the output path exist. If no try to create it
182  if (vpIoTools::checkDirectory(logdirname) == false) {
183  try {
184  // Create the dirname
185  vpIoTools::makeDirectory(logdirname);
186  }
187  catch (...) {
188  std::cerr << std::endl
189  << "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  vpRobotCamera 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 
212  // Sets the initial camera location
213  vpPoseVector c_r_o(// Translation tx,ty,tz
214  0.1, 0.2, 2,
215  // ThetaU rotation
216  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50) ) ;
217 
218  // From the camera pose build the corresponding homogeneous matrix
219  vpHomogeneousMatrix cMo(c_r_o) ;
220 
221  // Set the robot initial position
222  robot.setPosition(cMo) ;
223 
224  // Sets the desired camera location
225  vpPoseVector cd_r_o(// Translation tx,ty,tz
226  0, 0, 1,
227  // ThetaU rotation
229  // From the camera desired pose build the corresponding homogeneous matrix
230  vpHomogeneousMatrix cdMo(cd_r_o) ;
231 
232  vpHomogeneousMatrix cdMc; // Transformation between desired and current camera frame
233  vpRotationMatrix cdRc; // Rotation between desired and current camera frame
234  vpRotationMatrix cRcd; // Rotation between current and desired camera frame
235 
236  // Set the constant gain of the servo
237  double lambda = 1;
238 
239  unsigned int iter=0 ;
240  // Start the visual servoing loop. We stop the servo after 200 iterations
241  while(iter++ < 200) {
242  std::cout << "-----------------------------------" << iter <<std::endl ;
243 
244  // get the robot position
245  robot.getPosition(cMo) ;
246 
247  // new displacement to achieve
248  cdMc = cdMo*cMo.inverse() ;
249  // Extract the translation vector c*tc which is the current
250  // translational visual feature.
251  vpTranslationVector cdtc;
252  cdMc.extract(cdtc);
253  // Extract the rotation matrix c*Rc
254  cdMc.extract(cdRc);
255  // Compute the inverse rotation cRc* (in fact the transpose of c*Rc)
256  cRcd = cdRc.inverse();
257  // Compute the current theta U visual feature
258  vpThetaUVector tu_cdRc(cdMc);
259  // Compute the camera translational velocity
260  vpColVector v(3);
261  v = -lambda * cRcd * cdtc;
262  // Compute the camera rotational velocity
263  vpColVector w(3);
264  w = -lambda * tu_cdRc;
265 
266  // Update the complete camera velocity vector
267  vpColVector velocity(6);
268  for (unsigned int i=0; i<3; i++) {
269  velocity[i] = v[i]; // Translational velocity
270  velocity[i+3] = w[i]; // Rotational velocity
271  }
272 
273  // Send the camera velocity to the controller
274  robot.setVelocity(vpRobot::CAMERA_FRAME, velocity) ;
275 
276  // Retrieve the error (s-s*)
277  std::cout << cdtc.t() << " " << tu_cdRc.t() << std::endl;
278 
279  // Save log
280  flog << velocity.t() << " " << cdtc.t() << " " << tu_cdRc.t() << std::endl;
281  }
282  // Close the log file
283  flog.close();
284 }
285 
vpRotationMatrix inverse() const
invert the rotation matrix
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
Class that defines the simplest robot: a free flying camera.
Definition: vpRobotCamera.h:65
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
Class that consider the case of a translation vector.
Class that consider the case of the parameterization for the rotation.