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

@@ -17,6 +17,7 @@
#include <move_base2/control_loop.h>
#include "env_monitor_fixture.h"
#include "fake_ports.h"
using move_base2::ControlLoop;
@@ -117,7 +118,9 @@ NavigationRequest makeActionOnlyRequest(std::size_t count, std::uint64_t sequenc
class Fixture
{
public:
explicit Fixture(const ControlLoopConfig& config = baseConfig()) : recovery_(2)
explicit Fixture(const ControlLoopConfig& config = baseConfig(),
const env_monitor_core::EnvMonitorConfig& env = env_monitor_core::EnvMonitorConfig())
: recovery_(2)
{
pose_.setPosition(0.0, 0.0);
@@ -130,12 +133,14 @@ public:
deps_.action = &action_;
deps_.costmap_status = &costmap_status_;
deps_.environment = &environment_;
deps_.env_monitor = &env_monitor_;
deps_.components = &components_;
planner_.attachStatusBoard(&components_);
recovery_.attachStatusBoard(&components_);
action_.attachStatusBoard(&components_);
std::string error;
EXPECT_TRUE(move_base2::testing::initEnvMonitor(env_monitor_, env, error)) << error;
EXPECT_TRUE(loop_.configure(config, deps_, error)) << error;
}
@@ -173,6 +178,7 @@ public:
return hit_limit_;
}
env_monitor_core::EnvMonitor env_monitor_; ///< trước loop_: loop_ giữ con trỏ tới nó
ControlLoop loop_;
FakeClockPort clock_;
FakePosePort pose_;
@@ -2122,22 +2128,29 @@ ControlLoopConfig envContactConfig()
config.state_machine.recovery_routes.oscillation = { 0 };
config.state_machine.recovery_routes.path_blocked = { 1 };
config.state_machine.recovery_routes.collision = { 1 };
config.env_monitor.enabled = true;
config.env_monitor.stall.action = "log";
config.env_monitor.unreachable.action = "log";
config.env_monitor.contact.action = "recover";
config.env_monitor.contact.apply_command = true;
config.env_monitor.contact.confirm_cycles = 2;
config.env_monitor.contact.trigger_static_time = 0.3; // [s] = 6 cycle, đủ ngắn cho test
config.env_monitor.contact.clean_start_margin = 0.2; // [m]
config.env_sampling.check_period = 0.01; // [s] < chu kỳ 0.05 -> tick nặng mỗi cycle
return config;
}
/// Tham số lõi EnvMonitor của bộ test kContact (nạp qua YAML như runtime).
env_monitor_core::EnvMonitorConfig envContactMonitor()
{
env_monitor_core::EnvMonitorConfig env;
env.enabled = true;
env.stall.action = "log";
env.unreachable.action = "log";
env.contact.action = "recover";
env.contact.apply_command = true;
env.contact.confirm_cycles = 2;
env.contact.trigger_static_time = 0.3; // [s] = 6 cycle, đủ ngắn cho test
env.contact.clean_start_margin = 0.2; // [m]
return env;
}
} // namespace
TEST(WalkingSkeletonEnvContact, BrakesAfterConfirmThenRaisesCollisionAfterStaticTime)
{
Fixture fixture(envContactConfig());
Fixture fixture(envContactConfig(), envContactMonitor());
driveToControlling(fixture);
// Tiếp xúc 0.10 m trên quỹ đạo, trong clean_start_margin: cycle 1 đếm, cycle 2 phanh -> lệnh 0.
@@ -2166,7 +2179,7 @@ TEST(WalkingSkeletonEnvContact, HeldWithTheRouteSpentEscalatesToControllingFaile
// Cùng chuỗi bắt buộc như guard cũ: cạn route collision -> khoá -> brake giữ 0 nhưng controller
// phải bị báo no_valid_command -> controller_patience -> controlling_failed. Không có bước này
// robot đứng chết vô hạn với controller "ổn".
Fixture fixture(envContactConfig());
Fixture fixture(envContactConfig(), envContactMonitor());
driveToControlling(fixture);
fixture.recovery_.setScript({ RecoveryScript::kFailed });
@@ -2207,7 +2220,7 @@ TEST(WalkingSkeletonEnvContact, DetourFromContactThatDidNotMoveTheRobotArmsTheSt
// Run 6/9 16:30 (đám đông): người đứng 7 cm trước mũi -> cờ collision -> detour -> chạm lại ngay -> cờ -> detour ...
// 4 lần rồi ABORT, vì stand-down không được vũ trang (trigger đọc ở cycle tick là mặc định). Chuỗi đúng: sau
// detour do cờ collision mà robot không dịch, chạm lại thì kContact ĐỨNG RA — không phanh, không cờ thứ hai.
Fixture fixture(envContactConfig());
Fixture fixture(envContactConfig(), envContactMonitor());
driveToControlling(fixture);
std::vector<robot_geometry_msgs::PoseStamped> detour{ makeRequest(0.0).goal, makeRequest(3.0).goal };
fixture.recovery_.setScript({ RecoveryScript::kSucceeded });
@@ -2245,7 +2258,7 @@ TEST(WalkingSkeletonEnvContact, DetourFromContactThatDidNotMoveTheRobotArmsTheSt
TEST(WalkingSkeletonEnvContact, ContactBeyondTheStoppingDistanceCapsSpeedWithoutBraking)
{
Fixture fixture(envContactConfig());
Fixture fixture(envContactConfig(), envContactMonitor());
driveToControlling(fixture);
const double v_free = fixture.loop_.lastCommand().linear.x;
ASSERT_GT(v_free, 0.27);