refactor: host EnvMonitor from env_monitor_core, route per detector

EnvMonitor and its detectors now live in env_monitor_core. NavigationRuntime owns
the monitor; StateMachine runs each detector's own route (step or sequence,
lock or abort on exhaustion) with a cursor per detector. RecoveryRunner accepts
named routes for detectors outside the six built-in triggers.
This commit is contained in:
2026-11-27 10:08:52 +07:00
parent 605dbc8e22
commit 1aa2a07469
25 changed files with 828 additions and 3019 deletions

View File

@@ -11,6 +11,8 @@
*********************************************************************/
#include <gtest/gtest.h>
#include <cstdint>
#include <string>
#include <move_base2/core/state_machine.h>
@@ -32,6 +34,38 @@ using move_base2::VelocitySource;
namespace
{
/// Bit của bốn detector mà các test dưới dùng, cùng nghĩa với bốn cờ cố định trước 26/11.
enum LegacyDetector : std::uint32_t
{
kCollisionBit = 1u << 0, ///< contact -> route collision, khoá
kBlockedBit = 1u << 1, ///< unreachable -> route path_blocked, khoá
kOffPathBit = 1u << 2, ///< off_path -> route off_path, khoá
kNoProgressBit = 1u << 3, ///< stall -> route oscillation, ABORT
};
/// Bốn detector theo đúng thứ tự ưu tiên cũ (collision > path_blocked > off_path; stall riêng nhánh controller).
void addLegacyDetectors(move_base2::StateMachineConfig& config)
{
if (config.recovery_routes.empty())
{
config.recovery_routes.fillLegacy(config.recovery_behavior_count);
}
const auto route = [](const char* name, const std::vector<std::size_t>& indices, bool lock, RecoveryTrigger trigger) {
move_base2::DetectorRoute r;
r.name = name;
r.route = indices;
r.lock_on_exhausted = lock;
r.trigger = trigger;
return r;
};
config.detector_routes = {
route("contact", config.recovery_routes.collision, true, RecoveryTrigger::kCollision),
route("unreachable", config.recovery_routes.path_blocked, true, RecoveryTrigger::kPathBlocked),
route("off_path", config.recovery_routes.off_path, true, RecoveryTrigger::kOffPath),
route("stall", config.recovery_routes.oscillation, false, RecoveryTrigger::kOscillation),
};
}
/// @brief Cấu hình cơ sở cho test: ngưỡng ngắn để không phải tua thời gian dài.
StateMachineConfig baseConfig()
{
@@ -53,9 +87,13 @@ StateMachineConfig baseConfig()
class Driver
{
public:
explicit Driver(const StateMachineConfig& config = baseConfig())
explicit Driver(StateMachineConfig config = baseConfig())
{
std::string error;
if (config.detector_routes.empty())
{
addLegacyDetectors(config);
}
configured_ = machine_.configure(config, error);
EXPECT_TRUE(configured_) << error;
now_ = 1000.0;
@@ -884,7 +922,7 @@ TEST(StateMachineControlling, NoProgressFlagEscalatesToOscillationRecovery)
driver.driveToControlling();
StateMachineInput input = controller(ControllerFeedback::kCommandValid);
input.no_progress = true;
input.detector_fired |= kNoProgressBit;
const StateMachineOutput out = driver.tick(input);
EXPECT_EQ(out.state, NavigationState::kRecovering);
@@ -2197,7 +2235,7 @@ StateMachineInput collisionInput()
{
StateMachineInput input;
input.controller = ControllerFeedback::kCommandValid;
input.collision = true;
input.detector_fired |= kCollisionBit;
input.active_recovery_output = RecoveryOutputKind::kPath;
return input;
}
@@ -2206,7 +2244,7 @@ StateMachineInput blockedInput()
{
StateMachineInput input;
input.controller = ControllerFeedback::kCommandValid;
input.path_blocked = true;
input.detector_fired |= kBlockedBit;
// Cả hai route giám sát tuyến đều trỏ vào detour, họ kPath: nó KHÔNG lái robot, chỉ trả đường đi
// mới. Khai đúng họ ở đây để test kiểm được bất biến "robot đứng yên suốt lúc detour".
input.active_recovery_output = RecoveryOutputKind::kPath;
@@ -2217,7 +2255,7 @@ StateMachineInput offPathInput()
{
StateMachineInput input;
input.controller = ControllerFeedback::kCommandValid;
input.off_path = true;
input.detector_fired |= kOffPathBit;
input.active_recovery_output = RecoveryOutputKind::kPath;
return input;
}
@@ -2260,8 +2298,8 @@ TEST(StateMachinePathMonitor, BlockedPathWinsWhenBothFire)
StateMachineInput both;
both.controller = ControllerFeedback::kCommandValid;
both.path_blocked = true;
both.off_path = true;
both.detector_fired |= kBlockedBit;
both.detector_fired |= kOffPathBit;
EXPECT_EQ(driver.tick(both).recovery_trigger, RecoveryTrigger::kPathBlocked);
}
@@ -2303,8 +2341,8 @@ TEST(StateMachineCollisionTrigger, WinsOverPathBlockedAndOffPath)
driver.driveToControlling();
StateMachineInput all = collisionInput();
all.path_blocked = true;
all.off_path = true;
all.detector_fired |= kBlockedBit;
all.detector_fired |= kOffPathBit;
const StateMachineOutput out = driver.tick(all);
EXPECT_EQ(out.state, NavigationState::kRecovering);
EXPECT_EQ(out.recovery_trigger, RecoveryTrigger::kCollision);
@@ -3038,6 +3076,134 @@ TEST(StateMachineEmergency, IsNotTerminalAndMustBeStopped)
EXPECT_STREQ(move_base2::toString(NavigationState::kEmergency), "EMERGENCY");
}
// ------------------------------------------------------------------------------------------------
// route_mode: sequence (chốt 26/11, Test/docs/move_base2/ENV_DETECTOR_PLUGINS_PROMPT.md §4.5)
// ------------------------------------------------------------------------------------------------
namespace
{
/// Một detector `contact` chạy route [0, 1, 2] kiểu sequence, cạn thì khoá.
StateMachineConfig sequenceConfig()
{
StateMachineConfig config = baseConfig();
config.recovery_behavior_count = 3;
config.recovery_routes.planning_failed = { 0 };
config.recovery_routes.controlling_failed = { 0 };
config.recovery_routes.oscillation = { 0 };
move_base2::DetectorRoute contact;
contact.name = "contact";
contact.route = { 0, 1, 2 };
contact.lock_on_exhausted = true;
contact.sequence = true;
contact.trigger = RecoveryTrigger::kCollision;
config.detector_routes = { contact };
return config;
}
StateMachineInput sequenceFire()
{
StateMachineInput input;
input.controller = ControllerFeedback::kCommandValid;
input.detector_fired = 1u;
input.active_recovery_output = RecoveryOutputKind::kPath;
return input;
}
StateMachineInput behaviorDone(RecoveryFeedback result, bool with_path = false)
{
StateMachineInput input;
input.recovery = result;
input.planner = with_path ? PlannerFeedback::kPlanReady : PlannerFeedback::kIdle;
input.active_recovery_output = RecoveryOutputKind::kPath;
return input;
}
} // namespace
TEST(StateMachineSequence, OneFiringRunsTheWholeRouteEvenWhenABehaviorFails)
{
Driver driver(sequenceConfig());
driver.driveToControlling();
StateMachineOutput out = driver.tick(sequenceFire());
ASSERT_EQ(out.state, NavigationState::kRecovering);
EXPECT_TRUE(out.start_recovery);
EXPECT_EQ(out.recovery_index, 0u);
EXPECT_FALSE(out.recovery_chain_continues);
EXPECT_EQ(out.recovery_detector, 0);
out = driver.tick(behaviorDone(RecoveryFeedback::kFailed));
EXPECT_EQ(out.state, NavigationState::kRecovering) << "a failed behavior does not end a sequence";
EXPECT_TRUE(out.start_recovery);
EXPECT_TRUE(out.recovery_chain_continues);
EXPECT_EQ(out.recovery_index, 1u);
out = driver.tick(behaviorDone(RecoveryFeedback::kSucceeded));
EXPECT_TRUE(out.recovery_chain_continues);
EXPECT_EQ(out.recovery_index, 2u);
out = driver.tick(behaviorDone(RecoveryFeedback::kSucceeded));
EXPECT_TRUE(out.recovery_chain_finished);
EXPECT_FALSE(out.recovery_chain_has_path);
EXPECT_FALSE(out.recovery_chain_last_failed);
EXPECT_FALSE(out.apply_plan);
EXPECT_EQ(out.state, NavigationState::kControlling);
// Cả chuỗi là MỘT lượt: lần nổ sau route đã cạn -> khoá (lock), robot bám tiếp plan.
out = driver.tick(sequenceFire());
EXPECT_TRUE(out.monitor_trigger_suppressed);
EXPECT_FALSE(out.start_recovery);
EXPECT_EQ(out.state, NavigationState::kControlling);
}
TEST(StateMachineSequence, APathInTheMiddleIsHeldAppliedAtTheEndAndRefillsTheRoute)
{
Driver driver(sequenceConfig());
driver.driveToControlling();
driver.tick(sequenceFire());
StateMachineOutput out = driver.tick(behaviorDone(RecoveryFeedback::kSucceeded, true));
EXPECT_TRUE(out.recovery_chain_continues) << "a new path does not end a sequence";
EXPECT_FALSE(out.apply_plan) << "the path waits for the end of the chain";
EXPECT_EQ(out.state, NavigationState::kRecovering);
driver.tick(behaviorDone(RecoveryFeedback::kSucceeded));
out = driver.tick(behaviorDone(RecoveryFeedback::kFailed));
EXPECT_TRUE(out.recovery_chain_finished);
EXPECT_TRUE(out.recovery_chain_has_path);
EXPECT_TRUE(out.recovery_chain_last_failed);
EXPECT_TRUE(out.apply_plan);
EXPECT_EQ(out.state, NavigationState::kControlling);
EXPECT_STREQ(out.reason, "recovery produced a detour path");
// Chuỗi có path mới và đây là lần nạp đầu: route nạp lại, lần nổ sau chạy lại cả chuỗi.
out = driver.tick(sequenceFire());
EXPECT_FALSE(out.monitor_trigger_suppressed);
EXPECT_TRUE(out.start_recovery);
EXPECT_EQ(out.recovery_index, 0u);
}
TEST(StateMachineSequence, ANewPathWithoutTravelDoesNotRefillTheRoute)
{
StateMachineConfig config = sequenceConfig();
config.recovery_reset_min_travel = 0.10; // [m]
Driver driver(config);
driver.driveToControlling();
// Lượt 1: có path -> nạp lại (lần đầu luôn được), mốc quãng đường đặt ở 0.
driver.tick(sequenceFire());
driver.tick(behaviorDone(RecoveryFeedback::kSucceeded, true));
driver.tick(behaviorDone(RecoveryFeedback::kSucceeded));
ASSERT_TRUE(driver.tick(behaviorDone(RecoveryFeedback::kSucceeded)).recovery_chain_finished);
// Lượt 2: lại có path nhưng robot chưa đi được 0.10 m -> tính như một lượt, route cạn.
driver.tick(sequenceFire());
driver.tick(behaviorDone(RecoveryFeedback::kSucceeded, true));
driver.tick(behaviorDone(RecoveryFeedback::kSucceeded));
const StateMachineOutput end = driver.tick(behaviorDone(RecoveryFeedback::kSucceeded));
ASSERT_TRUE(end.recovery_chain_finished);
EXPECT_TRUE(end.recovery_reset_denied);
EXPECT_TRUE(driver.tick(sequenceFire()).monitor_trigger_suppressed);
}
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);