Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
odom_subscriber.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
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 copyright holder 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 HOLDER 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 #ifndef NAV_2D_UTILS__ODOM_SUBSCRIBER_HPP_
36 #define NAV_2D_UTILS__ODOM_SUBSCRIBER_HPP_
37 
38 #include <cmath>
39 #include <memory>
40 #include <mutex>
41 #include <string>
42 #include "nav_2d_msgs/msg/twist2_d_stamped.hpp"
43 #include "nav_msgs/msg/odometry.hpp"
44 #include "nav2_util/lifecycle_node.hpp"
45 #include "rclcpp/rclcpp.hpp"
46 #include "nav2_util/node_utils.hpp"
47 
48 namespace nav_2d_utils
49 {
50 
56 {
57 public:
64  explicit OdomSubscriber(
65  nav2_util::LifecycleNode::SharedPtr nh,
66  std::string default_topic = "odom")
67  {
68  nav2_util::declare_parameter_if_not_declared(
69  nh, "odom_topic", rclcpp::ParameterValue(default_topic));
70 
71  std::string odom_topic;
72  nh->get_parameter_or("odom_topic", odom_topic, default_topic);
73  odom_sub_ =
74  nh->create_subscription<nav_msgs::msg::Odometry>(
75  odom_topic,
76  rclcpp::SystemDefaultsQoS(),
77  std::bind(&OdomSubscriber::odomCallback, this, std::placeholders::_1));
78  }
79 
80  inline nav_2d_msgs::msg::Twist2D getTwist() {return odom_vel_.velocity;}
81  inline nav_2d_msgs::msg::Twist2DStamped getTwistStamped() {return odom_vel_;}
82 
83 protected:
84  void odomCallback(const nav_msgs::msg::Odometry::SharedPtr msg)
85  {
86  // ROS_INFO_ONCE("odom received!");
87  std::lock_guard<std::mutex> lock(odom_mutex_);
88  odom_vel_.header = msg->header;
89  odom_vel_.velocity.x = msg->twist.twist.linear.x;
90  odom_vel_.velocity.y = msg->twist.twist.linear.y;
91  odom_vel_.velocity.theta = msg->twist.twist.angular.z;
92  }
93 
94  rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_sub_;
95  nav_2d_msgs::msg::Twist2DStamped odom_vel_;
96  std::mutex odom_mutex_;
97 };
98 
99 } // namespace nav_2d_utils
100 
101 #endif // NAV_2D_UTILS__ODOM_SUBSCRIBER_HPP_
OdomSubscriber(nav2_util::LifecycleNode::SharedPtr nh, std::string default_topic="odom")
Constructor that subscribes to an Odometry topic.