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