Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
costmap_2d_publisher.hpp
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, 2013, Willow Garage, Inc.
6  * Copyright (c) 2019, Samsung Research America, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Author: Eitan Marder-Eppstein
37  * David V. Lu!!
38  *********************************************************************/
39 #ifndef NAV2_COSTMAP_2D__COSTMAP_2D_PUBLISHER_HPP_
40 #define NAV2_COSTMAP_2D__COSTMAP_2D_PUBLISHER_HPP_
41 
42 #include <algorithm>
43 #include <atomic>
44 #include <string>
45 #include <memory>
46 
47 #include "rclcpp_lifecycle/lifecycle_node.hpp"
48 #include "nav2_costmap_2d/costmap_2d.hpp"
49 #include "nav_msgs/msg/occupancy_grid.hpp"
50 #include "map_msgs/msg/occupancy_grid_update.hpp"
51 #include "nav2_msgs/msg/costmap.hpp"
52 #include "nav2_msgs/msg/costmap_update.hpp"
53 #include "nav2_msgs/srv/get_costmap.hpp"
54 #include "tf2/transform_datatypes.hpp"
55 #include "nav2_ros_common/lifecycle_node.hpp"
56 #include "tf2/LinearMath/Quaternion.hpp"
57 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
58 #include "nav2_ros_common/service_server.hpp"
59 
60 namespace nav2_costmap_2d
61 {
67 {
68 public:
73  const nav2::LifecycleNode::WeakPtr & parent,
74  Costmap2D * costmap,
75  std::string global_frame,
76  std::string topic_name,
77  bool always_send_full_costmap = false,
78  double map_vis_z = 0.0);
79 
84 
88  void on_configure() {}
89 
93  void on_activate()
94  {
95  costmap_pub_->on_activate();
96  costmap_update_pub_->on_activate();
97  costmap_raw_pub_->on_activate();
98  costmap_raw_update_pub_->on_activate();
99  }
100 
105  {
106  costmap_pub_->on_deactivate();
107  costmap_update_pub_->on_deactivate();
108  costmap_raw_pub_->on_deactivate();
109  costmap_raw_update_pub_->on_deactivate();
110  }
111 
115  void on_cleanup() {}
116 
118  void updateBounds(unsigned int x0, unsigned int xn, unsigned int y0, unsigned int yn)
119  {
120  x0_ = std::min(x0, x0_);
121  xn_ = std::max(xn, xn_);
122  y0_ = std::min(y0, y0_);
123  yn_ = std::max(yn, yn_);
124  }
125 
130  void publishCostmap();
131 
133  bool isRepublishRequested() const
134  {
135  return republish_costmap_.load();
136  }
137 
138 private:
140  void prepareGrid();
141  void prepareCostmap();
142 
144  std::unique_ptr<map_msgs::msg::OccupancyGridUpdate> createGridUpdateMsg();
146  std::unique_ptr<nav2_msgs::msg::CostmapUpdate> createCostmapUpdateMsg();
147 
148  void updateGridParams();
149 
151  void costmap_service_callback(
152  const std::shared_ptr<rmw_request_id_t> request_header,
153  const std::shared_ptr<nav2_msgs::srv::GetCostmap::Request> request,
154  const std::shared_ptr<nav2_msgs::srv::GetCostmap::Response> response);
155 
156  rclcpp::Clock::SharedPtr clock_;
157  rclcpp::Logger logger_{rclcpp::get_logger("nav2_costmap_2d")};
158 
159  Costmap2D * costmap_;
160  std::string global_frame_;
161  std::string topic_name_;
162  unsigned int x0_{0};
163  unsigned int xn_{0};
164  unsigned int y0_{0};
165  unsigned int yn_{0};
166  double saved_origin_x_{0.0};
167  double saved_origin_y_{0.0};
168  bool always_send_full_costmap_{false};
169  bool costmap_published_once_{false};
170  std::atomic<bool> republish_costmap_{false};
171  double map_vis_z_{0.0};
172 
173  // Publisher for translated costmap values as msg::OccupancyGrid used in visualization
174  nav2::Publisher<nav_msgs::msg::OccupancyGrid>::SharedPtr costmap_pub_;
175  nav2::Publisher<map_msgs::msg::OccupancyGridUpdate>::SharedPtr
176  costmap_update_pub_;
177 
178  // Publisher for raw costmap values as msg::Costmap from layered costmap
179  nav2::Publisher<nav2_msgs::msg::Costmap>::SharedPtr costmap_raw_pub_;
180  nav2::Publisher<nav2_msgs::msg::CostmapUpdate>::SharedPtr
181  costmap_raw_update_pub_;
182 
183  // Service for getting the costmaps
184  nav2::ServiceServer<nav2_msgs::srv::GetCostmap>::SharedPtr
185  costmap_service_;
186 
187  float grid_resolution_{0.0};
188  unsigned int grid_width_{0};
189  unsigned int grid_height_{0};
190  std::unique_ptr<nav_msgs::msg::OccupancyGrid> grid_;
191  std::unique_ptr<nav2_msgs::msg::Costmap> costmap_raw_;
192  // Translate from 0-255 values in costmap to -1 to 100 values in message.
193  static char * cost_translation_table_;
194 };
195 
196 } // namespace nav2_costmap_2d
197 
198 #endif // NAV2_COSTMAP_2D__COSTMAP_2D_PUBLISHER_HPP_
A tool to periodically publish visualization data from a Costmap2D.
Costmap2DPublisher(const nav2::LifecycleNode::WeakPtr &parent, Costmap2D *costmap, std::string global_frame, std::string topic_name, bool always_send_full_costmap=false, double map_vis_z=0.0)
Constructor for the Costmap2DPublisher.
bool isRepublishRequested() const
Whether a new subscriber has requested a full costmap publication.
void publishCostmap()
Publishes the visualization data over ROS.
void updateBounds(unsigned int x0, unsigned int xn, unsigned int y0, unsigned int yn)
Include the given bounds in the changed-rectangle.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69