Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimuPoint2DhalfCamVelocity1.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 2 1/2 D visual servoing.
32 */
33
43
44#include <stdio.h>
45#include <stdlib.h>
46
47#include <visp3/core/vpConfig.h>
48#include <visp3/core/vpHomogeneousMatrix.h>
49#include <visp3/core/vpMath.h>
50#include <visp3/core/vpPoint.h>
51#include <visp3/io/vpParseArgv.h>
52#include <visp3/robot/vpSimulatorCamera.h>
53#include <visp3/visual_features/vpFeatureBuilder.h>
54#include <visp3/visual_features/vpFeaturePoint.h>
55#include <visp3/visual_features/vpFeatureThetaU.h>
56#include <visp3/visual_features/vpGenericFeature.h>
57#include <visp3/vs/vpServo.h>
58
59// List of allowed command line options
60#define GETOPTARGS "h"
61
62#ifdef ENABLE_VISP_NAMESPACE
63using namespace VISP_NAMESPACE_NAME;
64#endif
65
66void usage(const char *name, const char *badparam);
67bool getOptions(int argc, const char **argv);
68
77void usage(const char *name, const char *badparam)
78{
79 fprintf(stdout, "\n\
80Simulation of a 2 1/2 D visual servoing (x,y,Z,theta U):\n\
81- eye-in-hand control law,\n\
82- velocity computed in the camera frame,\n\
83- without display.\n\
84 \n\
85SYNOPSIS\n\
86 %s [-h]\n",
87 name);
88
89 fprintf(stdout, "\n\
90OPTIONS: Default\n\
91 \n\
92 -h\n\
93 Print the help.\n");
94
95 if (badparam) {
96 fprintf(stderr, "ERROR: \n");
97 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
98 }
99}
100
111bool getOptions(int argc, const char **argv)
112{
113 const char *optarg_;
114 int c;
115 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116
117 switch (c) {
118 case 'h':
119 usage(argv[0], nullptr);
120 return false;
121
122 default:
123 usage(argv[0], optarg_);
124 return false;
125 }
126 }
127
128 if ((c == 1) || (c == -1)) {
129 // standalone param or error
130 usage(argv[0], nullptr);
131 std::cerr << "ERROR: " << std::endl;
132 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
133 return false;
134 }
135
136 return true;
137}
138
139int main(int argc, const char **argv)
140{
141#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
142 try {
143 // Read the command line options
144 if (getOptions(argc, argv) == false) {
145 return EXIT_FAILURE;
146 }
147
149 vpSimulatorCamera robot;
150
151 std::cout << std::endl;
152 std::cout << "-------------------------------------------------------" << std::endl;
153 std::cout << " Test program for vpServo " << std::endl;
154 std::cout << " task : 2 1/2 D visual servoing " << std::endl;
155 std::cout << "-------------------------------------------------------" << std::endl;
156 std::cout << std::endl;
157
158 // sets the initial camera location
159 vpPoseVector c_r_o(0.1, 0.2, 2, vpMath::rad(20), vpMath::rad(10), vpMath::rad(50));
160
162 // Compute the position of the object in the world frame
163 vpHomogeneousMatrix wMc, wMo;
164 robot.getPosition(wMc);
165 wMo = wMc * cMo;
166
167 // sets the desired camera location
168 vpPoseVector cd_r_o(0, 0, 1, vpMath::rad(0), vpMath::rad(0), vpMath::rad(0));
169 vpHomogeneousMatrix cdMo(cd_r_o);
170
171 // sets the point coordinates in the world frame
172 vpPoint point(0, 0, 0);
173 // computes the point coordinates in the camera frame and its 2D
174 // coordinates
175 point.track(cMo);
176
177 vpPoint pointd(0, 0, 0);
178 pointd.track(cdMo);
179 //------------------------------------------------------------------
180 // 1st feature (x,y)
181 // want to it at (0,0)
183 vpFeatureBuilder::create(p, point);
184
186 vpFeatureBuilder::create(pd, pointd);
187
188 //------------------------------------------------------------------
189 // 2nd feature (Z)
190 // not necessary to project twice (reuse p)
192 vpFeatureBuilder::create(Z, point); // retrieve x,y and Z of the vpPoint structure
193
194 // want to see it one meter away (here again use pd)
196 vpFeatureBuilder::create(Zd, pointd); // retrieve x,y and Z of the vpPoint structure
197
198 //------------------------------------------------------------------
199 // 3rd feature ThetaU
200 // compute the rotation that the camera has to achieve
202 cdMc = cdMo * cMo.inverse();
203
205 tu.buildFrom(cdMc);
206
207 // sets the desired rotation (always zero !)
208 // since s is the rotation that the camera has to achieve
209
210 //------------------------------------------------------------------
211 // define the task
212 // - we want an eye-in-hand control law
213 // - robot is controlled in the camera frame
215
216 task.addFeature(p, pd);
217 task.addFeature(Z, Zd, vpFeaturePoint3D::selectZ());
218 task.addFeature(tu);
219
220 // set the gain
221 task.setLambda(1);
222
223 // Display task information
224 task.print();
225
226 unsigned int iter = 0;
227 // loop
228 while (iter++ < 200) {
229 std::cout << "---------------------------------------------" << iter << std::endl;
231
232 // get the robot position
233 robot.getPosition(wMc);
234 // Compute the position of the object frame in the camera frame
235 cMo = wMc.inverse() * wMo;
236
237 // update the feature
238 point.track(cMo);
239 vpFeatureBuilder::create(p, point);
240 vpFeatureBuilder::create(Z, point);
241
242 cdMc = cdMo * cMo.inverse();
243 tu.buildFrom(cdMc);
244
245 // compute the control law
246 v = task.computeControlLaw();
247 // send the camera velocity to the controller ") ;
248 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
249
250 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
251 }
252
253 // Display task information
254 task.print();
255 std::cout << "Final camera location:\n " << cMo << std::endl;
256 return EXIT_SUCCESS;
257 }
258 catch (const vpException &e) {
259 std::cout << "Catch a ViSP exception: " << e << std::endl;
260 return EXIT_SUCCESS;
261 }
262#else
263 (void)argc;
264 (void)argv;
265 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
266 return EXIT_SUCCESS;
267#endif
268}
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 the 3D point visual feature.
static unsigned int selectZ()
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotati...
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition vpMath.h:129
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
Implementation of a pose vector and operations on poses.
@ CAMERA_FRAME
Definition vpRobot.h:81
@ EYEINHAND_CAMERA
Definition vpServo.h:176
Class that defines the simplest robot: a free flying camera.