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