Skip to content

Commit 5cf507b

Browse files
committed
format code
1 parent 1569607 commit 5cf507b

File tree

2 files changed

+135
-136
lines changed

2 files changed

+135
-136
lines changed

src/FastAccelStepper.cpp

Lines changed: 95 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -911,128 +911,123 @@ void FastAccelStepper::backwardStep(bool blocking) {
911911
int32_t FastAccelStepper::getCurrentPosition() {
912912
return fas_queue[_queue_num].getCurrentPosition();
913913
}
914-
int8_t FastAccelStepper::moveTimed(int16_t steps, uint32_t duration, uint32_t &actual_duration) {
914+
int8_t FastAccelStepper::moveTimed(int16_t steps, uint32_t duration,
915+
uint32_t& actual_duration) {
915916
// For now only a very crude implementation
916917
uint8_t freeEntries = QUEUE_LEN - queueEntries();
917918
actual_duration = 0;
918-
struct stepper_command_s cmd = {
919-
.ticks = 0, .steps = 0, .count_up = true};
919+
struct stepper_command_s cmd = {.ticks = 0, .steps = 0, .count_up = true};
920920
if (steps == 0) {
921-
if ((duration >> 16) >= QUEUE_LEN) {
922-
return MOVE_TIMED_TOO_LARGE_ERROR;
923-
}
924-
if ((duration >> 16) >= freeEntries) {
925-
return MOVE_TIMED_BUSY;
926-
}
927-
// Should fit
928-
while (duration > 0) {
929-
if (duration <= 65535) {
930-
// done using one command
931-
cmd.ticks = duration;
932-
}
933-
else if (duration >= 131072) {
934-
// need more than one command
935-
cmd.ticks = 65535;
936-
}
937-
else {
938-
// just use half of the duration now, and the other half in the next cmd.
939-
cmd.ticks = duration >> 1;
940-
}
941-
uint8_t ret = addQueueEntry(&cmd);
942-
if (ret != 0) {
943-
// unexpected
944-
return ret;
945-
}
946-
actual_duration += cmd.ticks;
947-
duration -= cmd.ticks;
948-
}
949-
return MOVE_TIMED_OK;
921+
if ((duration >> 16) >= QUEUE_LEN) {
922+
return MOVE_TIMED_TOO_LARGE_ERROR;
923+
}
924+
if ((duration >> 16) >= freeEntries) {
925+
return MOVE_TIMED_BUSY;
926+
}
927+
// Should fit
928+
while (duration > 0) {
929+
if (duration <= 65535) {
930+
// done using one command
931+
cmd.ticks = duration;
932+
} else if (duration >= 131072) {
933+
// need more than one command
934+
cmd.ticks = 65535;
935+
} else {
936+
// just use half of the duration now, and the other half in the next
937+
// cmd.
938+
cmd.ticks = duration >> 1;
939+
}
940+
uint8_t ret = addQueueEntry(&cmd);
941+
if (ret != 0) {
942+
// unexpected
943+
return ret;
944+
}
945+
actual_duration += cmd.ticks;
946+
duration -= cmd.ticks;
947+
}
948+
return MOVE_TIMED_OK;
950949
}
951950

952951
// let's evaluate the direction
953952
if (steps < 0) {
954-
cmd.count_up = false;
955-
steps = -steps;
953+
cmd.count_up = false;
954+
steps = -steps;
956955
}
957956

958957
// There are steps to execute
959958
// Let's first calculate the step rate
960959
uint32_t rate = duration;
961960
rate /= steps;
962961
if (rate > 65535) {
963-
// we need pauses, so only few steps can be executed
964-
uint16_t cmds_per_step = rate >> 16; // bit too small
965-
if (cmds_per_step >= QUEUE_LEN) {
966-
return MOVE_TIMED_TOO_LARGE_ERROR;
967-
}
968-
if (steps >= QUEUE_LEN) {
969-
return MOVE_TIMED_TOO_LARGE_ERROR;
970-
}
971-
uint8_t cmds = steps * cmds_per_step;
972-
if (cmds >= QUEUE_LEN) {
973-
return MOVE_TIMED_TOO_LARGE_ERROR;
974-
}
975-
if (cmds > freeEntries) {
976-
return MOVE_TIMED_BUSY;
977-
}
978-
// Should fit into the queue.
979-
for (uint8_t s = 0;s < steps;s++) {
980-
uint32_t this_duration = rate;
981-
cmd.steps = 1;
982-
while (this_duration) {
983-
if (this_duration > 131072) {
984-
cmd.ticks = 65535;
985-
}
986-
else if (this_duration > 65535) {
987-
cmd.ticks = this_duration/2;
988-
}
989-
else {
990-
cmd.ticks = this_duration;
991-
}
992-
this_duration -= cmd.ticks;
993-
994-
uint8_t ret = addQueueEntry(&cmd);
995-
if (ret != 0) {
996-
// unexpected
997-
return ret;
998-
}
999-
actual_duration += cmd.ticks;
1000-
// remaining are pauses
1001-
cmd.steps = 0;
1002-
}
1003-
}
1004-
return MOVE_TIMED_OK;
1005-
}
1006-
// Now we need to run steps at "high" speed.
1007-
if (steps > QUEUE_LEN * 255) {
962+
// we need pauses, so only few steps can be executed
963+
uint16_t cmds_per_step = rate >> 16; // bit too small
964+
if (cmds_per_step >= QUEUE_LEN) {
965+
return MOVE_TIMED_TOO_LARGE_ERROR;
966+
}
967+
if (steps >= QUEUE_LEN) {
1008968
return MOVE_TIMED_TOO_LARGE_ERROR;
1009969
}
1010-
if (steps > freeEntries * 255) {
970+
uint8_t cmds = steps * cmds_per_step;
971+
if (cmds >= QUEUE_LEN) {
972+
return MOVE_TIMED_TOO_LARGE_ERROR;
973+
}
974+
if (cmds > freeEntries) {
1011975
return MOVE_TIMED_BUSY;
1012976
}
1013-
// The steps should fit in
1014-
while (steps > 0) {
1015-
cmd.ticks = rate;
1016-
if (steps > 510) {
1017-
cmd.steps = 255;
1018-
}
1019-
else if (steps > 255) {
1020-
cmd.steps = steps/2;
1021-
}
1022-
else {
1023-
cmd.steps = steps;
1024-
}
1025-
uint8_t ret = addQueueEntry(&cmd);
1026-
if (ret != 0) {
1027-
// unexpected
1028-
return ret;
977+
// Should fit into the queue.
978+
for (uint8_t s = 0; s < steps; s++) {
979+
uint32_t this_duration = rate;
980+
cmd.steps = 1;
981+
while (this_duration) {
982+
if (this_duration > 131072) {
983+
cmd.ticks = 65535;
984+
} else if (this_duration > 65535) {
985+
cmd.ticks = this_duration / 2;
986+
} else {
987+
cmd.ticks = this_duration;
988+
}
989+
this_duration -= cmd.ticks;
990+
991+
uint8_t ret = addQueueEntry(&cmd);
992+
if (ret != 0) {
993+
// unexpected
994+
return ret;
995+
}
996+
actual_duration += cmd.ticks;
997+
// remaining are pauses
998+
cmd.steps = 0;
1029999
}
1030-
uint32_t cmd_duration = cmd.ticks;
1031-
cmd_duration *= cmd.steps;
1032-
actual_duration += cmd_duration;
1033-
steps -= cmd.steps;
10341000
}
10351001
return MOVE_TIMED_OK;
1002+
}
1003+
// Now we need to run steps at "high" speed.
1004+
if (steps > QUEUE_LEN * 255) {
1005+
return MOVE_TIMED_TOO_LARGE_ERROR;
1006+
}
1007+
if (steps > freeEntries * 255) {
1008+
return MOVE_TIMED_BUSY;
1009+
}
1010+
// The steps should fit in
1011+
while (steps > 0) {
1012+
cmd.ticks = rate;
1013+
if (steps > 510) {
1014+
cmd.steps = 255;
1015+
} else if (steps > 255) {
1016+
cmd.steps = steps / 2;
1017+
} else {
1018+
cmd.steps = steps;
1019+
}
1020+
uint8_t ret = addQueueEntry(&cmd);
1021+
if (ret != 0) {
1022+
// unexpected
1023+
return ret;
1024+
}
1025+
uint32_t cmd_duration = cmd.ticks;
1026+
cmd_duration *= cmd.steps;
1027+
actual_duration += cmd_duration;
1028+
steps -= cmd.steps;
1029+
}
1030+
return MOVE_TIMED_OK;
10361031
}
10371032
void FastAccelStepper::detachFromPin() { fas_queue[_queue_num].disconnect(); }
10381033
void FastAccelStepper::reAttachToPin() { fas_queue[_queue_num].connect(); }

src/FastAccelStepper.h

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -617,42 +617,46 @@ class FastAccelStepper {
617617
_forward_planning_in_ticks *= TICKS_PER_S / 1000; // ticks per ms
618618
}
619619

620-
// ## Intermediate Level Stepper Control for Advanced Users
621-
//
622-
// The main purpose is to bypass the ramp generator as mentioned in
623-
// [#299](https://github.com/gin66/FastAccelStepper/issues/299).
624-
// This shall allow to run consecutive small moves with fixed speed.
625-
// The parameters are distance (which can be 0) and duration in ticks.
626-
// Distance 0 makes sense in order to keep the time running and not
627-
// getting out of sync.
628-
// Due to integer arithmetics the actual duration may be off by a small value.
629-
// That's why the actual_duration in TICKS is returned.
630-
// The application should consider this for the next runTimed move.
631-
//
632-
// In order to not have another lightweight ramp generator running in
633-
// background interrupt, the expecation to the application is, that this
634-
// function is frequently enough called without the queue being emptied.
635-
//
636-
// The current implementation immediately starts with a step, if there should be one.
637-
// Perhaps performing the step in the middle of the duration is more appropriate ?
638-
//
639-
// Meaning of the return values - which are in addtion to AQE from below
640-
// - OK: Move has been successfully appended to the queue
641-
// - BUSY: Queue does not have sufficient entries to append this timed move.
642-
// - EMPTY: The queue has run out of commands, but the move has been appended.
643-
// - TOO_LARGE: The move request does not fit into the queue.
644-
// Reasons: The queue depth is (32/16) for SAM+ESP32/AVR.
645-
// Each queue entry can emit 255 steps => (8160/4080) steps
646-
// If the time between steps is >65535 ticks, then pauses
647-
// have to be generated. In this case only (16/8) steps
648-
// can be generated...but the queue shall not be empty
649-
// => so even less steps can be done.
650-
// Recommendation: keep the duration in the range of ms.
651-
#define MOVE_TIMED_OK ((int8_t)0)
652-
#define MOVE_TIMED_BUSY ((int8_t)5)
653-
#define MOVE_TIMED_EMPTY ((int8_t)6)
654-
#define MOVE_TIMED_TOO_LARGE_ERROR ((int8_t)-4)
655-
int8_t moveTimed(int16_t steps, uint32_t duration, uint32_t &actual_duration);
620+
// ## Intermediate Level Stepper Control for Advanced Users
621+
//
622+
// The main purpose is to bypass the ramp generator as mentioned in
623+
// [#299](https://github.com/gin66/FastAccelStepper/issues/299).
624+
// This shall allow to run consecutive small moves with fixed speed.
625+
// The parameters are distance (which can be 0) and duration in ticks.
626+
// Distance 0 makes sense in order to keep the time running and not
627+
// getting out of sync.
628+
// Due to integer arithmetics the actual duration may be off by a small value.
629+
// That's why the actual_duration in TICKS is returned.
630+
// The application should consider this for the next runTimed move.
631+
//
632+
// In order to not have another lightweight ramp generator running in
633+
// background interrupt, the expecation to the application is, that this
634+
// function is frequently enough called without the queue being emptied.
635+
//
636+
// The current implementation immediately starts with a step, if there should be
637+
// one. Perhaps performing the step in the middle of the duration is more
638+
// appropriate ?
639+
//
640+
// Meaning of the return values - which are in addtion to AQE from below
641+
// - OK: Move has been successfully appended to the queue
642+
// - BUSY: Queue does not have sufficient entries to append this timed
643+
// move.
644+
// - EMPTY: The queue has run out of commands, but the move has been
645+
// appended.
646+
// - TOO_LARGE: The move request does not fit into the queue.
647+
// Reasons: The queue depth is (32/16) for SAM+ESP32/AVR.
648+
// Each queue entry can emit 255 steps => (8160/4080)
649+
// steps If the time between steps is >65535 ticks, then
650+
// pauses have to be generated. In this case only (16/8)
651+
// steps can be generated...but the queue shall not be
652+
// empty
653+
// => so even less steps can be done.
654+
// Recommendation: keep the duration in the range of ms.
655+
#define MOVE_TIMED_OK ((int8_t)0)
656+
#define MOVE_TIMED_BUSY ((int8_t)5)
657+
#define MOVE_TIMED_EMPTY ((int8_t)6)
658+
#define MOVE_TIMED_TOO_LARGE_ERROR ((int8_t)-4)
659+
int8_t moveTimed(int16_t steps, uint32_t duration, uint32_t& actual_duration);
656660

657661
// ## Low Level Stepper Queue Management (low level access)
658662
//

0 commit comments

Comments
 (0)