Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
servoSimuSphere2DCamVelocity.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 on a sphere.
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/vpSphere.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/vpFeatureEllipse.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
60
using namespace
VISP_NAMESPACE_NAME
;
61
#endif
62
63
void
usage(
const
char
*name,
const
char
*badparam);
64
bool
getOptions(
int
argc,
const
char
**argv);
65
74
void
usage(
const
char
*name,
const
char
*badparam)
75
{
76
fprintf(stdout,
"\n\
77
Simulation of a 2D visual servoing on a sphere:\n\
78
- eye-in-hand control law,\n\
79
- velocity computed in the camera frame,\n\
80
- without display.\n\
81
\n\
82
SYNOPSIS\n\
83
%s [-h]\n"
,
84
name);
85
86
fprintf(stdout,
"\n\
87
OPTIONS: Default\n\
88
\n\
89
-h\n\
90
Print the help.\n"
);
91
92
if
(badparam)
93
fprintf(stdout,
"\nERROR: Bad parameter [%s]\n"
, badparam);
94
}
95
106
bool
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
134
int
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
143
vpServo
task
;
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 <<
" Simulation "
<< std::endl;
150
std::cout <<
" task : servo a sphere "
<< std::endl;
151
std::cout <<
"-------------------------------------------------------"
<< std::endl;
152
std::cout << std::endl;
153
154
// sets the initial camera location
155
vpHomogeneousMatrix
cMo
;
156
cMo
[0][3] = 0.1;
157
cMo
[1][3] = 0.2;
158
cMo
[2][3] = 2;
159
// Compute the position of the object in the world frame
160
vpHomogeneousMatrix
wMc, wMo;
161
robot.getPosition(wMc);
162
wMo = wMc *
cMo
;
163
164
vpHomogeneousMatrix
cMod;
165
cMod[0][3] = 0;
166
cMod[1][3] = 0;
167
cMod[2][3] = 1;
168
169
// sets the sphere coordinates in the world frame
170
vpSphere
sphere;
171
sphere.
setWorldCoordinates
(0, 0, 0, 0.1);
172
173
// sets the desired position of the visual feature
174
vpFeatureEllipse
pd;
175
sphere.
track
(cMod);
176
vpFeatureBuilder::create
(pd, sphere);
177
178
// computes the sphere coordinates in the camera frame and its 2D
179
// coordinates sets the current position of the visual feature
180
vpFeatureEllipse
p
;
181
sphere.
track
(cMo);
182
vpFeatureBuilder::create
(p, sphere);
183
184
// define the task
185
// - we want an eye-in-hand control law
186
// - robot is controlled in the camera frame
187
task
.setServo(
vpServo::EYEINHAND_CAMERA
);
188
189
// we want to see a sphere on a sphere
190
task
.addFeature(p, pd);
191
192
// set the gain
193
task
.setLambda(1);
194
195
// Display task information
196
task
.print();
197
198
unsigned
int
iter
= 0;
199
// loop
200
while
(iter++ < 200) {
201
std::cout <<
"---------------------------------------------"
<<
iter
<< std::endl;
202
vpColVector
v
;
203
204
// get the robot position
205
robot.getPosition(wMc);
206
// Compute the position of the object frame in the camera frame
207
cMo
= wMc.
inverse
() * wMo;
208
209
// new sphere position: retrieve x,y and Z of the vpSphere structure
210
sphere.
track
(cMo);
211
vpFeatureBuilder::create
(p, sphere);
212
213
// compute the control law
214
v
=
task
.computeControlLaw();
215
216
std::cout <<
"Task rank: "
<<
task
.getTaskRank() << std::endl;
217
// send the camera velocity to the controller
218
robot.setVelocity(
vpRobot::CAMERA_FRAME
, v);
219
220
std::cout <<
"|| s - s* || = "
<< (
task
.getError()).sumSquare() << std::endl;
221
}
222
223
// Display task information
224
task
.print();
225
return
EXIT_SUCCESS;
226
}
227
catch
(
const
vpException
&e) {
228
std::cout <<
"Catch a ViSP exception: "
<<
e
<< std::endl;
229
return
EXIT_FAILURE;
230
}
231
#else
232
(void)argc;
233
(void)argv;
234
std::cout <<
"Cannot run this example: install Lapack, Eigen3 or OpenCV"
<< std::endl;
235
return
EXIT_SUCCESS;
236
#endif
237
}
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
vpFeatureEllipse
Class that defines 2D ellipse visual feature.
Definition
vpFeatureEllipse.h:56
vpForwardProjection::track
void track(const vpHomogeneousMatrix &cMo)
Definition
vpForwardProjection.cpp:101
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
vpParseArgv::parse
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition
vpParseArgv.cpp:70
vpRobot::CAMERA_FRAME
@ CAMERA_FRAME
Definition
vpRobot.h:81
vpServo
Definition
vpServo.h:159
vpServo::EYEINHAND_CAMERA
@ EYEINHAND_CAMERA
Definition
vpServo.h:176
vpSimulatorCamera
Class that defines the simplest robot: a free flying camera.
Definition
vpSimulatorCamera.h:113
vpSphere
Class that defines a 3D sphere in the object frame and allows forward projection of a 3D sphere in th...
Definition
vpSphere.h:80
vpSphere::setWorldCoordinates
void setWorldCoordinates(const vpColVector &oP) VP_OVERRIDE
Definition
vpSphere.cpp:60
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
realsense-mbt.cMo
cMo
Definition
realsense-mbt.py:212
example
robot-simulator
camera
servoSimuSphere2DCamVelocity.cpp
Generated by
1.17.0