/home/runner/work/beluga/beluga/beluga_amcl/include/beluga_amcl/ros2_common.hpp Source File

/home/runner/work/beluga/beluga/beluga_amcl/include/beluga_amcl/ros2_common.hpp Source File#

Beluga AMCL: /home/runner/work/beluga/beluga/beluga_amcl/include/beluga_amcl/ros2_common.hpp Source File
Beluga AMCL
ros2_common.hpp
1 // Copyright 2024 Ekumen, Inc.
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 BELUGA_AMCL_ROS2_COMMON_HPP
16 #define BELUGA_AMCL_ROS2_COMMON_HPP
17 
18 #include <execution>
19 #include <rclcpp/node_options.hpp>
20 #include <rclcpp_lifecycle/lifecycle_node.hpp>
21 #include <rclcpp_lifecycle/state.hpp>
22 #include <string>
23 #include <string_view>
24 
25 #include <bondcpp/bond.hpp>
26 #include <geometry_msgs/msg/pose_stamped.hpp>
27 #include <rclcpp/rclcpp.hpp>
28 #include <rclcpp/time.hpp>
29 
30 #include <tf2/convert.h>
31 #include <tf2/utils.h>
32 #include <tf2_ros/create_timer_ros.h>
33 #include <tf2_ros/transform_broadcaster.h>
34 #include <tf2_ros/transform_listener.h>
35 
37 
38 namespace beluga_amcl {
39 
41 constexpr std::string_view kDifferentialModelName = "differential_drive";
43 constexpr std::string_view kOmnidirectionalModelName = "omnidirectional_drive";
45 constexpr std::string_view kStationaryModelName = "stationary";
47 constexpr std::string_view kNav2DifferentialModelName = "nav2_amcl::DifferentialMotionModel";
49 constexpr std::string_view kNav2OmnidirectionalModelName = "nav2_amcl::OmniMotionModel";
51 using ExecutionPolicyVariant = std::variant<std::execution::sequenced_policy, std::execution::parallel_policy>;
52 
55 class BaseAMCLNode : public rclcpp_lifecycle::LifecycleNode {
56  public:
58  explicit BaseAMCLNode(
59  const std::string& node_name = "amcl",
60  const std::string& node_namespace = "",
61  const rclcpp::NodeOptions& node_options = rclcpp::NodeOptions{});
62 
64  ~BaseAMCLNode() override;
65 
66  protected:
68  CallbackReturn on_configure(const rclcpp_lifecycle::State&) override;
69 
71  CallbackReturn on_activate(const rclcpp_lifecycle::State&) override;
72 
74  CallbackReturn on_deactivate(const rclcpp_lifecycle::State&) override;
75 
77  CallbackReturn on_cleanup(const rclcpp_lifecycle::State&) override;
78 
80  CallbackReturn on_shutdown(const rclcpp_lifecycle::State&) override;
81 
84 
87 
90 
92  virtual void do_autostart_callback(){};
93 
94  // Configuration points for extra steps needed for the different AMCL variants.
95 
97  virtual void do_configure([[maybe_unused]] const rclcpp_lifecycle::State& state) {}
99  virtual void do_deactivate([[maybe_unused]] const rclcpp_lifecycle::State& state) {}
101  virtual void do_shutdown([[maybe_unused]] const rclcpp_lifecycle::State& state) {}
103  virtual void do_cleanup([[maybe_unused]] const rclcpp_lifecycle::State& state) {}
105  virtual void do_activate([[maybe_unused]] const rclcpp_lifecycle::State& state) {}
107  virtual void do_periodic_timer_callback() {}
110  [[maybe_unused]] geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr message) {}
111 
113  void initial_pose_callback(geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr message);
114 
116  rclcpp_lifecycle::LifecyclePublisher<geometry_msgs::msg::PoseArray>::SharedPtr particle_cloud_pub_;
118  rclcpp_lifecycle::LifecyclePublisher<visualization_msgs::msg::MarkerArray>::SharedPtr particle_markers_pub_;
120  rclcpp_lifecycle::LifecyclePublisher<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr pose_pub_;
122  std::unique_ptr<bond::Bond> bond_;
124  rclcpp::TimerBase::SharedPtr timer_;
126  std::unique_ptr<tf2_ros::Buffer> tf_buffer_;
128  std::unique_ptr<tf2_ros::TransformBroadcaster> tf_broadcaster_;
130  std::unique_ptr<tf2_ros::TransformListener> tf_listener_;
132  rclcpp::TimerBase::SharedPtr autostart_timer_;
134  rclcpp::CallbackGroup::SharedPtr common_callback_group_;
136  rclcpp::SubscriptionOptions common_subscription_options_;
138  rclcpp::Subscription<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr initial_pose_sub_;
139 };
140 } // namespace beluga_amcl
141 #endif
Definition: ros2_common.hpp:55
void autostart_callback()
Callback for the autostart timer.
virtual void do_activate([[maybe_unused]] const rclcpp_lifecycle::State &state)
Extra steps for the on_activate callback. Defaults to no-op.
Definition: ros2_common.hpp:105
std::unique_ptr< bond::Bond > bond_
Node bond with the lifecycle manager.
Definition: ros2_common.hpp:122
virtual void do_configure([[maybe_unused]] const rclcpp_lifecycle::State &state)
Extra steps for the on_configure callback. Defaults to no-op.
Definition: ros2_common.hpp:97
CallbackReturn on_configure(const rclcpp_lifecycle::State &) override
Callback for lifecycle transitions from the UNCONFIGURED state to the INACTIVE state.
virtual void do_shutdown([[maybe_unused]] const rclcpp_lifecycle::State &state)
Extra steps for the on_shutdown callback. Defaults to no-op.
Definition: ros2_common.hpp:101
virtual void do_deactivate([[maybe_unused]] const rclcpp_lifecycle::State &state)
Extra steps for the on_deactivate callback. Defaults to no-op.
Definition: ros2_common.hpp:99
std::unique_ptr< tf2_ros::TransformListener > tf_listener_
Transforms listener.
Definition: ros2_common.hpp:130
virtual void do_periodic_timer_callback()
Extra steps for the periodic updates timer callback events. Defaults to no-op.
Definition: ros2_common.hpp:107
rclcpp::SubscriptionOptions common_subscription_options_
Common subscription options.
Definition: ros2_common.hpp:136
rclcpp_lifecycle::LifecyclePublisher< visualization_msgs::msg::MarkerArray >::SharedPtr particle_markers_pub_
Particle markers publisher.
Definition: ros2_common.hpp:118
CallbackReturn on_activate(const rclcpp_lifecycle::State &) override
Callback for lifecycle transitions from the INACTIVE state to the ACTIVE state.
CallbackReturn on_cleanup(const rclcpp_lifecycle::State &) override
Callback for lifecycle transitions from the INACTIVE state to the UNCONFIGURED state.
CallbackReturn on_shutdown(const rclcpp_lifecycle::State &) override
Callback for lifecycle transitions from most states to the FINALIZED state.
virtual void do_initial_pose_callback([[maybe_unused]] geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr message)
Extra steps for (re)initialization messages.
Definition: ros2_common.hpp:109
rclcpp_lifecycle::LifecyclePublisher< geometry_msgs::msg::PoseArray >::SharedPtr particle_cloud_pub_
Particle cloud publisher.
Definition: ros2_common.hpp:116
rclcpp::TimerBase::SharedPtr timer_
Timer for periodic particle cloud updates.
Definition: ros2_common.hpp:124
CallbackReturn on_deactivate(const rclcpp_lifecycle::State &) override
Callback for lifecycle transitions from the ACTIVE state to the INACTIVE state.
auto get_execution_policy() const -> ExecutionPolicyVariant
Get execution policy from parameters.
std::unique_ptr< tf2_ros::TransformBroadcaster > tf_broadcaster_
Transforms broadcaster.
Definition: ros2_common.hpp:128
virtual void do_cleanup([[maybe_unused]] const rclcpp_lifecycle::State &state)
Extra steps for the on_cleanup callback. Defaults to no-op.
Definition: ros2_common.hpp:103
std::unique_ptr< tf2_ros::Buffer > tf_buffer_
Transforms buffer.
Definition: ros2_common.hpp:126
void initial_pose_callback(geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr message)
Callback for (re)initialization messages.
~BaseAMCLNode() override
Destructor for the base AMCL node.
rclcpp::CallbackGroup::SharedPtr common_callback_group_
Common mutually exclusive callback group.
Definition: ros2_common.hpp:134
rclcpp::Subscription< geometry_msgs::msg::PoseWithCovarianceStamped >::SharedPtr initial_pose_sub_
Pose (re)initialization subscription.
Definition: ros2_common.hpp:138
rclcpp::TimerBase::SharedPtr autostart_timer_
Timer for node to configure and activate itself.
Definition: ros2_common.hpp:132
rclcpp_lifecycle::LifecyclePublisher< geometry_msgs::msg::PoseWithCovarianceStamped >::SharedPtr pose_pub_
Estimated pose publisher.
Definition: ros2_common.hpp:120
virtual void do_autostart_callback()
User provided extra steps for the autostart process.
Definition: ros2_common.hpp:92
void periodic_timer_callback()
Callback for the periodic particle updates.
BaseAMCLNode(const std::string &node_name="amcl", const std::string &node_namespace="", const rclcpp::NodeOptions &node_options=rclcpp::NodeOptions{})
Constructor, following LifecycleNode signature.
std::variant< std::execution::sequenced_policy, std::execution::parallel_policy > ExecutionPolicyVariant
Supported execution policies.
Definition: ndt_amcl_node.hpp:100