Visual Servoing Platform version 3.5.0
servoSimu3D_cMcd_CamVelocityWithoutVpServo.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 * Authors:
35 * Eric Marchand
36 * Fabien Spindler
37 *
38 *****************************************************************************/
82#include <stdio.h>
83#include <stdlib.h>
84
85#include <visp3/core/vpHomogeneousMatrix.h>
86#include <visp3/core/vpIoTools.h>
87#include <visp3/core/vpMath.h>
88#include <visp3/io/vpParseArgv.h>
89#include <visp3/robot/vpSimulatorCamera.h>
90#include <visp3/visual_features/vpFeatureThetaU.h>
91#include <visp3/visual_features/vpFeatureTranslation.h>
92#include <visp3/vs/vpServo.h>
93
94// List of allowed command line options
95#define GETOPTARGS "h"
96
97void usage(const char *name, const char *badparam);
98bool getOptions(int argc, const char **argv);
99
108void usage(const char *name, const char *badparam)
109{
110 fprintf(stdout, "\n\
111Simulation of a 3D visual servoing:\n\
112- eye-in-hand control law,\n\
113- velocity computed in the camera frame,\n\
114- without display.\n\
115\n\
116SYNOPSIS\n\
117 %s [-h]\n", 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], NULL);
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], NULL);
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 exit(-1);
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 } catch (...) {
195 std::cerr << std::endl << "ERROR:" << std::endl;
196 std::cerr << " Cannot create " << logdirname << std::endl;
197 exit(-1);
198 }
199 }
200 std::string logfilename;
201 logfilename = logdirname + "/log.dat";
202
203 // Open the log file name
204 std::ofstream flog(logfilename.c_str());
205
206 vpSimulatorCamera robot;
207
208 std::cout << std::endl;
209 std::cout << "-------------------------------------------------------" << std::endl;
210 std::cout << " Test program for vpServo " << std::endl;
211 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
212 std::cout << " Simulation " << std::endl;
213 std::cout << " task : 3D visual servoing " << std::endl;
214 std::cout << "-------------------------------------------------------" << std::endl;
215 std::cout << std::endl;
216
217 // Sets the initial camera location
218 vpPoseVector c_r_o( // Translation tx,ty,tz
219 0.1, 0.2, 2,
220 // ThetaU rotation
221 vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
222
223 // From the camera pose build the corresponding homogeneous matrix
224 vpHomogeneousMatrix cMo(c_r_o);
225
226 // Set the robot initial position
227 vpHomogeneousMatrix wMc, wMo;
228 robot.getPosition(wMc);
229 wMo = wMc * cMo; // Compute the position of the object in the world frame
230
231 // Sets the desired camera location
232 vpPoseVector cd_r_o( // Translation tx,ty,tz
233 0, 0, 1,
234 // ThetaU rotation
236
237 // From the camera desired pose build the corresponding homogeneous matrix
238 vpHomogeneousMatrix cdMo(cd_r_o);
239
240 vpHomogeneousMatrix cMcd; // Transformation between current and desired camera frame
241 vpRotationMatrix cRcd; // Rotation between current and desired camera frame
242
243 // Set the constant gain of the servo
244 double lambda = 1;
245
246 unsigned int iter = 0;
247 // Start the visual servoing loop. We stop the servo after 200 iterations
248 while (iter++ < 200) {
249 std::cout << "------------------------------------" << iter << std::endl;
250
251 // get the robot position
252 robot.getPosition(wMc);
253 // Compute the position of the object frame in the camera frame
254 cMo = wMc.inverse() * wMo;
255
256 // new displacement to achieve
257 cMcd = cMo * cdMo.inverse();
258
259 // Extract the translation vector ctc* which is the current
260 // translational visual feature.
262 cMcd.extract(ctcd);
263 // Compute the current theta U visual feature
264 vpThetaUVector tu_cRcd(cMcd);
265
266 // Create the identity matrix
267 vpMatrix I(3, 3);
268 I.eye();
269
270 // Compute the camera translational velocity
271 vpColVector v(3);
272 v = lambda * (I - vpColVector::skew(tu_cRcd)) * ctcd;
273 // Compute the camera rotational velocity
274 vpColVector w(3);
275 w = lambda * tu_cRcd;
276
277 // Update the complete camera velocity vector
278 vpColVector velocity(6);
279 for (unsigned int i = 0; i < 3; i++) {
280 velocity[i] = v[i]; // Translational velocity
281 velocity[i + 3] = w[i]; // Rotational velocity
282 }
283
284 // Send the camera velocity to the controller
285 robot.setVelocity(vpRobot::CAMERA_FRAME, velocity);
286
287 // Retrieve the error (s-s*)
288 std::cout << "|| s - s* || = " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
289
290 // Save log
291 flog << velocity.t() << " " << ctcd.t() << " " << tu_cRcd.t() << std::endl;
292 }
293
294 // Close the log file
295 flog.close();
296 return EXIT_SUCCESS;
297 } catch (const vpException &e) {
298 std::cout << "Catch a ViSP exception: " << e << std::endl;
299 return EXIT_FAILURE;
300 }
301}
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
static vpMatrix skew(const vpColVector &v)
vpRowVector t() const
error that can be emited by ViSP classes.
Definition: vpException.h:72
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)
Definition: vpIoTools.cpp:420
static std::string getUserName()
Definition: vpIoTools.cpp:316
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
static double rad(double deg)
Definition: vpMath.h:110
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:152
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
@ CAMERA_FRAME
Definition: vpRobot.h:82
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.
vpRowVector t() const