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
67 changes: 31 additions & 36 deletions src/fas_member/move_timed.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,39 @@
// *actual_duration carries the ticks of the one injected pause. The
// application accumulates this value per retry and computes the drift only
// once the move is accepted (see API documentation).
static uint32_t timedDurationEntries(uint32_t duration) {
return duration / UINT16_MAX + (duration % UINT16_MAX != 0);
}

MoveTimedResultCode StepperMotion::moveTimed(int16_t steps,
uint32_t duration,
uint32_t* actual_duration,
bool start,
bool prepare_revert) {
MoveTimedResultCode ret_ok =
isQueueEmpty() ? MOVE_TIMED_EMPTY : MOVE_TIMED_OK;
if (actual_duration != NULL) {
*actual_duration = 0;
}
if ((steps == 0) && (duration == 0)) {
if (start) {
addQueueEntry(NULL, true); // start the queue
AqeResultCode ret = addQueueEntry(NULL, true);
if (ret != AQE_OK) {
return tmrFrom(ret);
}
}
return ret_ok;
}
uint8_t freeEntries = QUEUE_LEN - queueEntries();
if (actual_duration != NULL) {
*actual_duration = 0;
}
_queue()->_injected_pause_ticks = 0;
bool count_up = _queue()->queue_end.count_up ^ prepare_revert;
struct stepper_command_s cmd = {.ticks = 0, .steps = 0, .count_up = count_up};
if (steps == 0) {
if ((duration >> 16) >= QUEUE_LEN) {
const uint32_t entries = timedDurationEntries(duration);
if (entries > QUEUE_LEN) {
return MOVE_TIMED_TOO_LARGE_ERROR;
}
if ((duration >> 16) >= freeEntries) {
if (entries > freeEntries) {
return MOVE_TIMED_BUSY;
}
while (duration > 0) {
Expand Down Expand Up @@ -85,36 +93,26 @@ MoveTimedResultCode StepperMotion::moveTimed(int16_t steps,
return ret_ok;
}

// let's evaluate the direction
if (steps < 0) {
cmd.count_up = false;
steps = -steps;
} else {
cmd.count_up = true;
cmd.count_up = steps > 0;
uint16_t steps_left = cmd.count_up ? steps : -(int32_t)steps;
if (steps_left > QUEUE_LEN * 255) {
return MOVE_TIMED_TOO_LARGE_ERROR;
}

// There are steps to execute
// Let's first calculate the step rate
uint32_t rate = duration;
rate /= steps;
rate /= steps_left;
if (rate > 65535) {
// we need pauses, so only few steps can be executed
uint16_t cmds_per_step = (rate >> 16) + 1; // bit too small
if (cmds_per_step >= QUEUE_LEN) {
return MOVE_TIMED_TOO_LARGE_ERROR;
}
if (steps >= QUEUE_LEN) {
return MOVE_TIMED_TOO_LARGE_ERROR;
}
uint8_t cmds = steps * cmds_per_step;
const uint32_t cmds = steps_left * timedDurationEntries(rate);
if (cmds >= QUEUE_LEN) {
return MOVE_TIMED_TOO_LARGE_ERROR;
}
if (cmds > freeEntries) {
return MOVE_TIMED_BUSY;
}
// Should fit into the queue.
for (uint8_t steps_i = 0; steps_i < steps; steps_i++) {
for (uint16_t steps_i = 0; steps_i < steps_left; steps_i++) {
uint32_t this_duration = rate;
cmd.steps = 1;
while (this_duration) {
Expand Down Expand Up @@ -144,27 +142,24 @@ MoveTimedResultCode StepperMotion::moveTimed(int16_t steps,
return ret_ok;
}
// Now we need to run steps at "high" speed.
if (steps > QUEUE_LEN * 255) {
return MOVE_TIMED_TOO_LARGE_ERROR;
}
if (steps > freeEntries * 255) {
if (steps_left > freeEntries * 255) {
return MOVE_TIMED_BUSY;
}
// The steps should fit in
cmd.ticks = rate;
uint32_t expected_duration = rate;
expected_duration *= steps;
expected_duration *= steps_left;
// duration must be larger than expected_duration
int16_t missing = duration - expected_duration;
while (steps > 0) {
if (steps > 510) {
uint16_t missing = duration - expected_duration;
while (steps_left > 0) {
if (steps_left > 510) {
cmd.steps = 255;
} else if (steps > 255) {
cmd.steps = steps / 2;
} else if (steps_left > 255) {
cmd.steps = steps_left / 2;
} else {
cmd.steps = steps;
cmd.steps = steps_left;
}
if (steps <= missing) {
if (steps_left <= missing && cmd.ticks < UINT16_MAX) {
// run the remaining steps bit slower to adjust for missing ticks
cmd.ticks++;
missing = 0; // only increase once
Expand All @@ -181,7 +176,7 @@ MoveTimedResultCode StepperMotion::moveTimed(int16_t steps,
d *= cmd.steps;
*actual_duration += d;
}
steps -= cmd.steps;
steps_left -= cmd.steps;
}
return ret_ok;
}
Expand Down
144 changes: 144 additions & 0 deletions tests/host/test_33.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include <inttypes.h>
#include <stdio.h>

#include "StepperMotion.h"
#include "fas_queue/stepper_queue.h"

void inject_fill_interrupt(int) {}
void noInterrupts() {}
void interrupts() {}

namespace {
int failures = 0;

void check(bool condition, const char* message) {
if (!condition) {
printf("FAIL: %s\n", message);
++failures;
}
}

struct Fixture {
StepperMotionEngine engine;
StepperMotion stepper;
StepperQueue& queue = fas_queue[0];

Fixture() {
engine.init();
stepper.init(&engine, 0, 0);
stepper.setDirectionPin(1);
}

void prefill(unsigned entries) {
const stepper_command_s step{3200, 1, true};
for (unsigned i = 0; i < entries; ++i) {
check(stepper.addQueueEntry(&step, false) == AQE_OK, "prefill accepted");
}
}

MoveTimedResultCode request(int16_t steps, uint32_t ticks, uint32_t& actual,
bool start = false) {
const auto result = stepper.moveTimed(steps, ticks, &actual, start);
printf("moveTimed(%d, %" PRIu32 ", start=%d): rc=%d actual=%" PRIu32
" entries=%u planned position=%" PRId32 " running=%d\n",
steps, ticks, start, static_cast<int>(result), actual,
stepper.queueEntries(), stepper.getPositionAfterCommandsCompleted(),
stepper.isQueueRunning());
return result;
}
};

void start_only_reports_queue_result() {
Fixture f;
uint32_t actual = 0;
check(f.request(0, 0, actual, true) ==
MoveTimedResultCode::ErrorEmptyQueueToStart,
"start-only call reports an empty queue");
f.prefill(1);
f.stepper.forceStop();
check(f.request(0, 0, actual, true) == MOVE_TIMED_COMMANDS_SUSPENDED,
"start-only call reports the admission latch");
check(!f.stepper.isQueueRunning() && f.stepper.queueEntries() == 1,
"a suspended start leaves the queued step untouched");
f.stepper.resumeCommands();
check(f.request(0, 0, actual, true) == MOVE_TIMED_OK &&
f.stepper.isQueueRunning(),
"start-only call starts a prepared queue");
}

void no_op_clears_stale_duration() {
Fixture f;
uint32_t actual = 12345;
check(f.request(0, 0, actual) == MOVE_TIMED_EMPTY,
"zero-duration no-op still reports an empty queue");
check(actual == 0, "no-op reports zero elapsed ticks");
}

void smallest_signed_step_count_is_too_large() {
Fixture f;
uint32_t actual = 12345;
check(f.request(INT16_MIN, 327680000, actual) == MOVE_TIMED_TOO_LARGE_ERROR,
"32,768 reverse steps exceed the queue capacity");
check(actual == 0 && f.stepper.isQueueEmpty(),
"oversized reverse move leaves the queue untouched");
}

void maximum_duration_does_not_wrap_command_count() {
Fixture f;
uint32_t actual = 0;
check(f.request(1, UINT32_MAX, actual) == MOVE_TIMED_TOO_LARGE_ERROR,
"a very long step exceeds the queue capacity");
check(actual == 0 && f.stepper.isQueueEmpty(),
"oversized slow move appends neither step nor pauses");
}

void pause_that_needs_three_slots_does_not_partially_append() {
Fixture f;
f.prefill(14);
uint32_t actual = 0;
check(f.request(0, 131071, actual) == MOVE_TIMED_BUSY,
"three-command dwell must wait when there are two slots");
check(actual == 0 && f.stepper.queueEntries() == 14,
"busy dwell leaves the existing queue untouched");
}

void slow_step_that_needs_three_slots_does_not_partially_append() {
Fixture f;
f.prefill(14);
uint32_t actual = 0;
check(f.request(1, 131071, actual) == MOVE_TIMED_BUSY,
"three-command step must wait when there are two slots");
check(actual == 0 && f.stepper.queueEntries() == 14 &&
f.stepper.getPositionAfterCommandsCompleted() == 14,
"busy step leaves the existing queue and its position untouched");
}

void maximum_period_with_remainder_does_not_wrap() {
Fixture f;
uint32_t actual = 0;
check(f.request(256, 16777215, actual) == MOVE_TIMED_EMPTY,
"maximum-period move accepts all its steps");
unsigned pulses = 0;
uint32_t queued_ticks = 0;
for (uint8_t i = f.queue.read_idx; i != f.queue.next_write_idx; ++i) {
const auto& command = f.queue.entry[i & QUEUE_LEN_MASK];
pulses += command.steps;
queued_ticks += (uint32_t)command.steps * command.ticks;
}
printf("maximum-period move: pulses=%u queued ticks=%" PRIu32 "\n", pulses,
queued_ticks);
check(pulses == 256 && actual == 16776960 && queued_ticks == actual,
"all pulses use the largest representable period");
}
} // namespace

int main() {
start_only_reports_queue_result();
no_op_clears_stale_duration();
smallest_signed_step_count_is_too_large();
maximum_duration_does_not_wrap_command_count();
pause_that_needs_three_slots_does_not_partially_append();
slow_step_that_needs_three_slots_does_not_partially_append();
maximum_period_with_remainder_does_not_wrap();
return failures != 0;
}
Loading