Skip to content

Commit 0585f46

Browse files
committed
esp32: add engine->task_rate(uint8_t delay_ms) function to adjust the stepper task rate to e.g. 1ms (see #288 for reference)
1 parent f83a711 commit 0585f46

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ TODO:
55
- rename RampConstAcceleration to e.g. RampControl
66
- for esp-idf 5 make use of espressif resource management of rmt channels
77

8+
0.31.3:
9+
- esp32: add `engine->task_rate(uint8_t delay_ms)` function to adjust the stepper task rate to e.g. 1ms (see #288 for reference)
10+
811
0.31.2:
912
- Move constants out of PoorManFloat.h and autogenerate these
1013
- Fix pmfl constant used by SAM Due aka 21MHz

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=FastAccelStepper
2-
version=0.31.2
2+
version=0.31.3
33
license=MIT
44
author=Jochen Kiemes <[email protected]>
55
maintainer=Jochen Kiemes <[email protected]>

src/FastAccelStepper.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,34 @@ class FastAccelStepperEngine {
9494
uint8_t driver_type = DRIVER_DONT_CARE);
9595
#endif
9696

97+
#if defined(SUPPORT_TASK_RATE_CHANGE)
98+
// For e.g. esp32 the repetition rate of the stepper task can be changed.
99+
// The default delay is 4ms.
100+
//
101+
// The steppertask is looping with:
102+
// manageSteppers()
103+
// wdt_reset()
104+
// delay()
105+
//
106+
// The actual repetition rate of the stepper task is delay + execution time of manageSteppers()
107+
//
108+
// This function is primary of interest in conjunction with setForwardPlanningTimeInMs().
109+
// If the delay is larger then forward planning time, then the stepper queue will always
110+
// run out of commands, which lead to a sudden stop of the motor. If the delay is 0,
111+
// then the stepper task will constantly looping, which may lead to the task blocking other
112+
// tasks. Consequently, this function is intended for advanced users.
113+
//
114+
// There is not planned to test this functionality, because automatic testing is only
115+
// available for avr devices and those continue to use fixed 4ms rate.
116+
//
117+
// Please be aware, that the configured tick rate aka portTICK_PERIOD_MS is relevant.
118+
// Apparently, arduino-esp32 has FreeRTOS configured to have a tick-rate of 1000Hz
119+
inline void task_rate(uint8_t delay_ms) {
120+
_delay_ms = delay_ms;
121+
};
122+
uint8_t _delay_ms;
123+
#endif
124+
97125
// Comments to valid pins:
98126
//
99127
// clang-format off

src/StepperISR_esp32.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,15 @@ int8_t StepperQueue::queueNumForStepPin(uint8_t step_pin) { return -1; }
106106
//*************************************************************************************************
107107
void StepperTask(void *parameter) {
108108
FastAccelStepperEngine *engine = (FastAccelStepperEngine *)parameter;
109-
const TickType_t delay_4ms =
110-
(DELAY_MS_BASE + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
111109
while (true) {
112110
engine->manageSteppers();
113111
#if ESP_IDF_VERSION_MAJOR == 4
114112
// not clear, if the wdt reset is needed. With idf-version 5, the reset
115113
// causes an issue.
116114
esp_task_wdt_reset();
117115
#endif
118-
vTaskDelay(delay_4ms);
116+
const TickType_t delay_time = (engine->_delay_ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
117+
vTaskDelay(delay_time);
119118
}
120119
}
121120

@@ -131,6 +130,7 @@ void fas_init_engine(FastAccelStepperEngine *engine, uint8_t cpu_core) {
131130
#define STACK_SIZE 3000
132131
#define PRIORITY (configMAX_PRIORITIES - 1)
133132
#endif
133+
engine->_delay_ms = DELAY_MS_BASE;
134134
if (cpu_core > 1) {
135135
xTaskCreate(StepperTask, "StepperTask", STACK_SIZE, engine, PRIORITY, NULL);
136136
} else {

src/fas_arch/common_esp32.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
// have more than one core
4646
#define SUPPORT_CPU_AFFINITY
4747

48+
// have adjustable stepper task rate
49+
#define SUPPORT_TASK_RATE_CHANGE
50+
4851
#define LL_TOGGLE_PIN(dirPin) \
4952
gpio_ll_set_level(&GPIO, (gpio_num_t)dirPin, \
5053
gpio_ll_get_level(&GPIO, (gpio_num_t)dirPin) ^ 1)

0 commit comments

Comments
 (0)