diff --git a/include/move_base2/navigation_server.h b/include/move_base2/navigation_server.h index 30403bf..63c8826 100644 --- a/include/move_base2/navigation_server.h +++ b/include/move_base2/navigation_server.h @@ -370,6 +370,20 @@ private: bool mission_cancel_requested_ = false; std::string last_reject_reason_; + + /** + * Kết quả cuối của yêu cầu vừa kết thúc, giữ lại cho tới khi có việc mới. + * + * Lõi chỉ đứng ở `kSucceeded`/`kAborted`/`kCancelled` đúng MỘT cycle rồi về `kIdle` — 33 ms ở + * `controller_frequency: 30`. Host thì đọc `getFeedback()` theo chu kỳ riêng của nó (đo được + * 100 ms trên robot thật), nên báo theo mức làm mất kết quả 2/3 số lần: host thấy CONTROLLING + * rồi lần đọc sau đã là PENDING, không có sự kiện hoàn thành nào, và người vận hành phải bấm + * cancel một order đã tới đích. + * + * Kết quả của một goal là SỰ KIỆN, không phải mức. Chốt lại đúng ngữ nghĩa actionlib: đọc được + * cho tới khi bị yêu cầu kế tiếp thay thế. `PENDING` = không có kết quả nào đang được chốt. + */ + robot::move_base_core::State latched_result_ = robot::move_base_core::State::PENDING; }; } // namespace move_base2 diff --git a/src/navigation_server.cpp b/src/navigation_server.cpp index 7128209..fb364aa 100644 --- a/src/navigation_server.cpp +++ b/src/navigation_server.cpp @@ -302,6 +302,35 @@ void NavigationServer::refreshFeedback() host_state = robot::move_base_core::State::ACTIVE; } + // Chốt kết quả cuối — xem @ref latched_result_. Phải chạy SAU khối mask ở trên: giữa hai chặng + // của cùng một order, SUCCEEDED đã được đổi thành ACTIVE nên không bị chốt nhầm thành "order xong". + // + // Ba nhánh, loại trừ nhau: + // terminal -> ghi chốt (lõi chỉ đi qua đây một cycle); + // PENDING (kIdle) mà đang có chốt -> báo lại chốt thay vì PENDING; + // mọi state hoạt động khác -> có việc mới, xoá chốt. + // + // Xoá chốt theo STATE chứ không theo lời gọi `submit`: goal còn vào qua mission layer + // (`missionLayer().submitGoal`) và qua bridge, ba đường vào khác nhau. Bám vào state là chỗ duy + // nhất cả ba đường đều đi qua. + if (host_state == robot::move_base_core::State::SUCCEEDED || + host_state == robot::move_base_core::State::ABORTED || + host_state == robot::move_base_core::State::PREEMPTED) + { + latched_result_ = host_state; + } + else if (host_state == robot::move_base_core::State::PENDING) + { + if (latched_result_ != robot::move_base_core::State::PENDING) + { + host_state = latched_result_; + } + } + else + { + latched_result_ = robot::move_base_core::State::PENDING; + } + nav_feedback_->navigation_state = host_state; const char* reason = loop_.lastReason(); diff --git a/test/navigation_server_test.cpp b/test/navigation_server_test.cpp index db39bab..bcd9cf3 100644 --- a/test/navigation_server_test.cpp +++ b/test/navigation_server_test.cpp @@ -860,6 +860,85 @@ TEST(NavigationServerPlannerData, PlanIsStampedWithTheControlLoopClock) "treat it as stale and drop it"; } +// ================================================================================================ +// getFeedback() — kết quả của một goal là SỰ KIỆN, không phải mức +// +// Lõi chỉ đứng ở state kết thúc đúng MỘT cycle (33 ms @ 30 Hz) rồi về kIdle. Host đọc getFeedback() +// theo chu kỳ riêng — đo được 100 ms trên robot thật — nên báo theo mức làm mất kết quả 2/3 số lần: +// host thấy CONTROLLING, lần đọc sau đã là PENDING, không có sự kiện hoàn thành nào, và người vận +// hành phải bấm cancel một order đã tới đích. Các test dưới đây khoá lại điều đó. +// ================================================================================================ + +TEST(NavigationServerFeedback, SucceededSurvivesLongerThanOneCycleSoASlowHostStillSeesIt) +{ + Fixture fixture; + fixture.configure(); + fixture.controller_.setNominalSpeed(0.3); // [m/s] + fixture.controller_.setScript({ ControllerScript::kOk, ControllerScript::kGoalReached }); + ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10)) + << fixture.server_.lastRejectReason(); + + fixture.spin(4); // IDLE -> PLANNING -> CONTROLLING -> tới đích (terminal) + ASSERT_FALSE(fixture.server_.loop().hasActiveRequest()); + + // 20 cycle = 0.67 s, gấp 6 lần chu kỳ đọc 100 ms của host thật. + fixture.spin(20); + EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, + robot::move_base_core::State::SUCCEEDED) + << "the result vanished once the core returned to idle — a host polling slower than one " + "control cycle would never learn the goal finished"; +} + +TEST(NavigationServerFeedback, AbortedIsLatchedTheSameWaySoFailuresAreNotLostEither) +{ + Fixture fixture; + ControlLoopConfig config = baseConfig(); + config.state_machine.recovery_enabled = false; // hỏng là ABORT thẳng, không đi qua recovery + fixture.configure(config); + fixture.controller_.setScript({ ControllerScript::kFail }); + ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10)) + << fixture.server_.lastRejectReason(); + + fixture.spin(30); + EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::ABORTED) + << "a failed goal reports PENDING once the core idles — the fleet would wait forever for a " + "result that already exists"; +} + +TEST(NavigationServerFeedback, ANewRequestClearsTheLatchedResult) +{ + Fixture fixture; + fixture.configure(); + fixture.controller_.setNominalSpeed(0.3); // [m/s] + fixture.controller_.setScript({ ControllerScript::kOk, ControllerScript::kGoalReached }); + ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10)) + << fixture.server_.lastRejectReason(); + fixture.spin(6); + ASSERT_EQ(fixture.server_.getFeedback()->navigation_state, + robot::move_base_core::State::SUCCEEDED); + + // Goal thứ hai: chốt cũ phải biến mất ngay, nếu không host đọc "đã xong" cho một goal vừa bắt đầu. + fixture.controller_.setScript({ ControllerScript::kOk }); + ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10)) + << fixture.server_.lastRejectReason(); + fixture.spin(3); + + EXPECT_NE(fixture.server_.getFeedback()->navigation_state, + robot::move_base_core::State::SUCCEEDED) + << "the previous result is still being reported while a new goal is running — the host would " + "mark the new order complete before the robot moved"; +} + +TEST(NavigationServerFeedback, NothingIsLatchedBeforeTheFirstRequest) +{ + Fixture fixture; + fixture.configure(); + + fixture.spin(5); + EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::PENDING) + << "a freshly booted runtime reports a terminal result although no goal has ever been sent"; +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv);