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

@@ -934,6 +934,211 @@ TEST(NavigationServerLifecycle, LifecycleRequestIsConsumedExactlyOnce)
EXPECT_NE(fixture.server_.loop().state(), NavigationState::kPausing);
}
// ================================================================================================
// EMERGENCY (2026-09-14) — phản ứng trên thread host trước, control thread dọn sau
// ================================================================================================
namespace
{
/// @brief Đưa fixture tới CONTROLLING với lệnh khác 0 đang phát.
void driveToControllingWithSpeed(Fixture& fixture, double speed)
{
fixture.configure();
fixture.controller_.setNominalSpeed(speed);
fixture.controller_.setScript({ ControllerScript::kOk, ControllerScript::kOk, ControllerScript::kOk,
ControllerScript::kOk, ControllerScript::kOk });
ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10))
<< fixture.server_.lastRejectReason();
fixture.spin(2);
ASSERT_EQ(fixture.server_.loop().state(), NavigationState::kControlling);
ASSERT_NEAR(fixture.server_.getTwist().velocity.x, speed, 1e-9);
}
} // namespace
TEST(NavigationServerEmergency, ForcesZeroTwistAndEmergencyStateBeforeTheNextCycle)
{
Fixture fixture;
driveToControllingWithSpeed(fixture, 0.3);
const robot::Time before = fixture.server_.getTwist().header.stamp;
fixture.server_.emergency();
// KHÔNG spin: đây là điều khác biệt với pause/cancel — host phải thấy 0 ngay trong lời gọi kế.
const robot_nav_2d_msgs::Twist2DStamped twist = fixture.server_.getTwist();
EXPECT_NEAR(twist.velocity.x, 0.0, 1e-9) << "the last command must not leak past emergency()";
EXPECT_NEAR(twist.velocity.theta, 0.0, 1e-9);
EXPECT_GE(twist.header.stamp.toSec(), before.toSec()) << "the zero must be fresh so the host publishes it";
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::EMERGENCY);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kControlling)
<< "the core itself only learns about it on the next cycle";
}
TEST(NavigationServerEmergency, EntersTheCoreStateOnTheNextCycleAndReportsTheLegCancelled)
{
Fixture fixture;
driveToControllingWithSpeed(fixture, 0.3);
fixture.server_.emergency();
fixture.spin(1);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::EMERGENCY);
EXPECT_NEAR(fixture.server_.getTwist().velocity.x, 0.0, 1e-9);
ASSERT_EQ(fixture.server_.loop().outcomeReportCount(), 1u) << "the interrupted leg is reported exactly once";
EXPECT_STREQ(fixture.server_.loop().lastOutcome(), "CANCELLED");
fixture.spin(5);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency) << "it is a latch, not a state that expires";
EXPECT_EQ(fixture.server_.loop().outcomeReportCount(), 1u);
}
TEST(NavigationServerEmergency, MoveToAndInstantActionsAreRejectedWhileLatched)
{
Fixture fixture;
fixture.configure();
fixture.server_.emergency();
fixture.spin(1);
EXPECT_FALSE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10));
EXPECT_NE(fixture.server_.lastRejectReason().find("emergency"), std::string::npos)
<< fixture.server_.lastRejectReason();
robot_protocol_msgs::Action action;
action.actionType = "PickUp";
action.actionId = "PickUp_1";
action.blockingType = "HARD";
EXPECT_FALSE(fixture.server_.executeInstantActions({ action }));
fixture.spin(3);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
}
TEST(NavigationServerEmergency, DropsAGoalThatWasStillPending)
{
Fixture fixture;
fixture.configure();
fixture.controller_.setScript({ ControllerScript::kOk });
ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10));
// Goal đã vào ControlLoop (pending) nhưng chưa qua cycle nào — dừng khẩn tới trước.
fixture.server_.emergency();
fixture.spin(3);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
EXPECT_EQ(fixture.server_.loop().outcomeReportCount(), 0u) << "a leg that never started has no outcome";
// Gỡ chốt: goal cũ KHÔNG được tự chạy.
fixture.server_.clearEmergency();
fixture.spin(3);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kIdle);
}
TEST(NavigationServerEmergency, ClearReportsPendingAndAcceptsANewGoal)
{
Fixture fixture;
driveToControllingWithSpeed(fixture, 0.3);
fixture.server_.emergency();
fixture.spin(1);
ASSERT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
fixture.server_.clearEmergency();
fixture.spin(1);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kIdle);
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::PENDING)
<< "a clean slate, not a latched PREEMPTED: the host must send a new goal";
ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10))
<< fixture.server_.lastRejectReason();
fixture.spin(2);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kControlling);
}
TEST(NavigationServerEmergency, WinsOverPauseAndCancelRequestedInTheSameDrain)
{
Fixture fixture;
driveToControllingWithSpeed(fixture, 0.3);
fixture.server_.pause();
fixture.server_.cancel();
fixture.server_.emergency();
fixture.spin(1);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
// Và ngược lại: dừng khẩn rồi mới pause/cancel cũng không đổi gì.
fixture.server_.pause();
fixture.server_.cancel();
fixture.server_.resume();
fixture.spin(2);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
}
TEST(NavigationServerEmergency, ClearThenEmergencyInTheSameDrainStaysLatched)
{
// Hai cờ đối nghịch dưới một lock: lệnh gọi SAU thắng, như pause/resume.
Fixture fixture;
fixture.configure();
fixture.server_.emergency();
fixture.spin(1);
ASSERT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
fixture.server_.clearEmergency();
fixture.server_.emergency();
fixture.spin(2);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::EMERGENCY);
}
TEST(NavigationServerEmergency, TwistStampFreezesAfterTheGraceWindowWhileStillLatched)
{
Fixture fixture;
driveToControllingWithSpeed(fixture, 0.3);
fixture.server_.emergency();
const robot::Time fresh = fixture.server_.getTwist().header.stamp;
EXPECT_NEAR(fresh.toSec(), fixture.clock_.now().toSec(), 1e-9);
// Trong cửa ân hạn: stamp tiến theo đồng hồ (host publish 0 tường minh).
fixture.clock_.advance(0.2);
EXPECT_NEAR(fixture.server_.getTwist().header.stamp.toSec(), fixture.clock_.now().toSec(), 1e-9);
// Hết cửa: stamp đứng — host thấy lệnh quá hạn, nhường /cmd_vel; vận tốc vẫn 0.
fixture.clock_.advance(1.0);
const robot_nav_2d_msgs::Twist2DStamped late = fixture.server_.getTwist();
EXPECT_LT(late.header.stamp.toSec(), fixture.clock_.now().toSec() - 0.5);
EXPECT_NEAR(late.velocity.x, 0.0, 1e-9);
}
TEST(NavigationServerEmergency, OverridesTwistEvenWhenTheControlThreadNeverSpins)
{
// Mô phỏng control thread kẹt: không spin sau emergency(). Lệnh 0.3 m/s cuối cùng còn nằm
// trong twist_ của control loop — host không được thấy nó.
Fixture fixture;
driveToControllingWithSpeed(fixture, 0.3);
fixture.server_.emergency();
for (int i = 0; i < 5; ++i)
{
fixture.clock_.advance(0.01);
EXPECT_NEAR(fixture.server_.getTwist().velocity.x, 0.0, 1e-9) << "read " << i;
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::EMERGENCY);
}
}
TEST(NavigationServerEmergency, IsIdempotentAndClearWithoutLatchIsANoOp)
{
Fixture fixture;
fixture.configure();
fixture.server_.clearEmergency(); // chưa chốt gì — không được đổi state
fixture.spin(1);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kIdle);
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::PENDING);
fixture.server_.emergency();
fixture.server_.emergency();
fixture.spin(2);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kEmergency);
fixture.server_.clearEmergency();
fixture.server_.clearEmergency();
fixture.spin(2);
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kIdle);
}
// ================================================================================================
// Control thread
//