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/rclcpp.hpp"
28 #include "rclcpp/qos.hpp"
29 #include "rclcpp_lifecycle/lifecycle_publisher.hpp"
30 
31 #include "nav2_ros_common/node_utils.hpp"
32 #include "nav2_ros_common/lifecycle_node.hpp"
33 
34 namespace nav2_util
35 {
36 
47 {
48 public:
55  explicit TwistPublisher(
56  nav2::LifecycleNode::SharedPtr node,
57  const std::string & topic,
58  const rclcpp::QoS & qos = nav2::qos::StandardTopicQoS())
59  : topic_(topic)
60  {
61  is_stamped_ = node->declare_or_get_parameter("enable_stamped_cmd_vel", true);
62  if (is_stamped_) {
63  twist_stamped_pub_ = node->create_publisher<geometry_msgs::msg::TwistStamped>(
64  topic_,
65  qos);
66  } else {
67  twist_pub_ = node->create_publisher<geometry_msgs::msg::Twist>(
68  topic_,
69  qos);
70  }
71  }
72 
73  void on_activate()
74  {
75  if (is_stamped_) {
76  twist_stamped_pub_->on_activate();
77  } else {
78  twist_pub_->on_activate();
79  }
80  }
81 
82  void on_deactivate()
83  {
84  if (is_stamped_) {
85  twist_stamped_pub_->on_deactivate();
86  } else {
87  twist_pub_->on_deactivate();
88  }
89  }
90 
91  [[nodiscard]] bool is_activated() const
92  {
93  if (is_stamped_) {
94  return twist_stamped_pub_->is_activated();
95  } else {
96  return twist_pub_->is_activated();
97  }
98  }
99 
100  void publish(std::unique_ptr<geometry_msgs::msg::TwistStamped> velocity)
101  {
102  if (is_stamped_) {
103  twist_stamped_pub_->publish(std::move(velocity));
104  } else {
105  auto twist_msg = std::make_unique<geometry_msgs::msg::Twist>(velocity->twist);
106  twist_pub_->publish(std::move(twist_msg));
107  }
108  }
109 
110  [[nodiscard]] size_t get_subscription_count() const
111  {
112  if (is_stamped_) {
113  return twist_stamped_pub_->get_subscription_count();
114  } else {
115  return twist_pub_->get_subscription_count();
116  }
117  }
118 
119 protected:
120  std::string topic_;
121  bool is_stamped_{true};
122  nav2::Publisher<geometry_msgs::msg::Twist>::SharedPtr twist_pub_;
123  nav2::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr
124  twist_stamped_pub_;
125 };
126 
127 } // namespace nav2_util
128 
129 #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.