Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
costmap_2d_ros.hpp
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, 2013, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Eitan Marder-Eppstein
36  * David V. Lu!!
37  *********************************************************************/
38 #ifndef NAV2_COSTMAP_2D__COSTMAP_2D_ROS_HPP_
39 #define NAV2_COSTMAP_2D__COSTMAP_2D_ROS_HPP_
40 
41 #include <atomic>
42 #include <memory>
43 #include <string>
44 #include <vector>
45 
46 #include "geometry_msgs/msg/polygon.hpp"
47 #include "geometry_msgs/msg/polygon_stamped.hpp"
48 #include "nav2_costmap_2d/costmap_2d_publisher.hpp"
49 #include "nav2_costmap_2d/footprint.hpp"
50 #include "nav2_costmap_2d/footprint_collision_checker.hpp"
51 #include "nav2_costmap_2d/clear_costmap_service.hpp"
52 #include "nav2_costmap_2d/layered_costmap.hpp"
53 #include "nav2_costmap_2d/layer.hpp"
54 #include "nav2_ros_common/lifecycle_node.hpp"
55 #include "nav2_msgs/srv/get_costs.hpp"
56 #include "pluginlib/class_loader.hpp"
57 #include "tf2/convert.hpp"
58 #include "tf2/LinearMath/Transform.hpp"
59 #include "nav2_ros_common/tf2_factories.hpp"
60 #include "tf2/time.hpp"
61 #include "tf2/transform_datatypes.hpp"
62 #include "nav2_ros_common/service_server.hpp"
63 #include "tf2/utils.hpp"
64 
65 namespace nav2_costmap_2d
66 {
67 
72 {
73 public:
78  explicit Costmap2DROS(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
79 
86  explicit Costmap2DROS(
87  const std::string & name,
88  const std::string & parent_namespace = "/",
89  const bool & use_sim_time = false,
90  const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
91 
95  void init();
96 
100  ~Costmap2DROS();
101 
105  nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
106 
110  nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
111 
115  nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
116 
120  nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
121 
125  nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
126 
132  void on_rcl_preshutdown() override
133  {
135  // Transitioning handled by parent node
136  return;
137  }
138 
139  // Else, if this is an independent node, this node needs to handle itself.
140  RCLCPP_INFO(
141  get_logger(), "Running Nav2 LifecycleNode rcl preshutdown (%s)",
142  this->get_name());
143 
144  runCleanups();
145 
146  destroyBond();
147  }
148 
154  void start();
155 
159  void stop();
160 
164  void pause();
165 
169  void resume();
170 
174  void updateMap();
175 
179  void resetLayers();
180 
182  bool isCurrent()
183  {
184  return layered_costmap_->isCurrent();
185  }
186 
192  void waitUntilCurrent(const rclcpp::Duration & timeout);
193 
199  bool getRobotPose(geometry_msgs::msg::PoseStamped & global_pose);
200 
208  const geometry_msgs::msg::PoseStamped & input_pose,
209  geometry_msgs::msg::PoseStamped & transformed_pose);
210 
212  std::string getName() const
213  {
214  return name_;
215  }
216 
218  double getTransformTolerance() const
219  {
220  return transform_tolerance_;
221  }
222 
229  {
230  return layered_costmap_->getCostmap();
231  }
232 
237  std::string getGlobalFrameID()
238  {
239  return global_frame_;
240  }
241 
246  std::string getBaseFrameID()
247  {
248  return robot_base_frame_;
249  }
250 
255  {
256  return layered_costmap_.get();
257  }
258 
260  geometry_msgs::msg::Polygon getRobotFootprintPolygon()
261  {
262 #ifdef __cpp_lib_atomic_shared_ptr
263  return nav2_costmap_2d::toPolygon(*padded_footprint_.load());
264 #else
265  return nav2_costmap_2d::toPolygon(*std::atomic_load(&padded_footprint_));
266 #endif
267  }
268 
277  std::vector<geometry_msgs::msg::Point> getRobotFootprint()
278  {
279 #ifdef __cpp_lib_atomic_shared_ptr
280  return *padded_footprint_.load();
281 #else
282  return *std::atomic_load(&padded_footprint_);
283 #endif
284  }
285 
293  std::vector<geometry_msgs::msg::Point> getUnpaddedRobotFootprint()
294  {
295 #ifdef __cpp_lib_atomic_shared_ptr
296  return *unpadded_footprint_.load();
297 #else
298  return *std::atomic_load(&unpadded_footprint_);
299 #endif
300  }
301 
306  void getOrientedFootprint(std::vector<geometry_msgs::msg::Point> & oriented_footprint);
307 
318  void setRobotFootprint(const std::vector<geometry_msgs::msg::Point> & points);
319 
330  void setRobotFootprintPolygon(const geometry_msgs::msg::Polygon & footprint);
331 
332  nav2::TransformBuffer::SharedPtr getTfBuffer() {return tf_buffer_;}
333 
340  bool getUseRadius() {return use_radius_;}
341 
348  double getRobotRadius() {return robot_radius_;}
349 
354  void getCostsCallback(
355  const std::shared_ptr<rmw_request_id_t>,
356  const std::shared_ptr<nav2_msgs::srv::GetCosts::Request> request,
357  const std::shared_ptr<nav2_msgs::srv::GetCosts::Response> response);
358 
359 protected:
360  // Publishers and subscribers
361  nav2::Publisher<geometry_msgs::msg::PolygonStamped>::SharedPtr
362  footprint_pub_;
363  std::unique_ptr<Costmap2DPublisher> costmap_publisher_;
364 
365  std::vector<std::unique_ptr<Costmap2DPublisher>> layer_publishers_;
366 
367  nav2::Subscription<geometry_msgs::msg::Polygon>::SharedPtr footprint_sub_;
368  nav2::Subscription<geometry_msgs::msg::PolygonStamped>::SharedPtr footprint_stamped_sub_;
369 
370  // Dedicated callback group and executor for tf timer_interface and message filter
371  rclcpp::CallbackGroup::SharedPtr callback_group_;
372  rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
373  std::unique_ptr<nav2::NodeThread> executor_thread_;
374 
375  // Transform listener
376  nav2::TransformBuffer::SharedPtr tf_buffer_;
377  nav2::TransformListener::SharedPtr tf_listener_;
378 
379  std::unique_ptr<LayeredCostmap> layered_costmap_{nullptr};
380  std::string name_;
381 
385  void mapUpdateLoop(double frequency);
386  std::atomic<bool> map_update_thread_shutdown_{false}; // [AI generated]
387  std::atomic<bool> stop_updates_{false};
388  std::atomic<bool> initialized_{false};
389  std::atomic<bool> stopped_{true};
390  std::mutex _dynamic_parameter_mutex;
391  std::unique_ptr<std::thread> map_update_thread_;
392  rclcpp::Time last_publish_{0, 0, RCL_ROS_TIME};
393  rclcpp::Duration publish_cycle_{1, 0};
394  pluginlib::ClassLoader<Layer> plugin_loader_{"nav2_costmap_2d", "nav2_costmap_2d::Layer"};
395 
399  void getParameters();
400  bool always_send_full_costmap_{false};
401  std::string footprint_;
402  float footprint_padding_{0};
403  std::string global_frame_;
404  int map_height_meters_{0};
405  double map_publish_frequency_{0};
406  double map_update_frequency_{0};
407  int map_width_meters_{0};
408  double origin_x_{0};
409  double origin_y_{0};
410  std::vector<std::string> default_plugins_;
411  std::vector<std::string> default_types_;
412  std::vector<std::string> plugin_names_;
413  std::vector<std::string> plugin_types_;
414  std::vector<std::string> filter_names_;
415  std::vector<std::string> filter_types_;
416  double resolution_{0};
417  std::string robot_base_frame_;
418  double robot_radius_;
419  bool rolling_window_{false};
420  bool track_unknown_space_{false};
423  double map_vis_z_{0};
426 
428 
429  // Derived parameters
430  bool use_radius_{false};
431 #ifdef __cpp_lib_atomic_shared_ptr
432  std::atomic<std::shared_ptr<std::vector<geometry_msgs::msg::Point>>> unpadded_footprint_{
433  std::make_shared<std::vector<geometry_msgs::msg::Point>>()};
434  std::atomic<std::shared_ptr<std::vector<geometry_msgs::msg::Point>>> padded_footprint_{
435  std::make_shared<std::vector<geometry_msgs::msg::Point>>()};
436 #else
437  std::shared_ptr<std::vector<geometry_msgs::msg::Point>> unpadded_footprint_{
438  std::make_shared<std::vector<geometry_msgs::msg::Point>>()};
439  std::shared_ptr<std::vector<geometry_msgs::msg::Point>> padded_footprint_{
440  std::make_shared<std::vector<geometry_msgs::msg::Point>>()};
441 #endif
442 
443  // Services
444  nav2::ServiceServer<nav2_msgs::srv::GetCosts>::SharedPtr get_cost_service_;
445  std::unique_ptr<ClearCostmapService> clear_costmap_service_;
446 
447  // Dynamic parameters handler
448  PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
449  OnSetParametersCallbackHandle::SharedPtr on_set_params_handler;
450 
459  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
460  const std::vector<rclcpp::Parameter> & parameters);
461 
468  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
469 };
470 
471 // free functions
472 
477 rclcpp::NodeOptions getChildNodeOptions(
478  const std::string & name,
479  const std::string & parent_namespace,
480  const bool & use_sim_time,
481  const rclcpp::NodeOptions & parent_options);
482 
483 } // namespace nav2_costmap_2d
484 
485 #endif // NAV2_COSTMAP_2D__COSTMAP_2D_ROS_HPP_
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
void destroyBond()
Destroy bond connection to lifecycle manager.
A ROS wrapper for a 2D Costmap. Handles subscribing to topics that provide observations about obstacl...
Costmap2DROS(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
Constructor for the wrapper.
nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Cleanup node.
void mapUpdateLoop(double frequency)
Function on timer for costmap update.
void getOrientedFootprint(std::vector< geometry_msgs::msg::Point > &oriented_footprint)
Build the oriented footprint of the robot at the robot's current pose.
bool getRobotPose(geometry_msgs::msg::PoseStamped &global_pose)
Get the pose of the robot in the global frame of the costmap.
void getParameters()
Get parameters for node.
geometry_msgs::msg::Polygon getRobotFootprintPolygon()
Returns the current padded footprint as a geometry_msgs::msg::Polygon.
void pause()
Stops the costmap from updating, but sensor data still comes in over the wire.
void getCostsCallback(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< nav2_msgs::srv::GetCosts::Request > request, const std::shared_ptr< nav2_msgs::srv::GetCosts::Response > response)
Get the cost at a point in costmap.
nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Configure node.
bool getUseRadius()
Get the costmap's use_radius_ parameter, corresponding to whether the footprint for the robot is a ci...
nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivate node.
std::string getName() const
Returns costmap name.
rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(const std::vector< rclcpp::Parameter > &parameters)
Validate incoming parameter updates before applying them. This callback is triggered when one or more...
nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activate node.
double transform_tolerance_
The timeout before transform errors.
bool rolling_window_
Whether to use a rolling window version of the costmap.
LayeredCostmap * getLayeredCostmap()
Get the layered costmap object used in the node.
void on_rcl_preshutdown() override
as a child-LifecycleNode : Costmap2DROS may be launched by another Lifecycle Node as a composed modul...
void resume()
Resumes costmap updates.
double initial_transform_timeout_
The timeout before activation of the node errors.
void updateMap()
Update the map with the layered costmap / plugins.
std::string getBaseFrameID()
Returns the local frame of the costmap.
std::vector< geometry_msgs::msg::Point > getRobotFootprint()
Return the current footprint of the robot as a vector of points.
void setRobotFootprint(const std::vector< geometry_msgs::msg::Point > &points)
Set the footprint of the robot to be the given set of points, padded by footprint_padding.
void resetLayers()
Reset each individual layer.
bool transformPoseToGlobalFrame(const geometry_msgs::msg::PoseStamped &input_pose, geometry_msgs::msg::PoseStamped &transformed_pose)
Transform the input_pose in the global frame of the costmap.
std::string global_frame_
The global frame for the costmap.
std::string getGlobalFrameID()
Returns the global frame of the costmap.
void start()
Subscribes to sensor topics if necessary and starts costmap updates, can be called to restart the cos...
void stop()
Stops costmap updates and unsubscribes from sensor topics.
std::string robot_base_frame_
The frame_id of the robot base.
bool subscribe_to_stamped_footprint_
If true, the footprint subscriber expects a PolygonStamped msg.
bool isCurrent()
Same as getLayeredCostmap()->isCurrent().
void updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
double getTransformTolerance() const
Returns the delay in transform (tf) data that is tolerable in seconds.
double getRobotRadius()
Get the costmap's robot_radius_ parameter, corresponding to raidus of the robot footprint when it is ...
void waitUntilCurrent(const rclcpp::Duration &timeout)
Wait for the costmap to become current after updates or parameter changes.
void setRobotFootprintPolygon(const geometry_msgs::msg::Polygon &footprint)
Set the footprint of the robot to be the given polygon, padded by footprint_padding.
std::unique_ptr< std::thread > map_update_thread_
A thread for updating the map.
Costmap2D * getCostmap()
Return a pointer to the "master" costmap which receives updates from all the layers.
void init()
Common initialization for constructors.
std::vector< geometry_msgs::msg::Point > getUnpaddedRobotFootprint()
Return the current unpadded footprint of the robot as a vector of points.
bool is_lifecycle_follower_
whether is a child-LifecycleNode or an independent node
nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
shutdown node
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
Instantiates different layer plugins and aggregates them into one score.
geometry_msgs::msg::Polygon toPolygon(const std::vector< geometry_msgs::msg::Point > &pts)
Convert vector of Points to Polygon msg.
Definition: footprint.cpp:92
rclcpp::NodeOptions getChildNodeOptions(const std::string &name, const std::string &parent_namespace, const bool &use_sim_time, const rclcpp::NodeOptions &parent_options)
Given the node options of a parent node, expands of replaces the fields for the node name,...