19 #include "nav2_costmap_2d/costmap_subscriber.hpp"
20 #include "nav2_ros_common/validate_messages.hpp"
27 if (!isCostmapReceived()) {
28 throw std::runtime_error(
"Costmap is not available");
30 processCurrentCostmapMsg();
36 if (!nav2::validateMsg(*msg)) {
37 RCLCPP_ERROR(logger_,
"Received costmap message is malformed. Rejecting.");
41 std::lock_guard<std::recursive_mutex> lock(costmap_msg_mutex_);
43 frame_id_ = costmap_msg_->header.frame_id;
45 if (!isCostmapReceived()) {
46 costmap_ = std::make_shared<Costmap2D>(
47 msg->metadata.size_x, msg->metadata.size_y,
48 msg->metadata.resolution, msg->metadata.origin.position.x,
49 msg->metadata.origin.position.y);
51 processCurrentCostmapMsg();
56 const nav2_msgs::msg::CostmapUpdate::ConstSharedPtr & update_msg)
58 if (isCostmapReceived()) {
59 processCurrentCostmapMsg();
61 std::lock_guard<Costmap2D::mutex_t> lock(*(costmap_->getMutex()));
63 auto map_cell_size_x = costmap_->getSizeInCellsX();
64 auto map_call_size_y = costmap_->getSizeInCellsY();
66 if (map_cell_size_x < update_msg->x + update_msg->size_x ||
67 map_call_size_y < update_msg->y + update_msg->size_y)
70 logger_,
"Update area outside of original map area. Costmap bounds: %d X %d, "
71 "Update origin: %d, %d bounds: %d X %d", map_cell_size_x, map_call_size_y,
72 update_msg->x, update_msg->y, update_msg->size_x, update_msg->size_y);
75 unsigned char * master_array = costmap_->getCharMap();
77 for (
size_t y = 0; y < update_msg->size_y; ++y) {
78 auto starting_index_of_row_update_in_costmap = (y + update_msg->y) * map_cell_size_x +
82 update_msg->data.begin() + (y * update_msg->size_x),
83 update_msg->size_x, &master_array[starting_index_of_row_update_in_costmap]);
86 RCLCPP_WARN(logger_,
"No costmap received.");
90 void CostmapSubscriber::processCurrentCostmapMsg()
92 std::scoped_lock lock(*(costmap_->getMutex()), costmap_msg_mutex_);
96 if (haveCostmapParametersChanged()) {
98 costmap_msg_->metadata.size_x, costmap_msg_->metadata.size_y,
99 costmap_msg_->metadata.resolution,
100 costmap_msg_->metadata.origin.position.x,
101 costmap_msg_->metadata.origin.position.y);
104 unsigned char * master_array = costmap_->getCharMap();
105 std::copy(costmap_msg_->data.begin(), costmap_msg_->data.end(), master_array);
106 costmap_msg_.reset();
109 bool CostmapSubscriber::haveCostmapParametersChanged()
111 return hasCostmapSizeChanged() ||
112 hasCostmapResolutionChanged() ||
113 hasCostmapOriginPositionChanged();
116 bool CostmapSubscriber::hasCostmapSizeChanged()
118 return costmap_->getSizeInCellsX() != costmap_msg_->metadata.size_x ||
119 costmap_->getSizeInCellsY() != costmap_msg_->metadata.size_y;
122 bool CostmapSubscriber::hasCostmapResolutionChanged()
124 return costmap_->getResolution() != costmap_msg_->metadata.resolution;
127 bool CostmapSubscriber::hasCostmapOriginPositionChanged()
129 return costmap_->getOriginX() != costmap_msg_->metadata.origin.position.x ||
130 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.