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

@@ -12,6 +12,7 @@
#include <string>
#include <robot/node_handle.h>
#include <robot_sensor_ports/sensor_store_config.h>
#include <move_base2/control_loop.h>
#include <move_base2/io/sensor_gateway.h>
@@ -72,6 +73,32 @@ struct MoveBase2Config
/// bộ lọc laser mặc định tắt.
SensorGatewayConfig sensors;
/**
* Kho cảm biến **thô** dùng chung, đọc từ namespace con `sensor_store`.
*
* Khác @ref sensors ở chỗ nó không nằm trên đường vào costmap: costmap nhận bản đã qua
* `prepareLaserScan`, kho nhận bản THÔ. Bật lọc SOR là hai bên cố ý nhìn hai bản khác nhau —
* costmap muốn bớt điểm nhiễu, bộ dò muốn hình dạng thật.
*
* Không khai sensor nào là hợp lệ: runtime không có consumer nào của dữ liệu thô thì giữ rỗng.
*/
robot_sensor_ports::SensorStoreConfig sensor_store;
/// Lỗi schema của `sensor_store`, giữ lại để @ref validate chặn boot thay vì chạy với kho rỗng.
std::string sensor_store_error;
/**
* [s] Chu kỳ in tình trạng kho cảm biến ra terminal. `0` = tắt.
*
* Đọc từ `sensor_store/report_period`. Mặc định tắt vì đây là công cụ chẩn đoán, không phải thứ
* chạy trên robot sản xuất.
*
* Việc thật của nó: khi chưa có consumer nào đọc kho, đây là cách DUY NHẤT nhìn thấy dữ liệu có
* thật sự vào tới nơi hay không. Không có nó thì gõ sai `topic` và cảm biến chết cho cùng một
* triệu chứng — im lặng.
*/
double sensor_store_report_period = 0.0;
// --- Ánh xạ profile -> planner ----------------------------------------------------------------
ProfileBinding position;

View File

@@ -0,0 +1,46 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* move_base2 — message types this runtime adds to the shared sensor store.
*
* Author: DuongTD
*********************************************************************/
#ifndef MOVE_BASE2_IO_SENSOR_STORE_TYPES_H_
#define MOVE_BASE2_IO_SENSOR_STORE_TYPES_H_
#include <robot_nav_msgs/OccupancyGrid.h>
#include <robot_sensor_ports/sensor_type.h>
#include <robot_sensor_ports/sensor_types.h>
namespace robot_sensor_ports
{
/**
* @brief The one door of `BaseNavigation` whose payload does not come from `robot_sensor_msgs`.
*
* `robot_sensor_ports` deliberately ships only its own package's types — registering this one there
* would drag `robot_nav_msgs` into every translation unit that touches the store. So it is
* registered where it is used, which is what the extension path in that package's README describes.
*
* One line here plus one `TypeNames::add()` call, and `data_type: OccupancyGrid` works in YAML.
* No interface changed, no plugin rebuilt.
*/
ROBOT_SENSOR_PORTS_REGISTER_TYPE(::robot_nav_msgs::OccupancyGrid, "robot_nav_msgs/OccupancyGrid")
} // namespace robot_sensor_ports
namespace move_base2
{
/**
* @brief Make this runtime's extra message types nameable in YAML.
*
* Must run **before** the `sensor_store` section is parsed, otherwise `data_type: OccupancyGrid` is
* rejected as unknown. Idempotent, so calling it more than once is harmless.
*/
void registerSensorStoreTypes();
} // namespace move_base2
#endif // MOVE_BASE2_IO_SENSOR_STORE_TYPES_H_

View File

@@ -34,6 +34,11 @@ namespace tf3
class BufferCore;
}
namespace robot_sensor_ports
{
class SensorReader;
}
namespace move_base2
{
@@ -212,6 +217,20 @@ public:
*/
bool buildRunners(std::string& error);
/**
* @brief Cấp kho cảm biến thô cho action handler và local planner.
*
* Phải gọi **trước** @ref buildRunners: cả hai bên nhận cổng này lúc nạp plugin, và một planner
* có quyền dùng nó ngay trong `initialize()` của nó.
*
* @param reader Non-owning, được phép null (không có kho). Phải sống lâu hơn runtime — chủ sở hữu
* kho là @ref NavigationServer, và nó thả runtime trước khi thả kho.
*/
void setSensorReader(const robot_sensor_ports::SensorReader* reader)
{
sensors_ = reader;
}
/// @brief Hai costmap đã dựng xong chưa (pha 1).
bool costmapsReady() const
{
@@ -338,6 +357,9 @@ private:
SystemClock clock_;
/// Kho cảm biến thô dùng chung. Non-owning; null = host không cấp.
const robot_sensor_ports::SensorReader* sensors_ = nullptr;
/**
* HAI nguồn pose, khác FRAME — không gộp được.
*

View File

@@ -23,6 +23,7 @@
#include <move_base2/core/navigation_request.h>
#include <move_base2/io/runtime_stats.h>
#include <move_base2/io/sensor_gateway.h>
#include <robot_sensor_ports/sensor_store.h>
#include <move_base2/navigation_runtime.h>
namespace move_base2
@@ -118,6 +119,35 @@ public:
*/
bool configureSensors(const SensorGatewayConfig& config, std::string& error);
/**
* @brief Khai báo kho cảm biến THÔ dùng chung.
*
* Phải gọi TRƯỚC `NavigationRuntime::buildRunners`: action handler và local planner đều nhận cổng
* đọc lúc nạp plugin. Khai sai thì trả false và kho giữ nguyên trạng thái cũ.
*
* Tách khỏi @ref configureSensors vì hai đường khác nhau: `configureSensors` là đường vào
* **costmap** (có bộ lọc), còn đây là kho **bản thô** cho consumer đọc lại.
*/
bool configureSensorStore(const robot_sensor_ports::SensorStoreConfig& config, std::string& error);
/// @brief Cổng chỉ-đọc của kho cảm biến thô. Dùng cho chẩn đoán và cho test.
const robot_sensor_ports::SensorReader& sensorStore() const
{
return sensor_store_;
}
/**
* @brief Bật in tình trạng kho cảm biến theo chu kỳ.
* @param period [s] 0 = tắt.
*
* Khi chưa có consumer nào đọc kho, đây là cách duy nhất thấy dữ liệu có vào tới nơi hay không —
* gõ sai `topic` và cảm biến chết cho cùng một triệu chứng.
*/
void setSensorStoreReportPeriod(double period)
{
sensor_store_report_period_ = period;
}
/**
* @brief Gắn hai costmap đích cho dữ liệu cảm biến, rồi phát lại các static map đã nhận.
*
@@ -297,9 +327,31 @@ private:
/// @brief Ánh xạ state của lõi sang enum trạng thái của contract host.
static robot::move_base_core::State toHostState(NavigationState state);
/// @brief In tình trạng kho cảm biến nếu tới chu kỳ. Gọi từ @ref spinOnce (control thread).
void reportSensorStore();
ControlLoop loop_;
SensorGateway sensors_;
/**
* Kho cảm biến THÔ dùng chung — bản trước bộ lọc của @ref sensors_.
*
* Khai TRƯỚC @ref runtime_ là bắt buộc, không phải phong cách: runtime giữ con trỏ tới kho này
* (`setSensorReader`), và thứ tự huỷ thành viên là ngược thứ tự khai — kho phải chết SAU runtime.
*/
robot_sensor_ports::SensorStore sensor_store_;
/// [s] Chu kỳ in tình trạng kho. 0 = tắt.
double sensor_store_report_period_ = 0.0;
robot::Time last_sensor_store_report_;
bool have_sensor_store_report_ = false;
/// Đồng hồ của control loop, mượn ở @ref configureLoop. Non-owning, có thể null.
///
/// 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, nên tuổi in ra sẽ vô nghĩa trong Gazebo.
ClockPort* clock_ = nullptr;
/// Thành phần runtime thật, dựng trong @ref initialize. Null khi cổng được bơm từ ngoài (test).
std::unique_ptr<NavigationRuntime> runtime_;

View File

@@ -33,6 +33,11 @@ namespace tf3
class BufferCore;
}
namespace robot_sensor_ports
{
class SensorReader;
}
namespace move_base2
{
@@ -121,6 +126,20 @@ public:
// ================================================================================================
bool swapPlanner(const std::string& planner_name) override;
/**
* @brief Cấp kho cảm biến thô cho local planner nào khai nhận nó.
*
* Phải gọi trước @ref configure. Planner không kế thừa `robot_sensor_ports::SensorConsumer` thì
* không có gì xảy ra — xem @ref attachSensors về lý do làm bằng `dynamic_cast` thay vì nới chữ ký
* `initialize`.
*
* @param reader Non-owning, được phép null. Sống lâu hơn mọi planner instance.
*/
void setSensorReader(const robot_sensor_ports::SensorReader* reader)
{
sensors_ = reader;
}
bool setDockingMarker(const std::string& marker) override;
bool setPlan(const std::vector<robot_geometry_msgs::PoseStamped>& plan) override;
@@ -176,6 +195,22 @@ private:
/// @brief Áp lại trần vận tốc đã lưu lên controller vừa đổi sang.
void applyPendingLimits(robot_nav_core2::LocalPlanner* controller);
/**
* @brief Đưa kho cảm biến cho @p controller nếu nó khai nhận, ngay TRƯỚC `initialize()`.
*
* `robot_nav_core2::LocalPlanner::initialize` nhận đúng (NodeHandle, tên, TF, costmap). Nới chữ
* ký đó để một planner đọc được LiDAR sẽ gãy TẤT CẢ planner còn lại trong workspace cùng lúc —
* hybrid, mppi, priest, stanley, deep_mpc. Nên planner nào cần thì kế thừa thêm
* `robot_sensor_ports::SensorConsumer`, và ở đây `dynamic_cast` hỏi xem nó có kế thừa không.
* Không kế thừa thì cast ra null và không có gì xảy ra: không chữ ký nào đổi, không `.so` nào
* phải build lại.
*
* @warning Cast này đi qua ranh giới `.so`, nên chỉ đúng khi hai bên thấy CÙNG một định nghĩa và
* build cùng toolchain/visibility. Build xanh không chứng minh điều đó — nên hàm này log
* kết quả cast thật, thay vì coi như đã khớp.
*/
void attachSensors(robot_nav_core2::LocalPlanner* controller) const;
/// @brief Pose hiện tại dạng 2D. @return false khi mất TF — bên gọi phải dừng, không đi mù.
bool currentPose(robot_nav_2d_msgs::Pose2DStamped& pose) const;
@@ -198,6 +233,9 @@ private:
std::string active_name_;
robot_nav_core2::LocalPlanner* active_ = nullptr; ///< Non-owning, trỏ vào @ref controllers_.
/// Kho cảm biến thô dùng chung. Non-owning; null = không có kho.
const robot_sensor_ports::SensorReader* sensors_ = nullptr;
/**
* Marker vừa đổi qua @ref setDockingMarker — instance ở lần @ref acquire kế tiếp phải được dựng
* lại từ factory: docking planner đọc `maker_name` đúng MỘT lần trong `initialize()` (getMaker),