Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
twist_subscriber.hpp
1 // Copyright (C) 2023 Ryan Friedman
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 #ifndef NAV2_UTIL__TWIST_SUBSCRIBER_HPP_
17 #define NAV2_UTIL__TWIST_SUBSCRIBER_HPP_
18 
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "geometry_msgs/msg/twist.hpp"
25 #include "geometry_msgs/msg/twist_stamped.hpp"
26 #include "rclcpp/parameter_service.hpp"
27 #include "rclcpp/rclcpp.hpp"
28 #include "rclcpp/qos.hpp"
29 #include "rclcpp_lifecycle/lifecycle_publisher.hpp"
30 
31 #include "nav2_ros_common/lifecycle_node.hpp"
32 #include "nav2_ros_common/node_utils.hpp"
33 
34 namespace nav2_util
35 {
36 
72 {
73 public:
82  template<typename TwistCallbackT,
83  typename TwistStampedCallbackT
84  >
85  explicit TwistSubscriber(
86  nav2::LifecycleNode::SharedPtr node,
87  const std::string & topic,
88  TwistCallbackT && TwistCallback,
89  TwistStampedCallbackT && TwistStampedCallback,
90  const rclcpp::QoS & qos = nav2::qos::StandardTopicQoS()
91  )
92  {
93  is_stamped_ = node->declare_or_get_parameter("enable_stamped_cmd_vel", true);
94  if (is_stamped_) {
95  twist_stamped_sub_ = node->create_subscription<geometry_msgs::msg::TwistStamped>(
96  topic,
97  std::forward<TwistStampedCallbackT>(TwistStampedCallback),
98  qos);
99  } else {
100  twist_sub_ = node->create_subscription<geometry_msgs::msg::Twist>(
101  topic,
102  std::forward<TwistCallbackT>(TwistCallback),
103  qos);
104  }
105  }
106 
115  template<typename TwistStampedCallbackT>
116  explicit TwistSubscriber(
117  nav2::LifecycleNode::SharedPtr node,
118  const std::string & topic,
119  TwistStampedCallbackT && TwistStampedCallback,
120  const rclcpp::QoS & qos = nav2::qos::StandardTopicQoS()
121  )
122  {
123  is_stamped_ = node->declare_or_get_parameter("enable_stamped_cmd_vel", true);
124  if (is_stamped_) {
125  twist_stamped_sub_ = node->create_subscription<geometry_msgs::msg::TwistStamped>(
126  topic,
127  std::forward<TwistStampedCallbackT>(TwistStampedCallback),
128  qos);
129  } else {
130  throw std::invalid_argument(
131  "enable_stamped_cmd_vel must be true when using this constructor!");
132  }
133  }
134 
135 protected:
137  bool is_stamped_{true};
139  nav2::Subscription<geometry_msgs::msg::Twist>::SharedPtr twist_sub_ {nullptr};
141  nav2::Subscription<geometry_msgs::msg::TwistStamped>::SharedPtr twist_stamped_sub_ {nullptr};
142 };
143 
144 
145 } // namespace nav2_util
146 
147 #endif // NAV2_UTIL__TWIST_SUBSCRIBER_HPP_
A QoS profile for standard reliable topics with a history of 10 messages.
A simple wrapper on a Twist subscriber that receives either geometry_msgs::msg::TwistStamped or geome...
TwistSubscriber(nav2::LifecycleNode::SharedPtr node, const std::string &topic, TwistStampedCallbackT &&TwistStampedCallback, const rclcpp::QoS &qos=nav2::qos::StandardTopicQoS())
A constructor that only supports TwistStamped.
bool is_stamped_
The user-configured value for ROS parameter enable_stamped_cmd_vel.
nav2::Subscription< geometry_msgs::msg::Twist >::SharedPtr twist_sub_
The subscription when using Twist.
nav2::Subscription< geometry_msgs::msg::TwistStamped >::SharedPtr twist_stamped_sub_
The subscription when using TwistStamped.
TwistSubscriber(nav2::LifecycleNode::SharedPtr node, const std::string &topic, TwistCallbackT &&TwistCallback, TwistStampedCallbackT &&TwistStampedCallback, const rclcpp::QoS &qos=nav2::qos::StandardTopicQoS())
A constructor that supports either Twist and TwistStamped.