Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/StepperMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
14 changes: 14 additions & 0 deletions src/StepperMotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down Expand Up @@ -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()
Expand Down
93 changes: 93 additions & 0 deletions tests/host/test_36.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <inttypes.h>
#include <stdio.h>

#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<unsigned>(running.stop_cause), stopped.commands_suspended,
static_cast<unsigned>(stopped.stop_cause),
static_cast<unsigned>(reread.stop_cause),
static_cast<unsigned>(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;
}
Loading