Keep raw sensor readings so plugins can reach what the costmap threw away

Sensor data entered this runtime and disappeared into the costmap. At 0.05 m
with inflation the costmap has destroyed ray order and object shape, so a dock
detector fitting lines to find a V plate cannot reconstruct what the laser
saw - two 0.2 m wings on that grid are one blob. A reinforcement-learning
local planner wants the raw scan for the same reason. Neither had a route:
ActionContext held only TF, and LocalPlanner::initialize takes only a costmap.

NavigationServer now owns a SensorStore and feeds it from all five doors,
handing a read-only port to the action registry and to the controller runner.

The store gets the reading BEFORE the filter. Anything that wants filtering
filters upstream; the store does not know a filter exists. With laser SOR on,
the store and the costmap deliberately see different data - one wants outliers
gone, the other wants the true shape - and the comment at addLaserScan now
says so, because the one it replaces claimed the opposite.

remove* mirrors into the store, dropping readings while keeping the topic
declared. A host that has disowned data while the store still serves it means
the two disagree about what the robot can currently see, and max_age only
covers the tail of that.

Reaching a local planner without breaking every other one: widening
LocalPlanner::initialize would break hybrid, mppi, priest, stanley and
deep_mpc at once for the sake of one planner. A planner that wants sensor data
inherits SensorConsumer instead, and ControllerRunner finds out with a
dynamic_cast after loading it, at all three sites that build an instance and
always before initialize(). Planners that do not inherit it cast to null and
nothing happens, so nothing needs rebuilding. That cast crosses a .so boundary
and depends on matching RTTI, which a green build does not prove - hence the
log line, and hence TestControllerSensorProbe, dlopen'd like a real plugin, to
check it actually matches.

sensor_store/report_period prints what the store holds every N seconds, off by
default. Until a consumer exists it is the only way to see whether data
arrives at all, and it is what separates a mistyped topic from a dead sensor -
without it the two look identical. It reads age from the control loop's clock,
not robot::Time::now(), which is the wall clock while a simulated scan is not.

Config is refused at boot, not at first use: an unknown data_type, a short name
two packages share, a duplicate topic, a capacity below 1, a max_age that is
not positive. Declaring no sensor at all is valid and leaves the store empty
and silent - a runtime with no consumer of raw data holds nothing, which is an
answer rather than a reason to refuse to start.

robot_nav_msgs/OccupancyGrid is registered here rather than in
robot_sensor_ports, which ships only its own package's types; pulling
robot_nav_msgs into every translation unit that touches the store would be the
cost of doing otherwise.
This commit is contained in:
2026-08-26 11:35:17 +07:00
parent ea02392c69
commit b102391fb8
18 changed files with 975 additions and 2 deletions

View File

@@ -8,6 +8,9 @@
*********************************************************************/
#include <move_base2/navigation_server.h>
#include <move_base2/io/sensor_store_types.h>
#include <robot_sensor_ports/sensor_writer.h>
#include <cmath>
#include <mission_adapters/mission_request.h>
@@ -128,6 +131,10 @@ bool NavigationServer::configureLoop(const ControlLoopConfig& config, const Cont
robot_base_frame_ = config.robot_base_frame;
// Mượn đồng hồ của control loop cho phần chẩn đoán kho cảm biến. Phải là đồng hồ này chứ không
// phải robot::Time::now(): cái sau đọc wall clock trong khi scan mang sim time.
clock_ = deps.clock;
nav_feedback_->is_ready = true;
nav_feedback_->feed_back_str = "ready";
@@ -135,6 +142,16 @@ bool NavigationServer::configureLoop(const ControlLoopConfig& config, const Cont
return true;
}
bool NavigationServer::configureSensorStore(const robot_sensor_ports::SensorStoreConfig& config,
std::string& error)
{
// Đăng ký kiểu riêng của gói này trước: khai `data_type: OccupancyGrid` mà bảng tên chưa có nó thì
// config bị từ chối là "unknown". Idempotent nên gọi lại vô hại.
registerSensorStoreTypes();
return sensor_store_.configure(config, error);
}
bool NavigationServer::configureSensors(const SensorGatewayConfig& config, std::string& error)
{
return sensors_.configure(config, error);
@@ -199,6 +216,7 @@ bool NavigationServer::spinOnce()
cachePlans();
}
refreshFeedback();
reportSensorStore();
return running;
}
@@ -285,6 +303,44 @@ bool NavigationServer::missionHasPendingWork() const
runtime_->mission().hasActiveMission();
}
void NavigationServer::reportSensorStore()
{
if (sensor_store_report_period_ <= 0.0 || clock_ == nullptr || !sensor_store_.configured())
{
return;
}
const robot::Time now = clock_->now();
if (have_sensor_store_report_ &&
(now - last_sensor_store_report_).toSec() < sensor_store_report_period_)
{
return;
}
last_sensor_store_report_ = now;
have_sensor_store_report_ = true;
for (const robot_sensor_ports::SensorInfo& info : sensor_store_.inventory())
{
if (info.sequence == 0)
{
// Chưa bao giờ có mẫu nào. Đây là ca đáng nói nhất: gõ sai `topic` và cảm biến chết cho cùng
// một triệu chứng, nên phải in ra chứ không phải bỏ qua vì "không có gì để in".
robot::log_warning("[move_base2] sensor store: '%s' (%s) — NO DATA YET. Kiểm tên topic host "
"đang dùng khi gọi add*().\n",
info.topic.c_str(), info.data_type.c_str());
continue;
}
const double age = (now - info.stamp).toSec();
robot::log_info("[move_base2] sensor store: '%s' %.1f Hz, age %.3f s%s, %zu/%zu kept, %llu "
"rejected\n",
info.topic.c_str(), info.rate_hz, age,
age > info.max_age ? " [STALE]" : "", info.size, info.capacity,
static_cast<unsigned long long>(info.rejected));
}
}
void NavigationServer::refreshFeedback()
{
robot::move_base_core::State host_state = toHostState(loop_.state());
@@ -393,6 +449,29 @@ void NavigationServer::initialize(robot::TFListenerPtr tf)
// ra khỏi phần "(không đăng ký)".
sensors_.attachTelemetry(stats_);
// Kho cảm biến THÔ dùng chung. Dựng trước buildRunners vì action handler và local planner đều
// nhận cổng đọc lúc nạp plugin.
if (!configureSensorStore(runtime_->config().sensor_store, error))
{
runtime_.reset();
stats_ = nullptr;
nav_feedback_->is_ready = false;
nav_feedback_->feed_back_str = "invalid sensor_store: " + error;
robot::log_error("[move_base2] initialize() failed: invalid sensor_store: %s\n", error.c_str());
return;
}
runtime_->setSensorReader(&sensor_store_);
setSensorStoreReportPeriod(runtime_->config().sensor_store_report_period);
if (sensor_store_.configured())
{
for (const robot_sensor_ports::SensorInfo& info : sensor_store_.inventory())
{
robot::log_info("[move_base2] sensor store: '%s' %s capacity=%zu max_age=%.2f s\n",
info.topic.c_str(), info.data_type.c_str(), info.capacity, info.max_age);
}
}
if (!configureSensors(runtime_->config().sensors, error))
{
runtime_.reset();
@@ -567,6 +646,7 @@ void NavigationServer::applyPendingFootprint()
void NavigationServer::addStaticMap(const std::string& map_name, robot_nav_msgs::OccupancyGrid map)
{
robot_sensor_ports::put(sensor_store_, map_name, map, map.header.stamp);
{
std::lock_guard<std::mutex> lock(data_mutex_);
static_maps_[map_name] = map;
@@ -577,8 +657,14 @@ void NavigationServer::addStaticMap(const std::string& map_name, robot_nav_msgs:
void NavigationServer::addLaserScan(const std::string& laser_scan_name,
robot_sensor_msgs::LaserScan laser_scan)
{
// Lọc TRƯỚC khi cất: bản được cất và bản costmap nhìn thấy phải là một. Bản cũ cũng cất bản đã
// lọc; nếu getter trả bản thô còn costmap thấy bản lọc thì hai nguồn sự thật sẽ lệch nhau.
// Kho dùng chung nhận bản THÔ, và cố ý nhận TRƯỚC bộ lọc: bộ dò dock fit đường thẳng trên đúng
// hình dạng laser đã thấy, còn costmap muốn bớt điểm nhiễu. Bật `laser_sor_enabled` là hai bên
// nhìn hai bản khác nhau — có chủ đích, không phải sai sót.
robot_sensor_ports::put(sensor_store_, laser_scan_name, laser_scan, laser_scan.header.stamp);
// Lọc TRƯỚC khi cất: bản được cất ở `laser_scans_` và bản costmap nhìn thấy phải là một. Bản cũ
// cũng cất bản đã lọc; nếu getter trả bản thô còn costmap thấy bản lọc thì hai nguồn sự thật sẽ
// lệch nhau.
const robot_sensor_msgs::LaserScan prepared = sensors_.prepareLaserScan(laser_scan);
{
std::lock_guard<std::mutex> lock(data_mutex_);
@@ -590,6 +676,7 @@ void NavigationServer::addLaserScan(const std::string& laser_scan_name,
void NavigationServer::addPointCloud(const std::string& point_cloud_name,
robot_sensor_msgs::PointCloud point_cloud)
{
robot_sensor_ports::put(sensor_store_, point_cloud_name, point_cloud, point_cloud.header.stamp);
{
std::lock_guard<std::mutex> lock(data_mutex_);
point_clouds_[point_cloud_name] = point_cloud;
@@ -600,6 +687,8 @@ void NavigationServer::addPointCloud(const std::string& point_cloud_name,
void NavigationServer::addPointCloud2(const std::string& point_cloud2_name,
robot_sensor_msgs::PointCloud2 point_cloud2)
{
robot_sensor_ports::put(sensor_store_, point_cloud2_name, point_cloud2,
point_cloud2.header.stamp);
{
std::lock_guard<std::mutex> lock(data_mutex_);
point_cloud2s_[point_cloud2_name] = point_cloud2;
@@ -614,6 +703,9 @@ void NavigationServer::addDepthCameraData(const std::string& topic,
{
return; // Bỏ mẫu null thay vì cất một con trỏ rỗng để tầng sau vấp phải.
}
// Zero copy: host đã cầm sẵn ConstPtr, và một mẫu depth là hàng MB — copy để cất là phí thuần tuý.
robot_sensor_ports::put(sensor_store_, topic, data, data->header.stamp);
{
std::lock_guard<std::mutex> lock(data_mutex_);
depth_camera_data_[topic] = data;
@@ -685,30 +777,50 @@ std::map<std::string, robot_sensor_msgs::PointCloud2> NavigationServer::getAllPo
bool NavigationServer::removeStaticMap(const std::string& map_name)
{
// Kho dùng chung phải quên theo: host đã bỏ dữ liệu đó, mà kho vẫn phát bản cũ thì hai
// nguồn nói hai chuyện khác nhau về thứ robot đang nhìn thấy.
sensor_store_.clearTopic(map_name);
std::lock_guard<std::mutex> lock(data_mutex_);
return static_maps_.erase(map_name) > 0;
}
bool NavigationServer::removeLaserScan(const std::string& laser_scan_name)
{
// Kho dùng chung phải quên theo: host đã bỏ dữ liệu đó, mà kho vẫn phát bản cũ thì hai
// nguồn nói hai chuyện khác nhau về thứ robot đang nhìn thấy.
sensor_store_.clearTopic(laser_scan_name);
std::lock_guard<std::mutex> lock(data_mutex_);
return laser_scans_.erase(laser_scan_name) > 0;
}
bool NavigationServer::removePointCloud(const std::string& point_cloud_name)
{
// Kho dùng chung phải quên theo: host đã bỏ dữ liệu đó, mà kho vẫn phát bản cũ thì hai
// nguồn nói hai chuyện khác nhau về thứ robot đang nhìn thấy.
sensor_store_.clearTopic(point_cloud_name);
std::lock_guard<std::mutex> lock(data_mutex_);
return point_clouds_.erase(point_cloud_name) > 0;
}
bool NavigationServer::removePointCloud2(const std::string& point_cloud2_name)
{
// Kho dùng chung phải quên theo: host đã bỏ dữ liệu đó, mà kho vẫn phát bản cũ thì hai
// nguồn nói hai chuyện khác nhau về thứ robot đang nhìn thấy.
sensor_store_.clearTopic(point_cloud2_name);
std::lock_guard<std::mutex> lock(data_mutex_);
return point_cloud2s_.erase(point_cloud2_name) > 0;
}
bool NavigationServer::removeAllStaticMaps()
{
// Kho dùng chung phải quên theo: host đã bỏ dữ liệu đó, mà kho vẫn phát bản cũ thì hai
// nguồn nói hai chuyện khác nhau về thứ robot đang nhìn thấy.
sensor_store_.clearOfType(robot_sensor_ports::SensorType<robot_nav_msgs::OccupancyGrid>::id());
std::lock_guard<std::mutex> lock(data_mutex_);
static_maps_.clear();
return true;
@@ -716,6 +828,10 @@ bool NavigationServer::removeAllStaticMaps()
bool NavigationServer::removeAllLaserScans()
{
// Kho dùng chung phải quên theo: host đã bỏ dữ liệu đó, mà kho vẫn phát bản cũ thì hai
// nguồn nói hai chuyện khác nhau về thứ robot đang nhìn thấy.
sensor_store_.clearOfType(robot_sensor_ports::SensorType<robot_sensor_msgs::LaserScan>::id());
std::lock_guard<std::mutex> lock(data_mutex_);
laser_scans_.clear();
return true;
@@ -723,6 +839,10 @@ bool NavigationServer::removeAllLaserScans()
bool NavigationServer::removeAllPointClouds()
{
// Kho dùng chung phải quên theo: host đã bỏ dữ liệu đó, mà kho vẫn phát bản cũ thì hai
// nguồn nói hai chuyện khác nhau về thứ robot đang nhìn thấy.
sensor_store_.clearOfType(robot_sensor_ports::SensorType<robot_sensor_msgs::PointCloud>::id());
std::lock_guard<std::mutex> lock(data_mutex_);
point_clouds_.clear();
return true;
@@ -730,6 +850,10 @@ bool NavigationServer::removeAllPointClouds()
bool NavigationServer::removeAllPointCloud2s()
{
// Kho dùng chung phải quên theo: host đã bỏ dữ liệu đó, mà kho vẫn phát bản cũ thì hai
// nguồn nói hai chuyện khác nhau về thứ robot đang nhìn thấy.
sensor_store_.clearOfType(robot_sensor_ports::SensorType<robot_sensor_msgs::PointCloud2>::id());
std::lock_guard<std::mutex> lock(data_mutex_);
point_cloud2s_.clear();
return true;