Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
keepout_filter.cpp
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2020 Samsung Research Russia
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 the <ORGANIZATION> 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: Alexey Merzlyakov
36  *********************************************************************/
37 
38 #include "nav2_costmap_2d/costmap_filters/keepout_filter.hpp"
39 
40 #include <string>
41 #include <memory>
42 #include <algorithm>
43 #include "tf2/convert.hpp"
44 #include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
45 
46 #include "nav2_costmap_2d/costmap_filters/filter_values.hpp"
47 #include "nav2_util/geometry_utils.hpp"
48 #include "nav2_util/occ_grid_utils.hpp"
49 
50 namespace nav2_costmap_2d
51 {
52 
54 : filter_info_sub_(nullptr), mask_sub_(nullptr), filter_mask_(nullptr),
55  global_frame_("")
56 {
57 }
58 
60  const std::string & filter_info_topic)
61 {
62  std::lock_guard<CostmapFilter::mutex_t> guard(*getMutex());
63 
64  nav2::LifecycleNode::SharedPtr node = node_.lock();
65  if (!node) {
66  throw std::runtime_error{"Failed to lock node"};
67  }
68 
69  filter_info_topic_ = joinWithParentNamespace(filter_info_topic);
70  // Setting new costmap filter info subscriber
71  RCLCPP_INFO(
72  logger_,
73  "KeepoutFilter: Subscribing to \"%s\" topic for filter info...",
74  filter_info_topic_.c_str());
75  filter_info_sub_ = node->create_subscription<nav2_msgs::msg::CostmapFilterInfo>(
77  std::bind(&KeepoutFilter::filterInfoCallback, this, std::placeholders::_1),
79 
80  global_frame_ = layered_costmap_->getGlobalFrameID();
81 
82  declareParameter("override_lethal_cost", rclcpp::ParameterValue(false));
83  node->get_parameter(name_ + "." + "override_lethal_cost", override_lethal_cost_);
84  declareParameter("lethal_override_cost", rclcpp::ParameterValue(MAX_NON_OBSTACLE));
85  node->get_parameter(name_ + "." + "lethal_override_cost", lethal_override_cost_);
86 
87  // clamp lethal_override_cost_ in case if higher than MAX_NON_OBSTACLE is given
88  lethal_override_cost_ = \
89  std::clamp<unsigned int>(lethal_override_cost_, FREE_SPACE, MAX_NON_OBSTACLE);
90  lethal_state_update_max_x_ = lethal_state_update_max_y_ = std::numeric_limits<double>::lowest();
91  lethal_state_update_min_x_ = lethal_state_update_min_y_ = std::numeric_limits<double>::max();
92 }
93 
94 void KeepoutFilter::filterInfoCallback(
95  const nav2_msgs::msg::CostmapFilterInfo::SharedPtr msg)
96 {
97  std::lock_guard<CostmapFilter::mutex_t> guard(*getMutex());
98 
99  nav2::LifecycleNode::SharedPtr node = node_.lock();
100  if (!node) {
101  throw std::runtime_error{"Failed to lock node"};
102  }
103 
104  if (!mask_sub_) {
105  RCLCPP_INFO(
106  logger_,
107  "KeepoutFilter: Received filter info from %s topic.", filter_info_topic_.c_str());
108  } else {
109  RCLCPP_WARN(
110  logger_,
111  "KeepoutFilter: New costmap filter info arrived from %s topic. Updating old filter info.",
112  filter_info_topic_.c_str());
113  // Resetting previous subscriber each time when new costmap filter information arrives
114  mask_sub_.reset();
115  }
116 
117  // Checking that base and multiplier are set to their default values
118  if (msg->base != BASE_DEFAULT || msg->multiplier != MULTIPLIER_DEFAULT) {
119  RCLCPP_ERROR(
120  logger_,
121  "KeepoutFilter: For proper use of keepout filter base and multiplier"
122  " in CostmapFilterInfo message should be set to their default values (%f and %f)",
123  BASE_DEFAULT, MULTIPLIER_DEFAULT);
124  }
125 
126  mask_topic_ = joinWithParentNamespace(msg->filter_mask_topic);
127 
128  // Setting new filter mask subscriber
129  RCLCPP_INFO(
130  logger_,
131  "KeepoutFilter: Subscribing to \"%s\" topic for filter mask...",
132  mask_topic_.c_str());
133  mask_sub_ = node->create_subscription<nav_msgs::msg::OccupancyGrid>(
134  mask_topic_,
135  std::bind(&KeepoutFilter::maskCallback, this, std::placeholders::_1),
137 }
138 
139 void KeepoutFilter::maskCallback(
140  const nav_msgs::msg::OccupancyGrid::SharedPtr msg)
141 {
142  std::lock_guard<CostmapFilter::mutex_t> guard(*getMutex());
143 
144  nav2::LifecycleNode::SharedPtr node = node_.lock();
145  if (!node) {
146  throw std::runtime_error{"Failed to lock node"};
147  }
148 
149  if (!filter_mask_) {
150  RCLCPP_INFO(
151  logger_,
152  "KeepoutFilter: Received filter mask from %s topic.", mask_topic_.c_str());
153  } else {
154  RCLCPP_WARN(
155  logger_,
156  "KeepoutFilter: New filter mask arrived from %s topic. Updating old filter mask.",
157  mask_topic_.c_str());
158  filter_mask_.reset();
159  }
160 
161  // Store filter_mask_
162  filter_mask_ = msg;
163  has_updated_data_ = true;
164  x_ = y_ = 0;
165  width_ = msg->info.width;
166  height_ = msg->info.height;
167 }
168 
170  double robot_x, double robot_y, double robot_yaw,
171  double * min_x, double * min_y, double * max_x, double * max_y)
172 {
173  if (!enabled_) {
174  return;
175  }
176 
177  CostmapFilter::updateBounds(robot_x, robot_y, robot_yaw, min_x, min_y, max_x, max_y);
178 
179  // If new keepout zone received
180  if(has_updated_data_) {
181  double wx, wy;
182  layered_costmap_->getCostmap()->mapToWorld(x_, y_, wx, wy);
183  *min_x = std::min(wx, *min_x);
184  *min_y = std::min(wy, *min_y);
185 
186  layered_costmap_->getCostmap()->mapToWorld(x_ + width_, y_ + height_, wx, wy);
187  *max_x = std::max(wx, *max_x);
188  *max_y = std::max(wy, *max_y);
189 
190  has_updated_data_ = false;
191  return;
192  }
193 
194  // Let's find the pose's cost if we are allowed to override the lethal cost
195  is_pose_lethal_ = false;
196  if (override_lethal_cost_) {
197  geometry_msgs::msg::Pose pose;
198  pose.position.x = robot_x;
199  pose.position.y = robot_y;
200  pose.position.z = 0.0;
201  pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(robot_yaw);
202  geometry_msgs::msg::Pose mask_pose;
203  if (transformPose(global_frame_, pose, filter_mask_->header.frame_id, mask_pose)) {
204  unsigned int mask_robot_i, mask_robot_j;
205  if (nav2_util::worldToMap(filter_mask_, mask_pose.position.x, mask_pose.position.y,
206  mask_robot_i, mask_robot_j))
207  {
208  auto data = getMaskCost(filter_mask_, mask_robot_i, mask_robot_j);
209  is_pose_lethal_ = (data == INSCRIBED_INFLATED_OBSTACLE || data == LETHAL_OBSTACLE);
210  if (is_pose_lethal_) {
211  RCLCPP_WARN_THROTTLE(
212  logger_, *(clock_), 2000,
213  "KeepoutFilter: Pose is in keepout zone, reducing cost override to navigate out.");
214  }
215  }
216  }
217 
218  // If in lethal space or just exited lethal space,
219  // we need to update all possible spaces touched during this state
220  if (is_pose_lethal_ || (last_pose_lethal_ && !is_pose_lethal_)) {
221  lethal_state_update_min_x_ = std::min(*min_x, lethal_state_update_min_x_);
222  *min_x = lethal_state_update_min_x_;
223  lethal_state_update_min_y_ = std::min(*min_y, lethal_state_update_min_y_);
224  *min_y = lethal_state_update_min_y_;
225  lethal_state_update_max_x_ = std::max(*max_x, lethal_state_update_max_x_);
226  *max_x = lethal_state_update_max_x_;
227  lethal_state_update_max_y_ = std::max(*max_y, lethal_state_update_max_y_);
228  *max_y = lethal_state_update_max_y_;
229  } else {
230  // If out of lethal space, reset managed lethal state sizes
231  lethal_state_update_min_x_ = std::numeric_limits<double>::max();
232  lethal_state_update_min_y_ = std::numeric_limits<double>::max();
233  lethal_state_update_max_x_ = std::numeric_limits<double>::lowest();
234  lethal_state_update_max_y_ = std::numeric_limits<double>::lowest();
235  }
236  }
237 }
238 
240  nav2_costmap_2d::Costmap2D & master_grid,
241  int min_i, int min_j, int max_i, int max_j,
242  const geometry_msgs::msg::Pose & /*pose*/)
243 {
244  std::lock_guard<CostmapFilter::mutex_t> guard(*getMutex());
245 
246  if (!filter_mask_) {
247  // Show warning message every 2 seconds to not litter an output
248  RCLCPP_WARN_THROTTLE(
249  logger_, *(clock_), 2000,
250  "KeepoutFilter: Filter mask was not received");
251  return;
252  }
253 
254  tf2::Transform tf2_transform;
255  tf2_transform.setIdentity(); // initialize by identical transform
256  int mg_min_x, mg_min_y; // master_grid indexes of bottom-left window corner
257  int mg_max_x, mg_max_y; // master_grid indexes of top-right window corner
258 
259  const std::string mask_frame = filter_mask_->header.frame_id;
260 
261  if (mask_frame != global_frame_) {
262  // Filter mask and current layer are in different frames:
263  // prepare frame transformation if mask_frame != global_frame_
264  geometry_msgs::msg::TransformStamped transform;
265  try {
266  transform = tf_->lookupTransform(
267  mask_frame, global_frame_, tf2::TimePointZero,
269  } catch (tf2::TransformException & ex) {
270  RCLCPP_ERROR(
271  logger_,
272  "KeepoutFilter: Failed to get costmap frame (%s) "
273  "transformation to mask frame (%s) with error: %s",
274  global_frame_.c_str(), mask_frame.c_str(), ex.what());
275  return;
276  }
277  tf2::fromMsg(transform.transform, tf2_transform);
278 
279  mg_min_x = min_i;
280  mg_min_y = min_j;
281  mg_max_x = max_i;
282  mg_max_y = max_j;
283  } else {
284  // Filter mask and current layer are in the same frame:
285  // apply the following optimization - iterate only in overlapped
286  // (min_i, min_j)..(max_i, max_j) & filter_mask_ area.
287  //
288  // filter_mask_
289  // *----------------------------*
290  // | |
291  // | |
292  // | (2) |
293  // *-----+-------* |
294  // | |///////|<- overlapped area |
295  // | |///////| to iterate in |
296  // | *-------+--------------------*
297  // | (1) |
298  // | |
299  // *-------------*
300  // master_grid (min_i, min_j)..(max_i, max_j) window
301  //
302  // ToDo: after costmap rotation will be added, this should be re-worked.
303 
304  double wx, wy; // world coordinates
305 
306  // Calculating bounds corresponding to bottom-left overlapping (1) corner
307  // filter_mask_ -> master_grid indexes conversion
308  const double half_cell_size = 0.5 * filter_mask_->info.resolution;
309  wx = filter_mask_->info.origin.position.x + half_cell_size;
310  wy = filter_mask_->info.origin.position.y + half_cell_size;
311  master_grid.worldToMapNoBounds(wx, wy, mg_min_x, mg_min_y);
312  // Calculation of (1) corner bounds
313  if (mg_min_x >= max_i || mg_min_y >= max_j) {
314  // There is no overlapping. Do nothing.
315  return;
316  }
317  mg_min_x = std::max(min_i, mg_min_x);
318  mg_min_y = std::max(min_j, mg_min_y);
319 
320  // Calculating bounds corresponding to top-right window (2) corner
321  // filter_mask_ -> master_grid indexes conversion
322  wx = filter_mask_->info.origin.position.x +
323  filter_mask_->info.width * filter_mask_->info.resolution + half_cell_size;
324  wy = filter_mask_->info.origin.position.y +
325  filter_mask_->info.height * filter_mask_->info.resolution + half_cell_size;
326  master_grid.worldToMapNoBounds(wx, wy, mg_max_x, mg_max_y);
327  // Calculation of (2) corner bounds
328  if (mg_max_x <= min_i || mg_max_y <= min_j) {
329  // There is no overlapping. Do nothing.
330  return;
331  }
332  mg_max_x = std::min(max_i, mg_max_x);
333  mg_max_y = std::min(max_j, mg_max_y);
334  }
335 
336  // unsigned<-signed conversions.
337  const unsigned int mg_min_x_u = static_cast<unsigned int>(mg_min_x);
338  const unsigned int mg_min_y_u = static_cast<unsigned int>(mg_min_y);
339  const unsigned int mg_max_x_u = static_cast<unsigned int>(mg_max_x);
340  const unsigned int mg_max_y_u = static_cast<unsigned int>(mg_max_y);
341 
342  unsigned int i, j; // master_grid iterators
343  unsigned int index; // corresponding index of master_grid
344  double gl_wx, gl_wy; // world coordinates in a global_frame_
345  double msk_wx, msk_wy; // world coordinates in a mask_frame
346  unsigned int mx, my; // filter_mask_ coordinates
347  unsigned char data, old_data; // master_grid element data
348 
349  // Main master_grid updating loop
350  // Iterate in costmap window by master_grid indexes
351  unsigned char * master_array = master_grid.getCharMap();
352  for (i = mg_min_x_u; i < mg_max_x_u; i++) {
353  for (j = mg_min_y_u; j < mg_max_y_u; j++) {
354  index = master_grid.getIndex(i, j);
355  old_data = master_array[index];
356  // Calculating corresponding to (i, j) point at filter_mask_:
357  // Get world coordinates in global_frame_
358  master_grid.mapToWorld(i, j, gl_wx, gl_wy);
359  if (mask_frame != global_frame_) {
360  // Transform (i, j) point from global_frame_ to mask_frame
361  tf2::Vector3 point(gl_wx, gl_wy, 0);
362  point = tf2_transform * point;
363  msk_wx = point.x();
364  msk_wy = point.y();
365  } else {
366  // In this case master_grid and filter-mask are in the same frame
367  msk_wx = gl_wx;
368  msk_wy = gl_wy;
369  }
370  // Get mask coordinates corresponding to (i, j) point at filter_mask_
371  if (nav2_util::worldToMap(filter_mask_, msk_wx, msk_wy, mx, my)) {
372  data = getMaskCost(filter_mask_, mx, my);
373  // Update if mask_ data is valid and greater than existing master_grid's one
374  if (data == NO_INFORMATION) {
375  continue;
376  }
377 
378  if (data > old_data || old_data == NO_INFORMATION) {
379  if (override_lethal_cost_ && is_pose_lethal_) {
380  master_array[index] = lethal_override_cost_;
381  } else {
382  master_array[index] = data;
383  }
384  }
385  }
386  }
387  }
388 
389  last_pose_lethal_ = is_pose_lethal_;
390 }
391 
393 {
394  std::lock_guard<CostmapFilter::mutex_t> guard(*getMutex());
395 
396  filter_info_sub_.reset();
397  mask_sub_.reset();
398 }
399 
401 {
402  std::lock_guard<CostmapFilter::mutex_t> guard(*getMutex());
403 
404  if (filter_mask_) {
405  return true;
406  }
407  return false;
408 }
409 
410 } // namespace nav2_costmap_2d
411 
412 #include "pluginlib/class_list_macros.hpp"
A QoS profile for latched, reliable topics with a history of 10 messages.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates.
Definition: costmap_2d.cpp:280
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
Definition: costmap_2d.hpp:231
unsigned char * getCharMap() const
Will return a pointer to the underlying unsigned char array used as the costmap.
Definition: costmap_2d.cpp:260
void worldToMapNoBounds(double wx, double wy, int &mx, int &my) const
Convert from world coordinates to map coordinates without checking for legal bounds.
Definition: costmap_2d.cpp:322
bool transformPose(const std::string global_frame, const geometry_msgs::msg::Pose &global_pose, const std::string mask_frame, geometry_msgs::msg::Pose &mask_pose) const
: Transforms robot pose from current layer frame to mask frame
std::string filter_info_topic_
: Name of costmap filter info topic
std::string mask_topic_
: Name of filter mask topic
tf2::Duration transform_tolerance_
: mask_frame->global_frame_ transform tolerance
unsigned char getMaskCost(nav_msgs::msg::OccupancyGrid::ConstSharedPtr filter_mask, const unsigned int mx, const unsigned int &my) const
Get the cost of a cell in the filter mask.
mutex_t * getMutex()
: returns pointer to a mutex
void updateBounds(double robot_x, double robot_y, double robot_yaw, double *min_x, double *min_y, double *max_x, double *max_y) override
Update the bounds of the master costmap by this layer's update dimensions.
Reads in a keepout mask and marks keepout regions in the map to prevent planning or control in restri...
void process(nav2_costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j, const geometry_msgs::msg::Pose &pose)
Process the keepout layer at the current pose / bounds / grid.
void resetFilter()
Reset the costmap filter / topic / info.
void initializeFilter(const std::string &filter_info_topic)
Initialize the filter and subscribe to the info topic.
bool isActive()
If this filter is active.
void updateBounds(double robot_x, double robot_y, double robot_yaw, double *min_x, double *min_y, double *max_x, double *max_y) override
Update the bounds of the master costmap by this layer's update dimensions.
Abstract class for layered costmap plugin implementations.
Definition: layer.hpp:59
std::string joinWithParentNamespace(const std::string &topic)
Definition: layer.cpp:121
void declareParameter(const std::string &param_name, const rclcpp::ParameterValue &value)
Convenience functions for declaring ROS parameters.
Definition: layer.cpp:76
Costmap2D * getCostmap()
Get the costmap pointer to the master costmap.