Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimu3D_cdMc_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 */
52
53#include <stdio.h>
54#include <stdlib.h>
55
56#include <visp3/core/vpConfig.h>
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#ifdef ENABLE_VISP_NAMESPACE
70using namespace VISP_NAMESPACE_NAME;
71#endif
72
73void usage(const char *name, const char *badparam);
74bool getOptions(int argc, const char **argv);
75
84void usage(const char *name, const char *badparam)
85{
86 fprintf(stdout, "\n\
87Simulation 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\
92SYNOPSIS\n\
93 %s [-h]\n",
94 name);
95
96 fprintf(stdout, "\n\
97OPTIONS: Default\n\
98 \n\
99 -h\n\
100 Print the help.\n");
101
102 if (badparam)
103 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
104}
105
115bool getOptions(int argc, const char **argv)
116{
117 const char *optarg_;
118 int c;
119 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
120
121 switch (c) {
122 case 'h':
123 usage(argv[0], nullptr);
124 return false;
125
126 default:
127 usage(argv[0], optarg_);
128 return false;
129 }
130 }
131
132 if ((c == 1) || (c == -1)) {
133 // standalone param or error
134 usage(argv[0], nullptr);
135 std::cerr << "ERROR: " << std::endl;
136 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137 return false;
138 }
139
140 return true;
141}
142
143int main(int argc, const char **argv)
144{
145#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
146 try {
147 // Read the command line options
148 if (getOptions(argc, argv) == false) {
149 return EXIT_FAILURE;
150 }
151
152 // Log file creation in /tmp/$USERNAME/log.dat
153 // This file contains by line:
154 // - the 6 computed camera velocities (m/s, rad/s) to achieve the task
155 // - the 6 values of s - s*
156 std::string username;
157 // Get the user login name
158 vpIoTools::getUserName(username);
159
160 // Create a log filename to save velocities...
161 std::string logdirname;
162#if defined(_WIN32)
163 logdirname = "C:/temp/" + username;
164#else
165 logdirname = "/tmp/" + username;
166#endif
167 // Test if the output path exist. If no try to create it
168 if (vpIoTools::checkDirectory(logdirname) == false) {
169 try {
170 // Create the dirname
171 vpIoTools::makeDirectory(logdirname);
172 }
173 catch (...) {
174 std::cerr << std::endl << "ERROR:" << std::endl;
175 std::cerr << " Cannot create " << logdirname << std::endl;
176 return EXIT_FAILURE;
177 }
178 }
179 std::string logfilename;
180 logfilename = logdirname + "/log.dat";
181
182 // Open the log file name
183 std::ofstream flog(logfilename.c_str());
184
186 vpSimulatorCamera robot;
187
188 std::cout << std::endl;
189 std::cout << "-------------------------------------------------------" << std::endl;
190 std::cout << " Test program for vpServo " << std::endl;
191 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
192 std::cout << " Simulation " << std::endl;
193 std::cout << " task : 3D visual servoing " << std::endl;
194 std::cout << "-------------------------------------------------------" << std::endl;
195 std::cout << std::endl;
196
197 // Sets the initial camera location
198 vpPoseVector c_r_o( // Translation tx,ty,tz
199 0.1, 0.2, 2,
200 // ThetaU rotation
201 vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
202
203 // From the camera pose build the corresponding homogeneous matrix
205
206 // Set the robot initial position
207 vpHomogeneousMatrix wMc, wMo;
208 robot.getPosition(wMc);
209 wMo = wMc * cMo; // Compute the position of the object in the world frame
210
211 // Sets the desired camera location
212 vpPoseVector cd_r_o( // Translation tx,ty,tz
213 0, 0, 1,
214 // ThetaU rotation
216 // From the camera desired pose build the corresponding homogeneous matrix
217 vpHomogeneousMatrix cdMo(cd_r_o);
218
219 // Compute the homogeneous transformation from the desired camera position
220 // to the initial one
222 cdMc = cdMo * cMo.inverse();
223
224 // Build the current visual features s = (c*tc, thetaU_c*Rc)^T
226 vpFeatureThetaU tu(vpFeatureThetaU::cdRc); // current feature
227 t.buildFrom(cdMc);
228 tu.buildFrom(cdMc);
229
230 // Sets the desired rotation (always zero !) since s is the
231 // rotation that the camera has to achieve. Here s* = (0, 0)^T
233 vpFeatureThetaU tud(vpFeatureThetaU::cdRc); // desired feature
234
235 // Define the task
236 // - we want an eye-in-hand control law
237 // - the robot is controlled in the camera frame
239 // - we use here the interaction matrix computed with the
240 // current features
241 task.setInteractionMatrixType(vpServo::CURRENT);
242
243 // Add the current and desired visual features
244 task.addFeature(t, td); // 3D translation
245 task.addFeature(tu, tud); // 3D rotation
246
247 // - set the constant gain to 1.0
248 task.setLambda(1);
249
250 // Display task information
251 task.print();
252
253 unsigned int iter = 0;
254 // Start the visual servoing loop. We stop the servo after 200 iterations
255 while (iter++ < 200) {
256 std::cout << "-----------------------------------" << iter << std::endl;
258
259 // get the robot position
260 robot.getPosition(wMc);
261 // Compute the position of the object frame in the camera frame
262 cMo = wMc.inverse() * wMo;
263
264 // new displacement to achieve
265 cdMc = cdMo * cMo.inverse();
266
267 // Update the current visual features
268 t.buildFrom(cdMc);
269 tu.buildFrom(cdMc);
270
271 // Compute the control law
272 v = task.computeControlLaw();
273
274 // Display task information
275 if (iter == 1)
276 task.print();
277
278 // Send the camera velocity to the controller
279 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
280
281 // Retrieve the error
282 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
283
284 // Save log
285 flog << v.t() << " " << (task.getError()).t() << std::endl;
286 }
287 // Display task information
288 task.print();
289
290 // Close the log file
291 flog.close();
292 return EXIT_SUCCESS;
293 }
294 catch (const vpException &e) {
295 std::cout << "Catch a ViSP exception: " << e << std::endl;
296 return EXIT_FAILURE;
297 }
298#else
299 (void)argc;
300 (void)argv;
301 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
302 return EXIT_SUCCESS;
303#endif
304}
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.