feat: navigation_get_component_status() + truthful PREEMPTING contract in the C API

Applies cleanly from T800_ws commit 2b0993b: this repo's nav_c_api_2.h/.cpp/
GUIDE.md were byte-identical to the pre-fix baseline (verified before
applying), so this is the same patch, not a manual reconstruction.

- NavComponentStatus struct + navigation_get_component_status(): lazy
  dlsym of the MoveBaseComponentStatus alias (see previous commit's two new
  headers); returns 0 rows if the loaded .so doesn't export it, so an old
  libmove_base2.so is not a crash, just no data.
- Doc: PREEMPTING is now a truthful "still winding down" state (see
  move_base2 commit 8bc4895, already pulled) — a host must poll to
  quiescence before resending, not resend on PREEMPTING as if it meant
  "acknowledged".

Kept for a future C# host per user request — the C API is the intended
integration point for that.
This commit is contained in:
2026-09-12 22:26:41 +07:00
parent 9095e58dec
commit f7398d9442
3 changed files with 154 additions and 0 deletions

View File

@@ -460,6 +460,52 @@ theo dõi `navigation_get_twist`. Nếu C# có tầng an toàn giả định d
`cancel` từ host huỷ **cả order** (gồm hàng đợi mission), khác với huỷ nội bộ do chính mission layer
phát ra chỉ dừng chặng đang chạy.
**Cancel nói thật từ 2026-09-11.** Sau `navigation_cancel()`, `navigation_get_state()` báo
`PREEMPTING` cho tới khi lõi **im hẳn**, rồi mới `PREEMPTED` (và chốt lại tới yêu cầu kế). "Im hẳn"
là ba điều kiện cùng đúng:
1. lệnh vận tốc = 0 **và** odom đọc ≈ 0 (bánh xe đã đứng, không chỉ lệnh);
2. thread global planner rảnh — một lượt `makePlan` đã huỷ mà plugin không cắt được (SBPL chạy tới
hết `allocated_time`) giữ thread bận nhiều giây, và `PREEMPTED` **không** được báo trong lúc đó;
3. action đang chạy (nếu có) đã trả terminal.
Luật cho host: **không gửi goal kế khi state còn `PREEMPTING`.**
```cpp
navigation_cancel(h);
NavigationState s;
do {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
navigation_get_state(h, &s);
} while (s == PREEMPTING);
// s == PREEMPTED (hoặc PENDING/… nếu đã có việc khác) → an toàn gửi goal kế
```
Gửi sớm không bị từ chối — goal kế đứng ở `PLANNING` tới khi thread rảnh — nhưng hàng đợi phía
host sẽ lệch nhịp với lõi. Trước 2026-09-11 lõi báo `PREEMPTED` sau đúng một cycle bất kể thread
planner; host tin, gửi goal kế, goal kế bị coi là hỏng và `ABORTED` sau ~3 s. Trần chờ là khoá
`settle_timeout` của `move_base2` (mặc định 2 s); quá trần lõi vẫn báo `PREEMPTED` và in lỗi.
### 6.5b. Trạng thái từng thành phần và `LOST` (2026-09-11)
| | Hàm | Đổi |
|---|---|---|
| + | `size_t navigation_get_component_status(NavigationHandle, NavComponentStatus* out, size_t max)` | mới |
Lõi giữ một bảng "ai đang bận / đã được bảo dừng / đã treo" cho 7 thành phần (`global_planner`,
`local_planner`, `recovery`, `action`, `costmap_global`, `costmap_local`, `control_loop`). Hàm này
chép bảng ra cho host; chỉ đọc atomic nên gọi được từ thread bất kỳ — kể cả khi control thread
đang treo, là lúc cần nó nhất. `phase`: 0 idle, 1 busy, 2 stopping, 3 paused, 4 hung; `hung` là kết
luận của watchdog (bận quá ngân sách mà không nhích). Trả 0 với runtime cũ không có capability.
**`LOST`:** control thread không kết thúc được cycle nào quá `control_loop_stall_budget` (mặc định
0.5 s) → `navigation_get_state()` báo `LOST` (ghi từ thread watchdog), `feed_back_str` nêu thời
gian. Host đọc `LOST` phải **tự đưa cmd_vel về 0** — twist từ `navigation_get_twist()` lúc đó là
giá trị cũ, stamp không tiến (mục 6). Hết treo, state thật quay lại.
Dùng khi cancel/pause có vẻ lâu: gọi hàm này, hàng nào `phase = 2` (stopping) lâu là plugin không
tuân preempt; `hung = 1` là treo thật.
### 6.6. Trần vận tốc
| | Hàm | Đổi |

View File

@@ -45,6 +45,17 @@
* the request; the control thread applies it on its next cycle. Returning from the call does
* not mean the robot stopped — watch the state, or watch the twist.
*
* Since 2026-09-11 the state tells the truth about a cancel: after navigation_cancel() the
* core reports PREEMPTING until it is quiescent — the issued command is zero AND odometry
* reads zero, the global-planner thread is idle (a cancelled makePlan() that the plugin
* cannot interrupt keeps that thread busy for seconds), and any running action has reported
* terminal. Only then does it report PREEMPTED, and that result is latched until the next
* request. Poll navigation_get_state() and do not submit the next goal while it is
* PREEMPTING. A goal submitted early is not rejected — it waits in PLANNING until the
* thread frees — but it is the caller's queue that ends up out of step. The wait is bounded
* by move_base2's `settle_timeout` (default 2 s); on expiry the core still reports
* PREEMPTED and logs an error.
*
* 5. navigation_set_twist_linear() / _angular() set a velocity **ceiling**, and the sign of the
* linear x component selects which direction the ceiling applies to. They now only reject
* NaN/Inf; the old runtime also returned false while a cancel was in flight, and that signal no
@@ -175,6 +186,38 @@ extern "C"
*/
char *navigation_get_status_text(NavigationHandle handle);
/**
* @brief One row of the runtime's component status board (added 2026-09-11).
*
* `phase`: 0 idle, 1 busy, 2 stopping (told to cancel, not yet returned), 3 paused, 4 hung.
* `hung`: the watchdog's verdict — busy longer than the component's budget with no progress.
* Rows, in order: global_planner, local_planner, recovery, action, costmap_global, costmap_local,
* control_loop. Use `name` rather than the index when displaying.
*/
typedef struct
{
char name[32];
unsigned char phase;
unsigned char hung;
double seconds_in_phase;
double seconds_since_progress;
} NavComponentStatus;
/**
* @brief Per-component liveness of the runtime: who is busy, who was told to stop, who is hung.
*
* Reads atomics only — safe from any thread and, critically, still answers while the control
* thread itself is stalled (navigation_get_state() then reports LOST). Poll it when a cancel or
* pause seems to take long, or after LOST, to see which component is stuck.
*
* @param handle Navigation handle.
* @param out Array of at least @p max entries; may be NULL with max = 0 to query the count.
* @param max Capacity of @p out.
* @return Number of rows the runtime has (may exceed @p max); 0 if the runtime does not offer
* the capability (older move_base2) or is not initialized.
*/
size_t navigation_get_component_status(NavigationHandle handle, NavComponentStatus *out, size_t max);
// ============================================================================================
// Sensor input
// ============================================================================================

View File

@@ -14,13 +14,19 @@
#include <cstdlib>
#include <cstring>
#include <functional>
#include <string>
#include <vector>
#include <boost/make_shared.hpp>
#include <boost/dll/import.hpp>
#include <boost/dll/runtime_symbol_info.hpp>
#include <robot/plugin_loader_helper.h>
#include <robot/robot.h>
#include <move_base_core/common.h>
#include <move_base_core/component_status_probe.h>
#include <move_base_core/navigation.h>
#include <robot_sensor_msgs/DepthCameraData.h>
@@ -179,6 +185,65 @@ extern "C" bool navigation_get_state(NavigationHandle handle, NavigationState *o
}
}
extern "C" size_t navigation_get_component_status(NavigationHandle handle, NavComponentStatus *out,
size_t max)
{
if (!handle)
{
return 0;
}
try
{
// Capability tra MỘT lần theo alias trong .so navigation đang nạp (cùng đường mà
// navigation_create() đã đi). Runtime cũ không export alias → 0, không phải lỗi.
using Probe = std::function<std::size_t(robot::move_base_core::BaseNavigation *,
robot::move_base_core::ComponentStatusEntry *, std::size_t)>;
static Probe probe;
static bool probed = false;
if (!probed)
{
probed = true;
try
{
robot::PluginLoaderHelper loader;
const std::string path = loader.findLibraryPath("MoveBase");
probe = boost::dll::import_alias<std::size_t(robot::move_base_core::BaseNavigation *,
robot::move_base_core::ComponentStatusEntry *,
std::size_t)>(
path, robot::move_base_core::kComponentStatusProbeAlias,
boost::dll::load_mode::append_decorations);
}
catch (const std::exception &e)
{
robot::log_warning("navigation_get_component_status: runtime offers no '%s' capability: %s",
robot::move_base_core::kComponentStatusProbeAlias, e.what());
probe = nullptr;
}
}
if (!probe)
{
return 0;
}
std::vector<robot::move_base_core::ComponentStatusEntry> rows(max);
const std::size_t count = probe(as_navigation(handle), rows.empty() ? nullptr : rows.data(), max);
const std::size_t copied = count < max ? count : max;
for (std::size_t i = 0; i < copied && out != nullptr; ++i)
{
std::memcpy(out[i].name, rows[i].name, sizeof(out[i].name));
out[i].phase = rows[i].phase;
out[i].hung = rows[i].hung;
out[i].seconds_in_phase = rows[i].seconds_in_phase;
out[i].seconds_since_progress = rows[i].seconds_since_progress;
}
return count;
}
catch (...)
{
return 0;
}
}
extern "C" char *navigation_get_status_text(NavigationHandle handle)
{
if (!handle)