Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
Loading...
Searching...
No Matches
vpRobotPololuPtu.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
* Common features for Pololu Maestro PanTiltUnit.
32
*/
33
34
#include <visp3/core/vpConfig.h>
35
36
#if defined(VISP_HAVE_POLOLU) && defined(VISP_HAVE_THREADS)
37
38
#include <visp3/core/vpDebug.h>
39
#include <visp3/robot/vpRobot.h>
40
#include <visp3/robot/vpRobotException.h>
41
#include <visp3/robot/vpRobotPololuPtu.h>
42
43
BEGIN_VISP_NAMESPACE
44
vpRobotPololuPtu::vpRobotPololuPtu
(
const
std::string &device,
int
baudrate,
bool
verbose)
45
: m_verbose(verbose)
46
{
47
nDof
= 2;
48
m_pan.connect(device, baudrate, 0);
49
m_pan.setPwmRange(4095, 7905);
50
m_pan.setAngularRange(
static_cast<
float
>
(
vpMath::rad
(-45)),
static_cast<
float
>
(
vpMath::rad
(45)));
51
m_pan.setVerbose(verbose);
52
53
m_tilt.connect(device, baudrate, 1);
54
m_tilt.setPwmRange(4095, 7905);
55
m_tilt.setAngularRange(
static_cast<
float
>
(
vpMath::rad
(-45)),
static_cast<
float
>
(
vpMath::rad
(45)));
56
m_tilt.setVerbose(verbose);
57
}
58
59
vpRobotPololuPtu::~vpRobotPololuPtu
()
60
{
61
m_pan.stopVelocityCmd();
62
m_tilt.stopVelocityCmd();
63
}
64
65
void
vpRobotPololuPtu::get_eJe
(
vpMatrix
&
eJe
)
66
{
67
vpColVector
q(
nDof
);
68
getPosition
(
vpRobot::JOINT_STATE
, q);
69
70
get_eJe
(q,
eJe
);
71
}
72
73
void
vpRobotPololuPtu::get_fJe
(
vpMatrix
&
fJe
)
74
{
75
vpColVector
q(
nDof
);
76
getPosition
(
vpRobot::JOINT_STATE
, q);
77
78
get_fJe
(q,
fJe
);
79
}
80
81
void
vpRobotPololuPtu::get_eJe
(
const
vpColVector
&q,
vpMatrix
&
eJe
)
const
82
{
83
eJe
.resize(6,
nDof
);
84
85
if
(q.
size
() !=
static_cast<
unsigned
int
>
(
nDof
)) {
86
throw
(
vpException
(
vpException::dimensionError
,
"Bad dimension for Pololu PTU joint position vector"
));
87
}
88
89
double
s2 = sin(q[1]);
90
double
c2 = cos(q[1]);
91
92
eJe
= 0;
93
94
eJe
[3][0] = -c2;
95
eJe
[4][1] = -1;
96
eJe
[5][0] = s2;
97
}
98
99
void
vpRobotPololuPtu::get_fJe
(
const
vpColVector
&q,
vpMatrix
&
fJe
)
const
100
{
101
if
(q.
size
() !=
static_cast<
unsigned
int
>
(
nDof
)) {
102
throw
(
vpException
(
vpException::dimensionError
,
"Bad dimension for Pololu PTU joint position vector"
));
103
}
104
105
fJe
.resize(6,
nDof
);
106
107
double
s1 = sin(q[0]);
108
double
c1 = cos(q[0]);
109
110
fJe
= 0;
111
112
fJe
[3][1] = s1;
113
fJe
[4][1] = -c1;
114
fJe
[5][0] = 1;
115
}
116
117
float
vpRobotPololuPtu::getAngularVelocityResolution
()
const
118
{
119
return
m_pan.speedToRadS(1);
120
}
121
122
void
vpRobotPololuPtu::setPosition
(
const
vpRobot::vpControlFrameType
frame,
const
vpColVector
&q)
123
{
124
if
(q.
size
() !=
static_cast<
unsigned
int
>
(
nDof
)) {
125
throw
(
vpException
(
vpException::dimensionError
,
"Bad dimension for Pololu PTU joint position vector"
));
126
}
127
128
if
(
vpRobot::STATE_POSITION_CONTROL
!=
getRobotState
()) {
129
std::cout <<
"Warning: Robot is not in position-based control. Modification of the robot state"
<< std::endl;
130
setRobotState
(
vpRobot::STATE_POSITION_CONTROL
);
131
}
132
133
switch
(frame) {
134
case
vpRobot::CAMERA_FRAME
:
135
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot move the robot in camera frame: "
136
"not implemented"
);
137
case
vpRobot::REFERENCE_FRAME
:
138
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot move the robot in reference frame: "
139
"not implemented"
);
140
case
vpRobot::MIXT_FRAME
:
141
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot move the robot in mixt frame: "
142
"not implemented"
);
143
case
vpRobot::END_EFFECTOR_FRAME
:
144
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot move the robot in end-effector frame: "
145
"not implemented"
);
146
case
vpRobot::JOINT_STATE
:
147
break
;
148
}
149
150
float
pos_vel = m_positioning_velocity_percentage *
static_cast<
float
>
(
getMaxRotationVelocity
());
151
m_pan.setAngularPosition(
static_cast<
float
>
(q[0]), pos_vel);
152
m_tilt.setAngularPosition(
static_cast<
float
>
(q[1]), pos_vel);
153
}
154
155
void
vpRobotPololuPtu::setVelocity
(
const
vpRobot::vpControlFrameType
frame,
const
vpColVector
&q_dot)
156
{
157
if
(q_dot.
size
() !=
static_cast<
unsigned
int
>
(
nDof
)) {
158
throw
(
vpException
(
vpException::dimensionError
,
"Bad dimension for Pololu PTU joint position vector"
));
159
}
160
161
if
(
vpRobot::STATE_VELOCITY_CONTROL
!=
getRobotState
()) {
162
throw
vpRobotException
(
vpRobotException::wrongStateError
,
163
"Cannot send a velocity to the robot "
164
"use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) "
);
165
}
166
167
switch
(frame) {
168
case
vpRobot::CAMERA_FRAME
: {
169
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot send a velocity to the robot "
170
"in the camera frame:"
171
"functionality not implemented"
);
172
}
173
case
vpRobot::JOINT_STATE
: {
174
if
(q_dot.
getRows
() != 2) {
175
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Bad dimension for speed vector "
176
"in joint state"
);
177
}
178
break
;
179
}
180
case
vpRobot::REFERENCE_FRAME
: {
181
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot send a velocity to the robot "
182
"in the reference frame:"
183
"functionality not implemented"
);
184
}
185
case
vpRobot::MIXT_FRAME
: {
186
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot send a velocity to the robot "
187
"in the mixt frame:"
188
"functionality not implemented"
);
189
}
190
case
vpRobot::END_EFFECTOR_FRAME
: {
191
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot send a velocity to the robot "
192
"in the end-effector frame:"
193
"functionality not implemented"
);
194
}
195
default
: {
196
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot send a velocity to the robot "
);
197
}
198
}
199
200
bool
norm =
false
;
// Flag to indicate when velocities need to be normalized
201
202
// Saturate joint speed
203
double
max =
getMaxRotationVelocity
();
204
vpColVector
q_dot_sat(
nDof
);
205
206
// Init q_dot_saturated
207
q_dot_sat = q_dot;
208
209
for
(
unsigned
int
i = 0; i < static_cast<unsigned int>(
nDof
); ++i) {
// q1 and q2
210
if
(fabs(q_dot[i]) > max) {
211
norm =
true
;
212
max = fabs(q_dot[i]);
213
vpERROR_TRACE
(
"Excess velocity: ROTATION "
214
"(axe nr.%d)."
,
215
i);
216
}
217
}
218
// Rotations velocities normalization
219
if
(norm ==
true
) {
220
max =
getMaxRotationVelocity
() / max;
221
q_dot_sat = q_dot * max;
222
}
223
224
std::cout <<
"Send velocity: "
<< q_dot_sat.
t
() << std::endl;
225
226
m_pan.setAngularVelocity(
static_cast<
float
>
(q_dot_sat[0]));
227
m_tilt.setAngularVelocity(
static_cast<
float
>
(q_dot_sat[1]));
228
}
229
230
vpRobot::vpRobotStateType
vpRobotPololuPtu::setRobotState
(
vpRobot::vpRobotStateType
newState)
231
{
232
switch
(newState) {
233
case
vpRobot::STATE_STOP
: {
234
if
(
vpRobot::STATE_STOP
!=
getRobotState
()) {
235
stopVelocity
();
236
}
237
break
;
238
}
239
case
vpRobot::STATE_POSITION_CONTROL
: {
240
if
(
vpRobot::STATE_VELOCITY_CONTROL
==
getRobotState
()) {
241
std::cout <<
"Switch from velocity to position control."
<< std::endl;
242
stopVelocity
();
243
}
244
245
break
;
246
}
247
case
vpRobot::STATE_VELOCITY_CONTROL
: {
248
}
249
default
:
250
break
;
251
}
252
253
return
vpRobot::setRobotState
(newState);
254
}
255
256
void
vpRobotPololuPtu::stopVelocity
()
257
{
258
m_pan.stopVelocityCmd();
259
m_tilt.stopVelocityCmd();
260
}
261
262
void
vpRobotPololuPtu::getPosition
(
const
vpRobot::vpControlFrameType
frame,
vpColVector
&q)
263
{
264
switch
(frame) {
265
case
vpRobot::CAMERA_FRAME
:
266
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot get position in camera frame: "
267
"not implemented"
);
268
case
vpRobot::REFERENCE_FRAME
:
269
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot get position in reference frame: "
270
"not implemented"
);
271
case
vpRobot::MIXT_FRAME
:
272
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot get position in mixt frame: "
273
"not implemented"
);
274
case
vpRobot::END_EFFECTOR_FRAME
:
275
throw
vpRobotException
(
vpRobotException::wrongStateError
,
"Cannot get position in end-effector frame: "
276
"not implemented"
);
277
case
vpRobot::JOINT_STATE
:
278
break
;
279
}
280
281
q.
resize
(
nDof
);
282
q[0] = m_pan.getAngularPosition();
283
q[1] = m_tilt.getAngularPosition();
284
}
285
END_VISP_NAMESPACE
286
#elif !defined(VISP_BUILD_SHARED_LIBS)
287
// Work around to avoid warning: libvisp_robot.a(vpRobotPololuPtu.cpp.o) has no symbols
288
void
dummy_vpRobotPololuPtu() { }
289
#endif
vpArray2D::size
unsigned int size() const
Return the number of elements of the 2D array.
Definition
vpArray2D.h:435
vpArray2D::getRows
unsigned int getRows() const
Definition
vpArray2D.h:433
vpColVector
Implementation of column vector and the associated operations.
Definition
vpColVector.h:191
vpColVector::t
vpRowVector t() const
Definition
vpColVector.cpp:558
vpColVector::resize
void resize(unsigned int i, bool flagNullify=true)
Definition
vpColVector.h:1159
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::dimensionError
@ dimensionError
Bad dimension.
Definition
vpException.h:71
vpMath::rad
static double rad(double deg)
Definition
vpMath.h:129
vpMatrix
Implementation of a matrix and operations on matrices.
Definition
vpMatrix.h:175
vpRobotException
Error that can be emitted by the vpRobot class and its derivatives.
Definition
vpRobotException.h:54
vpRobotException::wrongStateError
@ wrongStateError
Wrong robot state.
Definition
vpRobotException.h:70
vpRobotPololuPtu::setVelocity
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &q_dot) VP_OVERRIDE
Definition
vpRobotPololuPtu.cpp:155
vpRobotPololuPtu::getPosition
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &q) VP_OVERRIDE
Definition
vpRobotPololuPtu.cpp:262
vpRobotPololuPtu::get_eJe
void get_eJe(vpMatrix &eJe) VP_OVERRIDE
Definition
vpRobotPololuPtu.cpp:65
vpRobotPololuPtu::~vpRobotPololuPtu
~vpRobotPololuPtu() VP_OVERRIDE
Definition
vpRobotPololuPtu.cpp:59
vpRobotPololuPtu::vpRobotPololuPtu
vpRobotPololuPtu(const std::string &device="/dev/ttyACM0", int baudrate=9600, bool verbose=false)
Definition
vpRobotPololuPtu.cpp:44
vpRobotPololuPtu::setRobotState
vpRobot::vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState) VP_OVERRIDE
Definition
vpRobotPololuPtu.cpp:230
vpRobotPololuPtu::stopVelocity
void stopVelocity()
Definition
vpRobotPololuPtu.cpp:256
vpRobotPololuPtu::getAngularVelocityResolution
float getAngularVelocityResolution() const
Definition
vpRobotPololuPtu.cpp:117
vpRobotPololuPtu::setPosition
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q) VP_OVERRIDE
Definition
vpRobotPololuPtu.cpp:122
vpRobotPololuPtu::get_fJe
void get_fJe(vpMatrix &fJe) VP_OVERRIDE
Definition
vpRobotPololuPtu.cpp:73
vpRobot::nDof
int nDof
number of degrees of freedom
Definition
vpRobot.h:101
vpRobot::eJe
vpMatrix eJe
robot Jacobian expressed in the end-effector frame
Definition
vpRobot.h:103
vpRobot::getRobotState
virtual vpRobotStateType getRobotState(void) const
Definition
vpRobot.h:152
vpRobot::vpControlFrameType
vpControlFrameType
Definition
vpRobot.h:74
vpRobot::REFERENCE_FRAME
@ REFERENCE_FRAME
Definition
vpRobot.h:75
vpRobot::JOINT_STATE
@ JOINT_STATE
Definition
vpRobot.h:79
vpRobot::MIXT_FRAME
@ MIXT_FRAME
Definition
vpRobot.h:85
vpRobot::CAMERA_FRAME
@ CAMERA_FRAME
Definition
vpRobot.h:81
vpRobot::END_EFFECTOR_FRAME
@ END_EFFECTOR_FRAME
Definition
vpRobot.h:80
vpRobot::vpRobotStateType
vpRobotStateType
Definition
vpRobot.h:62
vpRobot::STATE_POSITION_CONTROL
@ STATE_POSITION_CONTROL
Initialize the position controller.
Definition
vpRobot.h:65
vpRobot::STATE_VELOCITY_CONTROL
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition
vpRobot.h:64
vpRobot::STATE_STOP
@ STATE_STOP
Stops robot motion especially in velocity and acceleration control.
Definition
vpRobot.h:63
vpRobot::getMaxRotationVelocity
double getMaxRotationVelocity(void) const
Definition
vpRobot.cpp:272
vpRobot::fJe
vpMatrix fJe
robot Jacobian expressed in the robot reference frame available
Definition
vpRobot.h:107
vpRobot::setRobotState
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition
vpRobot.cpp:200
vpERROR_TRACE
#define vpERROR_TRACE
Definition
vpDebug.h:423
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
robot
src
real-robot
pololu-maestro
vpRobotPololuPtu.cpp
Generated by
1.17.0