Visual Servoing Platform  version 3.6.1 under development (2024-04-26)
servoSimu3D_cMcd_CamVelocity.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
54 #include <stdio.h>
55 #include <stdlib.h>
56 
57 #include <visp3/core/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpIoTools.h>
59 #include <visp3/core/vpMath.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/robot/vpSimulatorCamera.h>
62 #include <visp3/visual_features/vpFeatureThetaU.h>
63 #include <visp3/visual_features/vpFeatureTranslation.h>
64 #include <visp3/vs/vpServo.h>
65 
66 // List of allowed command line options
67 #define GETOPTARGS "h"
68 
69 void usage(const char *name, const char *badparam);
70 bool getOptions(int argc, const char **argv);
71 
80 void usage(const char *name, const char *badparam)
81 {
82  fprintf(stdout, "\n\
83 Simulation of a 3D visual servoing:\n\
84 - eye-in-hand control law,\n\
85 - velocity computed in the camera frame,\n\
86 - without display.\n\
87  \n\
88 SYNOPSIS\n\
89  %s [-h]\n",
90  name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  \n\
95  -h\n\
96  Print the help.\n");
97 
98  if (badparam)
99  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
100 }
101 
111 bool getOptions(int argc, const char **argv)
112 {
113  const char *optarg_;
114  int c;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117  switch (c) {
118  case 'h':
119  usage(argv[0], nullptr);
120  return false;
121 
122  default:
123  usage(argv[0], optarg_);
124  return false;
125  }
126  }
127 
128  if ((c == 1) || (c == -1)) {
129  // standalone param or error
130  usage(argv[0], nullptr);
131  std::cerr << "ERROR: " << std::endl;
132  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
133  return false;
134  }
135 
136  return true;
137 }
138 
139 int main(int argc, const char **argv)
140 {
141 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
142  try {
143  // Read the command line options
144  if (getOptions(argc, argv) == false) {
145  return EXIT_FAILURE;
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 #if defined(_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 << "ERROR:" << std::endl;
171  std::cerr << " Cannot create " << logdirname << std::endl;
172  return EXIT_FAILURE;
173  }
174  }
175  std::string logfilename;
176  logfilename = logdirname + "/log.dat";
177 
178  // Open the log file name
179  std::ofstream flog(logfilename.c_str());
180 
181  vpServo task;
182  vpSimulatorCamera robot;
183 
184  std::cout << std::endl;
185  std::cout << "-------------------------------------------------------" << std::endl;
186  std::cout << " Test program for vpServo " << std::endl;
187  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
188  std::cout << " Simulation " << std::endl;
189  std::cout << " task : 3D visual servoing " << std::endl;
190  std::cout << "-------------------------------------------------------" << std::endl;
191  std::cout << std::endl;
192 
193  // Sets the initial camera location
194  vpPoseVector c_r_o( // Translation tx,ty,tz
195  0.1, 0.2, 2,
196  // ThetaU rotation
197  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
198 
199  // From the camera pose build the corresponding homogeneous matrix
200  vpHomogeneousMatrix cMo(c_r_o);
201 
202  // Set the robot initial position
203  vpHomogeneousMatrix wMc, wMo;
204  robot.getPosition(wMc);
205  wMo = wMc * cMo; // Compute the position of the object in the world frame
206 
207  // Sets the desired camera location
208  vpPoseVector cd_r_o( // Translation tx,ty,tz
209  0, 0, 1,
210  // ThetaU rotation
211  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
212 
213  // From the camera desired pose build the corresponding homogeneous matrix
214  vpHomogeneousMatrix cdMo(cd_r_o);
215 
216  // Compute the transformation from the initial camera position to the
217  // 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
238  // - we use here the interaction matrix computed with the current
239  // features
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 object frame in the camera 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)
275  task.print();
276 
277  // Send the camera velocity to the controller
279 
280  // Retrieve the error
281  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
282 
283  // Save log
284  flog << v.t() << " " << (task.getError()).t() << std::endl;
285  }
286  // Display task information
287  task.print();
288 
289  // Kill the task
290 
291  // Close the log file
292  flog.close();
293  return EXIT_SUCCESS;
294  }
295  catch (const vpException &e) {
296  std::cout << "Catch a ViSP exception: " << e << std::endl;
297  return EXIT_FAILURE;
298  }
299 #else
300  (void)argc;
301  (void)argv;
302  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
303  return EXIT_SUCCESS;
304 #endif
305 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:59
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
Class that defines the translation visual feature .
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:832
static std::string getUserName()
Definition: vpIoTools.cpp:725
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:981
static double rad(double deg)
Definition: vpMath.h:127
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:189
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
@ CAMERA_FRAME
Definition: vpRobot.h:82
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:378
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:329
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:169
void setLambda(double c)
Definition: vpServo.h:976
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:132
vpColVector getError() const
Definition: vpServo.h:504
vpColVector computeControlLaw()
Definition: vpServo.cpp:703
@ CURRENT
Definition: vpServo.h:196
Class that defines the simplest robot: a free flying camera.