19 #include "nav2_costmap_2d/costmap_subscriber.hpp"
26 if (!isCostmapReceived()) {
27 throw std::runtime_error(
"Costmap is not available");
29 processCurrentCostmapMsg();
35 std::lock_guard<std::recursive_mutex> lock(costmap_msg_mutex_);
37 frame_id_ = costmap_msg_->header.frame_id;
39 if (!isCostmapReceived()) {
40 costmap_ = std::make_shared<Costmap2D>(
41 msg->metadata.size_x, msg->metadata.size_y,
42 msg->metadata.resolution, msg->metadata.origin.position.x,
43 msg->metadata.origin.position.y);
45 processCurrentCostmapMsg();
50 const nav2_msgs::msg::CostmapUpdate::ConstSharedPtr & update_msg)
52 if (isCostmapReceived()) {
53 processCurrentCostmapMsg();
55 std::lock_guard<Costmap2D::mutex_t> lock(*(costmap_->getMutex()));
57 auto map_cell_size_x = costmap_->getSizeInCellsX();
58 auto map_call_size_y = costmap_->getSizeInCellsY();
60 if (map_cell_size_x < update_msg->x + update_msg->size_x ||
61 map_call_size_y < update_msg->y + update_msg->size_y)
64 logger_,
"Update area outside of original map area. Costmap bounds: %d X %d, "
65 "Update origin: %d, %d bounds: %d X %d", map_cell_size_x, map_call_size_y,
66 update_msg->x, update_msg->y, update_msg->size_x, update_msg->size_y);
69 unsigned char * master_array = costmap_->getCharMap();
71 for (
size_t y = 0; y < update_msg->size_y; ++y) {
72 auto starting_index_of_row_update_in_costmap = (y + update_msg->y) * map_cell_size_x +
76 update_msg->data.begin() + (y * update_msg->size_x),
77 update_msg->size_x, &master_array[starting_index_of_row_update_in_costmap]);
80 RCLCPP_WARN(logger_,
"No costmap received.");
84 void CostmapSubscriber::processCurrentCostmapMsg()
86 std::scoped_lock lock(*(costmap_->getMutex()), costmap_msg_mutex_);
90 if (haveCostmapParametersChanged()) {
92 costmap_msg_->metadata.size_x, costmap_msg_->metadata.size_y,
93 costmap_msg_->metadata.resolution,
94 costmap_msg_->metadata.origin.position.x,
95 costmap_msg_->metadata.origin.position.y);
98 unsigned char * master_array = costmap_->getCharMap();
99 std::copy(costmap_msg_->data.begin(), costmap_msg_->data.end(), master_array);
100 costmap_msg_.reset();
103 bool CostmapSubscriber::haveCostmapParametersChanged()
105 return hasCostmapSizeChanged() ||
106 hasCostmapResolutionChanged() ||
107 hasCostmapOriginPositionChanged();
110 bool CostmapSubscriber::hasCostmapSizeChanged()
112 return costmap_->getSizeInCellsX() != costmap_msg_->metadata.size_x ||
113 costmap_->getSizeInCellsY() != costmap_msg_->metadata.size_y;
116 bool CostmapSubscriber::hasCostmapResolutionChanged()
118 return costmap_->getResolution() != costmap_msg_->metadata.resolution;
121 bool CostmapSubscriber::hasCostmapOriginPositionChanged()
123 return costmap_->getOriginX() != costmap_msg_->metadata.origin.position.x ||
124 costmap_->getOriginY() != costmap_msg_->metadata.origin.position.y;
std::shared_ptr< Costmap2D > getCostmap()
Get current costmap.
void costmapCallback(const nav2_msgs::msg::Costmap::ConstSharedPtr &msg)
Callback for the costmap topic.
void costmapUpdateCallback(const nav2_msgs::msg::CostmapUpdate::ConstSharedPtr &update_msg)
Callback for the costmap's update topic.