- SensorGateway::pushStaticMap also dispatches to DIRECTIONAL_LAYER on the global costmap, before STATIC_LAYER: with a same-geometry map the directional layer goes PENDING before StaticLayer can report current, so a plan cannot pair the new map with the old zones. - Static maps are replayed in arrival order instead of topic order, so a direction grid is never replayed before its static map. - PlannerRunner checks every enabled global layer under the same lock as the snapshot. While a layer is not current it keeps the goal, reports waiting_for_costmap, logs the stale layers (throttled) and spends no planner_patience or watchdog budget. After costmap_ready_timeout [s] (default 10, 0 = legacy behaviour) the attempt fails as before. The key is added to config/runtime/move_base_common_params.yaml. - Test fixture destroys the server before its fake ports (the control thread could call a destroyed port). Full history: branch feature/direction-zone.
373 lines
12 KiB
C++
373 lines
12 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* move_base2 — cài đặt cửa vào dữ liệu cảm biến.
|
|
*
|
|
* Author: DuongTD
|
|
*********************************************************************/
|
|
#include <move_base2/io/sensor_gateway.h>
|
|
|
|
#include <exception>
|
|
#include <sstream>
|
|
|
|
#include <laser_filter/laser_filter.h>
|
|
#include <robot/console.h>
|
|
#include <robot_costmap_2d/layer.h>
|
|
#include <robot_costmap_2d/layered_costmap.h>
|
|
|
|
namespace move_base2
|
|
{
|
|
namespace
|
|
{
|
|
|
|
/// [s] Giãn cách log cho các cảnh báo phát sinh trong đường nóng — tần số cảm biến, không được spam.
|
|
constexpr double kHotPathLogThrottle = 5.0;
|
|
|
|
const char* toString(robot_costmap_2d::LayerType type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case robot_costmap_2d::LayerType::STATIC_LAYER:
|
|
return "StaticLayer";
|
|
case robot_costmap_2d::LayerType::OBSTACLE_LAYER:
|
|
return "ObstacleLayer";
|
|
case robot_costmap_2d::LayerType::VOXEL_LAYER:
|
|
return "VoxelLayer";
|
|
case robot_costmap_2d::LayerType::INFLATION_LAYER:
|
|
return "InflationLayer";
|
|
case robot_costmap_2d::LayerType::CRITICAL_LAYER:
|
|
return "CriticalLayer";
|
|
case robot_costmap_2d::LayerType::DIRECTIONAL_LAYER:
|
|
return "DirectionalLayer";
|
|
case robot_costmap_2d::LayerType::PREFERRED_LAYER:
|
|
return "PreferredLayer";
|
|
case robot_costmap_2d::LayerType::UNPREFERRED_LAYER:
|
|
return "UnpreferredLayer";
|
|
case robot_costmap_2d::LayerType::DEPTH_CAMERA_LAYER:
|
|
return "DepthCameraObstacleLayer";
|
|
case robot_costmap_2d::LayerType::UNKNOWN:
|
|
break;
|
|
}
|
|
return "Unknown";
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// ================================================================================================
|
|
// SensorGatewayConfig
|
|
// ================================================================================================
|
|
|
|
bool SensorGatewayConfig::validate(std::string& error) const
|
|
{
|
|
// Chỉ kiểm khi lọc bật: tham số của một tính năng đang tắt không nên chặn được cả runtime.
|
|
if (!laser_sor_enabled)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (laser_sor_mean_k < 2)
|
|
{
|
|
error = "laser_sor_mean_k must be >= 2 [points] when laser_sor_enabled = true";
|
|
return false;
|
|
}
|
|
if (!(laser_sor_stddev_mul > 0.0))
|
|
{
|
|
error = "laser_sor_stddev_mul must be > 0 when laser_sor_enabled = true";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string SensorGatewayConfig::describe() const
|
|
{
|
|
std::ostringstream out;
|
|
out << "SensorGateway:\n";
|
|
out << " laser_sor_enabled : " << (laser_sor_enabled ? "true" : "false") << '\n';
|
|
if (laser_sor_enabled)
|
|
{
|
|
out << " laser_sor_mean_k : " << laser_sor_mean_k << " points\n";
|
|
out << " laser_sor_stddev_mul : " << laser_sor_stddev_mul << '\n';
|
|
}
|
|
return out.str();
|
|
}
|
|
|
|
// ================================================================================================
|
|
// SensorGateway
|
|
// ================================================================================================
|
|
|
|
SensorGateway::SensorGateway() = default;
|
|
|
|
// Định nghĩa ở đây (không phải `= default` trong header) vì laser_sor_ là unique_ptr tới kiểu chưa
|
|
// hoàn chỉnh ở phía header.
|
|
SensorGateway::~SensorGateway() = default;
|
|
|
|
bool SensorGateway::configure(const SensorGatewayConfig& config, std::string& error)
|
|
{
|
|
if (!config.validate(error))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
config_ = config;
|
|
|
|
if (config_.laser_sor_enabled)
|
|
{
|
|
// Dựng một lần, không phải mỗi mẫu như bản cũ. Tham số cũng chỉ set ở đây.
|
|
laser_sor_.reset(new laser_filter::LaserScanSOR());
|
|
laser_sor_->setMeanK(config_.laser_sor_mean_k);
|
|
laser_sor_->setStddevMulThresh(config_.laser_sor_stddev_mul);
|
|
}
|
|
else
|
|
{
|
|
laser_sor_.reset();
|
|
}
|
|
|
|
configured_ = true;
|
|
return true;
|
|
}
|
|
|
|
void SensorGateway::attachTelemetry(RuntimeStats* telemetry)
|
|
{
|
|
telemetry_ = telemetry;
|
|
if (telemetry_ == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
section_static_map_ = telemetry_->section("sensors.staticMap");
|
|
section_laser_ = telemetry_->section("sensors.laserScan");
|
|
section_cloud_ = telemetry_->section("sensors.pointCloud");
|
|
section_depth_ = telemetry_->section("sensors.depthCamera");
|
|
}
|
|
|
|
void SensorGateway::attach(robot_costmap_2d::LayeredCostmap* global,
|
|
robot_costmap_2d::LayeredCostmap* local)
|
|
{
|
|
global_costmap_ = global;
|
|
local_costmap_ = local;
|
|
|
|
warnAboutUnreachableLayers(global_costmap_, "global");
|
|
warnAboutUnreachableLayers(local_costmap_, "local");
|
|
}
|
|
|
|
bool SensorGateway::attached() const
|
|
{
|
|
return global_costmap_ != nullptr || local_costmap_ != nullptr;
|
|
}
|
|
|
|
void SensorGateway::warnAboutUnreachableLayers(robot_costmap_2d::LayeredCostmap* costmap,
|
|
const char* which) const
|
|
{
|
|
if (costmap == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
auto* plugins = costmap->getPlugins();
|
|
if (plugins == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Dữ liệu vật cản được đẩy theo LayerType::VOXEL_LAYER, đúng như bản cũ. Một layer khai
|
|
// `type: ObstacleLayer` thuần sẽ không bao giờ khớp và không nhận được gì — im lặng. Đây là bẫy
|
|
// có thật: cây config `robot_costmap_2d/config/costmap_params.yaml` đang khai đúng kiểu đó.
|
|
// Không tự ý mở rộng đích để tránh đổi hành vi; thay vào đó nói ra lúc khởi tạo.
|
|
for (const auto& layer : *plugins)
|
|
{
|
|
if (!layer)
|
|
{
|
|
continue;
|
|
}
|
|
if (layer->getType() == robot_costmap_2d::LayerType::OBSTACLE_LAYER)
|
|
{
|
|
robot::log_warning(
|
|
"[SensorGateway] costmap %s: layer '%s' of type ObstacleLayer will NOT receive sensor "
|
|
"data — this gateway pushes obstacles as LayerType::VOXEL_LAYER. Switch it to 'type: "
|
|
"VoxelLayer' in the plugins list if that layer needs data.\n",
|
|
which, layer->getName().c_str());
|
|
}
|
|
}
|
|
}
|
|
|
|
robot_sensor_msgs::LaserScan SensorGateway::prepareLaserScan(
|
|
const robot_sensor_msgs::LaserScan& scan) const
|
|
{
|
|
if (!config_.laser_sor_enabled || laser_sor_ == nullptr)
|
|
{
|
|
return scan;
|
|
}
|
|
return laser_sor_->filter(scan);
|
|
}
|
|
|
|
namespace
|
|
{
|
|
|
|
/**
|
|
* @brief Đưa một mẫu tới mọi layer khớp kiểu trong một costmap.
|
|
*
|
|
* Template nằm trong .cpp có chủ đích: nó là chỗ duy nhất chạm `dataCallBack`, và giữ nó ở đây làm
|
|
* cho `sensor_gateway.h` không phải kéo theo `robot_costmap_2d`.
|
|
*/
|
|
template <typename T>
|
|
void dispatchTo(robot_costmap_2d::LayeredCostmap* costmap, const T& value,
|
|
robot_costmap_2d::LayerType type, const std::string& name,
|
|
SensorGatewayStats& stats)
|
|
{
|
|
if (costmap == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
auto* plugins = costmap->getPlugins();
|
|
if (plugins == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (const auto& layer : *plugins)
|
|
{
|
|
if (!layer)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Lọc CHỈ theo kiểu. Vế `|| getName() == name` của bản cũ bị bỏ — xem doc của lớp.
|
|
if (layer->getType() != type)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (!layer->isEnabled())
|
|
{
|
|
++stats.skipped_disabled;
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
layer->dataCallBack<T>(value, name);
|
|
++stats.delivered;
|
|
}
|
|
catch (const std::exception& ex)
|
|
{
|
|
// Bắt quanh TỪNG layer: bản cũ bắt quanh cả vòng lặp rồi return, nên một layer hỏng làm mọi
|
|
// layer đứng sau nó mất luôn mẫu này.
|
|
++stats.layer_exceptions;
|
|
robot::log_error_throttle(
|
|
kHotPathLogThrottle,
|
|
"[SensorGateway] layer '%s' (%s) threw an exception while taking '%s': %s\n",
|
|
layer->getName().c_str(), toString(type), name.c_str(), ex.what());
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void SensorGateway::pushStaticMap(const std::string& name, const robot_nav_msgs::OccupancyGrid& map)
|
|
{
|
|
if (!attached())
|
|
{
|
|
++stats_.dropped_no_costmap;
|
|
robot::log_warning_throttle(kHotPathLogThrottle,
|
|
"[SensorGateway] dropping static map '%s': no costmap attached\n",
|
|
name.c_str());
|
|
return;
|
|
}
|
|
|
|
ScopedSection timer(telemetry_, section_static_map_);
|
|
// DirectionalLayer TRƯỚC StaticLayer, có chủ đích. Layer tự lọc theo topic: map_topic chỉ đưa nó về
|
|
// PENDING (không cần geometry), direction_topic là grid hướng (StaticLayer bỏ qua, nên trong cùng
|
|
// lần gọi không có resize nào). Nếu StaticLayer nhận map trước, giữa hai lần dispatch updateMap có
|
|
// thể ghi xong map mới và StaticLayer báo current trong khi field hướng cũ vẫn READY — planner kiểm
|
|
// readiness đúng lúc đó sẽ plan theo map mới với zone cũ. Chỉ global costmap có layer này.
|
|
dispatchTo(global_costmap_, map, robot_costmap_2d::LayerType::DIRECTIONAL_LAYER, name, stats_);
|
|
dispatchTo(global_costmap_, map, robot_costmap_2d::LayerType::STATIC_LAYER, name, stats_);
|
|
dispatchTo(global_costmap_, map, robot_costmap_2d::LayerType::PREFERRED_LAYER, name, stats_);
|
|
dispatchTo(global_costmap_, map, robot_costmap_2d::LayerType::UNPREFERRED_LAYER, name, stats_);
|
|
|
|
dispatchTo(local_costmap_, map, robot_costmap_2d::LayerType::STATIC_LAYER, name, stats_);
|
|
dispatchTo(local_costmap_, map, robot_costmap_2d::LayerType::PREFERRED_LAYER, name, stats_);
|
|
dispatchTo(local_costmap_, map, robot_costmap_2d::LayerType::UNPREFERRED_LAYER, name, stats_);
|
|
}
|
|
|
|
void SensorGateway::pushLaserScan(const std::string& name, const robot_sensor_msgs::LaserScan& scan)
|
|
{
|
|
if (!attached())
|
|
{
|
|
++stats_.dropped_no_costmap;
|
|
robot::log_warning_throttle(kHotPathLogThrottle,
|
|
"[SensorGateway] dropping laser scan '%s': no costmap attached\n",
|
|
name.c_str());
|
|
return;
|
|
}
|
|
|
|
ScopedSection timer(telemetry_, section_laser_);
|
|
dispatchTo(local_costmap_, scan, robot_costmap_2d::LayerType::VOXEL_LAYER, name, stats_);
|
|
dispatchTo(global_costmap_, scan, robot_costmap_2d::LayerType::VOXEL_LAYER, name, stats_);
|
|
}
|
|
|
|
void SensorGateway::pushPointCloud(const std::string& name,
|
|
const robot_sensor_msgs::PointCloud& cloud)
|
|
{
|
|
if (!attached())
|
|
{
|
|
++stats_.dropped_no_costmap;
|
|
robot::log_warning_throttle(kHotPathLogThrottle,
|
|
"[SensorGateway] dropping point cloud '%s': no costmap attached\n",
|
|
name.c_str());
|
|
return;
|
|
}
|
|
|
|
ScopedSection timer(telemetry_, section_cloud_);
|
|
dispatchTo(local_costmap_, cloud, robot_costmap_2d::LayerType::VOXEL_LAYER, name, stats_);
|
|
dispatchTo(global_costmap_, cloud, robot_costmap_2d::LayerType::VOXEL_LAYER, name, stats_);
|
|
}
|
|
|
|
void SensorGateway::pushPointCloud2(const std::string& name,
|
|
const robot_sensor_msgs::PointCloud2& cloud)
|
|
{
|
|
if (!attached())
|
|
{
|
|
++stats_.dropped_no_costmap;
|
|
robot::log_warning_throttle(kHotPathLogThrottle,
|
|
"[SensorGateway] dropping point cloud2 '%s': no costmap attached\n",
|
|
name.c_str());
|
|
return;
|
|
}
|
|
|
|
ScopedSection timer(telemetry_, section_cloud_);
|
|
dispatchTo(local_costmap_, cloud, robot_costmap_2d::LayerType::VOXEL_LAYER, name, stats_);
|
|
dispatchTo(global_costmap_, cloud, robot_costmap_2d::LayerType::VOXEL_LAYER, name, stats_);
|
|
// DepthCameraObstacleLayer has its own type and marks from the camera's PointCloud2.
|
|
dispatchTo(local_costmap_, cloud, robot_costmap_2d::LayerType::DEPTH_CAMERA_LAYER, name, stats_);
|
|
dispatchTo(global_costmap_, cloud, robot_costmap_2d::LayerType::DEPTH_CAMERA_LAYER, name, stats_);
|
|
}
|
|
|
|
void SensorGateway::pushDepthCameraData(const std::string& topic,
|
|
const robot_sensor_msgs::DepthCameraData::ConstPtr& data)
|
|
{
|
|
if (data == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!attached())
|
|
{
|
|
++stats_.dropped_no_costmap;
|
|
robot::log_warning_throttle(kHotPathLogThrottle,
|
|
"[SensorGateway] dropping depth camera '%s': no costmap attached\n",
|
|
topic.c_str());
|
|
return;
|
|
}
|
|
|
|
ScopedSection timer(telemetry_, section_depth_);
|
|
|
|
// Phải giữ nguyên dạng ConstPtr: layer so `typeid(DepthCameraData::ConstPtr)`. Truyền giá trị sẽ
|
|
// rơi im lặng qua mọi nhánh của handleImpl.
|
|
dispatchTo(local_costmap_, data, robot_costmap_2d::LayerType::VOXEL_LAYER, topic, stats_);
|
|
dispatchTo(global_costmap_, data, robot_costmap_2d::LayerType::VOXEL_LAYER, topic, stats_);
|
|
}
|
|
|
|
} // namespace move_base2
|