Bring main up to the dev_cam_intel line
main had not moved since 11 July, while the depth-camera work continued on
dev_cam_intel for six weeks. The two branches did not diverge by accident: they
ended up holding two different clearing designs for the same layer.
main carried frustum clearing as VoxelLayer members - frustum_clearing_enabled_,
a pixel step and a min/max range - driven from costmap_2d_robot. dev_cam_intel
replaced that with a full-column clearing pass whose configuration lives per
observation, so the layer keeps no frustum state of its own.
This commit takes the dev_cam_intel tree whole. It is the line running on the
robot, the one rb_ws points at, and the one the NUC measurements were taken
against; the frustum design is superseded rather than merged, and stays in
history at 45c8227 if it is ever wanted back.
Squashed deliberately: the nineteen commits behind it are a working diary -
metrics that were added and removed, test toggles, an optimal-run series - and
none of that is worth carrying onto a branch other trees track.
This commit is contained in:
@@ -58,7 +58,6 @@ InflationLayer::InflationLayer()
|
||||
, inflate_unknown_(false)
|
||||
, cell_inflation_radius_(0)
|
||||
, cached_cell_inflation_radius_(0)
|
||||
, seen_(NULL)
|
||||
, cached_costs_(NULL)
|
||||
, cached_distances_(NULL)
|
||||
, last_min_x_(-std::numeric_limits<float>::max())
|
||||
@@ -76,10 +75,8 @@ void InflationLayer::onInitialize()
|
||||
|
||||
boost::unique_lock < boost::recursive_mutex > lock(*inflation_access_);
|
||||
current_ = true;
|
||||
if (seen_)
|
||||
delete[] seen_;
|
||||
seen_ = NULL;
|
||||
seen_size_ = 0;
|
||||
seen_.clear();
|
||||
seen_generation_ = 0;
|
||||
need_reinflation_ = false;
|
||||
std::string config_file_name = "inflation_layer_params.yaml";
|
||||
// std::cout << "InflationLayer: " << config_file_name << std::endl;
|
||||
@@ -144,10 +141,8 @@ void InflationLayer::matchSize()
|
||||
computeCaches();
|
||||
|
||||
unsigned int size_x = costmap->getSizeInCellsX(), size_y = costmap->getSizeInCellsY();
|
||||
if (seen_)
|
||||
delete[] seen_;
|
||||
seen_size_ = size_x * size_y;
|
||||
seen_ = new bool[seen_size_];
|
||||
seen_.assign(static_cast<std::size_t>(size_x) * size_y, 0);
|
||||
seen_generation_ = 0;
|
||||
}
|
||||
|
||||
void InflationLayer::updateBounds(double robot_x, double robot_y, double robot_yaw, double* min_x,
|
||||
@@ -203,26 +198,28 @@ void InflationLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int m
|
||||
if (cell_inflation_radius_ == 0)
|
||||
return;
|
||||
|
||||
// make sure the inflation list is empty at the beginning of the cycle (should always be true)
|
||||
if(!inflation_cells_.empty())
|
||||
robot::log_error("The inflation list must be empty at the beginning of inflation\n");
|
||||
for (std::vector<CellData>& cells : inflation_cells_)
|
||||
cells.clear();
|
||||
|
||||
unsigned char* master_array = master_grid.getCharMap();
|
||||
unsigned int size_x = master_grid.getSizeInCellsX(), size_y = master_grid.getSizeInCellsY();
|
||||
|
||||
if (seen_ == NULL) {
|
||||
robot::log_error("InflationLayer::updateCosts(): seen_ array is NULL\n");
|
||||
seen_size_ = size_x * size_y;
|
||||
seen_ = new bool[seen_size_];
|
||||
}
|
||||
else if (seen_size_ != size_x * size_y)
|
||||
const std::size_t map_size = static_cast<std::size_t>(size_x) * size_y;
|
||||
if (seen_.size() != map_size)
|
||||
{
|
||||
robot::log_error("InflationLayer::updateCosts(): seen_ array size is wrong\n");
|
||||
delete[] seen_;
|
||||
seen_size_ = size_x * size_y;
|
||||
seen_ = new bool[seen_size_];
|
||||
seen_.assign(map_size, 0);
|
||||
seen_generation_ = 0;
|
||||
}
|
||||
|
||||
if (seen_generation_ == std::numeric_limits<std::uint32_t>::max())
|
||||
{
|
||||
std::fill(seen_.begin(), seen_.end(), 0);
|
||||
seen_generation_ = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
++seen_generation_;
|
||||
}
|
||||
memset(seen_, false, size_x * size_y * sizeof(bool));
|
||||
|
||||
// We need to include in the inflation cells outside the bounding
|
||||
// box min_i...max_j, by the amount cell_inflation_radius_. Cells
|
||||
@@ -238,11 +235,13 @@ void InflationLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int m
|
||||
max_i = std::min(int(size_x), max_i);
|
||||
max_j = std::min(int(size_y), max_j);
|
||||
|
||||
// Inflation list; we append cells to visit in a list associated with its distance to the nearest obstacle
|
||||
// We use a map<distance, list> to emulate the priority queue used before, with a notable performance boost
|
||||
// Precomputed distance buckets preserve priority ordering without a tree lookup
|
||||
// for every enqueued cell.
|
||||
|
||||
// Start with lethal obstacles: by definition distance is 0.0
|
||||
std::vector<CellData>& obs_bin = inflation_cells_[0.0];
|
||||
if (inflation_cells_.empty())
|
||||
return;
|
||||
std::vector<CellData>& obs_bin = inflation_cells_.front();
|
||||
for (int j = min_j; j < max_j; j++)
|
||||
{
|
||||
for (int i = min_i; i < max_i; i++)
|
||||
@@ -258,23 +257,22 @@ void InflationLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int m
|
||||
|
||||
// Process cells by increasing distance; new cells are appended to the corresponding distance bin, so they
|
||||
// can overtake previously inserted but farther away cells
|
||||
std::map<double, std::vector<CellData> >::iterator bin;
|
||||
for (bin = inflation_cells_.begin(); bin != inflation_cells_.end(); ++bin)
|
||||
for (std::vector<CellData>& bin : inflation_cells_)
|
||||
{
|
||||
for (int i = 0; i < bin->second.size(); ++i)
|
||||
for (std::size_t i = 0; i < bin.size(); ++i)
|
||||
{
|
||||
// process all cells at distance dist_bin.first
|
||||
const CellData& cell = bin->second[i];
|
||||
const CellData& cell = bin[i];
|
||||
|
||||
unsigned int index = cell.index_;
|
||||
|
||||
// ignore if already visited
|
||||
if (seen_[index])
|
||||
if (seen_[index] == seen_generation_)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
seen_[index] = true;
|
||||
seen_[index] = seen_generation_;
|
||||
|
||||
unsigned int mx = cell.x_;
|
||||
unsigned int my = cell.y_;
|
||||
@@ -301,7 +299,6 @@ void InflationLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int m
|
||||
}
|
||||
}
|
||||
|
||||
inflation_cells_.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,7 +313,7 @@ void InflationLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int m
|
||||
inline void InflationLayer::enqueue(unsigned int index, unsigned int mx, unsigned int my,
|
||||
unsigned int src_x, unsigned int src_y)
|
||||
{
|
||||
if (!seen_[index])
|
||||
if (seen_[index] != seen_generation_)
|
||||
{
|
||||
// we compute our distance table one cell further than the inflation radius dictates so we can make the check below
|
||||
double distance = distanceLookup(mx, my, src_x, src_y);
|
||||
@@ -325,8 +322,10 @@ inline void InflationLayer::enqueue(unsigned int index, unsigned int mx, unsigne
|
||||
if (distance > cell_inflation_radius_)
|
||||
return;
|
||||
|
||||
// push the cell data onto the inflation list and mark
|
||||
inflation_cells_[distance].push_back(CellData(index, mx, my, src_x, src_y));
|
||||
const unsigned int dx = std::abs(static_cast<int>(mx) - static_cast<int>(src_x));
|
||||
const unsigned int dy = std::abs(static_cast<int>(my) - static_cast<int>(src_y));
|
||||
const unsigned int bin_index = distance_bin_lookup_[dx * distance_lookup_size_ + dy];
|
||||
inflation_cells_[bin_index].push_back(CellData(index, mx, my, src_x, src_y));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,6 +353,38 @@ void InflationLayer::computeCaches()
|
||||
}
|
||||
|
||||
cached_cell_inflation_radius_ = cell_inflation_radius_;
|
||||
|
||||
distance_lookup_size_ = cell_inflation_radius_ + 2;
|
||||
distance_levels_.clear();
|
||||
for (unsigned int i = 0; i < distance_lookup_size_; ++i)
|
||||
{
|
||||
for (unsigned int j = 0; j < distance_lookup_size_; ++j)
|
||||
{
|
||||
if (cached_distances_[i][j] <= cell_inflation_radius_)
|
||||
distance_levels_.push_back(cached_distances_[i][j]);
|
||||
}
|
||||
}
|
||||
std::sort(distance_levels_.begin(), distance_levels_.end());
|
||||
distance_levels_.erase(
|
||||
std::unique(distance_levels_.begin(), distance_levels_.end()), distance_levels_.end());
|
||||
|
||||
inflation_cells_.clear();
|
||||
inflation_cells_.resize(distance_levels_.size());
|
||||
distance_bin_lookup_.assign(
|
||||
static_cast<std::size_t>(distance_lookup_size_) * distance_lookup_size_, 0);
|
||||
for (unsigned int i = 0; i < distance_lookup_size_; ++i)
|
||||
{
|
||||
for (unsigned int j = 0; j < distance_lookup_size_; ++j)
|
||||
{
|
||||
const double distance = cached_distances_[i][j];
|
||||
if (distance > cell_inflation_radius_)
|
||||
continue;
|
||||
distance_bin_lookup_[i * distance_lookup_size_ + j] =
|
||||
static_cast<unsigned int>(
|
||||
std::lower_bound(distance_levels_.begin(), distance_levels_.end(), distance) -
|
||||
distance_levels_.begin());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i <= cell_inflation_radius_ + 1; ++i)
|
||||
@@ -367,6 +398,10 @@ void InflationLayer::computeCaches()
|
||||
|
||||
void InflationLayer::deleteKernels()
|
||||
{
|
||||
inflation_cells_.clear();
|
||||
distance_levels_.clear();
|
||||
distance_bin_lookup_.clear();
|
||||
distance_lookup_size_ = 0;
|
||||
if (cached_distances_ != NULL)
|
||||
{
|
||||
for (unsigned int i = 0; i <= cached_cell_inflation_radius_ + 1; ++i)
|
||||
|
||||
@@ -131,6 +131,9 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
|
||||
double observation_keep_time = 0, expected_update_rate = 0, min_obstacle_height = 0, max_obstacle_height = 2;
|
||||
std::string topic = "map", sensor_frame = "laser_frame", data_type = "PointCloud";
|
||||
bool inf_is_valid = false, clearing=false, marking=true;
|
||||
bool frustum_clearing_enabled = false;
|
||||
int frustum_pixel_step = 8;
|
||||
DepthFrustumConfig frustum_config;
|
||||
|
||||
robot::NodeHandle priv_nh(nh, source);
|
||||
topic = loadParam(layer[source],"topic", topic);
|
||||
@@ -143,6 +146,34 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
|
||||
inf_is_valid = loadParam(layer[source],"inf_is_valid", false);
|
||||
clearing = loadParam(layer[source],"clearing", false);
|
||||
marking = loadParam(layer[source],"marking", true);
|
||||
// frustum params are per-source; the layer-level key is kept as a
|
||||
// fallback for older YAMLs
|
||||
frustum_clearing_enabled = loadParam(layer[source], "frustum_clearing_enabled",
|
||||
loadParam(layer, "frustum_clearing_enabled", false));
|
||||
frustum_pixel_step = loadParam(layer[source], "frustum_clearing_pixel_step",
|
||||
loadParam(layer, "frustum_clearing_pixel_step", 8));
|
||||
frustum_config.min_range = loadParam(layer[source], "frustum_min_range",
|
||||
loadParam(layer, "frustum_min_range", 0.2));
|
||||
frustum_config.max_range = loadParam(layer[source], "frustum_max_range",
|
||||
loadParam(layer, "frustum_max_range", 3.0));
|
||||
frustum_config.skip_distance =
|
||||
loadParam(layer[source], "frustum_skip_distance", frustum_config.skip_distance);
|
||||
frustum_config.column_clearing =
|
||||
loadParam(layer[source], "frustum_column_clearing", frustum_config.column_clearing);
|
||||
frustum_config.column_min_height =
|
||||
loadParam(layer[source], "column_clear_min_height", frustum_config.column_min_height);
|
||||
frustum_config.column_max_height =
|
||||
loadParam(layer[source], "column_clear_max_height", frustum_config.column_max_height);
|
||||
frustum_config.column_skip_distance =
|
||||
loadParam(layer[source], "column_skip_distance", frustum_config.column_skip_distance);
|
||||
frustum_config.column_cover_distance =
|
||||
loadParam(layer[source], "column_cover_distance", frustum_config.column_cover_distance);
|
||||
int frustum_clear_left_border =
|
||||
loadParam(layer[source], "frustum_clear_left_border_px",
|
||||
loadParam(layer, "frustum_clear_left_border_px", 0));
|
||||
int frustum_clear_right_border =
|
||||
loadParam(layer[source], "frustum_clear_right_border_px",
|
||||
loadParam(layer, "frustum_clear_right_border_px", 0));
|
||||
|
||||
if (priv_nh.hasParam("topic"))
|
||||
priv_nh.getParam("topic", topic);
|
||||
@@ -164,52 +195,114 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
|
||||
priv_nh.getParam("clearing", clearing);
|
||||
if (priv_nh.hasParam("marking"))
|
||||
priv_nh.getParam("marking", marking);
|
||||
|
||||
if (!(data_type == "PointCloud2" || data_type == "PointCloud" || data_type == "LaserScan"))
|
||||
if (priv_nh.hasParam("frustum_clearing_enabled"))
|
||||
priv_nh.getParam("frustum_clearing_enabled", frustum_clearing_enabled);
|
||||
if (priv_nh.hasParam("frustum_clearing_pixel_step"))
|
||||
{
|
||||
robot::log_error("Only topics that use point clouds or laser scans are currently supported\n");
|
||||
throw std::runtime_error("Only topics that use point clouds or laser scans are currently supported");
|
||||
priv_nh.getParam("frustum_clearing_pixel_step", frustum_pixel_step);
|
||||
frustum_pixel_step = std::max(1, frustum_pixel_step);
|
||||
}
|
||||
if (priv_nh.hasParam("frustum_min_range"))
|
||||
priv_nh.getParam("frustum_min_range", frustum_config.min_range);
|
||||
if (priv_nh.hasParam("frustum_max_range"))
|
||||
priv_nh.getParam("frustum_max_range", frustum_config.max_range);
|
||||
if (priv_nh.hasParam("frustum_skip_distance"))
|
||||
priv_nh.getParam("frustum_skip_distance", frustum_config.skip_distance);
|
||||
if (priv_nh.hasParam("frustum_column_clearing"))
|
||||
priv_nh.getParam("frustum_column_clearing", frustum_config.column_clearing);
|
||||
if (priv_nh.hasParam("column_clear_min_height"))
|
||||
priv_nh.getParam("column_clear_min_height", frustum_config.column_min_height);
|
||||
if (priv_nh.hasParam("column_clear_max_height"))
|
||||
priv_nh.getParam("column_clear_max_height", frustum_config.column_max_height);
|
||||
if (priv_nh.hasParam("column_skip_distance"))
|
||||
priv_nh.getParam("column_skip_distance", frustum_config.column_skip_distance);
|
||||
if (priv_nh.hasParam("column_cover_distance"))
|
||||
priv_nh.getParam("column_cover_distance", frustum_config.column_cover_distance);
|
||||
if (priv_nh.hasParam("frustum_clear_left_border_px"))
|
||||
priv_nh.getParam("frustum_clear_left_border_px", frustum_clear_left_border);
|
||||
if (priv_nh.hasParam("frustum_clear_right_border_px"))
|
||||
priv_nh.getParam("frustum_clear_right_border_px", frustum_clear_right_border);
|
||||
if (priv_nh.hasParam("frustum_depth_camera_topic"))
|
||||
priv_nh.getParam("frustum_depth_camera_topic", depth_camera_data_topic_);
|
||||
|
||||
CallBackInfo info_tmp;
|
||||
info_tmp.observation_source = source;
|
||||
info_tmp.data_type = data_type;
|
||||
info_tmp.topic = topic;
|
||||
info_tmp.inf_is_valid = inf_is_valid;
|
||||
callback_infos_.push_back(info_tmp);
|
||||
frustum_config.pixel_step = static_cast<unsigned int>(std::max(1, frustum_pixel_step));
|
||||
frustum_config.clear_left_border_px =
|
||||
static_cast<unsigned int>(std::max(0, frustum_clear_left_border));
|
||||
frustum_config.clear_right_border_px =
|
||||
static_cast<unsigned int>(std::max(0, frustum_clear_right_border));
|
||||
|
||||
std::string raytrace_range_param_name, obstacle_range_param_name;
|
||||
robot::log_info("source %s: frustum_clearing_enabled: %s, pixel_step: %u, range: [%.2f, %.2f] m, "
|
||||
"skip: %.3f m, column_clearing: %s, column_band: [%.2f, %.2f] m, "
|
||||
"column_skip: %.3f m, column_cover: %.2f m, clear_left_border_px: %u px, "
|
||||
"clear_right_border_px: %u px\n",
|
||||
source.c_str(), frustum_clearing_enabled ? "true" : "false",
|
||||
frustum_config.pixel_step, frustum_config.min_range, frustum_config.max_range,
|
||||
frustum_config.skip_distance, frustum_config.column_clearing ? "true" : "false",
|
||||
frustum_config.column_min_height, frustum_config.column_max_height,
|
||||
frustum_config.column_skip_distance, frustum_config.column_cover_distance,
|
||||
frustum_config.clear_left_border_px, frustum_config.clear_right_border_px);
|
||||
|
||||
double obstacle_range = 2.5;
|
||||
obstacle_range = loadParam(layer[source],"obstacle_range", obstacle_range);
|
||||
double raytrace_range = 3.0;
|
||||
raytrace_range = loadParam(layer[source],"raytrace_range", raytrace_range);
|
||||
|
||||
|
||||
|
||||
if (priv_nh.hasParam("obstacle_range"))
|
||||
priv_nh.getParam("obstacle_range", obstacle_range);
|
||||
if (priv_nh.hasParam("raytrace_range"))
|
||||
priv_nh.getParam("raytrace_range", raytrace_range);
|
||||
|
||||
// enabled_ = enabled;
|
||||
if (!(data_type == "PointCloud2" || data_type == "PointCloud" || data_type == "LaserScan" || data_type == "DepthCameraData"))
|
||||
{
|
||||
robot::log_error("Only topics that use point clouds or laser scans are currently supported\n");
|
||||
throw std::runtime_error("Only topics that use point clouds or laser scans are currently supported");
|
||||
}
|
||||
|
||||
robot::log_info("Creating an observation buffer for topic %s, frame %s\n", topic.c_str(),
|
||||
priv_nh.getNamespace().c_str());
|
||||
if(!frustum_clearing_enabled)
|
||||
{
|
||||
|
||||
// create an observation buffer
|
||||
observation_buffers_.push_back(
|
||||
boost::shared_ptr < ObservationBuffer
|
||||
> (new ObservationBuffer(topic, observation_keep_time, expected_update_rate, min_obstacle_height,
|
||||
max_obstacle_height, obstacle_range, raytrace_range, *tf_, global_frame_,
|
||||
sensor_frame, transform_tolerance)));
|
||||
if (marking)
|
||||
marking_buffers_.push_back(observation_buffers_.back());
|
||||
CallBackInfo info_tmp;
|
||||
info_tmp.observation_source = source;
|
||||
info_tmp.data_type = data_type;
|
||||
info_tmp.topic = topic;
|
||||
info_tmp.inf_is_valid = inf_is_valid;
|
||||
callback_infos_.push_back(info_tmp);
|
||||
|
||||
// check if we'll also add this buffer to our clearing observation buffers
|
||||
if (clearing)
|
||||
clearing_buffers_.push_back(observation_buffers_.back());
|
||||
// enabled_ = enabled;
|
||||
|
||||
robot::log_info("Creating an observation buffer for topic %s, frame %s\n", topic.c_str(),
|
||||
priv_nh.getNamespace().c_str());
|
||||
|
||||
// create an observation buffer
|
||||
observation_buffers_.push_back(
|
||||
boost::shared_ptr < ObservationBuffer
|
||||
> (new ObservationBuffer(topic, observation_keep_time, expected_update_rate, min_obstacle_height,
|
||||
max_obstacle_height, obstacle_range, raytrace_range, *tf_, global_frame_,
|
||||
sensor_frame, transform_tolerance)));
|
||||
if (marking)
|
||||
marking_buffers_.push_back(observation_buffers_.back());
|
||||
|
||||
// check if we'll also add this buffer to our clearing observation buffers
|
||||
if (clearing)
|
||||
clearing_buffers_.push_back(observation_buffers_.back());
|
||||
}
|
||||
else
|
||||
{
|
||||
CallBackInfo info_tmp;
|
||||
info_tmp.observation_source = source;
|
||||
info_tmp.data_type = data_type;
|
||||
info_tmp.topic = topic;
|
||||
info_tmp.inf_is_valid = inf_is_valid;
|
||||
callback_depth_infos_.push_back(info_tmp);
|
||||
|
||||
depth_observation_buffers_.push_back(
|
||||
boost::shared_ptr < ObservationBuffer
|
||||
> (new ObservationBuffer(topic, observation_keep_time, expected_update_rate, min_obstacle_height,
|
||||
max_obstacle_height, obstacle_range, raytrace_range, frustum_config,
|
||||
*tf_, global_frame_,
|
||||
sensor_frame, transform_tolerance)));
|
||||
|
||||
}
|
||||
robot::log_info(
|
||||
"Created an observation buffer for topic %s, global frame: %s, "
|
||||
"expected update rate: %.2f, observation persistence: %.2f\n",
|
||||
@@ -230,29 +323,93 @@ void ObstacleLayer::handleImpl(const void* data,
|
||||
const std::type_info& type,
|
||||
const std::string& topic)
|
||||
{
|
||||
if(!stop_receiving_data_)
|
||||
if (!enabled_ || stop_receiving_data_)
|
||||
return;
|
||||
|
||||
if (type == typeid(robot_sensor_msgs::DepthCameraData::ConstPtr))
|
||||
{
|
||||
if (type == typeid(robot_sensor_msgs::DepthCameraData) &&
|
||||
(depth_camera_topic_.empty() || topic == depth_camera_topic_))
|
||||
{
|
||||
const robot_sensor_msgs::DepthCameraData::ConstPtr& depth_camera_data_ptr =
|
||||
*static_cast<const robot_sensor_msgs::DepthCameraData::ConstPtr*>(data);
|
||||
if (!depth_camera_data_ptr)
|
||||
return;
|
||||
|
||||
const robot_sensor_msgs::DepthCameraData& depth_camera_data =
|
||||
*static_cast<const robot_sensor_msgs::DepthCameraData*>(data);
|
||||
if (depth_camera_data.camera_info.K[0] <= 0.0 || depth_camera_data.camera_info.K[4] <= 0.0)
|
||||
*depth_camera_data_ptr;
|
||||
const robot_sensor_msgs::Image& depth = depth_camera_data.depth;
|
||||
const robot_sensor_msgs::CameraInfo& camera_info = depth_camera_data.camera_info;
|
||||
|
||||
std::size_t bytes_per_pixel = 0;
|
||||
if (depth.encoding == "16UC1" || depth.encoding == "mono16")
|
||||
bytes_per_pixel = 2;
|
||||
else if (depth.encoding == "32FC1")
|
||||
bytes_per_pixel = 4;
|
||||
else
|
||||
{
|
||||
robot::log_error("ObstacleLayer received unsupported depth encoding: %s\n", depth.encoding.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
const bool invalid_dimensions = depth.width == 0 || depth.height == 0 ||
|
||||
depth.step < static_cast<std::size_t>(depth.width) * bytes_per_pixel ||
|
||||
depth.data.size() < static_cast<std::size_t>(depth.step) * depth.height;
|
||||
if (invalid_dimensions)
|
||||
{
|
||||
robot::log_error("ObstacleLayer received malformed DepthCameraData image\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (camera_info.K[0] <= 0.0 || camera_info.K[4] <= 0.0)
|
||||
{
|
||||
robot::log_error("ObstacleLayer received invalid camera intrinsics for depth clearing\n");
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(depth_camera_mutex_);
|
||||
latest_depth_camera_data_ = depth_camera_data;
|
||||
have_depth_camera_data_ = true;
|
||||
if ((camera_info.width != 0 && camera_info.width != depth.width) ||
|
||||
(camera_info.height != 0 && camera_info.height != depth.height))
|
||||
{
|
||||
robot::log_error("ObstacleLayer received mismatched depth image and camera info dimensions\n");
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string& depth_frame = depth.header.frame_id;
|
||||
const std::string& camera_frame = camera_info.header.frame_id;
|
||||
if (!depth_frame.empty() && !camera_frame.empty() && depth_frame != camera_frame)
|
||||
{
|
||||
robot::log_error("ObstacleLayer received mismatched depth and camera-info frames: %s != %s\n",
|
||||
depth_frame.c_str(), camera_frame.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (depth_camera_data.header.frame_id.empty() && depth_frame.empty() && camera_frame.empty())
|
||||
{
|
||||
robot::log_error("ObstacleLayer received DepthCameraData without an optical frame\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// std::lock_guard<std::mutex> lock(depth_camera_data_mutex_);
|
||||
// pending_depth_camera_data_ = depth_camera_data_ptr;
|
||||
if (depth_observation_buffers_.empty() || callback_depth_infos_.empty())
|
||||
return;
|
||||
|
||||
int size_callback_depth = static_cast<int>(callback_depth_infos_.size());
|
||||
for(int i = 0; i < size_callback_depth; i++)
|
||||
{
|
||||
boost::shared_ptr<ObservationBuffer>& buffer = depth_observation_buffers_[i];
|
||||
if (type == typeid(robot_sensor_msgs::DepthCameraData::ConstPtr) &&
|
||||
topic == callback_depth_infos_[i].topic)
|
||||
{
|
||||
// robot::log_error_throttle(1.0,"TEST");
|
||||
depthImageCallback(depth_camera_data_ptr, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (observation_buffers_.empty() || callback_infos_.empty())
|
||||
return;
|
||||
}
|
||||
|
||||
if(observation_buffers_.empty() || callback_infos_.empty()) return;
|
||||
|
||||
|
||||
int size_callback = static_cast<int>(callback_infos_.size());
|
||||
for(int i = 0; i < size_callback; i++)
|
||||
for (int i = 0; i < size_callback; i++)
|
||||
{
|
||||
boost::shared_ptr<ObservationBuffer>& buffer = observation_buffers_[i];
|
||||
|
||||
@@ -303,11 +460,6 @@ void ObstacleLayer::handleImpl(const void* data,
|
||||
// }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
robot::log_info("Stop receiving data!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void ObstacleLayer::laserScanCallback(const robot_sensor_msgs::LaserScan& message,
|
||||
@@ -408,6 +560,14 @@ void ObstacleLayer::pointCloud2Callback(const robot_sensor_msgs::PointCloud2& me
|
||||
buffer->unlock();
|
||||
}
|
||||
|
||||
void ObstacleLayer::depthImageCallback(robot_sensor_msgs::DepthCameraData::ConstPtr message,
|
||||
const boost::shared_ptr<ObservationBuffer>& buffer)
|
||||
{
|
||||
buffer->lock();
|
||||
buffer->bufferDepthCamera(std::move(message));
|
||||
buffer->unlock();
|
||||
}
|
||||
|
||||
void ObstacleLayer::updateBounds(double robot_x, double robot_y, double robot_yaw, double* min_x,
|
||||
double* min_y, double* max_x, double* max_y)
|
||||
{
|
||||
@@ -446,6 +606,9 @@ void ObstacleLayer::updateBounds(double robot_x, double robot_y, double robot_ya
|
||||
robot_sensor_msgs::PointCloud2ConstIterator<float> iter_y(cloud, "y");
|
||||
robot_sensor_msgs::PointCloud2ConstIterator<float> iter_z(cloud, "z");
|
||||
|
||||
std::size_t rejected_height = 0;
|
||||
std::size_t rejected_range = 0;
|
||||
std::size_t rejected_bounds = 0;
|
||||
for (; iter_x !=iter_x.end(); ++iter_x, ++iter_y, ++iter_z)
|
||||
{
|
||||
double px = *iter_x, py = *iter_y, pz = *iter_z;
|
||||
@@ -453,7 +616,7 @@ void ObstacleLayer::updateBounds(double robot_x, double robot_y, double robot_ya
|
||||
// if the obstacle is too high or too far away from the robot we won't add it
|
||||
if (pz > max_obstacle_height_)
|
||||
{
|
||||
robot::log_error("The point is too high\n");
|
||||
++rejected_height;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -464,7 +627,7 @@ void ObstacleLayer::updateBounds(double robot_x, double robot_y, double robot_ya
|
||||
// if the point is far enough away... we won't consider it
|
||||
if (sq_dist >= sq_obstacle_range)
|
||||
{
|
||||
robot::log_error("The point is too far away\n");
|
||||
++rejected_range;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -472,7 +635,7 @@ void ObstacleLayer::updateBounds(double robot_x, double robot_y, double robot_ya
|
||||
unsigned int mx, my;
|
||||
if (!worldToMap(px, py, mx, my))
|
||||
{
|
||||
robot::log_error("Computing map coords failed\n");
|
||||
++rejected_bounds;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -480,6 +643,14 @@ void ObstacleLayer::updateBounds(double robot_x, double robot_y, double robot_ya
|
||||
costmap_[index] = LETHAL_OBSTACLE;
|
||||
touch(px, py, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
|
||||
if (rejected_height + rejected_range + rejected_bounds > 0)
|
||||
{
|
||||
robot::log_info_throttle(
|
||||
5.0,
|
||||
"ObstacleLayer filtered points: height=%zu range=%zu outside_map=%zu\n",
|
||||
rejected_height, rejected_range, rejected_bounds);
|
||||
}
|
||||
}
|
||||
|
||||
updateFootprint(robot_x, robot_y, robot_yaw, min_x, min_y, max_x, max_y);
|
||||
@@ -566,6 +737,23 @@ bool ObstacleLayer::getClearingObservations(std::vector<Observation>& clearing_o
|
||||
return current;
|
||||
}
|
||||
|
||||
bool ObstacleLayer::getFrustumClearingObservations(std::vector<DepthCameraObservation>& frustum_clearing_observations) const
|
||||
{
|
||||
bool current = true;
|
||||
// DepthCameraObservation depth_obs;
|
||||
|
||||
for (const boost::shared_ptr<ObservationBuffer>& buffer : depth_observation_buffers_)
|
||||
{
|
||||
buffer->lock();
|
||||
buffer->getDepthObservations(frustum_clearing_observations);
|
||||
current = buffer->isCurrent() && current;
|
||||
buffer->unlock();
|
||||
// frustum_clearing_observations.push_back(depth_obs);
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
void ObstacleLayer::raytraceFreespace(const Observation& clearing_observation, double* min_x, double* min_y,
|
||||
double* max_x, double* max_y)
|
||||
{
|
||||
|
||||
@@ -98,11 +98,6 @@ bool VoxelLayer::getParams(const std::string& config_file_name, robot::NodeHandl
|
||||
unknown_threshold_ = loadParam(layer, "unknown_threshold", 15.0) + (VOXEL_BITS - size_z_);
|
||||
mark_threshold_ = loadParam(layer, "mark_threshold", 0);
|
||||
combination_method_ = loadParam(layer, "combination_method", 0.0);
|
||||
frustum_clearing_enabled_ = loadParam(layer, "frustum_clearing_enabled", false);
|
||||
frustum_clearing_pixel_step_ = loadParam(layer, "frustum_clearing_pixel_step", 8);
|
||||
frustum_min_range_ = loadParam(layer, "frustum_min_range", 0.2);
|
||||
frustum_max_range_ = loadParam(layer, "frustum_max_range", 3.0);
|
||||
frustum_depth_camera_topic_ = loadParam(layer, "frustum_depth_camera_topic", std::string("/camera/depth/image_raw"));
|
||||
|
||||
int size_z, unknown_threshold, mark_threshold, frustum_pixel_step;
|
||||
if (nh.hasParam("enabled"))
|
||||
@@ -130,25 +125,6 @@ bool VoxelLayer::getParams(const std::string& config_file_name, robot::NodeHandl
|
||||
}
|
||||
if (nh.hasParam("combination_method"))
|
||||
nh.getParam("combination_method", combination_method_);
|
||||
if (nh.hasParam("frustum_clearing_enabled"))
|
||||
nh.getParam("frustum_clearing_enabled", frustum_clearing_enabled_);
|
||||
if (nh.hasParam("frustum_clearing_pixel_step"))
|
||||
{
|
||||
nh.getParam("frustum_clearing_pixel_step", frustum_pixel_step);
|
||||
frustum_clearing_pixel_step_ = std::max(1, frustum_pixel_step);
|
||||
}
|
||||
if (nh.hasParam("frustum_min_range"))
|
||||
nh.getParam("frustum_min_range", frustum_min_range_);
|
||||
if (nh.hasParam("frustum_max_range"))
|
||||
nh.getParam("frustum_max_range", frustum_max_range_);
|
||||
if (nh.hasParam("frustum_depth_camera_topic"))
|
||||
nh.getParam("frustum_depth_camera_topic", frustum_depth_camera_topic_);
|
||||
else if (nh.hasParam("frustum_depth_topic"))
|
||||
nh.getParam("frustum_depth_topic", frustum_depth_camera_topic_);
|
||||
|
||||
frustum_clearing_pixel_step_ = std::max(1u, frustum_clearing_pixel_step_);
|
||||
frustum_max_range_ = std::max(frustum_max_range_, frustum_min_range_ + resolution_);
|
||||
depth_camera_topic_ = frustum_depth_camera_topic_;
|
||||
|
||||
this->matchSize();
|
||||
}
|
||||
@@ -196,6 +172,7 @@ void VoxelLayer::updateBounds(double robot_x, double robot_y, double robot_yaw,
|
||||
|
||||
bool current = true;
|
||||
std::vector<Observation> observations, clearing_observations;
|
||||
std::vector<DepthCameraObservation> depth_observations;
|
||||
|
||||
// get the marking observations
|
||||
current = getMarkingObservations(observations) && current;
|
||||
@@ -203,11 +180,15 @@ void VoxelLayer::updateBounds(double robot_x, double robot_y, double robot_yaw,
|
||||
// get the clearing observations
|
||||
current = getClearingObservations(clearing_observations) && current;
|
||||
|
||||
current = getFrustumClearingObservations(depth_observations) && current;
|
||||
|
||||
// update the global current status
|
||||
current_ = current;
|
||||
|
||||
if (frustum_clearing_enabled_)
|
||||
raytraceDepthFrustum(min_x, min_y, max_x, max_y);
|
||||
for (const DepthCameraObservation& depth_observation : depth_observations)
|
||||
{
|
||||
raytraceDepthFrustum(depth_observation, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
|
||||
// raytrace freespace
|
||||
for (unsigned int i = 0; i < clearing_observations.size(); ++i)
|
||||
@@ -265,29 +246,6 @@ void VoxelLayer::updateBounds(double robot_x, double robot_y, double robot_yaw,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (publish_voxel_)
|
||||
// {
|
||||
// robot_costmap_2d::VoxelGrid grid_msg;
|
||||
// unsigned int size = robot_voxel_grid_.sizeX() * robot_voxel_grid_.sizeY();
|
||||
// grid_msg.size_x = robot_voxel_grid_.sizeX();
|
||||
// grid_msg.size_y = robot_voxel_grid_.sizeY();
|
||||
// grid_msg.size_z = robot_voxel_grid_.sizeZ();
|
||||
// grid_msg.data.resize(size);
|
||||
// memcpy(&grid_msg.data[0], robot_voxel_grid_.getData(), size * sizeof(unsigned int));
|
||||
|
||||
// grid_msg.origin.x = origin_x_;
|
||||
// grid_msg.origin.y = origin_y_;
|
||||
// grid_msg.origin.z = origin_z_;
|
||||
|
||||
// grid_msg.resolutions.x = resolution_;
|
||||
// grid_msg.resolutions.y = resolution_;
|
||||
// grid_msg.resolutions.z = z_resolution_;
|
||||
// grid_msg.header.frame_id = global_frame_;
|
||||
// grid_msg.header.stamp = robot::Time::now();
|
||||
// voxel_pub_.publish(grid_msg);
|
||||
// }
|
||||
|
||||
updateFootprint(robot_x, robot_y, robot_yaw, min_x, min_y, max_x, max_y);
|
||||
}
|
||||
|
||||
@@ -361,14 +319,6 @@ void VoxelLayer::raytraceFreespace(const Observation& clearing_observation, doub
|
||||
ox, oy, oz);
|
||||
return;
|
||||
}
|
||||
|
||||
// bool publish_clearing_points = (clearing_endpoints_pub_.getNumSubscribers() > 0);
|
||||
// if (publish_clearing_points)
|
||||
// {
|
||||
// clearing_endpoints_.points.clear();
|
||||
// clearing_endpoints_.points.reserve(clearing_observation_cloud_size);
|
||||
// }
|
||||
|
||||
// we can pre-compute the enpoints of the map outside of the inner loop... we'll need these later
|
||||
double map_end_x = origin_x_ + getSizeInMetersX();
|
||||
double map_end_y = origin_y_ + getSizeInMetersY();
|
||||
@@ -443,26 +393,8 @@ void VoxelLayer::raytraceFreespace(const Observation& clearing_observation, doub
|
||||
cell_raytrace_range);
|
||||
|
||||
updateRaytraceBounds(ox, oy, wpx, wpy, clearing_observation.raytrace_range_, min_x, min_y, max_x, max_y);
|
||||
|
||||
// if (publish_clearing_points)
|
||||
// {
|
||||
// robot_geometry_msgs::Point32 point;
|
||||
// point.x = wpx;
|
||||
// point.y = wpy;
|
||||
// point.z = wpz;
|
||||
// clearing_endpoints_.points.push_back(point);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
// if (publish_clearing_points)
|
||||
// {
|
||||
// clearing_endpoints_.header.frame_id = global_frame_;
|
||||
// clearing_endpoints_.header.stamp = clearing_observation.cloud_->header.stamp;
|
||||
// clearing_endpoints_.header.seq = clearing_observation.cloud_->header.seq;
|
||||
|
||||
// clearing_endpoints_pub_.publish(clearing_endpoints_);
|
||||
// }
|
||||
}
|
||||
|
||||
bool VoxelLayer::readDepthMeters(const robot_sensor_msgs::Image& depth, unsigned int u, unsigned int v,
|
||||
@@ -524,6 +456,58 @@ bool VoxelLayer::readDepthMeters(const robot_sensor_msgs::Image& depth, unsigned
|
||||
return false;
|
||||
}
|
||||
|
||||
void VoxelLayer::updateDepthRayCache(unsigned int width, unsigned int height,
|
||||
unsigned int pixel_step, double fx, double fy,
|
||||
double cx, double cy)
|
||||
{
|
||||
if (cached_depth_width_ == width && cached_depth_height_ == height &&
|
||||
cached_depth_pixel_step_ == pixel_step && cached_fx_ == fx && cached_fy_ == fy &&
|
||||
cached_cx_ == cx && cached_cy_ == cy)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
cached_depth_width_ = width;
|
||||
cached_depth_height_ = height;
|
||||
cached_depth_pixel_step_ = pixel_step;
|
||||
cached_fx_ = fx;
|
||||
cached_fy_ = fy;
|
||||
cached_cx_ = cx;
|
||||
cached_cy_ = cy;
|
||||
|
||||
// Sample every pixel_step-th row/column and always include the last image
|
||||
// row/column, so cells marked from border pixels stay inside the swept
|
||||
// clearing fan.
|
||||
std::vector<unsigned int> u_samples, v_samples;
|
||||
u_samples.reserve(width / pixel_step + 2);
|
||||
v_samples.reserve(height / pixel_step + 2);
|
||||
for (unsigned int u = 0; u < width; u += pixel_step)
|
||||
u_samples.push_back(u);
|
||||
if (width > 0 && u_samples.back() != width - 1)
|
||||
u_samples.push_back(width - 1);
|
||||
for (unsigned int v = 0; v < height; v += pixel_step)
|
||||
v_samples.push_back(v);
|
||||
if (height > 0 && v_samples.back() != height - 1)
|
||||
v_samples.push_back(height - 1);
|
||||
|
||||
cached_column_count_ = static_cast<unsigned int>(u_samples.size());
|
||||
depth_ray_cache_.clear();
|
||||
depth_ray_cache_.reserve(u_samples.size() * v_samples.size());
|
||||
|
||||
for (const unsigned int v : v_samples)
|
||||
{
|
||||
for (unsigned int col = 0; col < u_samples.size(); ++col)
|
||||
{
|
||||
const unsigned int u = u_samples[col];
|
||||
const double x = (static_cast<double>(u) - cx) / fx;
|
||||
const double y = (static_cast<double>(v) - cy) / fy;
|
||||
const double inverse_norm = 1.0 / std::sqrt(x * x + y * y + 1.0);
|
||||
depth_ray_cache_.push_back(
|
||||
DepthRay{u, v, col, x * inverse_norm, y * inverse_norm, inverse_norm});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool VoxelLayer::clipRaytraceEndpoint(double ox, double oy, double oz, double& wx, double& wy, double& wz)
|
||||
{
|
||||
double a = wx - ox;
|
||||
@@ -562,7 +546,8 @@ bool VoxelLayer::clipRaytraceEndpoint(double ox, double oy, double oz, double& w
|
||||
}
|
||||
|
||||
bool VoxelLayer::clearVoxelRay(double ox, double oy, double oz, double wx, double wy, double wz,
|
||||
double raytrace_range, double* min_x, double* min_y, double* max_x, double* max_y)
|
||||
double raytrace_range, unsigned int cell_raytrace_range,
|
||||
double* min_x, double* min_y, double* max_x, double* max_y)
|
||||
{
|
||||
double sensor_x, sensor_y, sensor_z;
|
||||
if (!worldToMap3DFloat(ox, oy, oz, sensor_x, sensor_y, sensor_z))
|
||||
@@ -577,21 +562,18 @@ bool VoxelLayer::clearVoxelRay(double ox, double oy, double oz, double wx, doubl
|
||||
|
||||
robot_voxel_grid_.clearVoxelLineInMap(sensor_x, sensor_y, sensor_z, point_x, point_y, point_z, costmap_,
|
||||
unknown_threshold_, mark_threshold_, FREE_SPACE, NO_INFORMATION,
|
||||
cellDistance(raytrace_range));
|
||||
cell_raytrace_range);
|
||||
updateRaytraceBounds(ox, oy, wx, wy, raytrace_range, min_x, min_y, max_x, max_y);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VoxelLayer::raytraceDepthFrustum(double* min_x, double* min_y, double* max_x, double* max_y)
|
||||
bool VoxelLayer::raytraceDepthFrustum(const DepthCameraObservation& observation,
|
||||
double* min_x, double* min_y, double* max_x, double* max_y)
|
||||
{
|
||||
robot_sensor_msgs::DepthCameraData depth_camera_data;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(depth_camera_mutex_);
|
||||
if (!have_depth_camera_data_)
|
||||
return false;
|
||||
depth_camera_data = latest_depth_camera_data_;
|
||||
}
|
||||
if (!observation.data_)
|
||||
return false;
|
||||
|
||||
const robot_sensor_msgs::DepthCameraData& depth_camera_data = *observation.data_;
|
||||
const robot_sensor_msgs::Image& depth = depth_camera_data.depth;
|
||||
const robot_sensor_msgs::CameraInfo& camera_info = depth_camera_data.camera_info;
|
||||
|
||||
@@ -605,13 +587,17 @@ bool VoxelLayer::raytraceDepthFrustum(double* min_x, double* min_y, double* max_
|
||||
if (fx <= 0.0 || fy <= 0.0)
|
||||
return false;
|
||||
|
||||
const std::string depth_frame = depth.header.frame_id.empty() ? camera_info.header.frame_id : depth.header.frame_id;
|
||||
std::string depth_frame = depth.header.frame_id.empty() ? depth_camera_data.header.frame_id : depth.header.frame_id;
|
||||
if (depth_frame.empty())
|
||||
depth_frame = camera_info.header.frame_id;
|
||||
if (depth_frame.empty() || tf_ == nullptr)
|
||||
return false;
|
||||
|
||||
robot_geometry_msgs::PointStamped local_origin;
|
||||
local_origin.header = depth.header;
|
||||
local_origin.header.frame_id = depth_frame;
|
||||
if (local_origin.header.stamp.isZero())
|
||||
local_origin.header.stamp = depth_camera_data.header.stamp;
|
||||
local_origin.point.x = 0.0;
|
||||
local_origin.point.y = 0.0;
|
||||
local_origin.point.z = 0.0;
|
||||
@@ -625,11 +611,12 @@ bool VoxelLayer::raytraceDepthFrustum(double* min_x, double* min_y, double* max_
|
||||
}
|
||||
catch (tf3::TransformException& ex)
|
||||
{
|
||||
robot::log_error("VoxelLayer frustum clearing TF exception from %s to %s: %s\n",
|
||||
depth_frame.c_str(), global_frame_.c_str(), ex.what());
|
||||
robot::log_error_throttle(
|
||||
5.0, "VoxelLayer depth topic [%s] TF exception from %s to %s: %s\n",
|
||||
observation.topic_.c_str(), depth_frame.c_str(), global_frame_.c_str(), ex.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
const double ox = global_origin.point.x;
|
||||
const double oy = global_origin.point.y;
|
||||
const double oz = global_origin.point.z;
|
||||
@@ -637,66 +624,307 @@ bool VoxelLayer::raytraceDepthFrustum(double* min_x, double* min_y, double* max_
|
||||
double sensor_x, sensor_y, sensor_z;
|
||||
if (!worldToMap3DFloat(ox, oy, oz, sensor_x, sensor_y, sensor_z))
|
||||
{
|
||||
robot::log_error(
|
||||
"The origin for the depth sensor at (%.2f, %.2f, %.2f) is out of map bounds. So, the costmap cannot frustum-clear for it.\n",
|
||||
ox, oy, oz);
|
||||
robot::log_error_throttle(
|
||||
5.0, "VoxelLayer depth topic [%s] origin at (%.2f, %.2f, %.2f) is outside the voxel map\n",
|
||||
observation.topic_.c_str(), ox, oy, oz);
|
||||
return false;
|
||||
}
|
||||
|
||||
const unsigned int step = std::max(1u, frustum_clearing_pixel_step_);
|
||||
const double skip_dist = 2.0 * resolution_;
|
||||
const DepthFrustumConfig& frustum = observation.frustum_;
|
||||
const unsigned int step = std::max(1u, frustum.pixel_step);
|
||||
const double min_range = frustum.min_range;
|
||||
const double max_range = frustum.max_range;
|
||||
const double skip_dist =
|
||||
frustum.skip_distance >= 0.0 ? frustum.skip_distance : 2.0 * resolution_;
|
||||
const unsigned int width = std::min(depth.width, camera_info.width == 0 ? depth.width : camera_info.width);
|
||||
const unsigned int height = std::min(depth.height, camera_info.height == 0 ? depth.height : camera_info.height);
|
||||
updateDepthRayCache(width, height, step, fx, fy, cx, cy);
|
||||
|
||||
double qx = tfm.transform.rotation.x;
|
||||
double qy = tfm.transform.rotation.y;
|
||||
double qz = tfm.transform.rotation.z;
|
||||
double qw = tfm.transform.rotation.w;
|
||||
const double quaternion_norm = std::sqrt(qx * qx + qy * qy + qz * qz + qw * qw);
|
||||
if (quaternion_norm <= 0.0)
|
||||
return false;
|
||||
qx /= quaternion_norm;
|
||||
qy /= quaternion_norm;
|
||||
qz /= quaternion_norm;
|
||||
qw /= quaternion_norm;
|
||||
|
||||
const double r00 = 1.0 - 2.0 * (qy * qy + qz * qz);
|
||||
const double r01 = 2.0 * (qx * qy - qz * qw);
|
||||
const double r02 = 2.0 * (qx * qz + qy * qw);
|
||||
const double r10 = 2.0 * (qx * qy + qz * qw);
|
||||
const double r11 = 1.0 - 2.0 * (qx * qx + qz * qz);
|
||||
const double r12 = 2.0 * (qy * qz - qx * qw);
|
||||
const double r20 = 2.0 * (qx * qz - qy * qw);
|
||||
const double r21 = 2.0 * (qy * qz + qx * qw);
|
||||
const double r22 = 1.0 - 2.0 * (qx * qx + qy * qy);
|
||||
const unsigned int cell_raytrace_range = cellDistance(max_range);
|
||||
bool cleared_any = false;
|
||||
|
||||
for (unsigned int v = 0; v < height; v += step)
|
||||
// Column clearing: certify the beam length per pixel column and the distance
|
||||
// window [cover, far] where the vertical FOV spans the whole height band.
|
||||
// Outside that window a real obstacle could sit above/below the FOV, so only
|
||||
// the per-pixel 3D rays may clear there.
|
||||
const double band_min_h = frustum.column_min_height;
|
||||
const double band_max_h =
|
||||
frustum.column_max_height >= 0.0 ? frustum.column_max_height : max_obstacle_height_;
|
||||
double cover_dist = frustum.column_cover_distance;
|
||||
double far_dist = std::numeric_limits<double>::infinity();
|
||||
bool column_pass = frustum.column_clearing && band_max_h > band_min_h;
|
||||
|
||||
if (column_pass && cover_dist < 0.0)
|
||||
{
|
||||
for (unsigned int u = 0; u < width; u += step)
|
||||
const double up_half = std::atan2(cy, fy);
|
||||
const double down_half = std::atan2(static_cast<double>(height) - 1.0 - cy, fy);
|
||||
const double axis_elev = std::atan2(r22, std::hypot(r02, r12));
|
||||
const double alpha_top = axis_elev + up_half;
|
||||
const double alpha_bot = axis_elev - down_half;
|
||||
const double band_top = band_max_h - oz;
|
||||
const double band_bot = band_min_h - oz;
|
||||
constexpr double kMinSlope = 1e-3;
|
||||
|
||||
cover_dist = 0.0;
|
||||
if (band_top > 0.0)
|
||||
{
|
||||
double depth_m = 0.0;
|
||||
bool valid = false;
|
||||
if (!readDepthMeters(depth, u, v, depth_m, valid))
|
||||
continue;
|
||||
|
||||
double ray_len = frustum_max_range_;
|
||||
if (valid && depth_m < frustum_max_range_)
|
||||
ray_len = std::max(0.0, depth_m - skip_dist);
|
||||
|
||||
if (ray_len <= frustum_min_range_)
|
||||
continue;
|
||||
|
||||
double dx = (static_cast<double>(u) - cx) / fx;
|
||||
double dy = (static_cast<double>(v) - cy) / fy;
|
||||
double dz = 1.0;
|
||||
const double norm = std::sqrt(dx * dx + dy * dy + dz * dz);
|
||||
if (norm <= 0.0)
|
||||
continue;
|
||||
|
||||
robot_geometry_msgs::Vector3 local_ray;
|
||||
local_ray.x = dx / norm;
|
||||
local_ray.y = dy / norm;
|
||||
local_ray.z = dz / norm;
|
||||
|
||||
robot_geometry_msgs::Vector3 global_ray;
|
||||
tf3::doTransform(local_ray, global_ray, tfm);
|
||||
const double global_norm =
|
||||
std::sqrt(global_ray.x * global_ray.x + global_ray.y * global_ray.y + global_ray.z * global_ray.z);
|
||||
if (global_norm <= 0.0)
|
||||
continue;
|
||||
|
||||
global_ray.x /= global_norm;
|
||||
global_ray.y /= global_norm;
|
||||
global_ray.z /= global_norm;
|
||||
|
||||
const double sx = ox + global_ray.x * frustum_min_range_;
|
||||
const double sy = oy + global_ray.y * frustum_min_range_;
|
||||
const double sz = oz + global_ray.z * frustum_min_range_;
|
||||
const double wx = ox + global_ray.x * ray_len;
|
||||
const double wy = oy + global_ray.y * ray_len;
|
||||
const double wz = oz + global_ray.z * ray_len;
|
||||
|
||||
cleared_any = clearVoxelRay(sx, sy, sz, wx, wy, wz, ray_len, min_x, min_y, max_x, max_y) || cleared_any;
|
||||
if (alpha_top <= kMinSlope)
|
||||
column_pass = false; // camera can never look up to the band top
|
||||
else
|
||||
cover_dist = std::max(cover_dist, band_top / std::tan(alpha_top));
|
||||
}
|
||||
else if (alpha_top < -kMinSlope)
|
||||
{
|
||||
far_dist = std::min(far_dist, band_top / std::tan(alpha_top));
|
||||
}
|
||||
if (band_bot < 0.0)
|
||||
{
|
||||
if (alpha_bot >= -kMinSlope)
|
||||
column_pass = false; // camera can never look down to the band bottom
|
||||
else
|
||||
cover_dist = std::max(cover_dist, band_bot / std::tan(alpha_bot));
|
||||
}
|
||||
else if (alpha_bot > kMinSlope)
|
||||
{
|
||||
far_dist = std::min(far_dist, band_bot / std::tan(alpha_bot));
|
||||
}
|
||||
|
||||
if (!column_pass)
|
||||
{
|
||||
robot::log_warning_throttle(
|
||||
10.0, "VoxelLayer column clearing disabled: vertical FOV [%.1f, %.1f] deg at camera "
|
||||
"height %.2f m never covers band [%.2f, %.2f] m\n",
|
||||
alpha_bot * 180.0 / M_PI, alpha_top * 180.0 / M_PI, oz, band_min_h, band_max_h);
|
||||
}
|
||||
}
|
||||
|
||||
if (column_pass)
|
||||
depth_column_stats_.assign(cached_column_count_, DepthColumnStat());
|
||||
|
||||
for (const DepthRay& local_ray : depth_ray_cache_)
|
||||
{
|
||||
double depth_m = 0.0;
|
||||
bool valid = false;
|
||||
if (!readDepthMeters(depth, local_ray.u, local_ray.v, depth_m, valid))
|
||||
continue;
|
||||
|
||||
// Edge stereo no-disparity strips (left and/or right, depending on the
|
||||
// camera). An INVALID pixel in such a strip is structurally invalid (carries
|
||||
// no free-space evidence), so clearing it out to max_range erases obstacles
|
||||
// rotating out of the FOV on that side (the turn bug) — skip only those. A
|
||||
// VALID return there is a real measured surface, so it must still clear
|
||||
// normally; otherwise the border becomes a clearing dead zone and obstacles
|
||||
// there never get cleared. Invalid pixels OUTSIDE the strips still clear to
|
||||
// max_range (ghost removal). Right edge measured inward from the last column;
|
||||
// the unsigned test avoids underflow when the border exceeds the width.
|
||||
const bool in_left_border = local_ray.u < frustum.clear_left_border_px;
|
||||
const bool in_right_border =
|
||||
frustum.clear_right_border_px > 0 &&
|
||||
local_ray.u + frustum.clear_right_border_px >= width;
|
||||
const bool in_border = in_left_border || in_right_border;
|
||||
if (in_border && !valid)
|
||||
continue;
|
||||
|
||||
// depth images store z-depth; local_ray.z is the unit ray's optical axis
|
||||
// component, so depth / z is the Euclidean range
|
||||
const double euclid_range = valid ? depth_m / local_ray.z : 0.0;
|
||||
|
||||
robot_geometry_msgs::Vector3 global_ray;
|
||||
global_ray.x = r00 * local_ray.x + r01 * local_ray.y + r02 * local_ray.z;
|
||||
global_ray.y = r10 * local_ray.x + r11 * local_ray.y + r12 * local_ray.z;
|
||||
global_ray.z = r20 * local_ray.x + r21 * local_ray.y + r22 * local_ray.z;
|
||||
|
||||
if (column_pass && local_ray.col < depth_column_stats_.size())
|
||||
{
|
||||
const double horiz_norm = std::hypot(global_ray.x, global_ray.y);
|
||||
if (horiz_norm > 1e-6)
|
||||
{
|
||||
DepthColumnStat& stat = depth_column_stats_[local_ray.col];
|
||||
const double row_delta = std::fabs(static_cast<double>(local_ray.v) - cy);
|
||||
if (stat.min_band_dist < 0.0 && row_delta < stat.best_row_delta)
|
||||
{
|
||||
// no in-band return yet: aim the beam along the ray nearest the
|
||||
// principal row
|
||||
stat.azimuth = std::atan2(global_ray.y, global_ray.x);
|
||||
stat.best_row_delta = row_delta;
|
||||
}
|
||||
stat.has_ray = true;
|
||||
if (in_border)
|
||||
stat.in_border = true;
|
||||
|
||||
if (valid)
|
||||
{
|
||||
const double pz = oz + global_ray.z * euclid_range;
|
||||
if (pz >= band_min_h && pz <= band_max_h)
|
||||
{
|
||||
const double dist_h = horiz_norm * euclid_range;
|
||||
if (stat.min_band_dist < 0.0 || dist_h < stat.min_band_dist)
|
||||
{
|
||||
stat.min_band_dist = dist_h;
|
||||
stat.azimuth = std::atan2(global_ray.y, global_ray.x);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double ray_len = max_range;
|
||||
if (valid && euclid_range < max_range)
|
||||
ray_len = std::max(0.0, euclid_range - skip_dist);
|
||||
|
||||
if (ray_len <= min_range)
|
||||
continue;
|
||||
|
||||
const double sx = ox + global_ray.x * min_range;
|
||||
const double sy = oy + global_ray.y * min_range;
|
||||
const double sz = oz + global_ray.z * min_range;
|
||||
const double wx = ox + global_ray.x * ray_len;
|
||||
const double wy = oy + global_ray.y * ray_len;
|
||||
const double wz = oz + global_ray.z * ray_len;
|
||||
|
||||
cleared_any = clearVoxelRay(sx, sy, sz, wx, wy, wz, ray_len, cell_raytrace_range,
|
||||
min_x, min_y, max_x, max_y) || cleared_any;
|
||||
}
|
||||
|
||||
if (column_pass)
|
||||
{
|
||||
cleared_any = clearDepthColumns(ox, oy, cover_dist, far_dist, min_range, max_range,
|
||||
std::max(0.0, frustum.column_skip_distance),
|
||||
min_x, min_y, max_x, max_y) ||
|
||||
cleared_any;
|
||||
}
|
||||
|
||||
return cleared_any;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
/// raytraceLine action: frees the 2D cell and wipes its whole voxel column.
|
||||
class ClearFullColumn
|
||||
{
|
||||
public:
|
||||
ClearFullColumn(unsigned char* costmap, robot_voxel_grid::VoxelGrid& voxel_grid)
|
||||
: costmap_(costmap), voxel_grid_(voxel_grid)
|
||||
{
|
||||
}
|
||||
|
||||
inline void operator()(unsigned int offset)
|
||||
{
|
||||
costmap_[offset] = FREE_SPACE;
|
||||
voxel_grid_.clearVoxelColumn(offset);
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned char* costmap_;
|
||||
robot_voxel_grid::VoxelGrid& voxel_grid_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
bool VoxelLayer::clipColumnSegment(double& sx, double& sy, double& ex, double& ey) const
|
||||
{
|
||||
// Liang-Barsky clip against the map interior; the half-resolution margin
|
||||
// keeps clipped endpoints valid for worldToMap.
|
||||
const double min_wx = origin_x_;
|
||||
const double min_wy = origin_y_;
|
||||
const double max_wx = origin_x_ + getSizeInMetersX() - 0.5 * resolution_;
|
||||
const double max_wy = origin_y_ + getSizeInMetersY() - 0.5 * resolution_;
|
||||
const double dx = ex - sx;
|
||||
const double dy = ey - sy;
|
||||
const double p[4] = {-dx, dx, -dy, dy};
|
||||
const double q[4] = {sx - min_wx, max_wx - sx, sy - min_wy, max_wy - sy};
|
||||
|
||||
double t0 = 0.0;
|
||||
double t1 = 1.0;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
if (std::fabs(p[i]) < 1e-12)
|
||||
{
|
||||
if (q[i] < 0.0)
|
||||
return false;
|
||||
continue;
|
||||
}
|
||||
const double r = q[i] / p[i];
|
||||
if (p[i] < 0.0)
|
||||
t0 = std::max(t0, r);
|
||||
else
|
||||
t1 = std::min(t1, r);
|
||||
}
|
||||
if (t0 > t1)
|
||||
return false;
|
||||
|
||||
const double bx = sx;
|
||||
const double by = sy;
|
||||
sx = bx + t0 * dx;
|
||||
sy = by + t0 * dy;
|
||||
ex = bx + t1 * dx;
|
||||
ey = by + t1 * dy;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool VoxelLayer::clearDepthColumns(double ox, double oy, double cover_distance,
|
||||
double far_distance, double min_range, double max_range,
|
||||
double skip_dist, double* min_x, double* min_y,
|
||||
double* max_x, double* max_y)
|
||||
{
|
||||
const double start_dist = std::max(cover_distance, min_range);
|
||||
bool cleared_any = false;
|
||||
|
||||
for (const DepthColumnStat& stat : depth_column_stats_)
|
||||
{
|
||||
if (!stat.has_ray)
|
||||
continue;
|
||||
|
||||
// In an edge stereo strip, never clear a whole column out to max_range on a
|
||||
// missing in-band return: that is exactly the no-free-space-evidence case that
|
||||
// erases obstacles turning out of view. Only an in-band measured surface may
|
||||
// shorten (and thus clear) a border column.
|
||||
if (stat.in_border && stat.min_band_dist < 0.0)
|
||||
continue;
|
||||
|
||||
double end_dist = stat.min_band_dist >= 0.0 ? stat.min_band_dist - skip_dist : max_range;
|
||||
end_dist = std::min(std::min(end_dist, max_range), far_distance);
|
||||
if (end_dist <= start_dist)
|
||||
continue;
|
||||
|
||||
const double cos_az = std::cos(stat.azimuth);
|
||||
const double sin_az = std::sin(stat.azimuth);
|
||||
double sx = ox + cos_az * start_dist;
|
||||
double sy = oy + sin_az * start_dist;
|
||||
double ex = ox + cos_az * end_dist;
|
||||
double ey = oy + sin_az * end_dist;
|
||||
if (!clipColumnSegment(sx, sy, ex, ey))
|
||||
continue;
|
||||
|
||||
unsigned int sx_m, sy_m, ex_m, ey_m;
|
||||
if (!worldToMap(sx, sy, sx_m, sy_m) || !worldToMap(ex, ey, ex_m, ey_m))
|
||||
continue;
|
||||
|
||||
ClearFullColumn clearer(costmap_, robot_voxel_grid_);
|
||||
raytraceLine(clearer, sx_m, sy_m, ex_m, ey_m);
|
||||
touch(sx, sy, min_x, min_y, max_x, max_y);
|
||||
touch(ex, ey, min_x, min_y, max_x, max_y);
|
||||
cleared_any = true;
|
||||
}
|
||||
|
||||
return cleared_any;
|
||||
@@ -709,6 +937,11 @@ void VoxelLayer::updateOrigin(double new_origin_x, double new_origin_y)
|
||||
cell_ox = int((new_origin_x - origin_x_) / resolution_);
|
||||
cell_oy = int((new_origin_y - origin_y_) / resolution_);
|
||||
|
||||
// Most update cycles do not cross a costmap cell boundary. Avoid copying and
|
||||
// resetting the complete 2D/3D grids when the cell-aligned origin is unchanged.
|
||||
if (cell_ox == 0 && cell_oy == 0)
|
||||
return;
|
||||
|
||||
// compute the associated world coordinates for the origin cell
|
||||
// beacuase we want to keep things grid-aligned
|
||||
double new_grid_ox, new_grid_oy;
|
||||
@@ -729,15 +962,20 @@ void VoxelLayer::updateOrigin(double new_origin_x, double new_origin_y)
|
||||
unsigned int cell_size_x = upper_right_x - lower_left_x;
|
||||
unsigned int cell_size_y = upper_right_y - lower_left_y;
|
||||
|
||||
// we need a map to store the obstacles in the window temporarily
|
||||
unsigned char* local_map = new unsigned char[cell_size_x * cell_size_y];
|
||||
unsigned int* local_voxel_map = new unsigned int[cell_size_x * cell_size_y];
|
||||
const std::size_t overlap_size = static_cast<std::size_t>(cell_size_x) * cell_size_y;
|
||||
rolling_costmap_scratch_.resize(overlap_size);
|
||||
rolling_voxel_scratch_.resize(overlap_size);
|
||||
unsigned char* local_map = rolling_costmap_scratch_.data();
|
||||
unsigned int* local_voxel_map = rolling_voxel_scratch_.data();
|
||||
unsigned int* voxel_map = robot_voxel_grid_.getData();
|
||||
|
||||
// copy the local window in the costmap to the local map
|
||||
copyMapRegion(costmap_, lower_left_x, lower_left_y, size_x_, local_map, 0, 0, cell_size_x, cell_size_x, cell_size_y);
|
||||
copyMapRegion(voxel_map, lower_left_x, lower_left_y, size_x_, local_voxel_map, 0, 0, cell_size_x, cell_size_x,
|
||||
cell_size_y);
|
||||
if (overlap_size > 0)
|
||||
{
|
||||
copyMapRegion(costmap_, lower_left_x, lower_left_y, size_x_, local_map, 0, 0,
|
||||
cell_size_x, cell_size_x, cell_size_y);
|
||||
copyMapRegion(voxel_map, lower_left_x, lower_left_y, size_x_, local_voxel_map, 0, 0,
|
||||
cell_size_x, cell_size_x, cell_size_y);
|
||||
}
|
||||
|
||||
// we'll reset our maps to unknown space if appropriate
|
||||
resetMaps();
|
||||
@@ -751,12 +989,14 @@ void VoxelLayer::updateOrigin(double new_origin_x, double new_origin_y)
|
||||
int start_y = lower_left_y - cell_oy;
|
||||
|
||||
// now we want to copy the overlapping information back into the map, but in its new location
|
||||
copyMapRegion(local_map, 0, 0, cell_size_x, costmap_, start_x, start_y, size_x_, cell_size_x, cell_size_y);
|
||||
copyMapRegion(local_voxel_map, 0, 0, cell_size_x, voxel_map, start_x, start_y, size_x_, cell_size_x, cell_size_y);
|
||||
if (overlap_size > 0)
|
||||
{
|
||||
copyMapRegion(local_map, 0, 0, cell_size_x, costmap_, start_x, start_y,
|
||||
size_x_, cell_size_x, cell_size_y);
|
||||
copyMapRegion(local_voxel_map, 0, 0, cell_size_x, voxel_map, start_x, start_y,
|
||||
size_x_, cell_size_x, cell_size_y);
|
||||
}
|
||||
|
||||
// make sure to clean up
|
||||
delete[] local_map;
|
||||
delete[] local_voxel_map;
|
||||
}
|
||||
|
||||
// Export factory function
|
||||
|
||||
Reference in New Issue
Block a user