1 |
|
|
/********************************************************************* |
2 |
|
|
* Software License Agreement (BSD License) |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2013, PAL Robotics, S.L. |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions |
9 |
|
|
* are met: |
10 |
|
|
* |
11 |
|
|
* * Redistributions of source code must retain the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
|
* * Redistributions in binary form must reproduce the above |
14 |
|
|
* copyright notice, this list of conditions and the following |
15 |
|
|
* disclaimer in the documentation and/or other materials provided |
16 |
|
|
* with the distribution. |
17 |
|
|
* * Neither the name of the PAL Robotics nor the names of its |
18 |
|
|
* contributors may be used to endorse or promote products derived |
19 |
|
|
* from this software without specific prior written permission. |
20 |
|
|
* |
21 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
24 |
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
25 |
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
26 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
27 |
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
28 |
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
29 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 |
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 |
|
|
* POSSIBILITY OF SUCH DAMAGE. |
33 |
|
|
*********************************************************************/ |
34 |
|
|
|
35 |
|
|
/* |
36 |
|
|
* Author: Luca Marchionni |
37 |
|
|
* Author: Bence Magyar |
38 |
|
|
* Author: Enrique Fernández |
39 |
|
|
* Author: Paul Mathieu |
40 |
|
|
*/ |
41 |
|
|
|
42 |
|
|
#ifndef ODOMETRY_H_ |
43 |
|
|
#define ODOMETRY_H_ |
44 |
|
|
|
45 |
|
|
#include "pinocchio/fwd.hpp" |
46 |
|
|
// include pinocchio before boost |
47 |
|
|
|
48 |
|
|
#include <dynamic-graph/entity.h> |
49 |
|
|
#include <dynamic-graph/signal-ptr.h> |
50 |
|
|
#include <dynamic-graph/signal-time-dependent.h> |
51 |
|
|
|
52 |
|
|
#include <boost/accumulators/accumulators.hpp> |
53 |
|
|
#include <boost/accumulators/statistics/rolling_mean.hpp> |
54 |
|
|
#include <boost/accumulators/statistics/stats.hpp> |
55 |
|
|
#include <boost/function.hpp> |
56 |
|
|
#include <sot/core/matrix-geometry.hh> |
57 |
|
|
|
58 |
|
|
namespace dynamicgraph { |
59 |
|
|
namespace details { |
60 |
|
|
namespace bacc = boost::accumulators; |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* \brief The Odometry class handles odometry readings |
64 |
|
|
* (2D pose and velocity with related timestamp) |
65 |
|
|
* \deprecated TO BE DELETED |
66 |
|
|
*/ |
67 |
|
|
class Odometry { |
68 |
|
|
public: |
69 |
|
|
/// Integration function, used to integrate the odometry: |
70 |
|
|
typedef boost::function<void(double, double)> IntegrationFunction; |
71 |
|
|
|
72 |
|
|
/** |
73 |
|
|
* \brief Constructor |
74 |
|
|
* Timestamp will get the current time value |
75 |
|
|
* Value will be set to zero |
76 |
|
|
* \param velocity_rolling_window_size Rolling window size used to compute the |
77 |
|
|
* velocity mean |
78 |
|
|
*/ |
79 |
|
|
Odometry(size_t velocity_rolling_window_size = 10); |
80 |
|
|
|
81 |
|
|
/** |
82 |
|
|
* \brief Initialize the odometry |
83 |
|
|
* \param time Current time |
84 |
|
|
*/ |
85 |
|
|
void init(); |
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* \brief Updates the odometry class with latest wheels position |
89 |
|
|
* \param left_pos Left wheel position [rad] |
90 |
|
|
* \param right_pos Right wheel position [rad] |
91 |
|
|
* \param dt the elapsed time since last call. |
92 |
|
|
* \return true if the odometry is actually updated |
93 |
|
|
*/ |
94 |
|
|
bool update(double left_pos, double right_pos, double dt); |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* \brief Updates the odometry class with latest velocity command |
98 |
|
|
* \param linear Linear velocity [m/s] |
99 |
|
|
* \param angular Angular velocity [rad/s] |
100 |
|
|
* \param dt the elapsed time since last call. |
101 |
|
|
*/ |
102 |
|
|
void updateOpenLoop(double linear, double angular, double dt); |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* \brief heading getter |
106 |
|
|
* \return heading [rad] |
107 |
|
|
*/ |
108 |
|
|
double getHeading() const { return heading_; } |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* \brief x position getter |
112 |
|
|
* \return x position [m] |
113 |
|
|
*/ |
114 |
|
|
double getX() const { return x_; } |
115 |
|
|
|
116 |
|
|
/** |
117 |
|
|
* \brief y position getter |
118 |
|
|
* \return y position [m] |
119 |
|
|
*/ |
120 |
|
|
double getY() const { return y_; } |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* \brief linear velocity getter |
124 |
|
|
* \return linear velocity [m/s] |
125 |
|
|
*/ |
126 |
|
|
double getLinear() const { return linear_; } |
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* \brief angular velocity getter |
130 |
|
|
* \return angular velocity [rad/s] |
131 |
|
|
*/ |
132 |
|
|
double getAngular() const { return angular_; } |
133 |
|
|
|
134 |
|
|
/** |
135 |
|
|
* \brief Sets the wheel parameters: radius and separation |
136 |
|
|
* \param wheel_separation Separation between left and right wheels [m] |
137 |
|
|
* \param left_wheelRadius Left wheel radius [m] |
138 |
|
|
* \param right_wheelRadius Right wheel radius [m] |
139 |
|
|
*/ |
140 |
|
|
void setWheelParams(double wheel_separation, double left_wheelRadius, |
141 |
|
|
double right_wheelRadius); |
142 |
|
|
|
143 |
|
|
/** |
144 |
|
|
* \brief Velocity rolling window size setter |
145 |
|
|
* \param velocity_rolling_window_size Velocity rolling window size |
146 |
|
|
*/ |
147 |
|
|
void setVelocityRollingWindowSize(size_t velocity_rolling_window_size); |
148 |
|
|
|
149 |
|
|
private: |
150 |
|
|
/// Rolling mean accumulator and window: |
151 |
|
|
typedef bacc::accumulator_set<double, bacc::stats<bacc::tag::rolling_mean> > |
152 |
|
|
RollingMeanAcc; |
153 |
|
|
typedef bacc::tag::rolling_window RollingWindow; |
154 |
|
|
|
155 |
|
|
/** |
156 |
|
|
* \brief Integrates the velocities (linear and angular) using 2nd order |
157 |
|
|
* Runge-Kutta \param linear Linear velocity [m] (linear displacement, |
158 |
|
|
* i.e. m/s * dt) computed by encoders \param angular Angular velocity [rad] |
159 |
|
|
* (angular displacement, i.e. m/s * dt) computed by encoders |
160 |
|
|
*/ |
161 |
|
|
void integrateRungeKutta2(double linear, double angular); |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* \brief Integrates the velocities (linear and angular) using exact method |
165 |
|
|
* \param linear Linear velocity [m] (linear displacement, i.e. m/s * dt) |
166 |
|
|
* computed by encoders \param angular Angular velocity [rad] (angular |
167 |
|
|
* displacement, i.e. m/s * dt) computed by encoders |
168 |
|
|
*/ |
169 |
|
|
void integrateExact(double linear, double angular); |
170 |
|
|
|
171 |
|
|
/** |
172 |
|
|
* \brief Reset linear and angular accumulators |
173 |
|
|
*/ |
174 |
|
|
void resetAccumulators(); |
175 |
|
|
|
176 |
|
|
/// Current pose: |
177 |
|
|
double x_; // [m] |
178 |
|
|
double y_; // [m] |
179 |
|
|
double heading_; // [rad] |
180 |
|
|
|
181 |
|
|
/// Current velocity: |
182 |
|
|
double linear_; // [m/s] |
183 |
|
|
double angular_; // [rad/s] |
184 |
|
|
|
185 |
|
|
/// Wheel kinematic parameters [m]: |
186 |
|
|
double wheelSeparation_; |
187 |
|
|
double left_wheelRadius_; |
188 |
|
|
double right_wheelRadius_; |
189 |
|
|
|
190 |
|
|
/// Previou wheel position/state [rad]: |
191 |
|
|
double left_wheel_old_pos_; |
192 |
|
|
double right_wheel_old_pos_; |
193 |
|
|
|
194 |
|
|
/// Rolling mean accumulators for the linar and angular velocities: |
195 |
|
|
size_t velocity_rolling_window_size_; |
196 |
|
|
RollingMeanAcc linear_acc_; |
197 |
|
|
RollingMeanAcc angular_acc_; |
198 |
|
|
|
199 |
|
|
/// Integration funcion, used to integrate the odometry: |
200 |
|
|
IntegrationFunction integrate_fun_; |
201 |
|
|
}; |
202 |
|
|
|
203 |
|
|
} // namespace details |
204 |
|
|
|
205 |
|
|
class Odometry : public Entity { |
206 |
|
|
public: |
207 |
|
|
DYNAMIC_GRAPH_ENTITY_DECL(); |
208 |
|
|
|
209 |
|
|
Odometry(const std::string& name); |
210 |
|
|
|
211 |
|
|
virtual std::string getDocString() const { |
212 |
|
|
return "Odometry using the wheels position sensors."; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
private: |
216 |
|
|
/// A vector of size 2 containing the left and right wheel velocities, in |
217 |
|
|
/// this order [rad]. |
218 |
|
|
SignalPtr<Vector, int> wheelsPositionSIN; |
219 |
|
|
|
220 |
|
|
/// Estimation of the velocity of the base using the wheel positions |
221 |
|
|
/// [m/s,rad/s]. |
222 |
|
|
SignalTimeDependent<Vector, int> baseVelocityEstimationSOUT; |
223 |
|
|
|
224 |
|
|
/// Filtered estimation of the velocity of the base using the wheel positions |
225 |
|
|
/// [m/s,rad/s]. |
226 |
|
|
SignalTimeDependent<Vector, int> baseVelocityFilteredEstimationSOUT; |
227 |
|
|
|
228 |
|
|
/// The base velocity to integrate [m/s,rad/s]. |
229 |
|
|
/// To enable open-loop odometry, plug this to the control sent to the robot |
230 |
|
|
/// base. |
231 |
|
|
SignalPtr<Vector, int> baseVelocitySIN; |
232 |
|
|
|
233 |
|
|
/// Outputs the position of the base as |
234 |
|
|
/// \f$ (x, y, \theta) \f$ [m,rad]. |
235 |
|
|
SignalTimeDependent<Vector, int> baseConfigSOUT; |
236 |
|
|
|
237 |
|
|
/// Outputs the position of the base as an homogeneous transformation. |
238 |
|
|
SignalTimeDependent<sot::MatrixHomogeneous, int> basePoseSOUT; |
239 |
|
|
|
240 |
|
|
Vector& computeVelocityEstimation(Vector& vel, int time); |
241 |
|
|
|
242 |
|
|
Vector& computeVelocityFilteredEstimation(Vector& vel, int time); |
243 |
|
|
|
244 |
|
|
Vector& computeBaseConfig(Vector& base, int time); |
245 |
|
|
|
246 |
|
|
sot::MatrixHomogeneous& computeBasePose(sot::MatrixHomogeneous& M, int time); |
247 |
|
|
|
248 |
|
|
/// Set the base pose to integrate from. |
249 |
|
|
void setBasePose(const double& x, const double& y, const double& heading); |
250 |
|
|
|
251 |
|
|
/** |
252 |
|
|
* \brief Sets the wheel parameters: radius and separation |
253 |
|
|
* \param wheel_separation Separation between left and right wheels [m] |
254 |
|
|
* \param left_wheelRadius Left wheel radius [m] |
255 |
|
|
* \param right_wheelRadius Right wheel radius [m] |
256 |
|
|
*/ |
257 |
|
|
void setWheelParams(const double& wheel_separation, |
258 |
|
|
const double& left_wheelRadius, |
259 |
|
|
const double& right_wheelRadius); |
260 |
|
|
|
261 |
|
|
/** |
262 |
|
|
* \brief Velocity rolling window size setter |
263 |
|
|
* \param velocity_rolling_window_size Velocity rolling window size |
264 |
|
|
*/ |
265 |
|
|
void setVelocityRollingWindowSize(const size_t& velocity_rolling_window_size); |
266 |
|
|
|
267 |
|
|
private: |
268 |
|
|
/// Rolling mean accumulator and window: |
269 |
|
|
typedef boost::accumulators::accumulator_set< |
270 |
|
|
double, |
271 |
|
|
boost::accumulators::stats<boost::accumulators::tag::rolling_mean> > |
272 |
|
|
RollingMeanAcc; |
273 |
|
|
typedef boost::accumulators::tag::rolling_window RollingWindow; |
274 |
|
|
|
275 |
|
|
/** |
276 |
|
|
* \brief Integrates the velocities (linear and angular) using 2nd order |
277 |
|
|
* Runge-Kutta \param linear Linear velocity [m] (linear displacement, |
278 |
|
|
* i.e. m/s * dt) computed by encoders \param angular Angular velocity [rad] |
279 |
|
|
* (angular displacement, i.e. m/s * dt) computed by encoders |
280 |
|
|
*/ |
281 |
|
|
void integrateRungeKutta2(double linear, double angular); |
282 |
|
|
|
283 |
|
|
/** |
284 |
|
|
* \brief Integrates the velocities (linear and angular) using exact method |
285 |
|
|
* \param linear Linear velocity [m] (linear displacement, i.e. m/s * dt) |
286 |
|
|
* computed by encoders \param angular Angular velocity [rad] (angular |
287 |
|
|
* displacement, i.e. m/s * dt) computed by encoders |
288 |
|
|
*/ |
289 |
|
|
void integrateExact(double linear, double angular); |
290 |
|
|
|
291 |
|
|
double dt_; |
292 |
|
|
|
293 |
|
|
/// Current pose: |
294 |
|
|
double x_; // [m] |
295 |
|
|
double y_; // [m] |
296 |
|
|
double heading_; // [rad] |
297 |
|
|
|
298 |
|
|
/// Wheel kinematic parameters [m]: |
299 |
|
|
double wheelSeparationInv_; |
300 |
|
|
/// Left and right wheel radii [m]. |
301 |
|
|
Eigen::Array2d wheelRadii_; |
302 |
|
|
|
303 |
|
|
/// Previous wheel position/state [m]: |
304 |
|
|
Eigen::Array2d wheelOldPos_; |
305 |
|
|
|
306 |
|
|
/// Rolling mean accumulators for the linar and angular velocities: |
307 |
|
|
size_t rollingWindowSize_; |
308 |
|
|
RollingMeanAcc linear_acc_; |
309 |
|
|
RollingMeanAcc angular_acc_; |
310 |
|
|
|
311 |
|
|
void resetAccumulators(); |
312 |
|
|
}; |
313 |
|
|
} // namespace dynamicgraph |
314 |
|
|
|
315 |
|
|
#endif /* ODOMETRY_H_ */ |