Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimuFourPoints2DCamVelocity.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 2D visual servoing using 4 points as visual feature.
32 */
33
47
48#include <stdio.h>
49#include <stdlib.h>
50
51#include <visp3/core/vpConfig.h>
52#include <visp3/core/vpHomogeneousMatrix.h>
53#include <visp3/core/vpMath.h>
54#include <visp3/io/vpParseArgv.h>
55#include <visp3/robot/vpSimulatorCamera.h>
56#include <visp3/visual_features/vpFeatureBuilder.h>
57#include <visp3/visual_features/vpFeaturePoint.h>
58#include <visp3/vs/vpServo.h>
59
60// List of allowed command line options
61#define GETOPTARGS "h"
62
63#ifdef ENABLE_VISP_NAMESPACE
64using namespace VISP_NAMESPACE_NAME;
65#endif
66
67void usage(const char *name, const char *badparam);
68bool getOptions(int argc, const char **argv);
69
78void usage(const char *name, const char *badparam)
79{
80 fprintf(stdout, "\n\
81Simulation of a 2D visual servoing:\n\
82- servo on 4 points,\n\
83- eye-in-hand control law,\n\
84- articular velocity are computed,\n\
85- without display.\n\
86 \n\
87SYNOPSIS\n\
88 %s [-h]\n",
89 name);
90
91 fprintf(stdout, "\n\
92OPTIONS: Default\n\
93 \n\
94 -h\n\
95 Print the help.\n");
96
97 if (badparam) {
98 fprintf(stderr, "ERROR: \n");
99 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
100 }
101}
102
113bool getOptions(int argc, const char **argv)
114{
115 const char *optarg_;
116 int c;
117 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118
119 switch (c) {
120 case 'h':
121 usage(argv[0], nullptr);
122 return false;
123
124 default:
125 usage(argv[0], optarg_);
126 return false;
127 }
128 }
129
130 if ((c == 1) || (c == -1)) {
131 // standalone param or error
132 usage(argv[0], nullptr);
133 std::cerr << "ERROR: " << std::endl;
134 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
135 return false;
136 }
137
138 return true;
139}
140
141int main(int argc, const char **argv)
142{
143#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
144 try {
145 // Read the command line options
146 if (getOptions(argc, argv) == false) {
147 return EXIT_FAILURE;
148 }
149
151 vpSimulatorCamera robot;
152
153 std::cout << std::endl;
154 std::cout << "-------------------------------------------------------" << std::endl;
155 std::cout << " Test program for vpServo " << std::endl;
156 std::cout << " Eye-in-hand task control, articular velocity are computed" << std::endl;
157 std::cout << " Simulation " << std::endl;
158 std::cout << " task : servo 4 points " << std::endl;
159 std::cout << "-------------------------------------------------------" << std::endl;
160 std::cout << std::endl;
161
162 // sets the initial camera location with respect to the object
164 cMo[0][3] = 0.1;
165 cMo[1][3] = 0.2;
166 cMo[2][3] = 2;
167
168 // Compute the position of the object in the world frame
169 vpHomogeneousMatrix wMc, wMo;
170 robot.getPosition(wMc);
171 wMo = wMc * cMo;
172
173 // sets the point coordinates in the object frame
174 vpPoint point[4];
175 point[0].setWorldCoordinates(-1, -1, 0);
176 point[1].setWorldCoordinates(1, -1, 0);
177 point[2].setWorldCoordinates(1, 1, 0);
178 point[3].setWorldCoordinates(-1, 1, 0);
179
180 // computes the point coordinates in the camera frame and its 2D
181 // coordinates
182 for (unsigned int i = 0; i < 4; i++)
183 point[i].track(cMo);
184
185 // sets the desired position of the point
186 vpFeaturePoint p[4];
187 for (unsigned int i = 0; i < 4; i++)
188 vpFeatureBuilder::create(p[i], point[i]); // retrieve x,y and Z of the vpPoint structure
189
190 // sets the desired position of the point
191 vpFeaturePoint pd[4];
192
193 pd[0].buildFrom(-0.1, -0.1, 1);
194 pd[1].buildFrom(0.1, -0.1, 1);
195 pd[2].buildFrom(0.1, 0.1, 1);
196 pd[3].buildFrom(-0.1, 0.1, 1);
197
198 // define the task
199 // - we want an eye-in-hand control law
200 // - articular velocity are computed
202 task.setInteractionMatrixType(vpServo::MEAN);
203
204 // Set the position of the end-effector frame in the camera frame as identity
206 vpVelocityTwistMatrix cVe(cMe);
207 task.set_cVe(cVe);
208
209 // Set the Jacobian (expressed in the end-effector frame)
210 vpMatrix eJe;
211 robot.get_eJe(eJe);
212 task.set_eJe(eJe);
213
214 // we want to see a point on a point
215 for (unsigned int i = 0; i < 4; i++)
216 task.addFeature(p[i], pd[i]);
217
218 // set the gain
219 task.setLambda(1);
220
221 // Display task information
222 task.print();
223
224 unsigned int iter = 0;
225 // loop
226 while (iter++ < 1500) {
227 std::cout << "---------------------------------------------" << iter << std::endl;
229
230 // Set the Jacobian (expressed in the end-effector frame)
231 // since q is modified eJe is modified
232 robot.get_eJe(eJe);
233 task.set_eJe(eJe);
234
235 // get the robot position
236 robot.getPosition(wMc);
237 // Compute the position of the object frame in the camera frame
238 cMo = wMc.inverse() * wMo;
239
240 // update new point position and corresponding features
241 for (unsigned int i = 0; i < 4; i++) {
242 point[i].track(cMo);
243 // retrieve x,y and Z of the vpPoint structure
244 vpFeatureBuilder::create(p[i], point[i]);
245 }
246 // since vpServo::MEAN interaction matrix is used, we need also to
247 // update the desired features at each iteration
248 pd[0].buildFrom(-0.1, -0.1, 1);
249 pd[1].buildFrom(0.1, -0.1, 1);
250 pd[2].buildFrom(0.1, 0.1, 1);
251 pd[3].buildFrom(-0.1, 0.1, 1);
252
253 // compute the control law ") ;
254 v = task.computeControlLaw();
255
256 // send the camera velocity to the controller ") ;
257 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
258
259 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
260 }
261
262 // Display task information
263 task.print();
264 return EXIT_SUCCESS;
265 }
266 catch (const vpException &e) {
267 std::cout << "Catch a ViSP exception: " << e << std::endl;
268 return EXIT_FAILURE;
269 }
270#else
271 (void)argc;
272 (void)argv;
273 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
274 return EXIT_SUCCESS;
275#endif
276}
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
@ CAMERA_FRAME
Definition vpRobot.h:81
@ EYEINHAND_L_cVe_eJe
Definition vpServo.h:183
Class that defines the simplest robot: a free flying camera.