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

@@ -19,6 +19,8 @@
#include <robot/node_handle.h>
#include <move_base2/runners/controller_runner.h>
#include <robot_sensor_ports/sensor_store.h>
#include <robot_sensor_ports/sensor_types.h>
#include "fake_ports.h"
@@ -471,6 +473,99 @@ TEST(ControllerRunnerDockingMarker, CachedPlannerIsReinitializedWhenMarkerChange
<< "instance cache kept the old maker_name — it must re-init when the marker changes";
}
// ================================================================================================
// Kho cảm biến thô: dynamic_cast qua ranh giới .so
// ================================================================================================
TEST(ControllerRunnerSensors, TheSharedStoreReachesAPlannerThatAsksForIt)
{
// Đây là test DUY NHẤT chứng minh được `dynamic_cast<SensorConsumer*>` khớp RTTI qua `dlopen`.
// Build xanh không nói gì về chuyện đó: cast lệch trả null, planner im lặng chạy không cảm biến.
robot_sensor_ports::SensorStore store;
robot_sensor_ports::SensorStoreConfig store_config;
robot_sensor_ports::SensorEntryConfig entry;
entry.topic = "/probe_scan";
entry.data_type = "robot_sensor_msgs/LaserScan";
entry.capacity = 1;
entry.max_age = 0.5; // [s]
store_config.sensors.push_back(entry);
std::string error;
ASSERT_TRUE(store.configure(store_config, error)) << error;
ControllerRunner runner;
runner.disableCostmapLockForTesting();
runner.setSensorReader(&store); // phải TRƯỚC configure()
robot::NodeHandle nh;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(), "TestControllerSensorProbe",
error))
<< error;
ASSERT_TRUE(runner.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
// theta = 1.0 nghĩa là plugin đã cầm cổng đọc TỪ LÚC initialize, không phải sau đó.
EXPECT_NEAR(cmd.angular.z, 1.0, 1e-9) << "dynamic_cast qua .so không khớp — RTTI lệch";
EXPECT_NEAR(cmd.linear.x, 0.0, 1e-9) << "chưa có scan nào thì chưa đọc được gì";
robot_sensor_msgs::LaserScan scan;
scan.ranges.assign(10, 1.0F);
ASSERT_TRUE(robot_sensor_ports::put(store, "/probe_scan", scan, robot::Time(10.0)));
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 1.0, 1e-9) << "plugin không đọc được scan từ kho";
// Không có scan mới: cursor phải giữ, không đếm lại đúng bản cũ.
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 1.0, 1e-9) << "cursor không giữ — cùng một scan bị đếm hai lần";
ASSERT_TRUE(robot_sensor_ports::put(store, "/probe_scan", scan, robot::Time(10.1)));
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, 2.0, 1e-9);
}
TEST(ControllerRunnerSensors, APlannerThatDoesNotAskForSensorsIsUnaffected)
{
// Phần lớn planner trong workspace không kế thừa SensorConsumer. Cast ra null và không có gì xảy
// ra — đó là lý do thêm kho không bắt gói nào build lại.
robot_sensor_ports::SensorStore store;
ControllerRunner runner;
runner.disableCostmapLockForTesting();
runner.setSensorReader(&store);
robot::NodeHandle nh;
std::string error;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(), "TestControllerOk", error))
<< error;
ASSERT_TRUE(runner.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.linear.x, kBaseSpeed, 1e-9);
}
TEST(ControllerRunnerSensors, APlannerStillLoadsWhenNoStoreIsConfigured)
{
// Runtime không khai `sensor_store` thì cổng là null. Planner khai nhận cảm biến vẫn phải nạp
// được và chạy được, chỉ là không đọc được gì.
ControllerRunner runner;
runner.disableCostmapLockForTesting();
robot::NodeHandle nh;
std::string error;
ASSERT_TRUE(runner.configure(nh, nullptr, dummyCostmap(), &fixedPose(),
"TestControllerSensorProbe", error))
<< error;
ASSERT_TRUE(runner.setPlan(makePlan()));
robot_geometry_msgs::Twist cmd;
ASSERT_TRUE(runner.computeVelocityCommands(cmd));
EXPECT_NEAR(cmd.angular.z, 0.0, 1e-9) << "không có kho thì cổng phải là null";
EXPECT_NEAR(cmd.linear.x, 0.0, 1e-9);
}
int main(int argc, char** argv)
{
setenv("PNKX_NAV_CORE_CONFIG_DIR", MOVE_BASE2_TEST_CONFIG_DIR, 0);