Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
servoSimuPoint2DhalfCamVelocity3.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 using theta U visual features.
32
*/
33
42
43
#include <stdio.h>
44
#include <stdlib.h>
45
46
#include <visp3/core/vpConfig.h>
47
#include <visp3/core/vpHomogeneousMatrix.h>
48
#include <visp3/core/vpMath.h>
49
#include <visp3/core/vpPoint.h>
50
#include <visp3/io/vpParseArgv.h>
51
#include <visp3/robot/vpSimulatorCamera.h>
52
#include <visp3/visual_features/vpFeatureBuilder.h>
53
#include <visp3/visual_features/vpFeaturePoint.h>
54
#include <visp3/visual_features/vpFeatureThetaU.h>
55
#include <visp3/visual_features/vpGenericFeature.h>
56
#include <visp3/vs/vpServo.h>
57
58
// List of allowed command line options
59
#define GETOPTARGS "h"
60
61
#ifdef ENABLE_VISP_NAMESPACE
62
using namespace
VISP_NAMESPACE_NAME
;
63
#endif
64
65
void
usage(
const
char
*name,
const
char
*badparam);
66
bool
getOptions(
int
argc,
const
char
**argv);
67
76
void
usage(
const
char
*name,
const
char
*badparam)
77
{
78
fprintf(stdout,
"\n\
79
Simulation of a 2 1/2 D visual servoing (x,y,logZ, theta U):\n\
80
- eye-in-hand control law,\n\
81
- velocity computed in the camera frame,\n\
82
- without display.\n\
83
\n\
84
SYNOPSIS\n\
85
%s [-h]\n"
,
86
name);
87
88
fprintf(stdout,
"\n\
89
OPTIONS: Default\n\
90
\n\
91
-h\n\
92
Print the help.\n"
);
93
94
if
(badparam)
95
fprintf(stdout,
"\nERROR: Bad parameter [%s]\n"
, badparam);
96
}
97
107
bool
getOptions(
int
argc,
const
char
**argv)
108
{
109
const
char
*optarg_;
110
int
c;
111
while
((c =
vpParseArgv::parse
(argc, argv, GETOPTARGS, &optarg_)) > 1) {
112
113
switch
(c) {
114
case
'h'
:
115
usage(argv[0],
nullptr
);
116
return
false
;
117
118
default
:
119
usage(argv[0], optarg_);
120
return
false
;
121
}
122
}
123
124
if
((c == 1) || (c == -1)) {
125
// standalone param or error
126
usage(argv[0],
nullptr
);
127
std::cerr <<
"ERROR: "
<< std::endl;
128
std::cerr <<
" Bad argument "
<< optarg_ << std::endl << std::endl;
129
return
false
;
130
}
131
132
return
true
;
133
}
134
135
int
main(
int
argc,
const
char
**argv)
136
{
137
#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
138
try
{
139
// Read the command line options
140
if
(getOptions(argc, argv) ==
false
) {
141
return
EXIT_FAILURE;
142
}
143
144
std::cout << std::endl;
145
std::cout <<
"-------------------------------------------------------"
<< std::endl;
146
std::cout <<
" simulation of a 2 1/2 D visual servoing "
<< std::endl;
147
std::cout <<
"-------------------------------------------------------"
<< std::endl;
148
std::cout << std::endl;
149
150
// In this example we will simulate a visual servoing task.
151
// In simulation, we have to define the scene frane Ro and the
152
// camera frame Rc.
153
// The camera location is given by an homogenous matrix cMo that
154
// describes the position of the scene or object frame in the camera frame.
155
156
vpServo
task
;
157
158
// sets the initial camera location
159
// we give the camera location as a size 6 vector (3 translations in meter
160
// and 3 rotation (theta U representation)
161
vpPoseVector
c_r_o(0.1, 0.2, 2,
vpMath::rad
(20),
vpMath::rad
(10),
vpMath::rad
(50));
162
163
// this pose vector is then transformed in a 4x4 homogeneous matrix
164
vpHomogeneousMatrix
cMo
(c_r_o);
165
166
// We define a robot
167
// The vpSimulatorCamera implements a simple moving that is juste defined
168
// by its location cMo
169
vpSimulatorCamera
robot;
170
171
// Compute the position of the object in the world frame
172
vpHomogeneousMatrix
wMc, wMo;
173
robot.getPosition(wMc);
174
wMo = wMc *
cMo
;
175
176
// Now that the current camera position has been defined,
177
// let us defined the defined camera location.
178
// It is defined by cdMo
179
// sets the desired camera location " ) ;
180
vpPoseVector
cd_r_o(0, 0, 1,
vpMath::rad
(0),
vpMath::rad
(0),
vpMath::rad
(0));
181
vpHomogeneousMatrix
cdMo(cd_r_o);
182
183
//----------------------------------------------------------------------
184
// A 2 1/2 D visual servoing can be defined by
185
// - the position of a point x,y
186
// - the difference between this point depth and a desire depth
187
// modeled by log Z/Zd to be regulated to 0
188
// - the rotation that the camera has to realized cdMc
189
190
// Let us now defined the current value of these features
191
192
// since we simulate we have to define a 3D point that will
193
// forward-projected to define the current position x,y of the
194
// reference point
195
196
//------------------------------------------------------------------
197
// First feature (x,y)
198
//------------------------------------------------------------------
199
// Let oP be this ... point,
200
// a vpPoint class has three main member
201
// .oP : 3D coordinates in scene frame
202
// .cP : 3D coordinates in camera frame
203
// .p : 2D
204
205
//------------------------------------------------------------------
206
// sets the point coordinates in the world frame
207
vpPoint
P(0, 0, 0);
208
// computes the P coordinates in the camera frame and its
209
// 2D coordinates cP and then p
210
// computes the point coordinates in the camera frame and its 2D
211
// coordinates
212
P.track(cMo);
213
214
// We also defined (again by forward projection) the desired position
215
// of this point according to the desired camera position
216
vpPoint
Pd(0, 0, 0);
217
Pd.track(cdMo);
218
219
// Nevertheless, a vpPoint is not a feature, this is just a "tracker"
220
// from which the feature are built
221
// a feature is juste defined by a vector s, a way to compute the
222
// interaction matrix and the error, and if required a (or a vector of)
223
// 3D information
224
225
// for a point (x,y) Visp implements the vpFeaturePoint class.
226
// we no defined a feature for x,y (and for (x*,y*))
227
vpFeaturePoint
p
, pd;
228
229
// and we initialized the vector s=(x,y) of p from the tracker P
230
// Z coordinates in p is also initialized, it will be used to compute
231
// the interaction matrix
232
vpFeatureBuilder::create
(p, P);
233
vpFeatureBuilder::create
(pd, Pd);
234
235
// This visual has to be regulated to zero
236
237
//------------------------------------------------------------------
238
// 2nd feature ThetaUz and 3rd feature t
239
// The thetaU feature is defined, tu represents the rotation that the
240
// camera has to realized. t the translation. the complete displacement is
241
// then defined by:
242
//------------------------------------------------------------------
243
vpHomogeneousMatrix
cdMc;
244
// compute the rotation that the camera has to achieve
245
cdMc = cdMo *
cMo
.inverse();
246
247
// from this displacement, we extract the rotation cdRc represented by
248
// the angle theta and the rotation axis u
249
vpFeatureThetaU
tuz(
vpFeatureThetaU::cdRc
);
250
tuz.buildFrom(cdMc);
251
// And the translations
252
vpFeatureTranslation
t
(
vpFeatureTranslation::cdMc
);
253
t
.buildFrom(cdMc);
254
255
// This visual has to be regulated to zero
256
257
// sets the desired rotation (always zero !)
258
// since s is the rotation that the camera has to achieve
259
260
//------------------------------------------------------------------
261
// Let us now the task itself
262
//------------------------------------------------------------------
263
264
// define the task
265
// - we want an eye-in-hand control law
266
// - robot is controlled in the camera frame
267
// we choose to control the robot in the camera frame
268
task
.setServo(
vpServo::EYEINHAND_CAMERA
);
269
// Interaction matrix is computed with the current value of s
270
task
.setInteractionMatrixType(
vpServo::CURRENT
);
271
272
// we build the task by "stacking" the visual feature
273
// previously defined
274
task
.addFeature(t);
275
task
.addFeature(p, pd);
276
task
.addFeature(tuz,
vpFeatureThetaU::TUz
);
// selection of TUz
277
278
// addFeature(X,Xd) means X should be regulated to Xd
279
// addFeature(X) means that X should be regulated to 0
280
// some features such as vpFeatureThetaU MUST be regulated to zero
281
// (otherwise, it will results in an error at exectution level)
282
283
// set the gain
284
task
.setLambda(1);
285
286
// Display task information " ) ;
287
task
.print();
288
//------------------------------------------------------------------
289
// An now the closed loop
290
291
unsigned
int
iter
= 0;
292
// loop
293
while
(iter++ < 200) {
294
std::cout <<
"---------------------------------------------"
<<
iter
<< std::endl;
295
vpColVector
v
;
296
297
// get the robot position
298
robot.getPosition(wMc);
299
// Compute the position of the object frame in the camera frame
300
cMo
= wMc.
inverse
() * wMo;
301
302
// update the feature
303
P.track(cMo);
304
vpFeatureBuilder::create
(p, P);
305
306
cdMc = cdMo *
cMo
.inverse();
307
tuz.buildFrom(cdMc);
308
t
.buildFrom(cdMc);
309
310
// compute the control law: v = -lambda L^+(s-sd)
311
v
=
task
.computeControlLaw();
312
313
// send the camera velocity to the controller
314
robot.setVelocity(
vpRobot::CAMERA_FRAME
, v);
315
316
std::cout <<
"|| s - s* || = "
<< (
task
.getError()).sumSquare() << std::endl;
317
}
318
319
// Display task information
320
task
.print();
321
// Final camera location
322
std::cout <<
"Final camera location: \n"
<<
cMo
<< std::endl;
323
return
EXIT_SUCCESS;
324
}
325
catch
(
const
vpException
&e) {
326
std::cout <<
"Catch a ViSP exception: "
<<
e
<< std::endl;
327
return
EXIT_SUCCESS;
328
}
329
#else
330
(void)argc;
331
(void)argv;
332
std::cout <<
"Cannot run this example: install Lapack, Eigen3 or OpenCV"
<< std::endl;
333
return
EXIT_SUCCESS;
334
#endif
335
}
vpColVector
Implementation of column vector and the associated operations.
Definition
vpColVector.h:191
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpFeatureBuilder::create
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Definition
vpFeatureBuilderPoint.cpp:90
vpFeaturePoint
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Definition
vpFeaturePoint.h:190
vpFeatureThetaU
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotati...
Definition
vpFeatureThetaU.h:227
vpFeatureThetaU::cdRc
@ cdRc
Definition
vpFeatureThetaU.h:240
vpFeatureThetaU::TUz
@ TUz
Definition
vpFeatureThetaU.h:235
vpFeatureTranslation
Class that defines the translation visual feature .
Definition
vpFeatureTranslation.h:282
vpFeatureTranslation::cdMc
@ cdMc
Definition
vpFeatureTranslation.h:293
vpHomogeneousMatrix
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition
vpHomogeneousMatrix.h:221
vpHomogeneousMatrix::inverse
vpHomogeneousMatrix inverse() const
Definition
vpHomogeneousMatrix.cpp:1016
vpMath::rad
static double rad(double deg)
Definition
vpMath.h:129
vpParseArgv::parse
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition
vpParseArgv.cpp:70
vpPoint
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition
vpPoint.h:79
vpPoseVector
Implementation of a pose vector and operations on poses.
Definition
vpPoseVector.h:203
vpRobot::CAMERA_FRAME
@ CAMERA_FRAME
Definition
vpRobot.h:81
vpServo
Definition
vpServo.h:159
vpServo::EYEINHAND_CAMERA
@ EYEINHAND_CAMERA
Definition
vpServo.h:176
vpServo::CURRENT
@ CURRENT
Definition
vpServo.h:217
vpSimulatorCamera
Class that defines the simplest robot: a free flying camera.
Definition
vpSimulatorCamera.h:113
VISP_NAMESPACE_NAME
Definition
vpEigenConversion.h:44
ibvs-four-points.p
p
Definition
ibvs-four-points.py:280
ibvs-four-points.iter
int iter
Definition
ibvs-four-points.py:236
ibvs-four-points.v
v
Definition
ibvs-four-points.py:271
ibvs-four-points.task
task
Definition
ibvs-four-points.py:210
ibvs-four-points.e
e
Definition
ibvs-four-points.py:272
pbvs-four-points.t
t
Definition
pbvs-four-points.py:220
realsense-mbt.cMo
cMo
Definition
realsense-mbt.py:212
example
robot-simulator
camera
servoSimuPoint2DhalfCamVelocity3.cpp
Generated by
1.17.0