Visual Servoing Platform version 3.7.0
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
44vpRobotPololuPtu::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
60{
61 m_pan.stopVelocityCmd();
62 m_tilt.stopVelocityCmd();
63}
64
72
80
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
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
118{
119 return m_pan.speedToRadS(1);
120}
121
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
129 std::cout << "Warning: Robot is not in position-based control. Modification of the robot state" << std::endl;
131 }
132
133 switch (frame) {
135 throw vpRobotException(vpRobotException::wrongStateError, "Cannot move the robot in camera frame: "
136 "not implemented");
138 throw vpRobotException(vpRobotException::wrongStateError, "Cannot move the robot in reference frame: "
139 "not implemented");
141 throw vpRobotException(vpRobotException::wrongStateError, "Cannot move the robot in mixt frame: "
142 "not implemented");
144 throw vpRobotException(vpRobotException::wrongStateError, "Cannot move the robot in end-effector frame: "
145 "not implemented");
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
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
163 "Cannot send a velocity to the robot "
164 "use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first) ");
165 }
166
167 switch (frame) {
169 throw vpRobotException(vpRobotException::wrongStateError, "Cannot send a velocity to the robot "
170 "in the camera frame:"
171 "functionality not implemented");
172 }
174 if (q_dot.getRows() != 2) {
175 throw vpRobotException(vpRobotException::wrongStateError, "Bad dimension for speed vector "
176 "in joint state");
177 }
178 break;
179 }
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 }
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
231{
232 switch (newState) {
233 case vpRobot::STATE_STOP: {
235 stopVelocity();
236 }
237 break;
238 }
241 std::cout << "Switch from velocity to position control." << std::endl;
242 stopVelocity();
243 }
244
245 break;
246 }
248 }
249 default:
250 break;
251 }
252
253 return vpRobot::setRobotState(newState);
254}
255
257{
258 m_pan.stopVelocityCmd();
259 m_tilt.stopVelocityCmd();
260}
261
263{
264 switch (frame) {
266 throw vpRobotException(vpRobotException::wrongStateError, "Cannot get position in camera frame: "
267 "not implemented");
269 throw vpRobotException(vpRobotException::wrongStateError, "Cannot get position in reference frame: "
270 "not implemented");
272 throw vpRobotException(vpRobotException::wrongStateError, "Cannot get position in mixt frame: "
273 "not implemented");
275 throw vpRobotException(vpRobotException::wrongStateError, "Cannot get position in end-effector frame: "
276 "not implemented");
278 break;
279 }
280
281 q.resize(nDof);
282 q[0] = m_pan.getAngularPosition();
283 q[1] = m_tilt.getAngularPosition();
284}
285END_VISP_NAMESPACE
286#elif !defined(VISP_BUILD_SHARED_LIBS)
287// Work around to avoid warning: libvisp_robot.a(vpRobotPololuPtu.cpp.o) has no symbols
288void dummy_vpRobotPololuPtu() { }
289#endif
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:435
unsigned int getRows() const
Definition vpArray2D.h:433
Implementation of column vector and the associated operations.
vpRowVector t() const
void resize(unsigned int i, bool flagNullify=true)
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ dimensionError
Bad dimension.
Definition vpException.h:71
static double rad(double deg)
Definition vpMath.h:129
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Error that can be emitted by the vpRobot class and its derivatives.
@ wrongStateError
Wrong robot state.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &q_dot) VP_OVERRIDE
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &q) VP_OVERRIDE
void get_eJe(vpMatrix &eJe) VP_OVERRIDE
~vpRobotPololuPtu() VP_OVERRIDE
vpRobotPololuPtu(const std::string &device="/dev/ttyACM0", int baudrate=9600, bool verbose=false)
vpRobot::vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState) VP_OVERRIDE
float getAngularVelocityResolution() const
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q) VP_OVERRIDE
void get_fJe(vpMatrix &fJe) VP_OVERRIDE
int nDof
number of degrees of freedom
Definition vpRobot.h:101
vpMatrix eJe
robot Jacobian expressed in the end-effector frame
Definition vpRobot.h:103
virtual vpRobotStateType getRobotState(void) const
Definition vpRobot.h:152
vpControlFrameType
Definition vpRobot.h:74
@ REFERENCE_FRAME
Definition vpRobot.h:75
@ JOINT_STATE
Definition vpRobot.h:79
@ MIXT_FRAME
Definition vpRobot.h:85
@ CAMERA_FRAME
Definition vpRobot.h:81
@ END_EFFECTOR_FRAME
Definition vpRobot.h:80
vpRobotStateType
Definition vpRobot.h:62
@ STATE_POSITION_CONTROL
Initialize the position controller.
Definition vpRobot.h:65
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
@ STATE_STOP
Stops robot motion especially in velocity and acceleration control.
Definition vpRobot.h:63
double getMaxRotationVelocity(void) const
Definition vpRobot.cpp:272
vpMatrix fJe
robot Jacobian expressed in the robot reference frame available
Definition vpRobot.h:107
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition vpRobot.cpp:200
#define vpERROR_TRACE
Definition vpDebug.h:423