Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
servoSimu3D_cdMc_CamVelocity.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Simulation of a 3D visual servoing.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
59 #include <stdio.h>
60 #include <stdlib.h>
61 
62 #include <visp3/core/vpHomogeneousMatrix.h>
63 #include <visp3/core/vpIoTools.h>
64 #include <visp3/core/vpMath.h>
65 #include <visp3/io/vpParseArgv.h>
66 #include <visp3/robot/vpSimulatorCamera.h>
67 #include <visp3/visual_features/vpFeatureThetaU.h>
68 #include <visp3/visual_features/vpFeatureTranslation.h>
69 #include <visp3/vs/vpServo.h>
70 
71 // List of allowed command line options
72 #define GETOPTARGS "h"
73 
74 void usage(const char *name, const char *badparam);
75 bool getOptions(int argc, const char **argv);
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':
123  usage(argv[0], NULL);
124  return false;
125  break;
126 
127  default:
128  usage(argv[0], optarg_);
129  return false;
130  break;
131  }
132  }
133 
134  if ((c == 1) || (c == -1)) {
135  // standalone param or error
136  usage(argv[0], NULL);
137  std::cerr << "ERROR: " << std::endl;
138  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
139  return false;
140  }
141 
142  return true;
143 }
144 
145 int 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  } catch (...) {
174  std::cerr << std::endl << "ERROR:" << std::endl;
175  std::cerr << " Cannot create " << logdirname << std::endl;
176  exit(-1);
177  }
178  }
179  std::string logfilename;
180  logfilename = logdirname + "/log.dat";
181 
182  // Open the log file name
183  std::ofstream flog(logfilename.c_str());
184 
185  vpServo task;
186  vpSimulatorCamera robot;
187 
188  std::cout << std::endl;
189  std::cout << "-------------------------------------------------------" << std::endl;
190  std::cout << " Test program for vpServo " << std::endl;
191  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
192  std::cout << " Simulation " << std::endl;
193  std::cout << " task : 3D visual servoing " << std::endl;
194  std::cout << "-------------------------------------------------------" << std::endl;
195  std::cout << std::endl;
196 
197  // Sets the initial camera location
198  vpPoseVector c_r_o( // Translation tx,ty,tz
199  0.1, 0.2, 2,
200  // ThetaU rotation
201  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
202 
203  // From the camera pose build the corresponding homogeneous matrix
204  vpHomogeneousMatrix cMo(c_r_o);
205 
206  // Set the robot initial position
207  vpHomogeneousMatrix wMc, wMo;
208  robot.getPosition(wMc);
209  wMo = wMc * cMo; // Compute the position of the object in the world frame
210 
211  // Sets the desired camera location
212  vpPoseVector cd_r_o( // Translation tx,ty,tz
213  0, 0, 1,
214  // ThetaU rotation
215  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
216  // From the camera desired pose build the corresponding homogeneous matrix
217  vpHomogeneousMatrix cdMo(cd_r_o);
218 
219  // Compute the homogeneous transformation from the desired camera position
220  // to the initial one
221  vpHomogeneousMatrix cdMc;
222  cdMc = cdMo * cMo.inverse();
223 
224  // Build the current visual features s = (c*tc, thetaU_c*Rc)^T
226  vpFeatureThetaU tu(vpFeatureThetaU::cdRc); // current feature
227  t.buildFrom(cdMc);
228  tu.buildFrom(cdMc);
229 
230  // Sets the desired rotation (always zero !) since s is the
231  // rotation that the camera has to achieve. Here s* = (0, 0)^T
233  vpFeatureThetaU tud(vpFeatureThetaU::cdRc); // desired feature
234 
235  // Define the task
236  // - we want an eye-in-hand control law
237  // - the robot is controlled in the camera frame
238  task.setServo(vpServo::EYEINHAND_CAMERA);
239  // - we use here the interaction matrix computed with the
240  // current features
241  task.setInteractionMatrixType(vpServo::CURRENT);
242 
243  // Add the current and desired visual features
244  task.addFeature(t, td); // 3D translation
245  task.addFeature(tu, tud); // 3D rotation
246 
247  // - set the constant gain to 1.0
248  task.setLambda(1);
249 
250  // Display task information
251  task.print();
252 
253  unsigned int iter = 0;
254  // Start the visual servoing loop. We stop the servo after 200 iterations
255  while (iter++ < 200) {
256  std::cout << "-----------------------------------" << iter << std::endl;
257  vpColVector v;
258 
259  // get the robot position
260  robot.getPosition(wMc);
261  // Compute the position of the camera wrt the object frame
262  cMo = wMc.inverse() * wMo;
263 
264  // new displacement to achieve
265  cdMc = cdMo * cMo.inverse();
266 
267  // Update the current visual features
268  t.buildFrom(cdMc);
269  tu.buildFrom(cdMc);
270 
271  // Compute the control law
272  v = task.computeControlLaw();
273 
274  // Display task information
275  if (iter == 1)
276  task.print();
277 
278  // Send the camera velocity to the controller
280 
281  // Retrieve the error
282  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
283 
284  // Save log
285  flog << v.t() << " " << (task.getError()).t() << std::endl;
286  }
287  // Display task information
288  task.print();
289 
290  // Kill the task
291  task.kill();
292 
293  // Close the log file
294  flog.close();
295  return EXIT_SUCCESS;
296  } catch (const vpException &e) {
297  std::cout << "Catch a ViSP exception: " << e << std::endl;
298  return EXIT_FAILURE;
299  }
300 }
Class that defines the translation visual feature .
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:467
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
error that can be emited by ViSP classes.
Definition: vpException.h:71
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:597
int print(std::ostream &s, unsigned int length, char const *intro=0) const
vpRowVector t() const
static std::string getUserName()
Definition: vpIoTools.cpp:298
vpHomogeneousMatrix getPosition() const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:102
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
vpHomogeneousMatrix inverse() const
int print(std::ostream &s, unsigned int length, char const *intro=0) const
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...