feat: lan truyen BaseNavigation::emergency()/clearEmergency() len tren

NavigationServer::emergency() phan anh ngay tren thread goi (getTwist
tra 0, getFeedback bao EMERGENCY) truoc khi ham tra ve, du control
thread dang block trong plugin nao; don dep that (planner/recovery/
action/mission layer) chay o chu ky ke cua control thread. State
NavigationState::kEmergency la state GIU, khong terminal: pause/resume/
cancel/goal moi deu bi bo qua, chi clearEmergency() moi thoat, ve
kIdle.

MissionLayer: emergency() -> drain hang doi, goi cancel toi navigation
dung 1 lan, chuyen MissionState::EMERGENCY; clearEmergency() ve
CLEAR_EMERGENCY, khong tu chay lai gi, cho order ke tiep.

Test moi cho ca 2 lop (navigation_server_test, mission_layer_test,
state_machine_test, walking_skeleton_test) phu: force-zero-truoc-cycle,
tu choi moveTo/instantActions khi dang chot, huy goal con pending, mo
chot roi nhan goal moi.
This commit is contained in:
2026-09-15 15:08:09 +07:00
parent ac307b0358
commit f42cff3510
16 changed files with 1058 additions and 11 deletions

View File

@@ -989,6 +989,147 @@ TEST(ControlLoop, GoalSentRightAfterCancelWaitsForTheBusyPlannerInsteadOfFailing
EXPECT_EQ(fixture.mission_.reportCountFor(4), 0u) << "mission 4 is still running";
}
TEST(ControlLoop, EmergencyWhileControllingZeroesTheCommandAndLatches)
{
Fixture fixture;
fixture.controller_.setScript({ControllerScript::kOk, ControllerScript::kOk, ControllerScript::kOk});
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, 5), reason)) << reason;
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kControlling);
ASSERT_GT(fixture.loop_.lastCommand().linear.x, 0.0);
fixture.loop_.requestEmergency();
fixture.clock_.advance(kControlPeriod);
EXPECT_TRUE(fixture.loop_.step()) << "EMERGENCY is not terminal: the loop keeps running";
EXPECT_EQ(fixture.loop_.state(), NavigationState::kEmergency);
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0) << "zero in the very cycle, no ramp";
EXPECT_STREQ(fixture.loop_.lastOutcome(), "CANCELLED");
EXPECT_EQ(fixture.mission_.reportCountFor(5), 1u);
EXPECT_FALSE(fixture.loop_.hasActiveRequest());
// Chốt: goal mới bị từ chối tại cửa, pause/resume/cancel không đổi gì, lệnh vẫn 0.
EXPECT_FALSE(fixture.loop_.submit(makeRequest(5.0, 6), reason));
EXPECT_NE(reason.find("emergency"), std::string::npos) << reason;
fixture.loop_.requestPause();
fixture.loop_.requestResume();
fixture.loop_.requestCancel();
for (int i = 0; i < 5; ++i)
{
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
ASSERT_EQ(fixture.loop_.state(), NavigationState::kEmergency) << "cycle " << i;
ASSERT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0);
}
EXPECT_EQ(fixture.mission_.reportCountFor(5), 1u) << "reported exactly once";
// Gỡ chốt: IDLE, không tự chạy; goal kế được nhận bình thường.
fixture.loop_.requestClearEmergency();
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kIdle);
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kIdle);
ASSERT_TRUE(fixture.loop_.submit(makeRequest(5.0, 6), reason)) << reason;
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kPlanning);
}
TEST(ControlLoop, EmergencyWhilePlannerIsBusyPreemptsItAndTheNextGoalWaitsInsteadOfFailing)
{
Fixture fixture(busyPlannerConfig());
driveToPlanningInFlight(fixture, 7);
const std::size_t cancels_before = fixture.planner_.cancelCount();
fixture.loop_.requestEmergency();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
ASSERT_EQ(fixture.loop_.state(), NavigationState::kEmergency)
<< "EMERGENCY is reported at once — it is a latch, not 'done'; nothing waits for quiescence";
EXPECT_EQ(fixture.planner_.cancelCount(), cancels_before + 1) << "the running makePlan is cut";
EXPECT_EQ(fixture.mission_.reportCountFor(7), 1u);
// Gỡ chốt trong lúc thread planner vẫn còn bận lượt đã huỷ, rồi gửi goal kế ngay: phải chờ
// (kBusy) chứ không được coi là hỏng.
fixture.loop_.requestClearEmergency();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
ASSERT_EQ(fixture.loop_.state(), NavigationState::kIdle);
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(5.0, 8), reason)) << reason;
bool saw_recovering = false;
bool saw_aborted = false;
bool reached_controlling = false;
for (int i = 0; i < 60 && !reached_controlling; ++i)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
const NavigationState s = fixture.loop_.state();
saw_recovering = saw_recovering || s == NavigationState::kRecovering;
saw_aborted = saw_aborted || s == NavigationState::kAborted;
reached_controlling = s == NavigationState::kControlling;
}
EXPECT_FALSE(saw_recovering);
EXPECT_FALSE(saw_aborted);
EXPECT_TRUE(reached_controlling);
EXPECT_EQ(fixture.mission_.reportCountFor(8), 0u);
}
TEST(ControlLoop, EmergencyDropsAPendingRequestSoNothingRunsAfterClear)
{
Fixture fixture;
fixture.controller_.setScript({ControllerScript::kOk});
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, 9), reason)) << reason;
ASSERT_EQ(fixture.loop_.state(), NavigationState::kIdle) << "still pending, no cycle ran";
fixture.loop_.requestEmergency();
fixture.loop_.step();
EXPECT_EQ(fixture.loop_.state(), NavigationState::kEmergency);
EXPECT_EQ(fixture.mission_.reportCountFor(9), 0u) << "never accepted, nothing to report";
fixture.loop_.requestClearEmergency();
for (int i = 0; i < 3; ++i)
{
fixture.clock_.advance(kControlPeriod);
fixture.loop_.step();
}
EXPECT_EQ(fixture.loop_.state(), NavigationState::kIdle) << "the dropped goal must not start by itself";
}
TEST(ControlLoop, EmergencyDuringRecoveryStopsTheBehavior)
{
Fixture fixture;
fixture.planner_.setScript({PlannerScript::kFail});
fixture.recovery_.setScript({RecoveryScript::kRunning});
fixture.recovery_.setRecoveryVelocity(true, -0.15);
std::string reason;
ASSERT_TRUE(fixture.loop_.submit(makeRequest(3.0, 10), reason)) << reason;
for (int i = 0; i < 40 && fixture.loop_.state() != NavigationState::kRecovering; ++i)
{
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
}
ASSERT_EQ(fixture.loop_.state(), NavigationState::kRecovering);
fixture.loop_.requestEmergency();
fixture.loop_.step();
fixture.clock_.advance(kControlPeriod);
EXPECT_EQ(fixture.loop_.state(), NavigationState::kEmergency);
EXPECT_EQ(fixture.recovery_.cancelCount(), 1u);
EXPECT_DOUBLE_EQ(fixture.loop_.lastCommand().linear.x, 0.0) << "the behavior's -0.15 m/s must not survive";
EXPECT_EQ(fixture.mission_.reportCountFor(10), 1u);
}
TEST(ControlLoop, PauseWhilePlanningThenResumeWaitsForTheBusyPlanner)
{
// pause lúc PLANNING đi cùng đường cancelPlan(); resume → PLANNING → startPlan() bị từ chối →