Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimu3D_cMcd_CamVelocity.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 */
51
52#include <stdio.h>
53#include <stdlib.h>
54
55#include <visp3/core/vpConfig.h>
56#include <visp3/core/vpHomogeneousMatrix.h>
57#include <visp3/core/vpIoTools.h>
58#include <visp3/core/vpMath.h>
59#include <visp3/io/vpParseArgv.h>
60#include <visp3/robot/vpSimulatorCamera.h>
61#include <visp3/visual_features/vpFeatureThetaU.h>
62#include <visp3/visual_features/vpFeatureTranslation.h>
63#include <visp3/vs/vpServo.h>
64
65// List of allowed command line options
66#define GETOPTARGS "h"
67
68#ifdef ENABLE_VISP_NAMESPACE
69using namespace VISP_NAMESPACE_NAME;
70#endif
71
72void usage(const char *name, const char *badparam);
73bool getOptions(int argc, const char **argv);
74
83void usage(const char *name, const char *badparam)
84{
85 fprintf(stdout, "\n\
86Simulation of a 3D visual servoing:\n\
87- eye-in-hand control law,\n\
88- velocity computed in the camera frame,\n\
89- without display.\n\
90 \n\
91SYNOPSIS\n\
92 %s [-h]\n",
93 name);
94
95 fprintf(stdout, "\n\
96OPTIONS: 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
114bool 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], nullptr);
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], nullptr);
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
142int 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 return EXIT_FAILURE;
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 }
172 catch (...) {
173 std::cerr << std::endl << "ERROR:" << std::endl;
174 std::cerr << " Cannot create " << logdirname << std::endl;
175 return EXIT_FAILURE;
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
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
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
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
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
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;
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
281 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
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 }
298 catch (const vpException &e) {
299 std::cout << "Catch a ViSP exception: " << e << std::endl;
300 return EXIT_FAILURE;
301 }
302#else
303 (void)argc;
304 (void)argv;
305 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
306 return EXIT_SUCCESS;
307#endif
308}
Implementation of column vector and the associated operations.
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 rotati...
Class that defines the translation visual feature .
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() 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
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
@ EYEINHAND_CAMERA
Definition vpServo.h:176
@ CURRENT
Definition vpServo.h:217
Class that defines the simplest robot: a free flying camera.