Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
twist_publisher.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_PUBLISHER_HPP_
17 #define NAV2_UTIL__TWIST_PUBLISHER_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/qos.hpp"
28 
29 #include "nav2_ros_common/node_utils.hpp"
30 #include "nav2_ros_common/lifecycle_node.hpp"
31 
32 namespace nav2_util
33 {
34 
45 {
46 public:
53  explicit TwistPublisher(
54  nav2::LifecycleNode::SharedPtr node,
55  const std::string & topic,
56  const rclcpp::QoS & qos = nav2::qos::StandardTopicQoS())
57  : topic_(topic)
58  {
59  is_stamped_ = node->declare_or_get_parameter("enable_stamped_cmd_vel", true);
60  if (is_stamped_) {
61  twist_stamped_pub_ = node->create_publisher<geometry_msgs::msg::TwistStamped>(
62  topic_,
63  qos);
64  } else {
65  twist_pub_ = node->create_publisher<geometry_msgs::msg::Twist>(
66  topic_,
67  qos);
68  }
69  }
70 
71  void on_activate()
72  {
73  if (is_stamped_) {
74  twist_stamped_pub_->on_activate();
75  } else {
76  twist_pub_->on_activate();
77  }
78  }
79 
80  void on_deactivate()
81  {
82  if (is_stamped_) {
83  twist_stamped_pub_->on_deactivate();
84  } else {
85  twist_pub_->on_deactivate();
86  }
87  }
88 
89  [[nodiscard]] bool is_activated() const
90  {
91  if (is_stamped_) {
92  return twist_stamped_pub_->is_activated();
93  } else {
94  return twist_pub_->is_activated();
95  }
96  }
97 
98  void publish(std::unique_ptr<geometry_msgs::msg::TwistStamped> velocity)
99  {
100  if (is_stamped_) {
101  twist_stamped_pub_->publish(std::move(velocity));
102  } else {
103  auto twist_msg = std::make_unique<geometry_msgs::msg::Twist>(velocity->twist);
104  twist_pub_->publish(std::move(twist_msg));
105  }
106  }
107 
108  [[nodiscard]] size_t get_subscription_count() const
109  {
110  if (is_stamped_) {
111  return twist_stamped_pub_->get_subscription_count();
112  } else {
113  return twist_pub_->get_subscription_count();
114  }
115  }
116 
117 protected:
118  std::string topic_;
119  bool is_stamped_{true};
120  nav2::Publisher<geometry_msgs::msg::Twist>::SharedPtr twist_pub_;
121  nav2::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr
122  twist_stamped_pub_;
123 };
124 
125 } // namespace nav2_util
126 
127 #endif // NAV2_UTIL__TWIST_PUBLISHER_HPP_
A QoS profile for standard reliable topics with a history of 10 messages.
A simple wrapper on a Twist publisher that provides either Twist or TwistStamped.
TwistPublisher(nav2::LifecycleNode::SharedPtr node, const std::string &topic, const rclcpp::QoS &qos=nav2::qos::StandardTopicQoS())
A constructor.