Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
base_footprint_publisher.hpp
1 // Copyright (c) 2023 Open Navigation LLC
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 #ifndef BASE_FOOTPRINT_PUBLISHER_HPP_
16 #define BASE_FOOTPRINT_PUBLISHER_HPP_
17 
18 #include <string>
19 #include <memory>
20 
21 #include "rclcpp/logging.hpp"
22 #include "rclcpp/node.hpp"
23 #include "rclcpp/node_options.hpp"
24 #include "tf2_msgs/msg/tf_message.hpp"
25 #include "geometry_msgs/msg/transform_stamped.hpp"
26 #include "nav2_ros_common/tf2_factories.hpp"
27 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
28 #include "tf2/utils.hpp"
29 #include "nav2_ros_common/node_utils.hpp"
30 
31 namespace nav2_util
32 {
33 
40 {
41 public:
42  // nosemgrep
43  BaseFootprintPublisherListener(tf2::BufferCore & buffer, bool spin_thread, rclcpp::Node & node)
44  : nav2::TransformListener(buffer, spin_thread)
45  {
46  base_link_frame_ = nav2::declare_or_get_parameter(
47  &node, "base_link_frame", std::string("base_link"));
48  base_footprint_frame_ = nav2::declare_or_get_parameter(
49  &node, "base_footprint_frame", std::string("base_footprint"));
50  tf_broadcaster_ = nav2::create_transform_broadcaster(&node);
51  }
52 
56  void subscription_callback(tf2_msgs::msg::TFMessage::ConstSharedPtr msg, bool is_static) override
57  {
58  TransformListener::subscription_callback(msg, is_static);
59 
60  if (is_static) {
61  return;
62  }
63 
64  for (unsigned int i = 0; i != msg->transforms.size(); i++) {
65  auto & t = msg->transforms[i];
66  if (t.child_frame_id == base_link_frame_) {
67  geometry_msgs::msg::TransformStamped transform;
68  transform.header.stamp = t.header.stamp;
69  transform.header.frame_id = base_link_frame_;
70  transform.child_frame_id = base_footprint_frame_;
71 
72  // Project to Z-zero
73  transform.transform.translation = t.transform.translation;
74  transform.transform.translation.z = 0.0;
75 
76  // Remove Roll and Pitch
77  tf2::Quaternion q;
78  q.setRPY(0, 0, tf2::getYaw(t.transform.rotation));
79  q.normalize();
80  transform.transform.rotation.x = q.x();
81  transform.transform.rotation.y = q.y();
82  transform.transform.rotation.z = q.z();
83  transform.transform.rotation.w = q.w();
84 
85  tf_broadcaster_->sendTransform(transform);
86  return;
87  }
88  }
89  }
90 
91 protected:
92  nav2::TransformBroadcaster::SharedPtr tf_broadcaster_;
93  std::string base_link_frame_, base_footprint_frame_;
94 };
95 
102 class BaseFootprintPublisher : public rclcpp::Node // nosemgrep
103 {
104 public:
108  explicit BaseFootprintPublisher(const rclcpp::NodeOptions & options = rclcpp::NodeOptions())
109  : Node("base_footprint_publisher", options)
110  {
111  RCLCPP_INFO(get_logger(), "Creating base footprint publisher");
112  tf_buffer_ = nav2::create_transform_buffer(this);
113  listener_publisher_ = std::make_shared<BaseFootprintPublisherListener>(
114  *tf_buffer_, true, *this);
115  }
116 
117 protected:
118  nav2::TransformBuffer::SharedPtr tf_buffer_;
119  std::shared_ptr<BaseFootprintPublisherListener> listener_publisher_;
120 };
121 
122 } // end namespace nav2_util
123 
124 #endif // BASE_FOOTPRINT_PUBLISHER_HPP_
A TF2 listener that overrides the subscription callback to inject base footprint publisher removing Z...
void subscription_callback(tf2_msgs::msg::TFMessage::ConstSharedPtr msg, bool is_static) override
Overrides TF2 subscription callback to inject base footprint publisher.
Republishes the base_link frame as base_footprint stripping away the Z, Roll, and Pitch of the full 3...
BaseFootprintPublisher(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
A constructor.
Nav2 type wrapper for tf2_ros::TransformListener.