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
38 changes: 24 additions & 14 deletions src/StepperMotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ MoveResultCode StepperMotion::move(int32_t move, bool blocking) {
if ((move < 0) && (_dirPin == PIN_UNDEFINED)) {
return MOVE_ERR_NO_DIRECTION_PIN;
}
MoveResultCode res = _rg.move(move);
MoveResultCode res = _rg.move(move, &_queue()->queue_end);
if ((res == MOVE_OK) && blocking) {
while (isRunning()) {
noop_or_wait;
Expand Down Expand Up @@ -620,30 +620,40 @@ int8_t StepperMotion::setSpeedInMilliHz(uint32_t speed_mhz) {
uint32_t ticks = _rg.divForMilliHz(speed_mhz);
return setSpeedInTicks(ticks);
}
void StepperMotion::setCurrentPosition(int32_t new_pos) {
int32_t delta = new_pos - getCurrentPosition();
if (delta != 0) {
StepperQueue* q = _queue();
static void shiftQueuePosition(StepperQueue* q, uint32_t delta) {
q->queue_end.pos = (uint32_t)q->queue_end.pos + delta;
#if defined(SUPPORT_RP_PICO)
q->pos_offset += delta;
q->pos_offset = (uint32_t)q->pos_offset + delta;
#endif
#if defined(SUPPORT_QUEUE_ENTRY_START_POS_U16) || \
defined(SUPPORT_QUEUE_ENTRY_END_POS_U16)
for (uint8_t i = q->read_idx; i != q->next_write_idx; ++i) {
queue_entry& command = q->entry[i & QUEUE_LEN_MASK];
#if defined(SUPPORT_QUEUE_ENTRY_START_POS_U16)
command.start_pos_last16 += (uint16_t)delta;
#endif
#if defined(SUPPORT_QUEUE_ENTRY_END_POS_U16)
command.end_pos_last16 += (uint16_t)delta;
#endif
}
#endif
struct queue_end_s* queue_end = &q->queue_end;
}

void StepperMotion::setCurrentPosition(int32_t new_pos) {
const uint32_t delta = (uint32_t)new_pos - getCurrentPosition();
if (delta != 0) {
fasDisableInterrupts();
queue_end->pos = queue_end->pos + delta;
shiftQueuePosition(_queue(), delta);
_rg.advanceTargetPosition(delta);
fasEnableInterrupts();
}
}
void StepperMotion::setPositionAfterCommandsCompleted(int32_t new_pos) {
StepperQueue* q = _queue();
struct queue_end_s* queue_end = &q->queue_end;
fasDisableInterrupts();
int32_t delta = new_pos - q->queue_end.pos;
queue_end->pos = new_pos;
const uint32_t delta = (uint32_t)new_pos - q->queue_end.pos;
if (delta != 0) {
#if defined(SUPPORT_RP_PICO)
q->pos_offset += delta;
#endif
shiftQueuePosition(q, delta);
_rg.advanceTargetPosition(delta);
}
fasEnableInterrupts();
Expand Down
3 changes: 2 additions & 1 deletion src/fas_queue/queue_add_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ AqeResultCode StepperQueue::addQueueEntry(const struct stepper_command_s* cmd,
#if defined(SUPPORT_QUEUE_ENTRY_START_POS_U16)
e->start_pos_last16 = (uint32_t)next_queue_end.pos & 0xffff;
#endif
next_queue_end.pos = next_queue_end.pos + (cmd->count_up ? steps : -steps);
next_queue_end.pos =
(uint32_t)next_queue_end.pos + (cmd->count_up ? steps : -steps);
#if defined(SUPPORT_QUEUE_ENTRY_END_POS_U16)
e->end_pos_last16 = (uint32_t)next_queue_end.pos & 0xffff;
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/fas_ramp/RampCalculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ struct ramp_parameters_s {
}
inline void setTargetRelativePosition(int32_t move) {
fasDisableInterrupts();
if (any_change && !move_absolute) {
move_value += move;
if (apply && !keep_running) {
move_value = (uint32_t)move_value + move;
} else {
move_value = move;
move_absolute = false;
}
move_absolute = false;
keep_running = false;
keep_running_count_up = true;
any_change = true;
Expand Down
8 changes: 4 additions & 4 deletions src/fas_ramp/RampControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ void _getNextCommand(const struct ramp_ro_s* ramp, const struct ramp_rw_s* rw,
need_count_up = ramp->config.parameters.keep_running_count_up;
remaining_steps = 0xfffffff;
} else {
// this can overflow, which is legal
int32_t delta = ramp->target_pos - queue_end->pos;
// Positions wrap in 32 bits. A half-range displacement counts down.
const uint32_t delta = ramp->target_pos - (uint32_t)queue_end->pos;

if (delta == 0) {
// this case can happen on overshoot. So reverse current direction
need_count_up = !count_up;
} else {
need_count_up = delta > 0;
need_count_up = delta <= (uint32_t)INT32_MAX;
}
remaining_steps = fas_abs(delta);
remaining_steps = need_count_up ? delta : 0u - delta;
}

uint32_t performed_ramp_up_steps = rw->performed_ramp_up_steps;
Expand Down
17 changes: 14 additions & 3 deletions src/fas_ramp/RampGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,29 @@ MoveResultCode RampGenerator::moveTo(int32_t position,
inject_fill_interrupt(2);
return MOVE_OK;
}
MoveResultCode RampGenerator::move(int32_t move) {
MoveResultCode RampGenerator::move(int32_t move,
const struct queue_end_s* queue_end) {
MoveResultCode res = _parameters.checkValidConfig();
if (res != MOVE_OK) {
return res;
}
_parameters.setTargetRelativePosition(move);
if (isRampGeneratorActive()) {
_parameters.setTargetRelativePosition(move);
} else {
fasDisableInterrupts();
const uint32_t queued = queue_end->pos;
fasEnableInterrupts();
_parameters.setTargetPosition(queued + move);
}
_startMove(move != 0);
return MOVE_OK;
}
void RampGenerator::advanceTargetPosition(int32_t delta) {
void RampGenerator::advanceTargetPosition(uint32_t delta) {
// called with interrupts disabled
_ro.target_pos += delta;
if (_parameters.apply && _parameters.move_absolute) {
_parameters.move_value = (uint32_t)_parameters.move_value + delta;
}
}

void RampGenerator::afterCommandEnqueued(const NextCommand* command) {
Expand Down
4 changes: 2 additions & 2 deletions src/fas_ramp/RampGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RampGenerator {
void init();
inline int32_t targetPosition() const { return _ro.targetPosition(); }
inline void setTargetPosition(int32_t pos) { _ro.setTargetPosition(pos); }
void advanceTargetPosition(int32_t delta);
void advanceTargetPosition(uint32_t delta);
inline void setSpeedInTicks(uint32_t min_step_ticks) {
_parameters.setSpeedInTicks(min_step_ticks);
}
Expand Down Expand Up @@ -72,7 +72,7 @@ class RampGenerator {
return _parameters.checkValidConfig() == MOVE_OK;
}
void applySpeedAcceleration();
MoveResultCode move(int32_t move);
MoveResultCode move(int32_t move, const struct queue_end_s* queue);
MoveResultCode moveTo(int32_t position, const struct queue_end_s* queue);
MoveResultCode startRun(bool countUp);
inline void forceStop() { _ro.immediateStop(); }
Expand Down
159 changes: 159 additions & 0 deletions tests/host/test_34.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#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];

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

void raw(uint8_t steps) {
const stepper_command_s command{3200, steps, true};
check(stepper.addQueueEntry(&command, false) == AQE_OK, "queue raw steps");
}

int drain() {
int pulses = 0;
for (int i = 0; i < 150 && 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(), "simulator move settles");
return pulses;
}
};

void relative_move_extends_pending_absolute_target() {
Fixture f;
auto& s = f.stepper;
check(s.moveTo(20) == MOVE_OK && s.move(10) == MOVE_OK,
"absolute and relative requests accepted before refill");
const int pulses = f.drain();
printf("moveTo(20), move(10): target=%" PRId32 " position=%" PRId32
" pulses=%d\n",
s.targetPos(), s.getCurrentPosition(), pulses);
check(pulses == 30 && s.getCurrentPosition() == 30,
"relative move extends the accepted absolute target");
}

void first_relative_move_starts_from_queued_position() {
Fixture f;
auto& s = f.stepper;
f.raw(10);
check(s.move(5) == MOVE_OK, "first ramp may follow raw commands");
const int pulses = f.drain();
printf("raw(10), move(5): target=%" PRId32 " position=%" PRId32
" pulses=%d\n",
s.targetPos(), s.getCurrentPosition(), pulses);
check(pulses == 15 && s.getCurrentPosition() == 15,
"first relative ramp appends five steps after the queue tail");
}

void rebase_updates_positions_of_queued_commands() {
Fixture f;
auto& s = f.stepper;
f.raw(2);
f.raw(3);
s.setCurrentPosition(1000);
const int32_t rebased = s.getCurrentPosition();
const int32_t tail = s.getPositionAfterCommandsCompleted();
++f.queue.read_idx;
const int32_t next = s.getCurrentPosition();
s.setPositionAfterCommandsCompleted(2005);
const int32_t rebased_again = s.getCurrentPosition();
printf("rebase raw(2), raw(3): current=%" PRId32 " tail=%" PRId32
" next=%" PRId32 " after tail rebase=%" PRId32 "\n",
rebased, tail, next, rebased_again);
check(rebased == 1000 && tail == 1005 && next == 1002 && rebased_again == 2002,
"coordinate shifts apply while queued commands remain");
}

void rebase_updates_pending_absolute_target() {
Fixture f;
auto& s = f.stepper;
check(s.moveTo(20) == MOVE_OK, "absolute move accepted before coordinate shift");
s.setPositionAfterCommandsCompleted(1000);
const int pulses = f.drain();
printf("moveTo(20), rebase(1000): target=%" PRId32 " position=%" PRId32
" pulses=%d\n",
s.targetPos(), s.getCurrentPosition(), pulses);
check(pulses == 20 && s.getCurrentPosition() == 1020,
"rebasing preserves planned travel before the first refill");
}

void half_range_move_plans_reverse_pulses() {
Fixture f;
auto& s = f.stepper;
printf("start moveTo(INT32_MIN) from zero\n");
check(s.moveTo(INT32_MIN) == MOVE_OK, "half-range reverse request accepted");
StepperMotionTest::fill(s);
const auto& first = f.queue.entry[f.queue.read_idx & QUEUE_LEN_MASK];
printf("half-range move: target=%" PRId32 " countUp=%u steps=%u entries=%u\n",
s.targetPos(), first.countUp, first.steps, s.queueEntries());
check(s.targetPos() == INT32_MIN && first.steps > 0 && !first.countUp,
"half-range request begins in the defined negative direction");
}

void queue_and_coordinate_shift_can_cross_signed_boundary() {
Fixture f;
auto& s = f.stepper;
printf("set position INT32_MAX, queue two forward pulses\n");
s.setCurrentPosition(INT32_MAX);
f.raw(2);
const int32_t wrapped_tail = s.getPositionAfterCommandsCompleted();
printf("queue boundary: current=%" PRId32 " tail=%" PRId32 "\n",
s.getCurrentPosition(), wrapped_tail);
check(wrapped_tail == INT32_MIN + 1 && s.getCurrentPosition() == INT32_MAX,
"queued position wraps while the current coordinate is unchanged");
s.setCurrentPosition(INT32_MIN);
printf("coordinate boundary: current=%" PRId32 " tail=%" PRId32 "\n",
s.getCurrentPosition(), s.getPositionAfterCommandsCompleted());
check(s.getCurrentPosition() == INT32_MIN &&
s.getPositionAfterCommandsCompleted() == INT32_MIN + 2,
"rebasing across the signed limit preserves the two queued steps");
}
} // namespace

int main() {
relative_move_extends_pending_absolute_target();
first_relative_move_starts_from_queued_position();
rebase_updates_positions_of_queued_commands();
rebase_updates_pending_absolute_target();
half_range_move_plans_reverse_pulses();
queue_and_coordinate_shift_can_cross_signed_boundary();
return failures != 0;
}
Loading