feat(costmap_2d): direction zones and map readiness
- DirectionalLayer rewritten as a DirectionFieldProvider. It no longer derives from StaticLayer and never writes the master grid. It takes an OccupancyGrid of direction codes (0 = no constraint, 1..8 = direction_index + 1) on direction_topic, validates it against the master geometry and global frame, and publishes an immutable DirectionField snapshot with a DISABLED / PENDING / READY state and a generation counter. Every static map on map_topic returns the layer to PENDING until a grid for that map arrives. - direction_field.h is the core API for planners: grid validation, bounds-checked codeAt(), a half-open-cell DDA (traverseGridSegment), allowedDirectionMask() and checkSegment(). LayeredCostmap::getDirectionFieldSnapshot() exposes the snapshot without adding data members. - max_deviation_deg (default 90, valid 0 < x < 180). The field carries the value so every planner uses the same rule; 90 keeps the previous masks bit for bit. - StaticLayer ignores an OccupancyGrid addressed to another layer's topic instead of logging two unthrottled errors per push. - StaticLayer and its subclasses report current only after updateCosts has written the newest map (markMapWritten, also used by CriticalLayer). Readiness flags are written under the master mutex; invalid partial updates are rejected with a throttled error. Full history: branch feature/direction-zone.
This commit is contained in:
@@ -1,428 +1,230 @@
|
||||
#include <robot_costmap_2d/directional_layer.h>
|
||||
#include <data_convert/data_convert.h>
|
||||
#include <tf3/convert.h>
|
||||
#include <tf3/utils.h>
|
||||
#include <robot_tf3_geometry_msgs/tf3_geometry_msgs.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <utility>
|
||||
|
||||
#include <boost/dll/alias.hpp>
|
||||
#include <boost/thread/locks.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#define MAP_IDX(sx, i, j) ((sx) * (j) + (i))
|
||||
|
||||
using robot_costmap_2d::CRITICAL_SPACE;
|
||||
using robot_costmap_2d::LETHAL_OBSTACLE;
|
||||
using robot_costmap_2d::NO_INFORMATION;
|
||||
#include <robot/console.h>
|
||||
#include <robot/node_handle.h>
|
||||
#include <robot_costmap_2d/utils.h>
|
||||
|
||||
namespace robot_costmap_2d
|
||||
{
|
||||
DirectionalLayer::DirectionalLayer()
|
||||
{}
|
||||
|
||||
DirectionalLayer::~DirectionalLayer() {}
|
||||
namespace
|
||||
{
|
||||
constexpr auto kPendingErrorDelay = std::chrono::seconds(5);
|
||||
constexpr auto kPendingErrorPeriod = std::chrono::seconds(5);
|
||||
|
||||
bool DirectionalLayer::laneFilter(const std::vector<robot_geometry_msgs::PoseStamped> plan)
|
||||
bool topicMatches(const std::string& topic, const std::string& configured)
|
||||
{
|
||||
return topic == configured || topic == "/" + configured;
|
||||
}
|
||||
|
||||
const char* stateName(DirectionFieldState state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
if (directional_areas_.empty())
|
||||
throw "Has no map data";
|
||||
robot_nav_msgs::Path path;
|
||||
path.header.stamp = robot::Time::now();
|
||||
path.header.frame_id = map_frame_;
|
||||
path.poses = plan;
|
||||
return this->laneFilter(directional_areas_, path);
|
||||
case DirectionFieldState::kDisabled:
|
||||
return "DISABLED";
|
||||
case DirectionFieldState::kPending:
|
||||
return "PENDING";
|
||||
case DirectionFieldState::kReady:
|
||||
return "READY";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
DirectionalLayer::DirectionalLayer()
|
||||
: Layer(), mutex_(), snapshot_(), seen_map_(false), debug_state_(true), rolling_(false), max_deviation_rad_(M_PI / 2.0),
|
||||
map_topic_("map"), direction_topic_("direction_map"), pending_since_(), last_pending_log_()
|
||||
{
|
||||
}
|
||||
|
||||
void DirectionalLayer::onInitialize()
|
||||
{
|
||||
robot::NodeHandle nh("~");
|
||||
robot::NodeHandle priv_nh(nh, name_);
|
||||
const std::string filename = "directional_layer_params.yaml";
|
||||
double max_deviation_deg = 90.0;
|
||||
try
|
||||
{
|
||||
const char* env = std::getenv("PNKX_NAV_CORE_CONFIG_DIR");
|
||||
const std::string folder = env && std::filesystem::exists(env) ? env : "";
|
||||
const YAML::Node layer = YAML::LoadFile(getSourceFile(folder, filename))["directional_layer"];
|
||||
enabled_ = loadParam(layer, "enabled", false);
|
||||
map_topic_ = loadParam(layer, "map_topic", std::string("map"));
|
||||
direction_topic_ = loadParam(layer, "direction_topic", std::string("direction_map"));
|
||||
max_deviation_deg = loadParam(layer, "max_deviation_deg", 90.0);
|
||||
debug_state_ = loadParam(layer, "debug_state", true);
|
||||
if (priv_nh.hasParam("enabled"))
|
||||
priv_nh.getParam("enabled", enabled_, enabled_);
|
||||
if (priv_nh.hasParam("map_topic"))
|
||||
priv_nh.getParam("map_topic", map_topic_);
|
||||
if (priv_nh.hasParam("direction_topic"))
|
||||
priv_nh.getParam("direction_topic", direction_topic_);
|
||||
if (priv_nh.hasParam("max_deviation_deg"))
|
||||
priv_nh.getParam("max_deviation_deg", max_deviation_deg);
|
||||
if (priv_nh.hasParam("debug_state"))
|
||||
priv_nh.getParam("debug_state", debug_state_);
|
||||
}
|
||||
catch (const YAML::BadFile&)
|
||||
{
|
||||
throw std::runtime_error("DirectionalLayer " + name_ + ": cannot open the default file " + filename);
|
||||
}
|
||||
if (!std::isfinite(max_deviation_deg) || max_deviation_deg <= 0.0 || max_deviation_deg >= 180.0)
|
||||
throw std::runtime_error("DirectionalLayer " + name_ + ": max_deviation_deg must be finite and in (0, 180)");
|
||||
if (max_deviation_deg < 22.5)
|
||||
robot::log_warning("DirectionalLayer %s: max_deviation_deg < 22.5 may leave SBPL without a path", name_.c_str());
|
||||
max_deviation_rad_ = (max_deviation_deg / 180.0) * M_PI;
|
||||
// Preserve the open interval even when conversion rounds at an extreme value.
|
||||
if (max_deviation_rad_ <= 0.0)
|
||||
max_deviation_rad_ = std::nextafter(0.0, M_PI);
|
||||
else if (max_deviation_rad_ >= M_PI)
|
||||
max_deviation_rad_ = std::nextafter(M_PI, 0.0);
|
||||
rolling_ = layered_costmap_->isRolling();
|
||||
if (enabled_ && rolling_)
|
||||
robot::log_error("DirectionalLayer %s: rolling costmap is unsupported", name_.c_str());
|
||||
if (enabled_ && (map_topic_.empty() || direction_topic_.empty() || topicMatches(map_topic_, direction_topic_) ||
|
||||
topicMatches(direction_topic_, map_topic_)))
|
||||
throw std::runtime_error("DirectionalLayer " + name_ + ": topics must be nonempty and distinct");
|
||||
robot::log_info("DirectionalLayer %s: enabled=%d map_topic=%s direction_topic=%s max_deviation_deg=%g debug_state=%d",
|
||||
name_.c_str(), enabled_, map_topic_.c_str(), direction_topic_.c_str(), max_deviation_deg, debug_state_);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
publishLocked(enabled_ ? DirectionFieldState::kPending : DirectionFieldState::kDisabled);
|
||||
}
|
||||
|
||||
void DirectionalLayer::publishLocked(DirectionFieldState state, std::shared_ptr<const DirectionField> field)
|
||||
{
|
||||
snapshot_.state = state;
|
||||
snapshot_.field = std::move(field);
|
||||
++snapshot_.generation;
|
||||
// Layer::isCurrent() reads this non-atomic bool without this mutex (legacy API limitation).
|
||||
current_ = state != DirectionFieldState::kPending;
|
||||
if (state == DirectionFieldState::kPending)
|
||||
{
|
||||
pending_since_ = std::chrono::steady_clock::now();
|
||||
last_pending_log_ = {};
|
||||
}
|
||||
if (debug_state_)
|
||||
robot::log_info("[debug] DirectionalLayer %s: %s generation=%llu", name_.c_str(), stateName(state),
|
||||
static_cast<unsigned long long>(snapshot_.generation));
|
||||
}
|
||||
|
||||
DirectionFieldSnapshot DirectionalLayer::directionSnapshot() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return snapshot_;
|
||||
}
|
||||
|
||||
DirectionGeometry DirectionalLayer::geometry() const
|
||||
{
|
||||
const auto* master = layered_costmap_->getCostmap();
|
||||
return {master->getSizeInCellsX(), master->getSizeInCellsY(), master->getResolution(),
|
||||
master->getOriginX(), master->getOriginY(), layered_costmap_->getGlobalFrameID()};
|
||||
}
|
||||
|
||||
void DirectionalLayer::handleImpl(const void* data, const std::type_info& type, const std::string& topic)
|
||||
{
|
||||
if (!enabled_ || type != typeid(robot_nav_msgs::OccupancyGrid) ||
|
||||
(!topicMatches(topic, map_topic_) && !topicMatches(topic, direction_topic_)))
|
||||
return;
|
||||
|
||||
if (topicMatches(topic, map_topic_))
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
seen_map_ = true;
|
||||
publishLocked(DirectionFieldState::kPending);
|
||||
return;
|
||||
}
|
||||
|
||||
void DirectionalLayer::incomingMap(const robot_nav_msgs::OccupancyGrid& new_map)
|
||||
DirectionGeometry expected;
|
||||
uint64_t generation;
|
||||
{
|
||||
if(!map_shutdown_)
|
||||
{
|
||||
unsigned int size_x = new_map.info.width, size_y = new_map.info.height;
|
||||
|
||||
robot::log_info("Received a %d X %d map at %f m/pix\n", size_x / 2, size_y / 2, new_map.info.resolution);
|
||||
|
||||
// resize costmap if size, resolution or origin do not match
|
||||
Costmap2D *master = layered_costmap_->getCostmap();
|
||||
if (!layered_costmap_->isRolling() &&
|
||||
(master->getSizeInCellsX() != size_x / 2 ||
|
||||
master->getSizeInCellsY() != size_y / 2 ||
|
||||
master->getResolution() != new_map.info.resolution ||
|
||||
master->getOriginX() != new_map.info.origin.position.x ||
|
||||
master->getOriginY() != new_map.info.origin.position.y))
|
||||
{
|
||||
// Update the size of the layered costmap (and all layers, including this one)
|
||||
robot::log_info("Resizing costmap to %d X %d at %f m/pix\n", size_x / 2, size_y / 2, new_map.info.resolution);
|
||||
layered_costmap_->resizeMap(size_x / 2, size_y / 2, new_map.info.resolution, new_map.info.origin.position.x,
|
||||
new_map.info.origin.position.y,
|
||||
true /* set size_locked to true, prevents reconfigureCb from overriding map size*/);
|
||||
}
|
||||
else if (size_x_ != size_x / 2 || size_y_ != size_y / 2 ||
|
||||
resolution_ != new_map.info.resolution ||
|
||||
origin_x_ != new_map.info.origin.position.x ||
|
||||
origin_y_ != new_map.info.origin.position.y)
|
||||
{
|
||||
// only update the size of the costmap stored locally in this layer
|
||||
robot::log_info("Resizing static layer to %d X %d at %f m/pix\n", size_x / 2, size_y / 2, new_map.info.resolution);
|
||||
resizeMap(size_x / 2, size_y / 2, new_map.info.resolution,
|
||||
new_map.info.origin.position.x, new_map.info.origin.position.y);
|
||||
}
|
||||
unsigned char values[4];
|
||||
directional_areas_.clear();
|
||||
directional_areas_.resize(size_x / 2 * size_y / 2);
|
||||
// initialize the costmap with static data
|
||||
for (unsigned int iy = 0; iy < size_y; iy += 2)
|
||||
{
|
||||
for (unsigned int ix = 0; ix < size_x; ix += 2)
|
||||
{
|
||||
values[0] = new_map.data[MAP_IDX(size_x, ix, iy)];
|
||||
values[1] = new_map.data[MAP_IDX(size_x, ix + 1, iy)];
|
||||
values[2] = new_map.data[MAP_IDX(size_x, ix, iy + 1)];
|
||||
values[3] = new_map.data[MAP_IDX(size_x, ix + 1, iy + 1)];
|
||||
uint32_t color_avg = (uint32_t)(values[0] | values[1] << 8u | values[2] << 16u | values[3] << 24u);
|
||||
int index = getIndex(ix / 2, iy / 2);
|
||||
directional_areas_[index][0] = (uint16_t)(color_avg & 0xfff);
|
||||
directional_areas_[index][1] = (uint16_t)(color_avg >> 12u);
|
||||
costmap_[index] = robot_costmap_2d::NO_INFORMATION;
|
||||
}
|
||||
}
|
||||
map_frame_ = new_map.header.frame_id;
|
||||
|
||||
// we have a new map, update full size of map
|
||||
x_ = y_ = 0;
|
||||
width_ = size_x_;
|
||||
height_ = size_y_;
|
||||
map_received_ = true;
|
||||
has_updated_data_ = true;
|
||||
|
||||
new_map_.header = new_map.header;
|
||||
new_map_.header.stamp = robot::Time::now();
|
||||
new_map_.info = new_map.info;
|
||||
new_map_.info.width = width_;
|
||||
new_map_.info.height = height_;
|
||||
}
|
||||
else
|
||||
{
|
||||
robot::log_info("Stop receive new map!\n");
|
||||
}
|
||||
// shutdown the map subscrber if firt_map_only_ flag is on
|
||||
if (first_map_only_)
|
||||
{
|
||||
robot::log_info("Shutting down the map subscriber. first_map_only flag is on\n");
|
||||
map_shutdown_ = true;
|
||||
// map_sub_.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
bool DirectionalLayer::laneFilter(std::vector<std::array<uint16_t, 2>> new_map, const robot_nav_msgs::Path path)
|
||||
{
|
||||
boost::unique_lock<mutex_t> lock(*layered_costmap_->getCostmap()->getMutex());
|
||||
if (new_map.empty())
|
||||
return false;
|
||||
|
||||
std::vector<std::pair<unsigned int, double>> X_List, Y_List;
|
||||
std::vector<double> Yaw_list;
|
||||
|
||||
const robot_geometry_msgs::PoseStamped &e = path.poses.back();
|
||||
|
||||
bool get_success = getRobotPose(pose_x_, pose_y_, pose_yaw_);
|
||||
if(!get_success) return false;
|
||||
for (auto it = path.poses.begin(); it != path.poses.end(); ++it)
|
||||
{
|
||||
const robot_geometry_msgs::PoseStamped &p = *it; // Get the pose from the iterator
|
||||
unsigned int mx, my;
|
||||
if (!worldToMap(p.pose.position.x, p.pose.position.y, mx, my))
|
||||
{
|
||||
robot::log_error("ERROR: trying to set a start cell %f %f that is outside of map\n", p.pose.position.x, p.pose.position.y);
|
||||
return false;
|
||||
}
|
||||
// Convert to yaw
|
||||
tf3::Quaternion quaternion = data_convert::convertQuaternion(p.pose.orientation);
|
||||
double theta = tf3::getYaw(quaternion);
|
||||
unsigned char value = laneFilter(mx, my, theta);
|
||||
if (get_success)
|
||||
{
|
||||
if (
|
||||
(!inSkipErea(pose_x_, pose_y_, p.pose.position.x, p.pose.position.y, distance_skip_) &
|
||||
!inSkipErea(p.pose.position.x, p.pose.position.y, e.pose.position.x, e.pose.position.y, distance_skip_))
|
||||
|| p == path.poses.back()
|
||||
)
|
||||
{
|
||||
if (value == robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE || value == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
{
|
||||
std::pair<unsigned int, double> x_val(mx, p.pose.position.x);
|
||||
std::pair<unsigned int, double> y_val(my, p.pose.position.y);
|
||||
X_List.push_back(x_val);
|
||||
Y_List.push_back(y_val);
|
||||
Yaw_list.push_back(theta);
|
||||
// costmap_[getIndex(mx, my)] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!Yaw_list.empty())
|
||||
{
|
||||
laneFilter(X_List, Y_List, Yaw_list);
|
||||
robot_nav_msgs::OccupancyGrid lanes;
|
||||
convertToMap(costmap_, lanes, 0.65, 0.196);
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void DirectionalLayer::convertToMap(unsigned char *costmap, robot_nav_msgs::OccupancyGrid &dir_map, double occ_th, double free_th)
|
||||
{
|
||||
dir_map.header = new_map_.header;
|
||||
dir_map.header.stamp = robot::Time::now();
|
||||
dir_map.info = new_map_.info;
|
||||
int size_costmap = new_map_.info.width * new_map_.info.height;
|
||||
dir_map.data.resize(size_costmap);
|
||||
for (int i = 0; i < size_costmap; i++)
|
||||
{
|
||||
int8_t value;
|
||||
if (costmap[i] == robot_costmap_2d::NO_INFORMATION)
|
||||
{
|
||||
value = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
double occ = (costmap[i]) / 255.0;
|
||||
if (occ > occ_th)
|
||||
value = +100;
|
||||
else if (occ < free_th)
|
||||
value = 0;
|
||||
else
|
||||
{
|
||||
double ratio = (occ - free_th) / (occ_th - free_th);
|
||||
value = 1 + 98 * ratio;
|
||||
}
|
||||
}
|
||||
dir_map.data[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char DirectionalLayer::laneFilter(unsigned int x, unsigned int y, double yaw_robot)
|
||||
{
|
||||
double yaw_lane;
|
||||
unsigned char cost = robot_costmap_2d::NO_INFORMATION;
|
||||
|
||||
int index = getIndex(x, y);
|
||||
for (auto &lane : directional_areas_[index])
|
||||
{
|
||||
if (lane > 359)
|
||||
{
|
||||
cost = std::min(cost, robot_costmap_2d::NO_INFORMATION);
|
||||
}
|
||||
else
|
||||
{
|
||||
double yaw_lane = (double)lane / 180 * M_PI;
|
||||
if (0.4 <= cos(yaw_robot - yaw_lane) && cos(yaw_robot - yaw_lane) <= 1.0)
|
||||
cost = std::min(cost, robot_costmap_2d::FREE_SPACE);
|
||||
else if (-1.0 <= cos(yaw_robot - yaw_lane) && cos(yaw_robot - yaw_lane) <= -0.4)
|
||||
cost = std::min(cost, robot_costmap_2d::LETHAL_OBSTACLE);
|
||||
else
|
||||
cost = std::min(cost, robot_costmap_2d::NO_INFORMATION);
|
||||
}
|
||||
}
|
||||
return cost;
|
||||
}
|
||||
|
||||
void DirectionalLayer::laneFilter(std::vector<std::pair<unsigned int, double>> x,
|
||||
std::vector<std::pair<unsigned int, double>> y,
|
||||
std::vector<double> yaw_robot)
|
||||
{
|
||||
if(x.empty() || y.empty() || yaw_robot.empty())
|
||||
boost::unique_lock<Costmap2D::mutex_t> master_lock(*layered_costmap_->getCostmap()->getMutex());
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (rolling_)
|
||||
return;
|
||||
|
||||
unsigned int x_min, y_min, x_max, y_max;
|
||||
double x_min_w, y_min_w, x_max_w, y_max_w;
|
||||
|
||||
x_min = x.front().first; y_min = y.front().first;
|
||||
x_max = x.front().first; y_max = y.front().first;
|
||||
x_min_w = x.front().second; y_min_w = y.front().second;
|
||||
x_max_w = x.front().second; y_max_w = y.front().second;
|
||||
for (int i = 1; i < yaw_robot.size(); i++)
|
||||
if (!seen_map_)
|
||||
{
|
||||
if(inSkipErea(pose_x_, pose_y_, x[i].second, y[i].second, distance_skip_))
|
||||
continue;
|
||||
x_min = std::min(x_min, x[i].first);
|
||||
y_min = std::min(y_min, y[i].first);
|
||||
x_max = std::max(x_max, x[i].first);
|
||||
y_max = std::max(y_max, y[i].first);
|
||||
|
||||
x_min_w = std::min(x_min_w, x[i].second);
|
||||
y_min_w = std::min(y_min_w, y[i].second);
|
||||
x_max_w = std::max(x_max_w, x[i].second);
|
||||
y_max_w = std::max(y_max_w, y[i].second);
|
||||
|
||||
}
|
||||
|
||||
for (int i = 0; i < yaw_robot.size(); i++)
|
||||
{
|
||||
if(inSkipErea(pose_x_, pose_y_, x[i].second, y[i].second, distance_skip_))
|
||||
continue;
|
||||
|
||||
double yaw_rad = (double)yaw_robot[i] / 180 * M_PI;
|
||||
if (fabs(cos(yaw_rad)) > fabs(sin(yaw_rad)))
|
||||
{
|
||||
int x_ = x[i].first;
|
||||
// Dưới lên trên
|
||||
for (int j = y[i].first; j <= y_max; j++)
|
||||
{
|
||||
int y_ = j;
|
||||
if (costmap_[getIndex(x_, y_)] == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
continue;
|
||||
unsigned char value = laneFilter(x_, y_, yaw_robot[i]);
|
||||
if (value >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE && value != robot_costmap_2d::NO_INFORMATION)
|
||||
costmap_[getIndex(x_, y_)] = value;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Trên xuống dưới
|
||||
for (int k = y[i].first; k >= y_min; k--)
|
||||
{
|
||||
int y_ = k;
|
||||
if (costmap_[getIndex(x_, y_)] == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
continue;
|
||||
unsigned char value = laneFilter(x_, y_, yaw_robot[i]);
|
||||
if (value >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE && value != robot_costmap_2d::NO_INFORMATION)
|
||||
costmap_[getIndex(x_, y_)] = value;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
int y_ = y[i].first;
|
||||
// Phải qua trái
|
||||
for (int j = x[i].first; j < layered_costmap_->getCostmap()->getSizeInCellsX(); j++)
|
||||
{
|
||||
int x_ = j;
|
||||
if (costmap_[getIndex(x_, y_)] == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
continue;
|
||||
unsigned char value = laneFilter(x_, y_, yaw_robot[i]);
|
||||
if (value >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE && value != robot_costmap_2d::NO_INFORMATION)
|
||||
costmap_[getIndex(x_, y_)] = value;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Trái qua phải
|
||||
for (int k = x[i].first; k >= 0; k--)
|
||||
{
|
||||
int x_ = k;
|
||||
if (costmap_[getIndex(x_, y_)] == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
continue;
|
||||
unsigned char value = laneFilter(x_, y_, yaw_robot[i]);
|
||||
if (value >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE && value != robot_costmap_2d::NO_INFORMATION)
|
||||
costmap_[getIndex(x_, y_)] = value;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(fabs(cos(yaw_rad)) < fabs(sin(yaw_rad)))
|
||||
{
|
||||
int y_ = y[i].first;
|
||||
// Phải qua trái
|
||||
for (int j = x[i].first; j <= x_max; j++)
|
||||
{
|
||||
int x_ = j;
|
||||
if (costmap_[getIndex(x_, y_)] == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
continue;
|
||||
unsigned char value = laneFilter(x_, y_, yaw_robot[i]);
|
||||
if (value >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE && value != robot_costmap_2d::NO_INFORMATION)
|
||||
costmap_[getIndex(x_, y_)] = value;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Trái qua phải
|
||||
for (int k = x[i].first; k >= x_min; k--)
|
||||
{
|
||||
int x_ = k;
|
||||
if (costmap_[getIndex(x_, y_)] == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
continue;
|
||||
unsigned char value = laneFilter(x_, y_, yaw_robot[i]);
|
||||
if (value >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE && value != robot_costmap_2d::NO_INFORMATION)
|
||||
costmap_[getIndex(x_, y_)] = value;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
int x_ = x[i].first;
|
||||
// Dưới lên trên
|
||||
for (int j = y[i].first; j < layered_costmap_->getCostmap()->getSizeInCellsY(); j++)
|
||||
{
|
||||
int y_ = j;
|
||||
if (costmap_[getIndex(x_, y_)] == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
continue;
|
||||
unsigned char value = laneFilter(x_, y_, yaw_robot[i]);
|
||||
if (value >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE && value != robot_costmap_2d::NO_INFORMATION)
|
||||
costmap_[getIndex(x_, y_)] = value;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Trên xuống dưới
|
||||
for (int k = y[i].first; k >= 0; k--)
|
||||
{
|
||||
int y_ = k;
|
||||
if (costmap_[getIndex(x_, y_)] == robot_costmap_2d::LETHAL_OBSTACLE)
|
||||
continue;
|
||||
unsigned char value = laneFilter(x_, y_, yaw_robot[i]);
|
||||
if (value >= robot_costmap_2d::INSCRIBED_INFLATED_OBSTACLE && value != robot_costmap_2d::NO_INFORMATION)
|
||||
costmap_[getIndex(x_, y_)] = value;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
robot::log_error("DirectionalLayer %s: direction grid arrived before static map", name_.c_str());
|
||||
return;
|
||||
}
|
||||
expected = geometry();
|
||||
generation = snapshot_.generation;
|
||||
}
|
||||
|
||||
void DirectionalLayer::resetMap()
|
||||
DirectionValidationResult validation;
|
||||
auto field = DirectionField::create(*static_cast<const robot_nav_msgs::OccupancyGrid*>(data), expected,
|
||||
max_deviation_rad_, &validation);
|
||||
if (!field)
|
||||
{
|
||||
unsigned int size_x = layered_costmap_->getCostmap()->getSizeInCellsX();
|
||||
unsigned int size_y = layered_costmap_->getCostmap()->getSizeInCellsY();
|
||||
for (unsigned int iy = 0; iy < size_y; iy++)
|
||||
{
|
||||
for (unsigned int ix = 0; ix < size_x; ix++)
|
||||
{
|
||||
int index = getIndex(ix, iy);
|
||||
costmap_[index] = robot_costmap_2d::NO_INFORMATION;
|
||||
}
|
||||
}
|
||||
robot::log_error("DirectionalLayer %s: invalid direction grid: %s", name_.c_str(), validation.message.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
bool DirectionalLayer::getRobotPose(double &x, double &y, double &yaw)
|
||||
// resizeMap holds the master mutex through matchSize(), which increments generation.
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (snapshot_.generation != generation)
|
||||
{
|
||||
tf3::TransformStampedMsg transformMsg;
|
||||
try
|
||||
{
|
||||
transformMsg = tf_->lookupTransform(map_frame_, base_frame_id_, tf3::Time());
|
||||
|
||||
// listener_.lookupTransform(map_frame_, base_frame_id_, tf3::Time(), transform);
|
||||
}
|
||||
catch (tf3::TransformException &ex)
|
||||
{
|
||||
robot::log_error("%s\n", ex.what());
|
||||
return false;
|
||||
}
|
||||
// Copy map data given proper transformations
|
||||
tf3::Transform tf3_transform;
|
||||
tf3_transform = data_convert::convertTotf3Transform(transformMsg.transform);
|
||||
x = tf3_transform.getOrigin().x();
|
||||
y = tf3_transform.getOrigin().y();
|
||||
// Extract the rotation as a quaternion from the transform
|
||||
tf3::Quaternion rotation = tf3_transform.getRotation();
|
||||
// Convert the quaternion to a yaw angle (in radians)
|
||||
yaw = tf3::getYaw(rotation);
|
||||
return true;
|
||||
robot::log_error("DirectionalLayer %s: grid discarded because map state changed during validation", name_.c_str());
|
||||
return;
|
||||
}
|
||||
publishLocked(DirectionFieldState::kReady, std::move(field));
|
||||
}
|
||||
|
||||
bool DirectionalLayer::inSkipErea(double start_x, double start_y, double end_x, double end_y, double skip_distance)
|
||||
void DirectionalLayer::matchSize()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (enabled_)
|
||||
publishLocked(DirectionFieldState::kPending);
|
||||
}
|
||||
|
||||
void DirectionalLayer::reset()
|
||||
{
|
||||
boost::unique_lock<Costmap2D::mutex_t> master_lock(*layered_costmap_->getCostmap()->getMutex());
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!enabled_ || snapshot_.state != DirectionFieldState::kReady)
|
||||
return;
|
||||
const auto now = geometry();
|
||||
const auto& old = snapshot_.field->geometry();
|
||||
if (old.width != now.width || old.height != now.height || old.frame_id != now.frame_id ||
|
||||
!std::isfinite(now.resolution) || !std::isfinite(now.origin_x) || !std::isfinite(now.origin_y) ||
|
||||
std::abs(old.resolution - now.resolution) > 1e-6 || std::abs(old.origin_x - now.origin_x) > 1e-6 ||
|
||||
std::abs(old.origin_y - now.origin_y) > 1e-6)
|
||||
publishLocked(DirectionFieldState::kPending);
|
||||
}
|
||||
|
||||
void DirectionalLayer::updateBounds(double, double, double, double*, double*, double*, double*)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (snapshot_.state != DirectionFieldState::kPending)
|
||||
return;
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now - pending_since_ >= kPendingErrorDelay &&
|
||||
(last_pending_log_ == std::chrono::steady_clock::time_point() ||
|
||||
now - last_pending_log_ >= kPendingErrorPeriod))
|
||||
{
|
||||
return fabs(hypot(start_x - end_x, start_y - end_y)) <= skip_distance;
|
||||
robot::log_error("DirectionalLayer %s: PENDING for %.1f seconds", name_.c_str(),
|
||||
std::chrono::duration<double>(now - pending_since_).count());
|
||||
last_pending_log_ = now;
|
||||
}
|
||||
}
|
||||
} // namespace robot_costmap_2d
|
||||
|
||||
// Export factory function
|
||||
static boost::shared_ptr<Layer> create_directional_plugin() {
|
||||
return boost::make_shared<DirectionalLayer>();
|
||||
}
|
||||
|
||||
// Alias cho Boost.DLL (nếu muốn dùng boost::dll::import_alias)
|
||||
BOOST_DLL_ALIAS(create_directional_plugin, DirectionalLayer)
|
||||
}
|
||||
boost::shared_ptr<robot_costmap_2d::Layer> create_directional_plugin()
|
||||
{
|
||||
return boost::shared_ptr<robot_costmap_2d::Layer>(new robot_costmap_2d::DirectionalLayer());
|
||||
}
|
||||
BOOST_DLL_ALIAS(create_directional_plugin, DirectionalLayer)
|
||||
|
||||
Reference in New Issue
Block a user