Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimu3D_cMcd_CamVelocityWithoutVpServo.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description
31 * Simulation of a 3D visual servoing.
32 */
75
76#include <stdio.h>
77#include <stdlib.h>
78
79#include <visp3/core/vpConfig.h>
80#include <visp3/core/vpHomogeneousMatrix.h>
81#include <visp3/core/vpIoTools.h>
82#include <visp3/core/vpMath.h>
83#include <visp3/io/vpParseArgv.h>
84#include <visp3/robot/vpSimulatorCamera.h>
85#include <visp3/visual_features/vpFeatureThetaU.h>
86#include <visp3/visual_features/vpFeatureTranslation.h>
87#include <visp3/vs/vpServo.h>
88
89// List of allowed command line options
90#define GETOPTARGS "h"
91
92#ifdef ENABLE_VISP_NAMESPACE
93using namespace VISP_NAMESPACE_NAME;
94#endif
95
96void usage(const char *name, const char *badparam);
97bool getOptions(int argc, const char **argv);
98
107void usage(const char *name, const char *badparam)
108{
109 fprintf(stdout, "\n\
110Simulation of a 3D visual servoing:\n\
111- eye-in-hand control law,\n\
112- velocity computed in the camera frame,\n\
113- without display.\n\
114\n\
115SYNOPSIS\n\
116 %s [-h]\n",
117 name);
118
119 fprintf(stdout, "\n\
120OPTIONS: Default\n\
121\n\
122 -h\n\
123 Print the help.\n");
124
125 if (badparam)
126 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
127}
128
138bool getOptions(int argc, const char **argv)
139{
140 const char *optarg_;
141 int c;
142 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
143
144 switch (c) {
145 case 'h':
146 usage(argv[0], nullptr);
147 return false;
148
149 default:
150 usage(argv[0], optarg_);
151 return false;
152 }
153 }
154
155 if ((c == 1) || (c == -1)) {
156 // standalone param or error
157 usage(argv[0], nullptr);
158 std::cerr << "ERROR: " << std::endl;
159 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
160 return false;
161 }
162
163 return true;
164}
165
166int main(int argc, const char **argv)
167{
168 try {
169 // Read the command line options
170 if (getOptions(argc, argv) == false) {
171 return EXIT_FAILURE;
172 }
173
174 // Log file creation in /tmp/$USERNAME/log.dat
175 // This file contains by line:
176 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
177 // - the 6 values of s - s*
178 std::string username;
179 // Get the user login name
180 vpIoTools::getUserName(username);
181
182 // Create a log filename to save velocities...
183 std::string logdirname;
184#if defined(_WIN32)
185 logdirname = "C:/temp/" + username;
186#else
187 logdirname = "/tmp/" + username;
188#endif
189 // Test if the output path exist. If no try to create it
190 if (vpIoTools::checkDirectory(logdirname) == false) {
191 try {
192 // Create the dirname
193 vpIoTools::makeDirectory(logdirname);
194 }
195 catch (...) {
196 std::cerr << std::endl << "ERROR:" << std::endl;
197 std::cerr << " Cannot create " << logdirname << std::endl;
198 return EXIT_FAILURE;
199 }
200 }
201 std::string logfilename;
202 logfilename = logdirname + "/log.dat";
203
204 // Open the log file name
205 std::ofstream flog(logfilename.c_str());
206
207 vpSimulatorCamera robot;
208
209 std::cout << std::endl;
210 std::cout << "-------------------------------------------------------" << std::endl;
211 std::cout << " Test program for vpServo " << std::endl;
212 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
213 std::cout << " Simulation " << std::endl;
214 std::cout << " task : 3D visual servoing " << std::endl;
215 std::cout << "-------------------------------------------------------" << std::endl;
216 std::cout << std::endl;
217
218 // Sets the initial camera location
219 vpPoseVector c_r_o( // Translation tx,ty,tz
220 0.1, 0.2, 2,
221 // ThetaU rotation
222 vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
223
224 // From the camera pose build the corresponding homogeneous matrix
226
227 // Set the robot initial position
228 vpHomogeneousMatrix wMc, wMo;
229 robot.getPosition(wMc);
230 wMo = wMc * cMo; // Compute the position of the object in the world frame
231
232 // Sets the desired camera location
233 vpPoseVector cd_r_o( // Translation tx,ty,tz
234 0, 0, 1,
235 // ThetaU rotation
237
238 // From the camera desired pose build the corresponding homogeneous matrix
239 vpHomogeneousMatrix cdMo(cd_r_o);
240
241 vpHomogeneousMatrix cMcd; // Transformation between current and desired camera frame
242 vpRotationMatrix cRcd; // Rotation between current and desired camera frame
243
244 // Set the constant gain of the servo
245 double lambda = 1;
246
247 unsigned int iter = 0;
248 // Start the visual servoing loop. We stop the servo after 200 iterations
249 while (iter++ < 200) {
250 std::cout << "------------------------------------" << iter << std::endl;
251
252 // get the robot position
253 robot.getPosition(wMc);
254 // Compute the position of the object frame in the camera frame
255 cMo = wMc.inverse() * wMo;
256
257 // new displacement to achieve
258 cMcd = cMo * cdMo.inverse();
259
260 // Extract the translation vector ctc* which is the current
261 // translational visual feature.
263 cMcd.extract(ctcd);
264 // Compute the current theta U visual feature
265 vpThetaUVector tu_cRcd(cMcd);
266
267 // Create the identity matrix
268 vpMatrix I(3, 3);
269 I.eye();
270
271 // Compute the camera translational velocity
272 vpColVector v(3);
273 v = lambda * (I - vpColVector::skew(vpColVector(tu_cRcd))) * ctcd;
274 // Compute the camera rotational velocity
275 vpColVector w(3);
276 w = lambda * tu_cRcd;
277
278 // Update the complete camera velocity vector
279 vpColVector velocity(6);
280 for (unsigned int i = 0; i < 3; i++) {
281 velocity[i] = v[i]; // Translational velocity
282 velocity[i + 3] = w[i]; // Rotational velocity
283 }
284
285 // Send the camera velocity to the controller
286 robot.setVelocity(vpRobot::CAMERA_FRAME, velocity);
287
288 // Retrieve the error (s-s*)
289 std::cout << "|| s - s* || = " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
290
291 // Save log
292 flog << velocity.t() << " " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
293 }
294
295 // Close the log file
296 flog.close();
297 return EXIT_SUCCESS;
298 }
299 catch (const vpException &e) {
300 std::cout << "Catch a ViSP exception: " << e << std::endl;
301 return EXIT_FAILURE;
302 }
303}
Implementation of column vector and the associated operations.
static vpMatrix skew(const vpColVector &v)
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
void extract(vpRotationMatrix &R) const
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static void makeDirectory(const std::string &dirname)
static double rad(double deg)
Definition vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Implementation of a pose vector and operations on poses.
@ CAMERA_FRAME
Definition vpRobot.h:81
Implementation of a rotation matrix and operations on such kind of matrices.
vpColVector t() const
Class that defines the simplest robot: a free flying camera.
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.