Visual Servoing Platform  version 3.5.1 under development (2022-08-18)
servoSimu3D_cMcd_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  *****************************************************************************/
58 #include <stdio.h>
59 #include <stdlib.h>
60 
61 #include <visp3/core/vpHomogeneousMatrix.h>
62 #include <visp3/core/vpIoTools.h>
63 #include <visp3/core/vpMath.h>
64 #include <visp3/io/vpParseArgv.h>
65 #include <visp3/robot/vpSimulatorCamera.h>
66 #include <visp3/visual_features/vpFeatureThetaU.h>
67 #include <visp3/visual_features/vpFeatureTranslation.h>
68 #include <visp3/vs/vpServo.h>
69 
70 // List of allowed command line options
71 #define GETOPTARGS "h"
72 
73 void usage(const char *name, const char *badparam);
74 bool getOptions(int argc, const char **argv);
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",
94  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 
126  default:
127  usage(argv[0], optarg_);
128  return false;
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 main(int argc, const char **argv)
144 {
145 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
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  } catch (...) {
173  std::cerr << std::endl << "ERROR:" << std::endl;
174  std::cerr << " Cannot create " << logdirname << std::endl;
175  exit(-1);
176  }
177  }
178  std::string logfilename;
179  logfilename = logdirname + "/log.dat";
180 
181  // Open the log file name
182  std::ofstream flog(logfilename.c_str());
183 
184  vpServo task;
185  vpSimulatorCamera robot;
186 
187  std::cout << std::endl;
188  std::cout << "-------------------------------------------------------" << std::endl;
189  std::cout << " Test program for vpServo " << std::endl;
190  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
191  std::cout << " Simulation " << std::endl;
192  std::cout << " task : 3D visual servoing " << std::endl;
193  std::cout << "-------------------------------------------------------" << std::endl;
194  std::cout << std::endl;
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
214  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
215 
216  // From the camera desired pose build the corresponding homogeneous matrix
217  vpHomogeneousMatrix cdMo(cd_r_o);
218 
219  // Compute the transformation from the initial camera position to the
220  // desired one
221  vpHomogeneousMatrix cMcd;
222  cMcd = cMo * cdMo.inverse();
223 
224  // Build the 3D translation feature: ctc*
226  t.buildFrom(cMcd);
227 
228  // Build the 3D rotation feature: thetaU_cRc*
229  vpFeatureThetaU tu(vpFeatureThetaU::cRcd); // current feature
230  tu.buildFrom(cMcd);
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::cRcd); // 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 current
242  // 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 theta u
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 object frame in the camera frame
264  cMo = wMc.inverse() * wMo;
265 
266  // new displacement to achieve
267  cMcd = cMo * cdMo.inverse();
268 
269  // Update the current visual features
270  t.buildFrom(cMcd);
271  tu.buildFrom(cMcd);
272 
273  // Compute the control law
274  v = task.computeControlLaw();
275 
276  // Display task information
277  if (iter == 1)
278  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 
294  // Close the log file
295  flog.close();
296  return EXIT_SUCCESS;
297  } catch (const vpException &e) {
298  std::cout << "Catch a ViSP exception: " << e << std::endl;
299  return EXIT_FAILURE;
300  }
301 #else
302  (void)argc;
303  (void)argv;
304  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
305  return EXIT_SUCCESS;
306 #endif
307 }
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:568
Class that defines the translation visual feature .
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
vpHomogeneousMatrix inverse() const
vpRowVector t() const
vpHomogeneousMatrix getPosition() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
int print(std::ostream &s, unsigned int length, char const *intro=0) const
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:418
static std::string getUserName()
Definition: vpIoTools.cpp:314
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:117
int print(std::ostream &s, unsigned int length, char const *intro=0) const
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:151
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...