Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
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/vpConfig.h>
58 #include <visp3/core/vpHomogeneousMatrix.h>
59 #include <visp3/core/vpIoTools.h>
60 #include <visp3/core/vpMath.h>
61 #include <visp3/io/vpParseArgv.h>
62 #include <visp3/robot/vpSimulatorCamera.h>
63 #include <visp3/visual_features/vpFeatureThetaU.h>
64 #include <visp3/visual_features/vpFeatureTranslation.h>
65 #include <visp3/vs/vpServo.h>
66 
67 // List of allowed command line options
68 #define GETOPTARGS "h"
69 
70 #ifdef ENABLE_VISP_NAMESPACE
71 using namespace VISP_NAMESPACE_NAME;
72 #endif
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",
95  name);
96 
97  fprintf(stdout, "\n\
98 OPTIONS: Default\n\
99  \n\
100  -h\n\
101  Print the help.\n");
102 
103  if (badparam)
104  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
105 }
106 
116 bool getOptions(int argc, const char **argv)
117 {
118  const char *optarg_;
119  int c;
120  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
121 
122  switch (c) {
123  case 'h':
124  usage(argv[0], nullptr);
125  return false;
126 
127  default:
128  usage(argv[0], optarg_);
129  return false;
130  }
131  }
132 
133  if ((c == 1) || (c == -1)) {
134  // standalone param or error
135  usage(argv[0], nullptr);
136  std::cerr << "ERROR: " << std::endl;
137  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
138  return false;
139  }
140 
141  return true;
142 }
143 
144 int main(int argc, const char **argv)
145 {
146 #if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
147  try {
148  // Read the command line options
149  if (getOptions(argc, argv) == false) {
150  return EXIT_FAILURE;
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  }
174  catch (...) {
175  std::cerr << std::endl << "ERROR:" << std::endl;
176  std::cerr << " Cannot create " << logdirname << std::endl;
177  return EXIT_FAILURE;
178  }
179  }
180  std::string logfilename;
181  logfilename = logdirname + "/log.dat";
182 
183  // Open the log file name
184  std::ofstream flog(logfilename.c_str());
185 
186  vpServo task;
187  vpSimulatorCamera robot;
188 
189  std::cout << std::endl;
190  std::cout << "-------------------------------------------------------" << std::endl;
191  std::cout << " Test program for vpServo " << std::endl;
192  std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
193  std::cout << " Simulation " << std::endl;
194  std::cout << " task : 3D visual servoing " << std::endl;
195  std::cout << "-------------------------------------------------------" << std::endl;
196  std::cout << std::endl;
197 
198  // Sets the initial camera location
199  vpPoseVector c_r_o( // Translation tx,ty,tz
200  0.1, 0.2, 2,
201  // ThetaU rotation
202  vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
203 
204  // From the camera pose build the corresponding homogeneous matrix
205  vpHomogeneousMatrix cMo(c_r_o);
206 
207  // Set the robot initial position
208  vpHomogeneousMatrix wMc, wMo;
209  robot.getPosition(wMc);
210  wMo = wMc * cMo; // Compute the position of the object in the world frame
211 
212  // Sets the desired camera location
213  vpPoseVector cd_r_o( // Translation tx,ty,tz
214  0, 0, 1,
215  // ThetaU rotation
216  vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
217 
218  // From the camera desired pose build the corresponding homogeneous matrix
219  vpHomogeneousMatrix cdMo(cd_r_o);
220 
221  // Compute the transformation from the initial camera position to the
222  // desired one
223  vpHomogeneousMatrix cMcd;
224  cMcd = cMo * cdMo.inverse();
225 
226  // Build the 3D translation feature: ctc*
228  t.buildFrom(cMcd);
229 
230  // Build the 3D rotation feature: thetaU_cRc*
231  vpFeatureThetaU tu(vpFeatureThetaU::cRcd); // current feature
232  tu.buildFrom(cMcd);
233 
234  // Sets the desired rotation (always zero !) since s is the
235  // rotation that the camera has to achieve. Here s* = (0, 0)^T
237  vpFeatureThetaU tud(vpFeatureThetaU::cRcd); // desired feature
238 
239  // Define the task
240  // - we want an eye-in-hand control law
241  // - the robot is controlled in the camera frame
243  // - we use here the interaction matrix computed with the current
244  // features
246 
247  // Add the current and desired visual features
248  task.addFeature(t, td); // 3D translation
249  task.addFeature(tu, tud); // 3D rotation theta u
250 
251  // - set the constant gain to 1.0
252  task.setLambda(1);
253 
254  // Display task information
255  task.print();
256 
257  unsigned int iter = 0;
258  // Start the visual servoing loop. We stop the servo after 200 iterations
259  while (iter++ < 200) {
260  std::cout << "------------------------------------" << iter << std::endl;
261  vpColVector v;
262 
263  // get the robot position
264  robot.getPosition(wMc);
265  // Compute the position of the object frame in the camera frame
266  cMo = wMc.inverse() * wMo;
267 
268  // new displacement to achieve
269  cMcd = cMo * cdMo.inverse();
270 
271  // Update the current visual features
272  t.buildFrom(cMcd);
273  tu.buildFrom(cMcd);
274 
275  // Compute the control law
276  v = task.computeControlLaw();
277 
278  // Display task information
279  if (iter == 1)
280  task.print();
281 
282  // Send the camera velocity to the controller
284 
285  // Retrieve the error
286  std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
287 
288  // Save log
289  flog << v.t() << " " << (task.getError()).t() << std::endl;
290  }
291  // Display task information
292  task.print();
293 
294  // Kill the task
295 
296  // Close the log file
297  flog.close();
298  return EXIT_SUCCESS;
299  }
300  catch (const vpException &e) {
301  std::cout << "Catch a ViSP exception: " << e << std::endl;
302  return EXIT_FAILURE;
303  }
304 #else
305  (void)argc;
306  (void)argv;
307  std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
308  return EXIT_SUCCESS;
309 #endif
310 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:60
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 & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix inverse() const
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string getUserName()
Definition: vpIoTools.cpp:285
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static double rad(double deg)
Definition: vpMath.h:129
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ CAMERA_FRAME
Definition: vpRobot.h:84
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:986
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:510
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
Class that defines the simplest robot: a free flying camera.