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/rclcpp.hpp"
22 #include "tf2_msgs/msg/tf_message.hpp"
23 #include "geometry_msgs/msg/transform_stamped.hpp"
24 #include "nav2_ros_common/tf2_factories.hpp"
25 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
26 #include "tf2/utils.hpp"
27 #include "nav2_ros_common/node_utils.hpp"
28 
29 namespace nav2_util
30 {
31 
38 {
39 public:
40  // nosemgrep
41  BaseFootprintPublisherListener(tf2::BufferCore & buffer, bool spin_thread, rclcpp::Node & node)
42  : nav2::TransformListener(buffer, spin_thread)
43  {
44  base_link_frame_ = nav2::declare_or_get_parameter(
45  &node, "base_link_frame", std::string("base_link"));
46  base_footprint_frame_ = nav2::declare_or_get_parameter(
47  &node, "base_footprint_frame", std::string("base_footprint"));
48  tf_broadcaster_ = nav2::create_transform_broadcaster(&node);
49  }
50 
54  void subscription_callback(tf2_msgs::msg::TFMessage::ConstSharedPtr msg, bool is_static) override
55  {
56  TransformListener::subscription_callback(msg, is_static);
57 
58  if (is_static) {
59  return;
60  }
61 
62  for (unsigned int i = 0; i != msg->transforms.size(); i++) {
63  auto & t = msg->transforms[i];
64  if (t.child_frame_id == base_link_frame_) {
65  geometry_msgs::msg::TransformStamped transform;
66  transform.header.stamp = t.header.stamp;
67  transform.header.frame_id = base_link_frame_;
68  transform.child_frame_id = base_footprint_frame_;
69 
70  // Project to Z-zero
71  transform.transform.translation = t.transform.translation;
72  transform.transform.translation.z = 0.0;
73 
74  // Remove Roll and Pitch
75  tf2::Quaternion q;
76  q.setRPY(0, 0, tf2::getYaw(t.transform.rotation));
77  q.normalize();
78  transform.transform.rotation.x = q.x();
79  transform.transform.rotation.y = q.y();
80  transform.transform.rotation.z = q.z();
81  transform.transform.rotation.w = q.w();
82 
83  tf_broadcaster_->sendTransform(transform);
84  return;
85  }
86  }
87  }
88 
89 protected:
90  nav2::TransformBroadcaster::SharedPtr tf_broadcaster_;
91  std::string base_link_frame_, base_footprint_frame_;
92 };
93 
100 class BaseFootprintPublisher : public rclcpp::Node // nosemgrep
101 {
102 public:
106  explicit BaseFootprintPublisher(const rclcpp::NodeOptions & options = rclcpp::NodeOptions())
107  : Node("base_footprint_publisher", options)
108  {
109  RCLCPP_INFO(get_logger(), "Creating base footprint publisher");
110  tf_buffer_ = nav2::create_transform_buffer(this);
111  listener_publisher_ = std::make_shared<BaseFootprintPublisherListener>(
112  *tf_buffer_, true, *this);
113  }
114 
115 protected:
116  nav2::TransformBuffer::SharedPtr tf_buffer_;
117  std::shared_ptr<BaseFootprintPublisherListener> listener_publisher_;
118 };
119 
120 } // end namespace nav2_util
121 
122 #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.