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