39 #include "nav2_costmap_2d/costmap_2d_publisher.hpp"
45 #include "nav2_costmap_2d/cost_values.hpp"
50 char * Costmap2DPublisher::cost_translation_table_ = NULL;
53 const nav2_util::LifecycleNode::WeakPtr & parent,
55 std::string global_frame,
56 std::string topic_name,
57 bool always_send_full_costmap,
60 global_frame_(global_frame),
61 topic_name_(topic_name),
63 always_send_full_costmap_(always_send_full_costmap),
66 auto node = parent.lock();
67 clock_ = node->get_clock();
68 logger_ = node->get_logger();
70 auto custom_qos = rclcpp::QoS(rclcpp::KeepLast(1)).transient_local().reliable();
73 costmap_pub_ = node->create_publisher<nav_msgs::msg::OccupancyGrid>(
76 costmap_raw_pub_ = node->create_publisher<nav2_msgs::msg::Costmap>(
79 costmap_update_pub_ = node->create_publisher<map_msgs::msg::OccupancyGridUpdate>(
80 topic_name +
"_updates", custom_qos);
81 costmap_raw_update_pub_ = node->create_publisher<nav2_msgs::msg::CostmapUpdate>(
82 topic_name +
"_raw_updates", custom_qos);
85 costmap_service_ = node->create_service<nav2_msgs::srv::GetCostmap>(
86 "get_" + topic_name, std::bind(
87 &Costmap2DPublisher::costmap_service_callback,
88 this, std::placeholders::_1, std::placeholders::_2,
89 std::placeholders::_3));
91 if (cost_translation_table_ == NULL) {
92 cost_translation_table_ =
new char[256];
95 cost_translation_table_[0] = 0;
96 cost_translation_table_[253] = 99;
97 cost_translation_table_[254] = 100;
98 cost_translation_table_[255] = -1;
102 for (
int i = 1; i < 253; i++) {
103 cost_translation_table_[i] =
static_cast<char>(1 + (97 * (i - 1)) / 251);
123 void Costmap2DPublisher::updateGridParams()
133 void Costmap2DPublisher::prepareGrid()
135 std::unique_lock<Costmap2D::mutex_t> lock(*(costmap_->getMutex()));
137 grid_ = std::make_unique<nav_msgs::msg::OccupancyGrid>();
139 grid_->header.frame_id = global_frame_;
140 grid_->header.stamp = clock_->now();
142 grid_->info.resolution = grid_resolution_;
144 grid_->info.width = grid_width_;
145 grid_->info.height = grid_height_;
149 grid_->info.origin.position.x = wx - grid_resolution_ / 2;
150 grid_->info.origin.position.y = wy - grid_resolution_ / 2;
151 grid_->info.origin.position.z = map_vis_z_;
152 grid_->info.origin.orientation.w = 1.0;
154 grid_->data.resize(grid_->info.width * grid_->info.height);
156 unsigned char * data = costmap_->
getCharMap();
157 for (
unsigned int i = 0; i < grid_->data.size(); i++) {
158 grid_->data[i] = cost_translation_table_[data[i]];
162 void Costmap2DPublisher::prepareCostmap()
164 std::unique_lock<Costmap2D::mutex_t> lock(*(costmap_->getMutex()));
167 costmap_raw_ = std::make_unique<nav2_msgs::msg::Costmap>();
169 costmap_raw_->header.frame_id = global_frame_;
170 costmap_raw_->header.stamp = clock_->now();
172 costmap_raw_->metadata.layer =
"master";
173 costmap_raw_->metadata.resolution = resolution;
180 costmap_raw_->metadata.origin.position.x = wx - resolution / 2;
181 costmap_raw_->metadata.origin.position.y = wy - resolution / 2;
182 costmap_raw_->metadata.origin.position.z = 0.0;
183 costmap_raw_->metadata.origin.orientation.w = 1.0;
185 costmap_raw_->data.resize(costmap_raw_->metadata.size_x * costmap_raw_->metadata.size_y);
187 unsigned char * data = costmap_->
getCharMap();
188 memcpy(costmap_raw_->data.data(), data, costmap_raw_->data.size());
191 std::unique_ptr<map_msgs::msg::OccupancyGridUpdate> Costmap2DPublisher::createGridUpdateMsg()
193 auto update = std::make_unique<map_msgs::msg::OccupancyGridUpdate>();
195 update->header.stamp = clock_->now();
196 update->header.frame_id = global_frame_;
199 update->width = xn_ - x0_;
200 update->height = yn_ - y0_;
201 update->data.resize(update->width * update->height);
204 for (std::uint32_t y = y0_; y < yn_; y++) {
205 for (std::uint32_t x = x0_; x < xn_; x++) {
206 update->data[i++] = cost_translation_table_[costmap_->
getCost(x, y)];
212 std::unique_ptr<nav2_msgs::msg::CostmapUpdate> Costmap2DPublisher::createCostmapUpdateMsg()
214 auto msg = std::make_unique<nav2_msgs::msg::CostmapUpdate>();
216 msg->header.stamp = clock_->now();
217 msg->header.frame_id = global_frame_;
220 msg->size_x = xn_ - x0_;
221 msg->size_y = yn_ - y0_;
222 msg->data.resize(msg->size_x * msg->size_y);
225 for (std::uint32_t y = y0_; y < yn_; y++) {
226 for (std::uint32_t x = x0_; x < xn_; x++) {
227 msg->data[i++] = costmap_->
getCost(x, y);
236 if (always_send_full_costmap_ || grid_resolution_ != resolution ||
243 if (costmap_pub_->get_subscription_count() > 0) {
245 costmap_pub_->publish(std::move(grid_));
247 if (costmap_raw_pub_->get_subscription_count() > 0) {
249 costmap_raw_pub_->publish(std::move(costmap_raw_));
251 }
else if (x0_ < xn_) {
253 std::unique_lock<Costmap2D::mutex_t> lock(*(costmap_->getMutex()));
254 if (costmap_update_pub_->get_subscription_count() > 0) {
255 costmap_update_pub_->publish(createGridUpdateMsg());
257 if (costmap_raw_update_pub_->get_subscription_count() > 0) {
258 costmap_raw_update_pub_->publish(createCostmapUpdateMsg());
268 Costmap2DPublisher::costmap_service_callback(
269 const std::shared_ptr<rmw_request_id_t>,
270 const std::shared_ptr<nav2_msgs::srv::GetCostmap::Request>,
271 const std::shared_ptr<nav2_msgs::srv::GetCostmap::Response> response)
273 RCLCPP_DEBUG(logger_,
"Received costmap service request");
276 tf2::Quaternion quaternion;
277 quaternion.setRPY(0.0, 0.0, 0.0);
281 auto data_length = size_x * size_y;
282 unsigned char * data = costmap_->
getCharMap();
283 auto current_time = clock_->now();
285 response->map.header.stamp = current_time;
286 response->map.header.frame_id = global_frame_;
287 response->map.metadata.size_x = size_x;
288 response->map.metadata.size_y = size_y;
289 response->map.metadata.resolution = costmap_->
getResolution();
290 response->map.metadata.layer =
"master";
291 response->map.metadata.map_load_time = current_time;
292 response->map.metadata.update_time = current_time;
293 response->map.metadata.origin.position.x = costmap_->
getOriginX();
294 response->map.metadata.origin.position.y = costmap_->
getOriginY();
295 response->map.metadata.origin.position.z = 0.0;
296 response->map.metadata.origin.orientation = tf2::toMsg(quaternion);
297 response->map.data.resize(data_length);
298 response->map.data.assign(data, data + data_length);
~Costmap2DPublisher()
Destructor.
void publishCostmap()
Publishes the visualization data over ROS.
Costmap2DPublisher(const nav2_util::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.
A 2D costmap provides a mapping between points in the world and their associated "costs".
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates.
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
unsigned char * getCharMap() const
Will return a pointer to the underlying unsigned char array used as the costmap.
double getResolution() const
Accessor for the resolution of the costmap.
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
double getOriginY() const
Accessor for the y origin of the costmap.
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
double getOriginX() const
Accessor for the x origin of the costmap.