From bc19628decf7fdd368bb0cec54084f9e13c5d01c Mon Sep 17 00:00:00 2001 From: Xia Chao Date: Tue, 24 Nov 2026 21:08:53 +0800 Subject: [PATCH] Expose a coherent stepper queue status snapshot --- src/StepperMotion.cpp | 16 ++++++++ src/StepperMotion.h | 14 +++++++ tests/host/test_36.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tests/host/test_36.cpp diff --git a/src/StepperMotion.cpp b/src/StepperMotion.cpp index e59f9af..779ef07 100644 --- a/src/StepperMotion.cpp +++ b/src/StepperMotion.cpp @@ -452,6 +452,22 @@ void StepperMotion::resumeCommands() { _queue()->resumeCommands(); } bool StepperMotion::areCommandsSuspended() const { return _queue()->areCommandsSuspended(); } +StepperQueueStatus StepperMotion::getQueueStatus() const { + const StepperQueue* q = _queue(); + fasDisableInterrupts(); + const uint8_t entries = (uint8_t)(q->next_write_idx - q->read_idx); + const StepperQueueStatus status{ + .position_after_commands = q->queue_end.pos, + .entries = entries, + .free_entries = (uint8_t)(QUEUE_LEN - entries), + .running = q->isRunning(), + .ramp_active = _rg.isRampGeneratorActive(), + .commands_suspended = q->areCommandsSuspended(), + .stop_cause = getStopCause(), + }; + fasEnableInterrupts(); + return status; +} void StepperMotion::forceStop() { StepperQueue* q = _queue(); diff --git a/src/StepperMotion.h b/src/StepperMotion.h index fabbdf9..ff118c4 100644 --- a/src/StepperMotion.h +++ b/src/StepperMotion.h @@ -6,6 +6,16 @@ enum class ExtDirPendingState : uint8_t { None = 0xff, Low = 0, High = 1 }; +struct StepperQueueStatus { + int32_t position_after_commands; + uint8_t entries; + uint8_t free_entries; + bool running; + bool ramp_active; + bool commands_suspended; + StepperStopCause stop_cause; +}; + class StepperMotion; // ### Return codes of calls to `move()` and `moveTo()` @@ -843,6 +853,10 @@ class StepperMotion { bool isQueueFull() const; bool isQueueRunning() const; + // Read queue occupancy, the queued position and motion state in one + // interrupt-protected snapshot. The stop cause is read without clearing it. + StepperQueueStatus getQueueStatus() const; + // ### functions to get the fill level of the queue // // To retrieve the forward planning time in the queue, ticksInQueue() diff --git a/tests/host/test_36.cpp b/tests/host/test_36.cpp new file mode 100644 index 0000000..6deb6ff --- /dev/null +++ b/tests/host/test_36.cpp @@ -0,0 +1,93 @@ +#include +#include + +#include "StepperMotion.h" +#include "fas_queue/stepper_queue.h" + +namespace { +StepperQueue* insert_after_sample = nullptr; +AqeResultCode insertion = AQE_OK; +int failures = 0; + +void check(bool condition, const char* message) { + if (!condition) { + printf("FAIL: %s\n", message); + ++failures; + } +} +} // namespace + +void inject_fill_interrupt(int mark) { + if (mark == 0 && insert_after_sample != nullptr) { + StepperQueue* queue = insert_after_sample; + insert_after_sample = nullptr; + const stepper_command_s command{3200, 3, true}; + insertion = queue->addQueueEntry(&command, false); + } +} + +void noInterrupts() {} +void interrupts() {} + +int main() { + StepperMotionEngine engine; + engine.init(); + StepperMotion x; + StepperMotion y; + x.init(&engine, 0, 0); + y.init(&engine, 1, 1); + + const stepper_command_s first{3200, 2, true}; + check(x.addQueueEntry(&first, false) == AQE_OK, "prefill first axis"); + const auto initial = x.getQueueStatus(); + insert_after_sample = &fas_queue[0]; + const unsigned previous_count = x.queueEntries(); + const int32_t later_position = x.getPositionAfterCommandsCompleted(); + const auto updated = x.getQueueStatus(); + const auto idle = y.getQueueStatus(); + printf("queue transition: snapshots=(%u,%" PRId32 ")->(%u,%" PRId32 + ") separate reads=(%u,%" PRId32 ") free=%u idle axis entries=%u\n", + initial.entries, initial.position_after_commands, updated.entries, + updated.position_after_commands, previous_count, later_position, + updated.free_entries, idle.entries); + check(insertion == AQE_OK && previous_count == 1 && later_position == 5, + "a new command can land between separate diagnostics"); + check(initial.entries == 1 && initial.free_entries == QUEUE_LEN - 1 && + initial.position_after_commands == 2 && !initial.running, + "prefill snapshot reports the first command and its position together"); + check(updated.entries == 2 && updated.free_entries == QUEUE_LEN - 2 && + updated.position_after_commands == 5 && !updated.running, + "after enqueue both diagnostics include the second command"); + check(idle.entries == 0 && idle.free_entries == QUEUE_LEN && + idle.position_after_commands == 0 && !idle.running, + "status belongs to the requested axis"); + + check(x.addQueueEntry(nullptr, true) == AQE_OK, "start the prefilled queue"); + check(x.setSpeedInUs(1000) == 0 && x.setAcceleration(10000) == 0 && + x.move(20) == MOVE_OK, + "start ramp admission"); + const auto running = x.getQueueStatus(); + x.forceStop(); + const auto stopped = x.getQueueStatus(); + const auto reread = x.getQueueStatus(); + const auto untouched_axis = y.getQueueStatus(); + printf("stop snapshots: before queue=%d ramp=%d cause=%u after suspended=%d" + " cause=%u reread cause=%u other axis cause=%u\n", + running.running, running.ramp_active, + static_cast(running.stop_cause), stopped.commands_suspended, + static_cast(stopped.stop_cause), + static_cast(reread.stop_cause), + static_cast(untouched_axis.stop_cause)); + check(running.running && running.ramp_active && + !running.commands_suspended && running.stop_cause == StepperStopCause::None, + "snapshot reports active queue and ramp separately"); + check(stopped.commands_suspended && + stopped.stop_cause == StepperStopCause::ForceStop && + reread.stop_cause == StepperStopCause::ForceStop && + x.takeStopCause() == StepperStopCause::ForceStop, + "status exposes the stop cause without consuming it"); + check(!untouched_axis.commands_suspended && + untouched_axis.stop_cause == StepperStopCause::None, + "one axis stopping leaves the other axis status alone"); + return failures != 0; +}