16 #ifndef NAV2_UTIL__ODOMETRY_UTILS_HPP_
17 #define NAV2_UTIL__ODOMETRY_UTILS_HPP_
26 #include "geometry_msgs/msg/twist.hpp"
27 #include "geometry_msgs/msg/twist_stamped.hpp"
28 #include "nav_msgs/msg/odometry.hpp"
29 #include "nav2_ros_common/lifecycle_node.hpp"
30 #include "rclcpp/rclcpp.hpp"
31 #include "nav2_ros_common/node_utils.hpp"
51 template<
typename NodeT>
54 double filter_duration = 0.3,
55 const std::string & odom_topic =
"odom")
56 : odom_history_duration_(rclcpp::Duration::from_seconds(filter_duration))
60 odom_sub_ = nav2::interfaces::create_subscription<nav_msgs::msg::Odometry>(
64 odom_cumulate_.twist.twist.linear.x = 0;
65 odom_cumulate_.twist.twist.linear.y = 0;
66 odom_cumulate_.twist.twist.linear.z = 0;
67 odom_cumulate_.twist.twist.angular.x = 0;
68 odom_cumulate_.twist.twist.angular.y = 0;
69 odom_cumulate_.twist.twist.angular.z = 0;
78 std::lock_guard<std::mutex> lock(odom_mutex_);
79 if (!received_odom_) {
80 RCLCPP_ERROR(rclcpp::get_logger(
"OdomSmoother"),
81 "OdomSmoother has not received any data yet, returning empty Twist");
82 geometry_msgs::msg::Twist twist;
85 return vel_smooth_.twist;
94 std::lock_guard<std::mutex> lock(odom_mutex_);
95 if (!received_odom_) {
96 RCLCPP_ERROR(rclcpp::get_logger(
"OdomSmoother"),
97 "OdomSmoother has not received any data yet, returning empty Twist");
98 geometry_msgs::msg::TwistStamped twist_stamped;
110 std::lock_guard<std::mutex> lock(odom_mutex_);
111 if (!received_odom_) {
112 RCLCPP_ERROR(rclcpp::get_logger(
"OdomSmoother"),
113 "OdomSmoother has not received any data yet, returning empty Twist");
114 geometry_msgs::msg::Twist twist;
117 return odom_history_.back().twist.twist;
126 std::lock_guard<std::mutex> lock(odom_mutex_);
127 geometry_msgs::msg::TwistStamped twist_stamped;
128 if (!received_odom_) {
129 RCLCPP_ERROR(rclcpp::get_logger(
"OdomSmoother"),
130 "OdomSmoother has not received any data yet, returning empty Twist");
131 return twist_stamped;
133 twist_stamped.header = odom_history_.back().header;
134 twist_stamped.twist = odom_history_.back().twist.twist;
135 return twist_stamped;
143 void odomCallback(nav_msgs::msg::Odometry::SharedPtr msg);
150 bool received_odom_{
false};
151 nav2::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_sub_;
152 nav_msgs::msg::Odometry odom_cumulate_;
153 geometry_msgs::msg::TwistStamped vel_smooth_;
154 std::mutex odom_mutex_;
156 rclcpp::Duration odom_history_duration_;
157 std::deque<nav_msgs::msg::Odometry> odom_history_;
void odomCallback(nav_msgs::msg::Odometry::SharedPtr msg)
Callback of odometry subscriber to process.
void updateState()
Update internal state of the smoother after getting new data.
geometry_msgs::msg::TwistStamped getRawTwistStamped()
Get raw twist stamped msg from smoother (without smoothing)
geometry_msgs::msg::Twist getRawTwist()
Get raw twist msg from smoother (without smoothing)
OdomSmoother(const NodeT &parent, double filter_duration=0.3, const std::string &odom_topic="odom")
Overloadded Constructor for nav_util::LifecycleNode parent that subscribes to an Odometry topic.
geometry_msgs::msg::Twist getTwist()
Get twist msg from smoother.
geometry_msgs::msg::TwistStamped getTwistStamped()
Get twist stamped msg from smoother.