Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoSimuPoint3DCamVelocity.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 on a 3D point.
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/vpFeaturePoint3D.h>
54#include <visp3/vs/vpServo.h>
55
56// List of allowed command line options
57#define GETOPTARGS "h"
58
59#ifdef ENABLE_VISP_NAMESPACE
60using namespace VISP_NAMESPACE_NAME;
61#endif
62
63void usage(const char *name, const char *badparam);
64bool getOptions(int argc, const char **argv);
65
74void usage(const char *name, const char *badparam)
75{
76 fprintf(stdout, "\n\
77Simulation of a 3D visual servoing:\n\
78- servo a 3D point,\n\
79- eye-in-hand control law,\n\
80- velocity computed in the camera frame,\n\
81- without display.\n\
82 \n\
83SYNOPSIS\n\
84 %s [-h]\n",
85 name);
86
87 fprintf(stdout, "\n\
88OPTIONS: Default\n\
89 \n\
90 -h\n\
91 Print the help.\n");
92
93 if (badparam)
94 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
95}
96
106bool getOptions(int argc, const char **argv)
107{
108 const char *optarg_;
109 int c;
110 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
111
112 switch (c) {
113 case 'h':
114 usage(argv[0], nullptr);
115 return false;
116
117 default:
118 usage(argv[0], optarg_);
119 return false;
120 }
121 }
122
123 if ((c == 1) || (c == -1)) {
124 // standalone param or error
125 usage(argv[0], nullptr);
126 std::cerr << "ERROR: " << std::endl;
127 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
128 return false;
129 }
130
131 return true;
132}
133
134int main(int argc, const char **argv)
135{
136#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
137 try {
138 // Read the command line options
139 if (getOptions(argc, argv) == false) {
140 return EXIT_FAILURE;
141 }
142
144 vpSimulatorCamera robot;
145
146 std::cout << std::endl;
147 std::cout << "-------------------------------------------------------" << std::endl;
148 std::cout << " Test program for vpServo " << std::endl;
149 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
150 std::cout << " Simulation " << std::endl;
151 std::cout << " task : servo a 3D point " << std::endl;
152 std::cout << "-------------------------------------------------------" << std::endl;
153 std::cout << std::endl;
154
155 // sets the initial camera location
157 cMo[0][3] = 0.1;
158 cMo[1][3] = 0.2;
159 cMo[2][3] = 2;
160 // Compute the position of the object in the world frame
161 vpHomogeneousMatrix wMc, wMo;
162 robot.getPosition(wMc);
163 wMo = wMc * cMo;
164
165 // sets the point coordinates in the world frame
166 vpPoint point(0, 0, 0);
167
168 // computes the point coordinates in the camera frame
169 point.track(cMo);
170
171 std::cout << "Point coordinates in the camera frame: " << point.cP.t();
172
174 p.buildFrom(point);
175
176 // sets the desired position of the point
178 pd.set_XYZ(0, 0, 1);
179
180 // define the task
181 // - we want an eye-in-hand control law
182 // - robot is controlled in the camera frame
184
185 // we want to see a point on a point
186 std::cout << std::endl;
187 task.addFeature(p, pd);
188
189 // set the gain") ;
190 task.setLambda(1);
191
192 // Display task information
193 task.print();
194
195 unsigned int iter = 0;
196 // loop
197 while (iter++ < 200) {
198 std::cout << "---------------------------------------------" << iter << std::endl;
200
201 // get the robot position
202 robot.getPosition(wMc);
203 // Compute the position of the object frame in the camera frame
204 cMo = wMc.inverse() * wMo;
205
206 // new point position
207 point.track(cMo);
208 p.buildFrom(point);
209 // std::cout << p.cP.t() ;
210 // std::cout << (p.get_s()).t() ;
211
212 // compute the control law
213 v = task.computeControlLaw();
214 // send the camera velocity to the controller
215 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
216
217 std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
218 }
219
220 // Display task information
221 task.print();
222 return EXIT_SUCCESS;
223 }
224 catch (const vpException &e) {
225 std::cout << "Catch a ViSP exception: " << e << std::endl;
226 return EXIT_FAILURE;
227 }
228#else
229 (void)argc;
230 (void)argv;
231 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
232 return EXIT_SUCCESS;
233#endif
234}
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:60
Class that defines the 3D point visual feature.
void set_XYZ(double X, double Y, double Z)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
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
@ CAMERA_FRAME
Definition vpRobot.h:81
@ EYEINHAND_CAMERA
Definition vpServo.h:176
Class that defines the simplest robot: a free flying camera.