ViSP  2.8.0
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 - 2013 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 
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': usage(argv[0], NULL); return false; break;
146 
147  default:
148  usage(argv[0], optarg);
149  return false; break;
150  }
151  }
152 
153  if ((c == 1) || (c == -1)) {
154  // standalone param or error
155  usage(argv[0], NULL);
156  std::cerr << "ERROR: " << std::endl;
157  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
158  return false;
159  }
160 
161  return true;
162 }
163 
164 int
165 main(int argc, const char ** argv)
166 {
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 #ifdef 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.setIdentity();
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 }
297 
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
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.
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:404
vpColVector t() const
Transpose the vector.
vpRowVector t() const
transpose of Vector
static std::string getUserName()
Definition: vpIoTools.cpp:140
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.