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

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

Beluga AMCL: /home/runner/work/beluga/beluga/beluga_amcl/include/beluga_amcl/amcl_nodelet.hpp Source File
Beluga AMCL
amcl_nodelet.hpp
Go to the documentation of this file.
1 // Copyright 2023-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_AMCL_NODELET_HPP
16 #define BELUGA_AMCL_AMCL_NODELET_HPP
17 
18 #include <nodelet/nodelet.h>
19 #include <ros/ros.h>
20 
21 #include <diagnostic_updater/diagnostic_updater.h>
22 #include <dynamic_reconfigure/server.h>
23 
24 #include <message_filters/subscriber.h>
25 
26 #include <tf2_ros/buffer.h>
27 #include <tf2_ros/message_filter.h>
28 #include <tf2_ros/transform_broadcaster.h>
29 #include <tf2_ros/transform_listener.h>
30 
31 #include <geometry_msgs/PoseWithCovarianceStamped.h>
32 #include <nav_msgs/GetMap.h>
33 #include <nav_msgs/OccupancyGrid.h>
34 #include <nav_msgs/SetMap.h>
35 #include <sensor_msgs/LaserScan.h>
36 #include <std_srvs/Empty.h>
37 
38 #include <memory>
39 #include <mutex>
40 #include <utility>
41 
42 #include <sophus/se2.hpp>
43 
44 #include <beluga_amcl/AmclConfig.h>
45 #include <beluga_ros/amcl.hpp>
46 
52 namespace beluga_amcl {
53 
55 class AmclNodelet : public nodelet::Nodelet {
56  public:
57  AmclNodelet() = default;
58  ~AmclNodelet() override = default;
59 
60  protected:
62  void onInit() override;
63 
65  auto get_initial_estimate() const -> std::optional<std::pair<Sophus::SE2d, Eigen::Matrix3d>>;
66 
68  auto get_motion_model(std::string_view) const -> beluga_ros::Amcl::motion_model_variant;
69 
71  auto get_sensor_model(std::string_view, const nav_msgs::OccupancyGrid::ConstPtr&) const
72  -> beluga_ros::Amcl::sensor_model_variant;
73 
75  static auto get_execution_policy(std::string_view) -> beluga_ros::Amcl::execution_policy_variant;
76 
78  auto make_particle_filter(const nav_msgs::OccupancyGrid::ConstPtr&) const -> std::unique_ptr<beluga_ros::Amcl>;
79 
81  void config_callback(beluga_amcl::AmclConfig& config, uint32_t level);
82 
84  void map_callback(const nav_msgs::OccupancyGrid::ConstPtr& message);
85 
87  void map_timer_callback(const ros::TimerEvent& ev);
88 
90  bool set_map_callback(nav_msgs::SetMap::Request& request, nav_msgs::SetMap::Response& response);
91 
93 
96  void handle_map_with_default_initial_pose(const nav_msgs::OccupancyGrid::ConstPtr& map);
97 
99  void particle_cloud_timer_callback(const ros::TimerEvent& ev);
100 
102  void laser_callback(const sensor_msgs::LaserScan::ConstPtr& laser_scan);
103 
105  void initial_pose_callback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& message);
106 
108  bool global_localization_callback(std_srvs::Empty::Request& request, std_srvs::Empty::Response& response);
109 
111  bool nomotion_update_callback(std_srvs::Empty::Request& request, std_srvs::Empty::Response& response);
112 
114  void save_pose_timer_callback(const ros::TimerEvent& ev);
115 
117 
125  bool initialize_from_estimate(const std::pair<Sophus::SE2d, Eigen::Matrix3d>& estimate);
126 
128 
136 
138  void update_covariance_diagnostics(diagnostic_updater::DiagnosticStatusWrapper& status);
139 
141  mutable std::mutex mutex_;
142 
146  ros::Publisher particle_cloud_pub_;
148  ros::Publisher pose_pub_;
150  ros::Timer save_pose_timer_;
152  ros::Subscriber initial_pose_sub_;
154  ros::Subscriber map_sub_;
155 
157  ros::Timer map_timer_;
159  ros::ServiceServer set_map_server_;
161  ros::ServiceClient get_map_client_;
163  ros::ServiceServer global_localization_server_;
165  ros::ServiceServer nomotion_update_server_;
166 
168  bool configured_{false};
170  beluga_amcl::AmclConfig config_;
172  beluga_amcl::AmclConfig default_config_;
174  using AmclConfigServer = dynamic_reconfigure::Server<beluga_amcl::AmclConfig>;
176  std::unique_ptr<AmclConfigServer> config_server_;
177 
179  std::unique_ptr<tf2_ros::Buffer> tf_buffer_;
181  std::unique_ptr<tf2_ros::TransformBroadcaster> tf_broadcaster_;
183  std::unique_ptr<tf2_ros::TransformListener> tf_listener_;
184 
186  diagnostic_updater::Updater diagnosic_updater_;
187 
189  message_filters::Subscriber<sensor_msgs::LaserScan> laser_scan_sub_;
191  std::unique_ptr<tf2_ros::MessageFilter<sensor_msgs::LaserScan>> laser_scan_filter_;
193  message_filters::Connection laser_scan_connection_;
194 
196  std::unique_ptr<beluga_ros::Amcl> particle_filter_;
198  std::optional<std::pair<Sophus::SE2d, Eigen::Matrix3d>> last_known_estimate_;
200  std::optional<Sophus::SE2d> last_known_odom_transform_in_map_;
202  nav_msgs::OccupancyGrid::ConstPtr last_known_map_;
204  bool enable_tf_broadcast_{false};
205 };
206 
207 } // namespace beluga_amcl
208 
209 #endif // BELUGA_AMCL_AMCL_NODELET_HPP
2D AMCL as a ROS (1) nodelet.
Definition: amcl_nodelet.hpp:55
bool set_map_callback(nav_msgs::SetMap::Request &request, nav_msgs::SetMap::Response &response)
Callback for the map update service.
bool initialize_from_estimate(const std::pair< Sophus::SE2d, Eigen::Matrix3d > &estimate)
Initialize particles from an estimated pose and covariance.
void handle_map_with_default_initial_pose(const nav_msgs::OccupancyGrid::ConstPtr &map)
Particle filter (re)initialization helper method.
ros::ServiceClient get_map_client_
Map initialization service client.
Definition: amcl_nodelet.hpp:161
std::unique_ptr< tf2_ros::Buffer > tf_buffer_
Transforms buffer.
Definition: amcl_nodelet.hpp:179
std::unique_ptr< tf2_ros::MessageFilter< sensor_msgs::LaserScan > > laser_scan_filter_
Transform synchronization filter for laser scan updates.
Definition: amcl_nodelet.hpp:191
ros::Timer map_timer_
Timer for repeated map initialization requests.
Definition: amcl_nodelet.hpp:157
ros::Publisher particle_cloud_pub_
Particle cloud publisher.
Definition: amcl_nodelet.hpp:146
ros::ServiceServer nomotion_update_server_
No motion update service server.
Definition: amcl_nodelet.hpp:165
void onInit() override
Callback for nodelet initialization.
diagnostic_updater::Updater diagnosic_updater_
Diagnostics updater.
Definition: amcl_nodelet.hpp:186
ros::Subscriber map_sub_
Occupancy grid map updates subscriber.
Definition: amcl_nodelet.hpp:154
ros::Publisher pose_pub_
Estimated pose publisher.
Definition: amcl_nodelet.hpp:148
auto get_sensor_model(std::string_view, const nav_msgs::OccupancyGrid::ConstPtr &) const -> beluga_ros::Amcl::sensor_model_variant
Get sensor model as per current parametrization.
bool nomotion_update_callback(std_srvs::Empty::Request &request, std_srvs::Empty::Response &response)
Callback for the no motion update service.
auto get_initial_estimate() const -> std::optional< std::pair< Sophus::SE2d, Eigen::Matrix3d >>
Get initial pose estimate from parameters if set.
dynamic_reconfigure::Server< beluga_amcl::AmclConfig > AmclConfigServer
Type alias for a dynamic_reconfigure::Server bound to beluga_amcl configuration.
Definition: amcl_nodelet.hpp:174
void initial_pose_callback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &message)
Callback for pose (re)initialization.
void map_callback(const nav_msgs::OccupancyGrid::ConstPtr &message)
Callback for occupancy grid map updates.
bool global_localization_callback(std_srvs::Empty::Request &request, std_srvs::Empty::Response &response)
Callback for the global relocalization service.
void config_callback(beluga_amcl::AmclConfig &config, uint32_t level)
Callback for dynamic_reconfigure updates.
ros::Subscriber initial_pose_sub_
Pose (re)initialization subscriber.
Definition: amcl_nodelet.hpp:152
void save_pose_timer_callback(const ros::TimerEvent &ev)
Callback for periodic pose saving.
ros::ServiceServer set_map_server_
Map update service server.
Definition: amcl_nodelet.hpp:159
void laser_callback(const sensor_msgs::LaserScan::ConstPtr &laser_scan)
Callback for laser scan updates.
std::unique_ptr< tf2_ros::TransformBroadcaster > tf_broadcaster_
Transforms broadcaster.
Definition: amcl_nodelet.hpp:181
std::mutex mutex_
Mutex for particle filter access.
Definition: amcl_nodelet.hpp:141
std::unique_ptr< AmclConfigServer > config_server_
beluga_amcl configuration server.
Definition: amcl_nodelet.hpp:176
bool configured_
Flag set on first configuration.
Definition: amcl_nodelet.hpp:168
std::optional< Sophus::SE2d > last_known_odom_transform_in_map_
Last known map to odom correction estimate, if any.
Definition: amcl_nodelet.hpp:200
ros::ServiceServer global_localization_server_
Global relocalization service server.
Definition: amcl_nodelet.hpp:163
beluga_amcl::AmclConfig default_config_
Default beluga_amcl configuration.
Definition: amcl_nodelet.hpp:172
void update_covariance_diagnostics(diagnostic_updater::DiagnosticStatusWrapper &status)
Callback for estimated pose covariance diagnostics.
std::optional< std::pair< Sophus::SE2d, Eigen::Matrix3d > > last_known_estimate_
Last known pose estimate, if any.
Definition: amcl_nodelet.hpp:198
message_filters::Connection laser_scan_connection_
Connection for laser scan updates filter and callback.
Definition: amcl_nodelet.hpp:193
ros::Timer save_pose_timer_
Timer for pose saving.
Definition: amcl_nodelet.hpp:150
std::unique_ptr< beluga_ros::Amcl > particle_filter_
Particle filter instance.
Definition: amcl_nodelet.hpp:196
void particle_cloud_timer_callback(const ros::TimerEvent &ev)
Callback for periodic particle cloud updates.
bool enable_tf_broadcast_
Whether to broadcast transforms or not.
Definition: amcl_nodelet.hpp:204
bool initialize_from_map()
Initialize particles from map.
message_filters::Subscriber< sensor_msgs::LaserScan > laser_scan_sub_
Laser scan updates subscriber.
Definition: amcl_nodelet.hpp:189
nav_msgs::OccupancyGrid::ConstPtr last_known_map_
Last known occupancy grid map.
Definition: amcl_nodelet.hpp:202
auto make_particle_filter(const nav_msgs::OccupancyGrid::ConstPtr &) const -> std::unique_ptr< beluga_ros::Amcl >
Instantiate particle filter given an initial occupancy grid map and the current parametrization.
auto get_motion_model(std::string_view) const -> beluga_ros::Amcl::motion_model_variant
Get motion model as per current parametrization.
void map_timer_callback(const ros::TimerEvent &ev)
Callback for repeated map initialization requests.
std::unique_ptr< tf2_ros::TransformListener > tf_listener_
Transforms listener.
Definition: amcl_nodelet.hpp:183
static auto get_execution_policy(std::string_view) -> beluga_ros::Amcl::execution_policy_variant
Get execution policy given its name.
beluga_amcl::AmclConfig config_
Current beluga_amcl configuration.
Definition: amcl_nodelet.hpp:170
ros::Timer particle_cloud_timer_
Timer for periodic particle cloud updates.
Definition: amcl_nodelet.hpp:144