Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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", 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':
122  usage(argv[0], NULL);
123  return false;
124 
125  default:
126  usage(argv[0], optarg_);
127  return false;
128  }
129  }
130 
131  if ((c == 1) || (c == -1)) {
132  // standalone param or error
133  usage(argv[0], NULL);
134  std::cerr << "ERROR: " << std::endl;
135  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
136  return false;
137  }
138 
139  return true;
140 }
141 
142 int main(int argc, const char **argv)
143 {
144 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
145  try {
146  // Read the command line options
147  if (getOptions(argc, argv) == false) {
148  exit(-1);
149  }
150 
151  // Log file creation in /tmp/$USERNAME/log.dat
152  // This file contains by line:
153  // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
154  // - the 6 values of s - s*
155  std::string username;
156  // Get the user login name
157  vpIoTools::getUserName(username);
158 
159  // Create a log filename to save velocities...
160  std::string logdirname;
161 #if defined(_WIN32)
162  logdirname = "C:/temp/" + username;
163 #else
164  logdirname = "/tmp/" + username;
165 #endif
166  // Test if the output path exist. If no try to create it
167  if (vpIoTools::checkDirectory(logdirname) == false) {
168  try {
169  // Create the dirname
170  vpIoTools::makeDirectory(logdirname);
171  } catch (...) {
172  std::cerr << std::endl << "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  // Sets the initial camera location
196  vpPoseVector c_r_o( // Translation tx,ty,tz
197  0.1, 0.2, 2,
198  // ThetaU rotation
199  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
200 
201  // From the camera pose build the corresponding homogeneous matrix
202  vpHomogeneousMatrix cMo(c_r_o);
203 
204  // Set the robot initial position
205  vpHomogeneousMatrix wMc, wMo;
206  robot.getPosition(wMc);
207  wMo = wMc * cMo; // Compute the position of the object in the world frame
208 
209  // Sets the desired camera location
210  vpPoseVector cd_r_o( // Translation tx,ty,tz
211  0, 0, 1,
212  // ThetaU rotation
213  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
214 
215  // From the camera desired pose build the corresponding homogeneous matrix
216  vpHomogeneousMatrix cdMo(cd_r_o);
217 
218  // Compute the transformation from the initial camera position to the
219  // desired one
220  vpHomogeneousMatrix cMcd;
221  cMcd = cMo * cdMo.inverse();
222 
223  // Build the 3D translation feature: ctc*
225  t.buildFrom(cMcd);
226 
227  // Build the 3D rotation feature: thetaU_cRc*
228  vpFeatureThetaU tu(vpFeatureThetaU::cRcd); // current feature
229  tu.buildFrom(cMcd);
230 
231  // Sets the desired rotation (always zero !) since s is the
232  // rotation that the camera has to achieve. Here s* = (0, 0)^T
234  vpFeatureThetaU tud(vpFeatureThetaU::cRcd); // desired feature
235 
236  // Define the task
237  // - we want an eye-in-hand control law
238  // - the robot is controlled in the camera frame
239  task.setServo(vpServo::EYEINHAND_CAMERA);
240  // - we use here the interaction matrix computed with the current
241  // features
242  task.setInteractionMatrixType(vpServo::CURRENT);
243 
244  // Add the current and desired visual features
245  task.addFeature(t, td); // 3D translation
246  task.addFeature(tu, tud); // 3D rotation theta u
247 
248  // - set the constant gain to 1.0
249  task.setLambda(1);
250 
251  // Display task information
252  task.print();
253 
254  unsigned int iter = 0;
255  // Start the visual servoing loop. We stop the servo after 200 iterations
256  while (iter++ < 200) {
257  std::cout << "------------------------------------" << iter << std::endl;
258  vpColVector v;
259 
260  // get the robot position
261  robot.getPosition(wMc);
262  // Compute the position of the object frame in the camera frame
263  cMo = wMc.inverse() * wMo;
264 
265  // new displacement to achieve
266  cMcd = cMo * cdMo.inverse();
267 
268  // Update the current visual features
269  t.buildFrom(cMcd);
270  tu.buildFrom(cMcd);
271 
272  // Compute the control law
273  v = task.computeControlLaw();
274 
275  // Display task information
276  if (iter == 1)
277  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 
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 #else
301  (void)argc;
302  (void)argv;
303  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
304  return EXIT_SUCCESS;
305 #endif
306 }
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
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:420
static std::string getUserName()
Definition: vpIoTools.cpp:316
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
Definition: vpMath.h:110
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...