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