refactor(depth): DepthCameraObstacleLayer follows the CostmapLayer contract
The port wrote LETHAL straight into the master from its stored 3D points and left its own grid empty, as costmap_depth_camera does. Everything CostmapLayer offers on top of the own grid therefore missed the layer: clearArea() (the ClearCostmapRecovery path), reset() (Costmap2DROBOT::resetLayers()), footprint clearing and combination_method all acted on an empty grid, and the next cycle wrote the ghosts back. Cleared cells also stayed on the master until another layer's bounds covered them (the source's B7). The stored points stay the source of truth; the clearing and marking algorithm is unchanged. The own grid now mirrors them cell by cell, the way VoxelLayer keeps a voxel grid next to its 2D grid: - marking sets the column's cell LETHAL, the last point of a column leaving sets it FREE_SPACE, and both touch the bounds, so a cleared cell leaves the master in the same cycle (clear_touches_bounds is gone, always on); - updateCosts() only merges the own grid with combination_method, like ObstacleLayer; combination_method other than 0/1 now means "no effect"; - reset() drops the stored points and buffered observations; - clearArea() follows CostmapLayer::clearArea(): invert_area false clears outside the box and keeps the inside, true clears the inside; - footprint clearing erases the stored columns under the robot; - markings that leave the window are dropped in updateBounds() (B6), and a column whose cell went missing is re-marked; - the layer mutex guards the point map, since ClearCostmapRecovery and resetLayers() reach it from other threads (recursive mutex, held by the recovery around clearArea()). Tests: the two clear_touches_bounds cases become one bounded-update case, and clearArea (both directions), reset and footprint clearing get their own.
This commit is contained in:
@@ -41,6 +41,7 @@
|
||||
#include <data_convert/data_convert.h>
|
||||
#include <boost/dll/alias.hpp>
|
||||
#include <boost/make_shared.hpp>
|
||||
#include <boost/thread/locks.hpp>
|
||||
|
||||
#include <pcl/point_cloud.h>
|
||||
#include <pcl/point_types.h>
|
||||
@@ -55,6 +56,7 @@
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace robot_costmap_2d
|
||||
{
|
||||
@@ -270,7 +272,9 @@ void DepthCameraObstacleLayer::loadParameters()
|
||||
use_voxelized_observation_ = p.get("use_voxelized_observation", true);
|
||||
transform_tolerance_ = p.get("transform_tolerance", 0.2);
|
||||
const bool use_global_frame_to_mark = p.get("use_global_frame_to_mark", true);
|
||||
clear_touches_bounds_ = p.get("clear_touches_bounds", false);
|
||||
if (p.has("clear_touches_bounds"))
|
||||
robot::log_warning("DepthCameraObstacleLayer %s: clear_touches_bounds is obsolete and ignored: a cleared "
|
||||
"cell now always resets its master cell\n", name_.c_str());
|
||||
debug_stats_ = p.get("debug/stats", true);
|
||||
debug_stats_period_ = p.get("debug/stats_period", 5.0);
|
||||
|
||||
@@ -348,14 +352,14 @@ void DepthCameraObstacleLayer::loadParameters()
|
||||
"ec_seg_distance=%.2f m ec_cluster_min_size=%d size_of_cluster_rejection=%d "
|
||||
"forced_clearing_distance=%.2f m near_blocked_protection=%s (%d pts) "
|
||||
"voxelized_observation=%s transform_tolerance=%.2f s combination_method=%d "
|
||||
"footprint_clearing=%s clear_touches_bounds=%s marking_height=[%.2f, %.2f] m debug/stats=%s\n",
|
||||
"footprint_clearing=%s marking_height=[%.2f, %.2f] m debug/stats=%s\n",
|
||||
name_.c_str(), global_frame_.c_str(), rolling_window_ ? "true" : "false",
|
||||
track_unknown_space ? "true" : "false", voxel_resolution_, check_radius_,
|
||||
number_clearing_threshold_, ec_seg_distance_, ec_cluster_min_size_,
|
||||
size_of_cluster_rejection_, forced_clearing_distance_,
|
||||
enable_near_blocked_protection_ ? "true" : "false", number_points_considered_as_blocked_,
|
||||
use_voxelized_observation_ ? "true" : "false", transform_tolerance_, combination_method_,
|
||||
footprint_clearing_enabled_ ? "true" : "false", clear_touches_bounds_ ? "true" : "false",
|
||||
footprint_clearing_enabled_ ? "true" : "false",
|
||||
marking_height_under_ground_, marking_height_above_ground_, debug_stats_ ? "on" : "off");
|
||||
}
|
||||
|
||||
@@ -502,12 +506,15 @@ bool DepthCameraObstacleLayer::sourceIsCurrent(const Source& source) const
|
||||
void DepthCameraObstacleLayer::updateBounds(double robot_x, double robot_y, double robot_yaw, double* min_x,
|
||||
double* min_y, double* max_x, double* max_y)
|
||||
{
|
||||
// ClearCostmapRecovery and resetLayers() reach the stored points from other threads.
|
||||
boost::unique_lock<mutex_t> lock(*getMutex());
|
||||
if (rolling_window_)
|
||||
updateOrigin(robot_x - getSizeInMetersX() / 2, robot_y - getSizeInMetersY() / 2);
|
||||
if (!enabled_)
|
||||
return;
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
useExtraBounds(min_x, min_y, max_x, max_y);
|
||||
dropColumnsOutsideWindow(min_x, min_y, max_x, max_y);
|
||||
|
||||
bool current = true;
|
||||
std::vector<DepthCameraFrustum> frustums;
|
||||
@@ -638,16 +645,19 @@ void DepthCameraObstacleLayer::clearMarkingByKdtree(const std::vector<Eigen::Vec
|
||||
const double resolution = layered_costmap_->getCostmap()->getResolution();
|
||||
std::vector<int> indices;
|
||||
std::vector<float> sq_distances;
|
||||
for (auto& column : pc_3d_map_global_)
|
||||
for (auto column = pc_3d_map_global_.begin(); column != pc_3d_map_global_.end();)
|
||||
{
|
||||
double wx, wy;
|
||||
intIndexToWorld(wx, wy, column.first.first, column.first.second, resolution);
|
||||
intIndexToWorld(wx, wy, column->first.first, column->first.second, resolution);
|
||||
// Marked points this far from the robot are out of every frustum.
|
||||
const double pc_dis = std::hypot(wx - robot_x, wy - robot_y);
|
||||
if (pc_dis > kClearingSkipDistance)
|
||||
{
|
||||
++column;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto& heights = column.second;
|
||||
auto& heights = column->second;
|
||||
for (auto it = heights.begin(), next_it = it; it != heights.end(); it = next_it)
|
||||
{
|
||||
++next_it;
|
||||
@@ -673,9 +683,10 @@ void DepthCameraObstacleLayer::clearMarkingByKdtree(const std::vector<Eigen::Vec
|
||||
continue;
|
||||
heights.erase(it);
|
||||
++stats_.cleared;
|
||||
if (clear_touches_bounds_)
|
||||
touch(wx, wy, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
// The last point of the column is gone: the cell is free again, in the own grid and,
|
||||
// through the bounds, in the master this cycle.
|
||||
column = heights.empty() ? eraseColumn(column, min_x, min_y, max_x, max_y) : std::next(column);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -696,11 +707,18 @@ void DepthCameraObstacleLayer::processCluster(const std::vector<DepthCameraFrust
|
||||
const int h_ind = static_cast<int>(std::round(p.z() * (1 / voxel_resolution_)));
|
||||
if (h_ind > h_max || h_ind < h_min)
|
||||
continue;
|
||||
auto& slot = pc_3d_map_global_[std::make_pair(mmx, mmy)];
|
||||
const std::pair<int, int> key(mmx, mmy);
|
||||
unsigned int cx, cy;
|
||||
if (!columnCell(key, cx, cy))
|
||||
continue; // would be dropped as out of the window anyway
|
||||
auto& slot = pc_3d_map_global_[key];
|
||||
auto inserted = slot.insert(std::make_pair(h_ind, 0.f));
|
||||
if (inserted.second)
|
||||
++stats_.marked;
|
||||
touch(p.x(), p.y(), min_x, min_y, max_x, max_y);
|
||||
setCost(cx, cy, LETHAL_OBSTACLE);
|
||||
double wx, wy;
|
||||
intIndexToWorld(wx, wy, mmx, mmy, resolution);
|
||||
touch(wx, wy, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -712,6 +730,35 @@ void DepthCameraObstacleLayer::updateFootprint(double robot_x, double robot_y, d
|
||||
transformFootprint(robot_x, robot_y, robot_yaw, getFootprint(), transformed_footprint_);
|
||||
for (const auto& pt : transformed_footprint_)
|
||||
touch(pt.x, pt.y, min_x, min_y, max_x, max_y);
|
||||
|
||||
// updateCosts() frees the footprint in the own grid like ObstacleLayer; the stored points under
|
||||
// it must go too, or the next marking pass would not bring them back but nothing would ever
|
||||
// remove them either.
|
||||
std::vector<MapLocation> polygon, cells;
|
||||
for (const auto& pt : transformed_footprint_)
|
||||
{
|
||||
MapLocation loc;
|
||||
if (!worldToMap(pt.x, pt.y, loc.x, loc.y))
|
||||
return; // footprint leaves the window: same give-up as setConvexPolygonCost
|
||||
polygon.push_back(loc);
|
||||
}
|
||||
if (polygon.size() < 3)
|
||||
return;
|
||||
convexFillCells(polygon, cells);
|
||||
std::unordered_set<unsigned int> under;
|
||||
for (const MapLocation& c : cells)
|
||||
under.insert(getIndex(c.x, c.y));
|
||||
for (auto it = pc_3d_map_global_.begin(); it != pc_3d_map_global_.end();)
|
||||
{
|
||||
unsigned int mx, my;
|
||||
if (columnCell(it->first, mx, my) && under.count(getIndex(mx, my)))
|
||||
{
|
||||
++stats_.footprint_cleared;
|
||||
it = eraseColumn(it, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
bool DepthCameraObstacleLayer::isValid(int mx, int my) const
|
||||
@@ -721,42 +768,75 @@ bool DepthCameraObstacleLayer::isValid(int mx, int my) const
|
||||
my <= static_cast<int>(getSizeInCellsY());
|
||||
}
|
||||
|
||||
bool DepthCameraObstacleLayer::columnCell(const std::pair<int, int>& column, unsigned int& mx,
|
||||
unsigned int& my) const
|
||||
{
|
||||
double wx, wy;
|
||||
intIndexToWorld(wx, wy, column.first, column.second, getResolution());
|
||||
if (!worldToMap(wx, wy, mx, my))
|
||||
return false;
|
||||
// The +-1 cells guard the rounding of the stored corner, as in the source.
|
||||
const int x = static_cast<int>(mx), y = static_cast<int>(my);
|
||||
return isValid(x - 1, y - 1) && isValid(x - 1, y + 1) && isValid(x + 1, y - 1) && isValid(x + 1, y + 1);
|
||||
}
|
||||
|
||||
std::map<std::pair<int, int>, std::map<int, float>>::iterator DepthCameraObstacleLayer::eraseColumn(
|
||||
std::map<std::pair<int, int>, std::map<int, float>>::iterator it, double* min_x, double* min_y,
|
||||
double* max_x, double* max_y)
|
||||
{
|
||||
unsigned int mx, my;
|
||||
if (columnCell(it->first, mx, my))
|
||||
{
|
||||
setCost(mx, my, FREE_SPACE);
|
||||
double wx, wy;
|
||||
intIndexToWorld(wx, wy, it->first.first, it->first.second, getResolution());
|
||||
touch(wx, wy, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
return pc_3d_map_global_.erase(it);
|
||||
}
|
||||
|
||||
void DepthCameraObstacleLayer::dropColumnsOutsideWindow(double* min_x, double* min_y, double* max_x,
|
||||
double* max_y)
|
||||
{
|
||||
for (auto it = pc_3d_map_global_.begin(); it != pc_3d_map_global_.end();)
|
||||
{
|
||||
unsigned int mx, my;
|
||||
if (columnCell(it->first, mx, my))
|
||||
{
|
||||
// Keep the own grid in step with the points even after matchSize() or a cleared area
|
||||
// wiped the cell: one O(columns) pass that writes only when something is missing.
|
||||
if (getCost(mx, my) != LETHAL_OBSTACLE)
|
||||
{
|
||||
setCost(mx, my, LETHAL_OBSTACLE);
|
||||
double wx, wy;
|
||||
intIndexToWorld(wx, wy, it->first.first, it->first.second, getResolution());
|
||||
touch(wx, wy, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
// B6: the source keeps such points forever, so the map and every loop over it grow.
|
||||
++stats_.dropped_out_of_window;
|
||||
double wx, wy;
|
||||
intIndexToWorld(wx, wy, it->first.first, it->first.second, getResolution());
|
||||
if (worldToMap(wx, wy, mx, my))
|
||||
{
|
||||
// Still inside the window, on its edge row/column: free that cell as well.
|
||||
setCost(mx, my, FREE_SPACE);
|
||||
touch(wx, wy, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
it = pc_3d_map_global_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void DepthCameraObstacleLayer::updateCosts(Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j)
|
||||
{
|
||||
boost::unique_lock<mutex_t> lock(*getMutex());
|
||||
if (!enabled_)
|
||||
return;
|
||||
|
||||
unsigned char* master_array = master_grid.getCharMap();
|
||||
const double resolution = layered_costmap_->getCostmap()->getResolution();
|
||||
for (auto it = pc_3d_map_global_.begin(); it != pc_3d_map_global_.end();)
|
||||
{
|
||||
double wx, wy;
|
||||
intIndexToWorld(wx, wy, it->first.first, it->first.second, resolution);
|
||||
unsigned int mx, my;
|
||||
if (!worldToMap(wx, wy, mx, my))
|
||||
{
|
||||
// B6: the source keeps such points forever, so the map and this loop grow all shift.
|
||||
++stats_.dropped_out_of_window;
|
||||
it = pc_3d_map_global_.erase(it);
|
||||
continue;
|
||||
}
|
||||
// The +-1 cells guard the rounding of the stored corner, as in the source.
|
||||
const int x = static_cast<int>(mx), y = static_cast<int>(my);
|
||||
if (isValid(x - 1, y - 1) && isValid(x - 1, y + 1) && isValid(x + 1, y - 1) && isValid(x + 1, y + 1) &&
|
||||
!it->second.empty())
|
||||
{
|
||||
const unsigned int index = getIndex(mx, my);
|
||||
master_array[index] = std::max(LETHAL_OBSTACLE, master_array[index]);
|
||||
++it;
|
||||
}
|
||||
else
|
||||
{
|
||||
it = pc_3d_map_global_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
// Footprint and combination act on this layer's own grid, which holds no markings: as in the
|
||||
// source, they do not remove LETHAL already written to the master above.
|
||||
// The own grid mirrors the stored points (see updateBounds), so the layer merges like
|
||||
// ObstacleLayer and the master is only changed inside the update bounds.
|
||||
if (footprint_clearing_enabled_)
|
||||
setConvexPolygonCost(transformed_footprint_, FREE_SPACE);
|
||||
|
||||
@@ -790,10 +870,10 @@ void DepthCameraObstacleLayer::logStats()
|
||||
last_stats_log_ = now;
|
||||
if (debug_stats_ && stats_.cycles > 0)
|
||||
robot::log_info("[debug/stats] DepthCameraObstacleLayer %s: cycles=%zu cloud_pts/cycle=%.0f stored=%zu "
|
||||
"columns=%zu marked=%zu cleared=%zu clear_all_cycles=%zu bypass_cycles=%zu tf_wait=%zu "
|
||||
"tf_drop=%zu out_of_window=%zu bounds_ms avg=%.1f max=%.1f\n",
|
||||
"columns=%zu marked=%zu cleared=%zu footprint_cleared=%zu clear_all_cycles=%zu bypass_cycles=%zu "
|
||||
"tf_wait=%zu tf_drop=%zu out_of_window=%zu bounds_ms avg=%.1f max=%.1f\n",
|
||||
name_.c_str(), stats_.cycles, static_cast<double>(stats_.cloud_points) / stats_.cycles,
|
||||
storedPoints(), pc_3d_map_global_.size(), stats_.marked, stats_.cleared,
|
||||
storedPoints(), pc_3d_map_global_.size(), stats_.marked, stats_.cleared, stats_.footprint_cleared,
|
||||
stats_.cleared_all_frames, stats_.bypassed_frames, stats_.tf_waits, stats_.tf_drops,
|
||||
stats_.dropped_out_of_window, stats_.bounds_ms_sum / stats_.cycles, stats_.bounds_ms_max);
|
||||
stats_ = Stats();
|
||||
@@ -817,12 +897,40 @@ void DepthCameraObstacleLayer::deactivate()
|
||||
|
||||
void DepthCameraObstacleLayer::reset()
|
||||
{
|
||||
boost::unique_lock<mutex_t> lock(*getMutex());
|
||||
deactivate();
|
||||
resetMaps();
|
||||
// The source keeps its global-mode points here (B9), so a reset changed nothing on the master.
|
||||
pc_3d_map_global_.clear();
|
||||
for (const auto& source : sources_)
|
||||
source->observations.clear();
|
||||
current_ = true;
|
||||
activate();
|
||||
}
|
||||
|
||||
void DepthCameraObstacleLayer::clearArea(int start_x, int start_y, int end_x, int end_y, bool invert_area)
|
||||
{
|
||||
boost::unique_lock<mutex_t> lock(*getMutex());
|
||||
CostmapLayer::clearArea(start_x, start_y, end_x, end_y, invert_area);
|
||||
for (auto it = pc_3d_map_global_.begin(); it != pc_3d_map_global_.end();)
|
||||
{
|
||||
unsigned int mx, my;
|
||||
if (columnCell(it->first, mx, my))
|
||||
{
|
||||
// Same test as CostmapLayer::clearArea: keep what it keeps.
|
||||
const int x = static_cast<int>(mx), y = static_cast<int>(my);
|
||||
const bool inside = x > start_x && x < end_x && y > start_y && y < end_y;
|
||||
if (inside != invert_area)
|
||||
{
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// The own grid cell is already NO_INFORMATION from CostmapLayer::clearArea.
|
||||
it = pc_3d_map_global_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
// Export factory function
|
||||
static boost::shared_ptr<Layer> create_depth_camera_obstacle_plugin()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user