Skip to content

Commit

Permalink
add parameter realtime to getCurrentSpeedIn...()
Browse files Browse the repository at this point in the history
  • Loading branch information
gin66 committed Nov 20, 2023
1 parent 4192bd5 commit 7b5ffe5
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 160 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ TODO:
- merge the two esp32 rmt drivers as soon as esp32c3 works

pre-0.30.7:
- Fix for issue #208: close to speed reversal is the sign of current speed incorrect.
Still the speed increase during acceleration/deceleraion is not monotonic
- Fix for issue #208: the sign of current speed may be incorrect close to direction change
- The functions `getCurrentSpeedInMilliHz()` and `getCurrentSpeedInUs()` have been extended to supply a bool parameter about being realtime.

0.30.6:
- Support for ESP32C3
Expand Down
77 changes: 38 additions & 39 deletions examples/Issue208/Issue208.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,61 @@ FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper;

bool test() {
bool verbose = false;
int32_t errCnt = 0;
Serial.println("Start test: speed up");
int32_t last_v_mHz = 0;
stepper->setAcceleration(10000);
stepper->setSpeedInHz(36800);
stepper->runForward();

while(stepper->rampState() != (RAMP_STATE_COAST | RAMP_DIRECTION_COUNT_UP)) {
int32_t v_mHz = stepper->getCurrentSpeedInMilliHz();
if (v_mHz == last_v_mHz) {
continue;
}
Serial.println(v_mHz);
if (v_mHz < last_v_mHz) {
Serial.print("FAIL: last=");
Serial.print(last_v_mHz);
Serial.print(" new=");
Serial.println(v_mHz);
errCnt++;
// return false;
}
last_v_mHz = v_mHz;
while (stepper->rampState() != (RAMP_STATE_COAST | RAMP_DIRECTION_COUNT_UP)) {
int32_t v_mHz = stepper->getCurrentSpeedInMilliHz(false);
if (v_mHz == last_v_mHz) {
continue;
}
if (verbose) Serial.println(v_mHz);
if (v_mHz < last_v_mHz) {
Serial.print("FAIL: last=");
Serial.print(last_v_mHz);
Serial.print(" new=");
Serial.println(v_mHz);
errCnt++;
// return false;
}
last_v_mHz = v_mHz;
}
// There can be still speed increases in the queue, even so the ramp generator
// is already coasting
delay(20);
last_v_mHz = stepper->getCurrentSpeedInMilliHz();
last_v_mHz = stepper->getCurrentSpeedInMilliHz(false);

Serial.println("Reverse");

stepper->runBackward();

while(stepper->rampState() != (RAMP_STATE_COAST | RAMP_DIRECTION_COUNT_DOWN)) {
int32_t v_mHz = stepper->getCurrentSpeedInMilliHz();
if (v_mHz == last_v_mHz) {
continue;
}
Serial.println(v_mHz);
if (v_mHz > last_v_mHz) {
Serial.print("FAIL: last=");
Serial.print(last_v_mHz);
Serial.print(" new=");
Serial.println(v_mHz);
errCnt++;
// return false;
}
last_v_mHz = v_mHz;
while (stepper->rampState() !=
(RAMP_STATE_COAST | RAMP_DIRECTION_COUNT_DOWN)) {
int32_t v_mHz = stepper->getCurrentSpeedInMilliHz(false);
if (v_mHz == last_v_mHz) {
continue;
}
if (verbose) Serial.println(v_mHz);
if (v_mHz > last_v_mHz) {
Serial.print("FAIL: last=");
Serial.print(last_v_mHz);
Serial.print(" new=");
Serial.println(v_mHz);
errCnt++;
// return false;
}
last_v_mHz = v_mHz;
}
if (errCnt > 0) {
Serial.print("Errors=");
Serial.println(errCnt);
Serial.print("Errors=");
Serial.println(errCnt);
}
return errCnt != 0;
return errCnt == 0;
}

void setup() {
Expand Down Expand Up @@ -112,8 +114,7 @@ void setup() {
Serial.println("PASS");
digitalWrite(PIN, HIGH);
digitalWrite(PIN, LOW);
}
else {
} else {
Serial.println("FAIL");
}
delay(1000);
Expand All @@ -122,6 +123,4 @@ void setup() {
#endif
}


void loop() {
}
void loop() {}
2 changes: 1 addition & 1 deletion examples/StepperDemo/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#error "Unsupported derivate"
#endif

#if defined(CONFIG_IDF_TARGET_ESP32C3) && (ARDUINO_USB_MODE==1)
#if defined(CONFIG_IDF_TARGET_ESP32C3) && (ARDUINO_USB_MODE == 1)
#define SerialInterface USBSerial
#else
#define SerialInterface Serial
Expand Down
21 changes: 17 additions & 4 deletions extras/doc/FastAccelStepper_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ And the two directions of a move
#define RAMP_DIRECTION_COUNT_DOWN 64
```
A ramp state value of 2 is set after any move call on a stopped motor
and until the stepper task. The stepper task will then control the direction
flags
and until the stepper task is serviced. The stepper task will then
control the direction flags
## Timing values - Architecture dependent
Expand Down Expand Up @@ -326,9 +326,22 @@ retrieves the actual speed.
| > 0 | while position counting up  |
| < 0 | while position counting down |

If the parameter realtime is true, then the most actual speed
from the stepper queue is derived. This works only, if the queue
does not contain pauses, which is normally the case for slow speeds.
Otherwise the speed from the ramp generator is reported, which is
done always in case of `realtime == false`. That speed is typically
the value of the speed a couple of milliseconds later.

The drawback of `realtime == true` is, that the reported speed
may either come from the queue or from the ramp generator.
This means the returned speed may have jumps during
acceleration/deceleration.

For backward compatibility, the default is true.
```cpp
int32_t getCurrentSpeedInUs();
int32_t getCurrentSpeedInMilliHz();
int32_t getCurrentSpeedInUs(bool realtime = true);
int32_t getCurrentSpeedInMilliHz(bool realtime = true);
```
## Acceleration
setAcceleration() expects as parameter the change of speed
Expand Down
18 changes: 8 additions & 10 deletions extras/tests/simavr_based/test_issue208/expect.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
DirA: 0*L->H, 1*H->L
DirB: 1*L->H, 0*H->L
EnableA: 3*L->H, 2*H->L
EnableA: 0*L->H, 0*H->L
EnableB: 0*L->H, 0*H->L
StepA: 1048*L->H, 1048*H->L, Max High=10us Total High=5476us
StepB: 2*L->H, 2*H->L, Max High=4us Total High=9us
Position[A]=0
StepA: 235909*L->H, 235909*H->L, Max High=11us Total High=1237097us
StepB: 2*L->H, 2*H->L, Max High=13us Total High=17us
Position[A]=37461

Position[B]=2

Time in EnableA max=4204 us, total=8408 us
Time in FillISR max=769 us, total=1237221 us

Time in FillISR max=1906 us, total=25682 us
Time in StepA max=11 us, total=1237097 us

Time in StepA max=10 us, total=5476 us
Time in StepB max=13 us, total=17 us

Time in StepB max=4 us, total=9 us

Time in StepISR max=7 us, total=4875 us
Time in StepISR max=7 us, total=1021996 us

14 changes: 7 additions & 7 deletions extras/tests/simavr_based/test_sd_04_timing_2560/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
EnableA: 1*L->H, 1*H->L
EnableB: 2*L->H, 1*H->L
EnableC: 2*L->H, 1*H->L
StepA: 64000*L->H, 64000*H->L, Max High=28us Total High=521699us
StepB: 64000*L->H, 64000*H->L, Max High=35us Total High=393630us
StepC: 64000*L->H, 64000*H->L, Max High=40us Total High=484932us
StepA: 64000*L->H, 64000*H->L, Max High=28us Total High=580688us
StepB: 64000*L->H, 64000*H->L, Max High=31us Total High=392513us
StepC: 64000*L->H, 64000*H->L, Max High=41us Total High=426208us
Position[A]=64000

Position[B]=64000
Expand All @@ -19,11 +19,11 @@ Time in EnableB max=246080 us, total=246080 us

Time in EnableC max=254235 us, total=254235 us

Time in FillISR max=2686 us, total=2092758 us
Time in FillISR max=2686 us, total=2091889 us

Time in StepA max=28 us, total=521699 us
Time in StepA max=28 us, total=580688 us

Time in StepB max=35 us, total=393630 us
Time in StepB max=31 us, total=392513 us

Time in StepC max=40 us, total=484932 us
Time in StepC max=41 us, total=426208 us

10 changes: 5 additions & 5 deletions extras/tests/simavr_based/test_sd_04_timing_328p/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
DirB: 1*L->H, 0*H->L
EnableA: 1*L->H, 1*H->L
EnableB: 2*L->H, 1*H->L
StepA: 1000*L->H, 1000*H->L, Max High=13us Total High=5539us
StepB: 1000*L->H, 1000*H->L, Max High=15us Total High=5513us
StepA: 1000*L->H, 1000*H->L, Max High=13us Total High=5545us
StepB: 1000*L->H, 1000*H->L, Max High=16us Total High=5520us
Position[A]=1000

Position[B]=1000
Expand All @@ -12,11 +12,11 @@ Time in EnableA max=225398 us, total=225398 us

Time in EnableB max=238116 us, total=238116 us

Time in FillISR max=2644 us, total=47660 us
Time in FillISR max=2644 us, total=47652 us

Time in StepA max=13 us, total=5539 us
Time in StepA max=13 us, total=5545 us

Time in StepB max=15 us, total=5513 us
Time in StepB max=16 us, total=5520 us

Time in StepISR max=7 us, total=9224 us

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
DirB: 1*L->H, 0*H->L
EnableA: 1*L->H, 1*H->L
EnableB: 1*L->H, 0*H->L
StepA: 1000*L->H, 1000*H->L, Max High=11us Total High=5276us
StepA: 1000*L->H, 1000*H->L, Max High=12us Total High=5274us
StepB: 0*L->H, 0*H->L, Max High=0us Total High=0us
Position[A]=1000

Time in EnableA max=225399 us, total=225399 us

Time in FillISR max=2015 us, total=27549 us
Time in FillISR max=2015 us, total=27557 us

Time in StepA max=11 us, total=5276 us
Time in StepA max=12 us, total=5274 us

Time in StepISR max=7 us, total=4581 us

32 changes: 19 additions & 13 deletions src/FastAccelStepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,32 +775,38 @@ uint32_t FastAccelStepper::getPeriodInUsAfterCommandsCompleted() {
}
return 0;
}
void FastAccelStepper::getCurrentSpeedInTicks(struct actual_ticks_s *speed) {
bool valid = fas_queue[_queue_num].getActualTicksWithDirection(speed);
void FastAccelStepper::getCurrentSpeedInTicks(struct actual_ticks_s* speed,
bool realtime) {
bool valid;
if (realtime) {
valid = fas_queue[_queue_num].getActualTicksWithDirection(speed);
} else {
valid = false;
}
if (!valid) {
if (_rg.isRampGeneratorActive()) {
_rg.getCurrentSpeedInTicks(speed);
}
_rg.getCurrentSpeedInTicks(speed);
}
}
}
int32_t FastAccelStepper::getCurrentSpeedInUs() {
int32_t FastAccelStepper::getCurrentSpeedInUs(bool realtime) {
struct actual_ticks_s speed;
getCurrentSpeedInTicks(&speed);
getCurrentSpeedInTicks(&speed, realtime);
int32_t speed_in_us = speed.ticks / (TICKS_PER_S / 1000000);
if (speed.count_up) {
return speed_in_us;
return speed_in_us;
}
return -speed_in_us;
}
int32_t FastAccelStepper::getCurrentSpeedInMilliHz() {
int32_t FastAccelStepper::getCurrentSpeedInMilliHz(bool realtime) {
struct actual_ticks_s speed;
getCurrentSpeedInTicks(&speed);
getCurrentSpeedInTicks(&speed, realtime);
if (speed.ticks > 0) {
int32_t speed_in_mhz = ((uint32_t)250 * TICKS_PER_S) / speed.ticks * 4;
if (speed.count_up) {
return speed_in_mhz;
}
return -speed_in_mhz;
if (speed.count_up) {
return speed_in_mhz;
}
return -speed_in_mhz;
}
return 0;
}
Expand Down
23 changes: 18 additions & 5 deletions src/FastAccelStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ class FastAccelStepperEngine {
#define RAMP_DIRECTION_COUNT_DOWN 64

// A ramp state value of 2 is set after any move call on a stopped motor
// and until the stepper task. The stepper task will then control the direction
// flags
// and until the stepper task is serviced. The stepper task will then
// control the direction flags

#include "RampGenerator.h"

Expand Down Expand Up @@ -365,8 +365,21 @@ class FastAccelStepper {
// | > 0 | while position counting up  |
// | < 0 | while position counting down |
//
int32_t getCurrentSpeedInUs();
int32_t getCurrentSpeedInMilliHz();
// If the parameter realtime is true, then the most actual speed
// from the stepper queue is derived. This works only, if the queue
// does not contain pauses, which is normally the case for slow speeds.
// Otherwise the speed from the ramp generator is reported, which is
// done always in case of `realtime == false`. That speed is typically
// the value of the speed a couple of milliseconds later.
//
// The drawback of `realtime == true` is, that the reported speed
// may either come from the queue or from the ramp generator.
// This means the returned speed may have jumps during
// acceleration/deceleration.
//
// For backward compatibility, the default is true.
int32_t getCurrentSpeedInUs(bool realtime = true);
int32_t getCurrentSpeedInMilliHz(bool realtime = true);

// ## Acceleration
// setAcceleration() expects as parameter the change of speed
Expand Down Expand Up @@ -646,7 +659,7 @@ class FastAccelStepper {
bool needAutoDisable();
bool agreeWithAutoDisable();
bool usesAutoEnablePin(uint8_t pin);
void getCurrentSpeedInTicks(struct actual_ticks_s *speed);
void getCurrentSpeedInTicks(struct actual_ticks_s* speed, bool realtime);

FastAccelStepperEngine* _engine;
RampGenerator _rg;
Expand Down
4 changes: 2 additions & 2 deletions src/RampGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ class RampGenerator {
void getCurrentSpeedInTicks(struct actual_ticks_s *speed) {
fasDisableInterrupts();
speed->ticks = _rw.curr_ticks;
uint8_t rs = _rw.rampState();
uint8_t rs = _rw.rampState();
fasEnableInterrupts();
speed->count_up = ((rs & RAMP_DIRECTION_COUNT_UP) != 0);
speed->count_up = ((rs & RAMP_DIRECTION_COUNT_UP) != 0);
}
uint32_t getCurrentPeriodInTicks() {
fasDisableInterrupts();
Expand Down
Loading

0 comments on commit 7b5ffe5

Please sign in to comment.