ros2_control - rolling
odometry.hpp
1 // Copyright 2022 Pixel Robotics.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /*
16  * Author: Tony Najjar
17  */
18 
19 #ifndef TRICYCLE_CONTROLLER__ODOMETRY_HPP_
20 #define TRICYCLE_CONTROLLER__ODOMETRY_HPP_
21 
22 #include <cmath>
23 
24 #include <rclcpp/duration.hpp>
25 // \note The versions conditioning is added here to support the source-compatibility with Humble
26 #if RCPPUTILS_VERSION_MAJOR >= 2 && RCPPUTILS_VERSION_MINOR >= 6
27 #include "rcpputils/rolling_mean_accumulator.hpp"
28 #else
29 #include "rcppmath/rolling_mean_accumulator.hpp"
30 #endif
31 
32 namespace tricycle_controller
33 {
34 class Odometry
35 {
36 public:
37  explicit Odometry(size_t velocity_rolling_window_size = 10);
38 
39  bool update(double left_vel, double right_vel, const rclcpp::Duration & dt);
40  void updateOpenLoop(double linear, double angular, const rclcpp::Duration & dt);
41  void resetOdometry();
42 
43  double getX() const { return x_; }
44  double getY() const { return y_; }
45  double getHeading() const { return heading_; }
46  double getLinear() const { return linear_; }
47  double getAngular() const { return angular_; }
48 
49  void setWheelParams(double wheel_separation, double wheel_radius);
50  void setVelocityRollingWindowSize(size_t velocity_rolling_window_size);
51 
52 private:
53 // \note The versions conditioning is added here to support the source-compatibility with Humble
54 #if RCPPUTILS_VERSION_MAJOR >= 2 && RCPPUTILS_VERSION_MINOR >= 6
55  using RollingMeanAccumulator = rcpputils::RollingMeanAccumulator<double>;
56 #else
57  using RollingMeanAccumulator = rcppmath::RollingMeanAccumulator<double>;
58 #endif
59 
60  void integrateRungeKutta2(double linear, double angular);
61  void integrateExact(double linear, double angular);
62  void resetAccumulators();
63 
64  // Current pose:
65  double x_; // [m]
66  double y_; // [m]
67  double heading_; // [rad]
68 
69  // Current velocity:
70  double linear_; // [m/s]
71  double angular_; // [rad/s]
72 
73  // Wheel kinematic parameters [m]:
74  double wheelbase_;
75  double wheel_radius_;
76 
77  // Rolling mean accumulators for the linear and angular velocities:
78  size_t velocity_rolling_window_size_;
79  RollingMeanAccumulator linear_accumulator_;
80  RollingMeanAccumulator angular_accumulator_;
81 };
82 
83 } // namespace tricycle_controller
84 
85 #endif // TRICYCLE_CONTROLLER__ODOMETRY_HPP_
Definition: odometry.hpp:35
void updateOpenLoop(double linear, double angular, const rclcpp::Duration &dt)
Definition: odometry.cpp:57