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
36 changes: 28 additions & 8 deletions src/StepperMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ void StepperMotion::setAutoEnable(bool auto_enable) {
}
}
DelayResultCode StepperMotion::setDelayToEnable(uint32_t delay_us) {
uint32_t delay_ticks = US_TO_TICKS(delay_us);
const uint64_t delay_ticks = (uint64_t)delay_us * TICKS_PER_S / 1000000;
if (delay_ticks > 0) {
if (delay_ticks < MIN_CMD_TICKS) {
return DELAY_TOO_LOW;
Expand All @@ -393,8 +393,22 @@ void StepperMotion::setDelayToDisable(uint16_t delay_ms) {
_off_delay_count = fas_max(delay_count, (uint16_t)1);
}
MoveResultCode StepperMotion::runForward() { return _rg.startRun(true); }
MoveResultCode StepperMotion::runBackward() { return _rg.startRun(false); }
MoveResultCode StepperMotion::runBackward() {
if (_dirPin == PIN_UNDEFINED) {
return MOVE_ERR_NO_DIRECTION_PIN;
}
return _rg.startRun(false);
}
MoveResultCode StepperMotion::moveTo(int32_t position, bool blocking) {
if (_dirPin == PIN_UNDEFINED) {
fasDisableInterrupts();
const uint32_t queued = _queue()->queue_end.pos;
fasEnableInterrupts();
const uint32_t distance = (uint32_t)position - queued;
if (distance > (uint32_t)INT32_MAX) {
return MOVE_ERR_NO_DIRECTION_PIN;
}
}
MoveResultCode res = _rg.moveTo(position, &_queue()->queue_end);
if ((res == MOVE_OK) && blocking) {
while (isRunning()) {
Expand Down Expand Up @@ -425,6 +439,9 @@ void StepperMotion::applySpeedAcceleration() {
}
MoveResultCode StepperMotion::moveByAcceleration(int32_t acceleration,
bool allow_reverse) {
if (acceleration == INT32_MIN) {
return MOVE_ERR_ACCELERATION_OUT_OF_RANGE;
}
MoveResultCode res = MOVE_OK;
if (acceleration > 0) {
setAcceleration(acceleration);
Expand Down Expand Up @@ -558,7 +575,7 @@ int32_t StepperMotion::getCurrentSpeedInMilliHz(bool realtime) const {
struct actual_ticks_s speed;
getCurrentSpeedInTicks(&speed, realtime);
if (speed.ticks > 0) {
int32_t speed_in_mhz = ((uint32_t)250 * TICKS_PER_S) / speed.ticks * 4;
int32_t speed_in_mhz = (1000ULL * TICKS_PER_S) / speed.ticks;
if (speed.count_up) {
return speed_in_mhz;
}
Expand All @@ -581,7 +598,7 @@ uint32_t StepperMotion::getMaxSpeedInHz() const {
}
uint32_t StepperMotion::getMaxSpeedInMilliHz() const {
uint16_t ticks = getMaxSpeedInTicks();
uint32_t speed_in_milli_hz = ((uint32_t)250 * TICKS_PER_S) / ticks * 4;
uint32_t speed_in_milli_hz = (1000ULL * TICKS_PER_S) / ticks;
return speed_in_milli_hz;
}
#if defined(SUPPORT_UNSAFE_ABS_SPEED_LIMIT_SETTING)
Expand All @@ -600,10 +617,10 @@ int8_t StepperMotion::setSpeedInTicks(uint32_t min_step_ticks) {
return 0;
}
int8_t StepperMotion::setSpeedInUs(uint32_t min_step_us) {
if (min_step_us >= TICKS_TO_US(0xffffffff)) {
const uint64_t min_step_ticks = (uint64_t)min_step_us * TICKS_PER_S / 1000000;
if (min_step_ticks >= TICKS_FOR_STOPPED_MOTOR) {
return -1;
}
uint32_t min_step_ticks = US_TO_TICKS(min_step_us);
return setSpeedInTicks(min_step_ticks);
}
int8_t StepperMotion::setSpeedInHz(uint32_t speed_hz) {
Expand All @@ -614,10 +631,13 @@ int8_t StepperMotion::setSpeedInHz(uint32_t speed_hz) {
return setSpeedInTicks(ticks);
}
int8_t StepperMotion::setSpeedInMilliHz(uint32_t speed_mhz) {
if (speed_mhz <= (1000LL * TICKS_PER_S / 0xffffffff + 1)) {
if (speed_mhz == 0) {
return -1;
}
const uint64_t ticks = _rg.divForMilliHz(speed_mhz);
if (ticks >= TICKS_FOR_STOPPED_MOTOR) {
return -1;
}
uint32_t ticks = _rg.divForMilliHz(speed_mhz);
return setSpeedInTicks(ticks);
}
void StepperMotion::setCurrentPosition(int32_t new_pos) {
Expand Down
8 changes: 6 additions & 2 deletions src/StepperMotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ class StepperMotion {
// setDelayToEnable() sets the delay from enable to first step.
// Return values:
// - DelayResultCode::OK: Delay accepted
// - DelayResultCode::TOO_LOW: Delay is 0 (minimum is 1µs)
// - DelayResultCode::TOO_LOW: Nonzero delay is shorter than MIN_CMD_TICKS
// - DelayResultCode::TOO_HIGH: Delay exceeds MAX_ON_DELAY_TICKS
// (~120ms ESP32, ~60ms AVR @ 16MHz)
// A zero delay disables the enable hold time.
//
// setDelayToDisable() sets the delay from last step to disable.
// This is executed in the periodic stepper task (~4-10ms rate) and thus
Expand Down Expand Up @@ -259,7 +260,8 @@ class StepperMotion {
// Note: no update on `stopMove()`
//
// Returns 0 on success, or -1 on invalid value.
// Invalid is faster than MaxSpeed or slower than ~250 Mio ticks/step.
// The period must be at least getMaxSpeedInTicks() and less than
// TICKS_FOR_STOPPED_MOTOR.
int8_t setSpeedInUs(uint32_t min_step_us);
int8_t setSpeedInTicks(uint32_t min_step_ticks);
int8_t setSpeedInHz(uint32_t speed_hz);
Expand Down Expand Up @@ -390,6 +392,7 @@ class StepperMotion {
// ### runForward() and runBackwards()
// These commands just let the motor run continuously in one direction.
// If the motor is running in the opposite direction, it will reverse
// runBackward() requires a direction pin.
// return value as with move/moveTo
MoveResultCode runForward();
MoveResultCode runBackward();
Expand All @@ -413,6 +416,7 @@ class StepperMotion {
// acceleration < 0
// => accelerate towards negative maximum speed if allow_reverse
// => decelerate towards motor stop if allow_reverse = false
// INT32_MIN returns MOVE_ERR_ACCELERATION_OUT_OF_RANGE.
// return value as with move/moveTo
MoveResultCode moveByAcceleration(int32_t acceleration,
bool allow_reverse = true);
Expand Down
7 changes: 6 additions & 1 deletion src/fas_arch/result_codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ enum class MoveResultCode : int8_t {
ErrorNoDirectionPin = -1, // Equivalent to MOVE_ERR_NO_DIRECTION_PIN
ErrorSpeedIsUndefined = -2, // Equivalent to MOVE_ERR_SPEED_IS_UNDEFINED
ErrorAccelerationIsUndefined =
-3 // Equivalent to MOVE_ERR_ACCELERATION_IS_UNDEFINED
-3, // Equivalent to MOVE_ERR_ACCELERATION_IS_UNDEFINED
ErrorAccelerationOutOfRange = -4,
};

static inline bool moveIsOk(MoveResultCode status) {
Expand All @@ -118,6 +119,8 @@ static inline const char* toString(MoveResultCode code) {
return FAS_PSTR("Error: Speed is Undefined");
case MoveResultCode::ErrorAccelerationIsUndefined:
return FAS_PSTR("Error: Acceleration is Undefined");
case MoveResultCode::ErrorAccelerationOutOfRange:
return FAS_PSTR("Error: Acceleration is Out of Range");
default:
return FAS_PSTR("Unknown Error");
}
Expand All @@ -129,6 +132,8 @@ static inline const char* toString(MoveResultCode code) {
#define MOVE_ERR_SPEED_IS_UNDEFINED MoveResultCode::ErrorSpeedIsUndefined
#define MOVE_ERR_ACCELERATION_IS_UNDEFINED \
MoveResultCode::ErrorAccelerationIsUndefined
#define MOVE_ERR_ACCELERATION_OUT_OF_RANGE \
MoveResultCode::ErrorAccelerationOutOfRange

// MoveTimedResultCode values 0..6 mirror AqeResultCode so that tmrFrom() is a
// value-preserving cast; MoveBusy/MoveEmpty sit above the AQE range to avoid
Expand Down
11 changes: 2 additions & 9 deletions src/fas_ramp/RampGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ class RampGenerator {
inline uint32_t getSpeedInTicks() const {
return _parameters.min_travel_ticks;
}
static uint32_t divForMilliHz(uint32_t f) {
uint32_t base = (uint32_t)250 * TICKS_PER_S;
uint32_t res = base / f;
base -= res * f;
base <<= 2;
res <<= 2;
base += f / 2; // add rounding
res += base / f;
return res;
static uint64_t divForMilliHz(uint32_t f) {
return (1000ULL * TICKS_PER_S + f / 2) / f;
}
static uint32_t divForHz(uint32_t f) {
uint32_t base = TICKS_PER_S;
Expand Down
188 changes: 188 additions & 0 deletions tests/host/test_32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#include <inttypes.h>
#include <stdio.h>

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

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

class StepperMotionTest {
public:
static void fill(StepperMotion& stepper) { stepper.fill_queue(); }
};

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];

explicit Fixture(bool direction_pin = false) {
engine.init();
stepper.init(&engine, 0, 0);
if (direction_pin) {
stepper.setDirectionPin(1);
}
check(stepper.setSpeedInUs(1000) == 0, "configure speed");
check(stepper.setAcceleration(10000) == 0, "configure acceleration");
}

int drain() {
int pulses = 0;
for (int i = 0; i < 100 && stepper.isRunning(); ++i) {
StepperMotionTest::fill(stepper);
while (!stepper.isQueueEmpty()) {
const auto& command = queue.entry[queue.read_idx & QUEUE_LEN_MASK];
pulses += command.countUp ? command.steps : -command.steps;
++queue.read_idx;
}
queue._isRunning = false;
}
check(!stepper.isRunning(), "short simulator move settles");
return pulses;
}
};

void reverse_without_direction_pin() {
Fixture f;
auto& s = f.stepper;
check(s.move(40) == MOVE_OK, "forward move accepted without DIR");
StepperMotionTest::fill(s);
const int32_t pending_position = s.getPositionAfterCommandsCompleted();
const unsigned entries = s.queueEntries();
const auto result = s.runBackward();
printf("runBackward without DIR: rc=%d planned=%" PRId32 " entries=%u\n",
static_cast<int>(result), pending_position, entries);
check(result == MOVE_ERR_NO_DIRECTION_PIN, "runBackward needs DIR");
if (result == MOVE_ERR_NO_DIRECTION_PIN) {
const int pulses = f.drain();
printf("after rejection: pulses=%d position=%" PRId32 "\n", pulses,
s.getCurrentPosition());
check(pulses == 40 && s.getCurrentPosition() == 40,
"rejected reverse preserves the forward move");
}
}

void reverse_absolute_without_direction_pin() {
Fixture f;
auto& s = f.stepper;
s.setCurrentPosition(20);
const auto result = s.moveTo(19);
printf("moveTo(19) without DIR from 20: rc=%d active=%d entries=%u\n",
static_cast<int>(result), s.isRampGeneratorActive(), s.queueEntries());
check(result == MOVE_ERR_NO_DIRECTION_PIN, "reverse absolute move needs DIR");
check(!s.isRunning() && s.getCurrentPosition() == 20,
"rejected absolute move leaves the axis idle");
}

void invalid_signed_acceleration_preserves_move() {
Fixture f(true);
auto& s = f.stepper;
check(s.move(40) == MOVE_OK, "queue the move before invalid acceleration");
const auto result = s.moveByAcceleration(INT32_MIN);
printf("moveByAcceleration(INT32_MIN): rc=%d accel=%" PRIu32 "\n",
static_cast<int>(result), s.getAcceleration());
check(result == MOVE_ERR_ACCELERATION_OUT_OF_RANGE,
"out-of-range acceleration rejected");
check(s.getAcceleration() == 10000, "configured acceleration preserved");
if (result == MOVE_ERR_ACCELERATION_OUT_OF_RANGE) {
const int pulses = f.drain();
printf("after rejection: pulses=%d position=%" PRId32 "\n", pulses,
s.getCurrentPosition());
check(pulses == 40 && s.getCurrentPosition() == 40,
"rejected acceleration preserves the finite move");
}
}

void overflowing_enable_delay_preserves_configuration() {
Fixture f;
auto& s = f.stepper;
s.setAutoEnable(true);
check(s.setDelayToEnable(500) == DELAY_OK, "valid enable delay accepted");
const auto result = s.setDelayToEnable(268435456);
s.forwardStep();
const auto& first = f.queue.entry[f.queue.read_idx & QUEUE_LEN_MASK];
printf("enable delay 268435456 us: rc=%d first steps=%u ticks=%u\n",
static_cast<int>(result), first.steps, first.ticks);
check(result == DELAY_TOO_HIGH, "overflowing enable delay rejected");
check(first.steps == 0 && first.ticks == 8000,
"first step still waits for the configured enable delay");
}

void slow_microsecond_limit() {
Fixture f;
auto& s = f.stepper;
const int accepted = s.setSpeedInUs(268435455);
const uint32_t selected = s.getSpeedInTicks();
const int rejected = s.setSpeedInUs(268435456);
printf("speed period 268435455/268435456 us: rc=%d/%d ticks=%" PRIu32
" after rejection=%" PRIu32 "\n",
accepted, rejected, selected, s.getSpeedInTicks());
check(accepted == 0 && selected == 4294967280u,
"last representable whole microsecond period is accepted");
check(rejected == -1 && s.getSpeedInTicks() == selected,
"overflowing period does not replace the configured speed");
}

void slow_millihertz_limit() {
Fixture f;
auto& s = f.stepper;
const int accepted = s.setSpeedInMilliHz(4);
const uint32_t selected = s.getSpeedInTicks();
const int rejected = s.setSpeedInMilliHz(3);
printf("speed 4/3 mHz: rc=%d/%d ticks=%" PRIu32
" after rejection=%" PRIu32 "\n",
accepted, rejected, selected, s.getSpeedInTicks());
check(accepted == 0 && selected == 4000000000u,
"four millihertz is representable at 16 MHz");
check(rejected == -1 && s.getSpeedInTicks() == selected,
"three millihertz exceeds the representable period");
}

void slow_configured_speed_rounding() {
Fixture f;
auto& s = f.stepper;
check(s.setSpeedInTicks(3000000000u) == 0, "slow period configured");
const uint32_t frequency = s.getSpeedInMilliHz();
printf("configured period 3000000000 ticks: frequency=%" PRIu32 " mHz\n",
frequency);
check(frequency == 5, "millihertz rounding does not overflow");
}

void actual_and_maximum_speed_precision() {
Fixture f(true);
auto& s = f.stepper;
s.setAbsoluteSpeedLimit(96);
const stepper_command_s reverse = {8192, 2, false};
check(s.addQueueEntry(&reverse) == AQE_OK, "queue two reverse steps");
const int32_t actual = s.getCurrentSpeedInMilliHz();
const uint32_t maximum = s.getMaxSpeedInMilliHz();
printf("speed period 8192/96 ticks: actual=%" PRId32
" maximum=%" PRIu32 " mHz\n", actual, maximum);
check(actual == -1953125, "actual reverse frequency retains all digits");
check(maximum == 166666666, "maximum frequency retains all digits");
}
} // namespace

int main() {
reverse_without_direction_pin();
reverse_absolute_without_direction_pin();
invalid_signed_acceleration_preserves_move();
overflowing_enable_delay_preserves_configuration();
slow_microsecond_limit();
slow_millihertz_limit();
slow_configured_speed_rounding();
actual_and_maximum_speed_precision();
return failures != 0;
}
Loading