first commit
This commit is contained in:
337
src/io/sensor_gateway.cpp
Normal file
337
src/io/sensor_gateway.cpp
Normal file
@@ -0,0 +1,337 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* 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::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 phải >= 2 [điểm] khi laser_sor_enabled = true";
|
||||
return false;
|
||||
}
|
||||
if (!(laser_sor_stddev_mul > 0.0))
|
||||
{
|
||||
error = "laser_sor_stddev_mul phải > 0 khi 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 << " điểm\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::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' kiểu ObstacleLayer sẽ KHÔNG nhận dữ liệu cảm "
|
||||
"biến — cổng này đẩy vật cản theo LayerType::VOXEL_LAYER. Đổi sang 'type: VoxelLayer' "
|
||||
"trong danh sách plugins nếu layer đó cần dữ liệu.\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) ném exception khi nhận '%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] bỏ static map '%s': chưa gắn costmap nào\n",
|
||||
name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
dispatchTo(global_costmap_, map, robot_costmap_2d::LayerType::STATIC_LAYER, name, stats_);
|
||||
dispatchTo(local_costmap_, map, robot_costmap_2d::LayerType::STATIC_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] bỏ laser scan '%s': chưa gắn costmap nào\n",
|
||||
name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
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] bỏ point cloud '%s': chưa gắn costmap nào\n",
|
||||
name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
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] bỏ point cloud2 '%s': chưa gắn costmap nào\n",
|
||||
name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
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::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] bỏ depth camera '%s': chưa gắn costmap nào\n",
|
||||
topic.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
// 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
|
||||
Reference in New Issue
Block a user