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