ViSP  2.9.0
servoSimu3D_cMcd_CamVelocity.cpp
1 /****************************************************************************
2  *
3  * $Id: servoSimu3D_cMcd_CamVelocity.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  *****************************************************************************/
61 #include <stdlib.h>
62 #include <stdio.h>
63 
64 #include <visp/vpFeatureThetaU.h>
65 #include <visp/vpFeatureTranslation.h>
66 #include <visp/vpHomogeneousMatrix.h>
67 #include <visp/vpIoTools.h>
68 #include <visp/vpMath.h>
69 #include <visp/vpParseArgv.h>
70 #include <visp/vpServo.h>
71 #include <visp/vpSimulatorCamera.h>
72 
73 // List of allowed command line options
74 #define GETOPTARGS "h"
75 
76 void usage(const char *name, const char *badparam);
77 bool getOptions(int argc, const char **argv);
78 
87 void usage(const char *name, const char *badparam)
88 {
89  fprintf(stdout, "\n\
90 Simulation of a 3D visual servoing:\n\
91 - eye-in-hand control law,\n\
92 - velocity computed in the camera frame,\n\
93 - without display.\n\
94  \n\
95 SYNOPSIS\n\
96  %s [-h]\n", name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  \n\
101  -h\n\
102  Print the help.\n");
103 
104  if (badparam)
105  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
106 }
107 
117 bool getOptions(int argc, const char **argv)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'h': usage(argv[0], NULL); return false; break;
125 
126  default:
127  usage(argv[0], optarg_);
128  return false; break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], NULL);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
143 int
144 main(int argc, const char ** argv)
145 {
146  try {
147  // Read the command line options
148  if (getOptions(argc, argv) == false) {
149  exit (-1);
150  }
151 
152  // Log file creation in /tmp/$USERNAME/log.dat
153  // This file contains by line:
154  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
155  // - the 6 values of s - s*
156  std::string username;
157  // Get the user login name
158  vpIoTools::getUserName(username);
159 
160  // Create a log filename to save velocities...
161  std::string logdirname;
162 #if defined(_WIN32)
163  logdirname ="C:/temp/" + username;
164 #else
165  logdirname ="/tmp/" + username;
166 #endif
167  // Test if the output path exist. If no try to create it
168  if (vpIoTools::checkDirectory(logdirname) == false) {
169  try {
170  // Create the dirname
171  vpIoTools::makeDirectory(logdirname);
172  }
173  catch (...) {
174  std::cerr << std::endl
175  << "ERROR:" << std::endl;
176  std::cerr << " Cannot create " << logdirname << std::endl;
177  exit(-1);
178  }
179  }
180  std::string logfilename;
181  logfilename = logdirname + "/log.dat";
182 
183  // Open the log file name
184  std::ofstream flog(logfilename.c_str());
185 
186  vpServo task ;
187  vpSimulatorCamera robot ;
188 
189  std::cout << std::endl ;
190  std::cout << "-------------------------------------------------------" << std::endl ;
191  std::cout << " Test program for vpServo " <<std::endl ;
192  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl ;
193  std::cout << " Simulation " << std::endl ;
194  std::cout << " task : 3D visual servoing " << std::endl ;
195  std::cout << "-------------------------------------------------------" << std::endl ;
196  std::cout << std::endl ;
197 
198  // Sets the initial camera location
199  vpPoseVector c_r_o(// Translation tx,ty,tz
200  0.1, 0.2, 2,
201  // ThetaU rotation
202  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50) ) ;
203 
204  // From the camera pose build the corresponding homogeneous matrix
205  vpHomogeneousMatrix cMo(c_r_o) ;
206 
207  // Set the robot initial position
208  vpHomogeneousMatrix wMc, wMo;
209  robot.getPosition(wMc) ;
210  wMo = wMc * cMo; // Compute the position of the object in the world frame
211 
212  // Sets the desired camera location
213  vpPoseVector cd_r_o(// Translation tx,ty,tz
214  0, 0, 1,
215  // ThetaU rotation
217 
218  // From the camera desired pose build the corresponding homogeneous matrix
219  vpHomogeneousMatrix cdMo(cd_r_o) ;
220 
221  // Compute the transformation from the initial camera position to the desired one
222  vpHomogeneousMatrix cMcd ;
223  cMcd = cMo*cdMo.inverse() ;
224 
225  // Build the 3D translation feature: ctc*
227  t.buildFrom(cMcd) ;
228 
229  // Build the 3D rotation feature: thetaU_cRc*
230  vpFeatureThetaU tu(vpFeatureThetaU::cRcd); // current feature
231  tu.buildFrom(cMcd) ;
232 
233  // Sets the desired rotation (always zero !) since s is the
234  // rotation that the camera has to achieve. Here s* = (0, 0)^T
236  vpFeatureThetaU tud(vpFeatureThetaU::cRcd); // desired feature
237 
238  // Define the task
239  // - we want an eye-in-hand control law
240  // - the robot is controlled in the camera frame
241  task.setServo(vpServo::EYEINHAND_CAMERA) ;
242  // - we use here the interaction matrix computed with the current
243  // features
244  task.setInteractionMatrixType(vpServo::CURRENT);
245 
246  // Add the current and desired visual features
247  task.addFeature(t,td) ; // 3D translation
248  task.addFeature(tu,tud) ; // 3D rotation theta u
249 
250  // - set the constant gain to 1.0
251  task.setLambda(1) ;
252 
253  // Display task information
254  task.print() ;
255 
256  unsigned int iter=0 ;
257  // Start the visual servoing loop. We stop the servo after 200 iterations
258  while(iter++ < 200) {
259  std::cout << "------------------------------------" << iter <<std::endl ;
260  vpColVector v ;
261 
262  // get the robot position
263  robot.getPosition(wMc) ;
264  // Compute the position of the camera wrt the object frame
265  cMo = wMc.inverse() * wMo;
266 
267  // new displacement to achieve
268  cMcd = cMo*cdMo.inverse() ;
269 
270  // Update the current visual features
271  t.buildFrom(cMcd) ;
272  tu.buildFrom(cMcd) ;
273 
274  // Compute the control law
275  v = task.computeControlLaw() ;
276 
277  // Display task information
278  if (iter==1) task.print() ;
279 
280  // Send the camera velocity to the controller
282 
283  // Retrieve the error
284  std::cout << "|| s - s* || = " << ( task.getError() ).sumSquare() <<std::endl ;
285 
286  // Save log
287  flog << v.t() << " " << ( task.getError() ).t() << std::endl;
288  }
289  // Display task information
290  task.print() ;
291 
292  // Kill the task
293  task.kill();
294 
295  // Close the log file
296  flog.close();
297  return 0;
298  }
299  catch(vpException e) {
300  std::cout << "Catch a ViSP exception: " << e << std::endl;
301  return 1;
302  }
303 }
304 
Class that defines the translation visual feature .
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
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
int print(std::ostream &s, unsigned int length, char const *intro=0)
Definition: vpMatrix.cpp:2668
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
vpRowVector t() const
transpose of Vector
void getPosition(vpHomogeneousMatrix &wMc) const
static std::string getUserName()
Definition: vpIoTools.cpp:140
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Construction from translation vector and rotation matrix.
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 defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...