Skip to content

Commit fefdbed

Browse files
committed
Do BABYSTEPPING in stepper ISR
1 parent 64b96f3 commit fefdbed

File tree

6 files changed

+78
-6
lines changed

6 files changed

+78
-6
lines changed

Marlin/Configuration_adv.h

+1
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,7 @@
14471447
*/
14481448
//#define BABYSTEPPING
14491449
#if ENABLED(BABYSTEPPING)
1450+
//#define INTEGRATED_BABYSTEPPING // EXPERIMENTAL integration of babystepping into the Stepper ISR
14501451
//#define BABYSTEP_WITHOUT_HOMING
14511452
//#define BABYSTEP_XY // Also enable X/Y Babystepping. Not supported on DELTA!
14521453
#define BABYSTEP_INVERT_Z false // Change if Z babysteps should go the other way

Marlin/src/feature/babystep.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ void Babystep::add_steps(const AxisEnum axis, const int16_t distance) {
117117
#if ENABLED(BABYSTEP_ALWAYS_AVAILABLE)
118118
gcode.reset_stepper_timeout();
119119
#endif
120+
121+
#if ENABLED(INTEGRATED_BABYSTEPPING)
122+
if (has_steps()) stepper.initiateBabystepping();
123+
#endif
120124
}
121125

122126
#endif // BABYSTEPPING

Marlin/src/feature/babystep.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323

2424
#include "../inc/MarlinConfigPre.h"
2525

26+
#if ENABLED(INTEGRATED_BABYSTEPPING)
27+
#define BABYSTEPS_PER_SEC 1000UL
28+
#define BABYSTEP_TICKS ((STEPPER_TIMER_RATE) / (BABYSTEPS_PER_SEC))
29+
#else
30+
#define BABYSTEPS_PER_SEC 976UL
31+
#define BABYSTEP_TICKS ((TEMP_TIMER_RATE) / (BABYSTEPS_PER_SEC))
32+
#endif
33+
2634
#if IS_CORE || EITHER(BABYSTEP_XY, I2C_POSITION_ENCODERS)
2735
#define BS_TODO_AXIS(A) A
2836
#else
@@ -56,8 +64,12 @@ class Babystep {
5664
static void add_steps(const AxisEnum axis, const int16_t distance);
5765
static void add_mm(const AxisEnum axis, const float &mm);
5866

67+
static inline bool has_steps() {
68+
return steps[BS_TODO_AXIS(X_AXIS)] || steps[BS_TODO_AXIS(Y_AXIS)] || steps[BS_TODO_AXIS(Z_AXIS)];
69+
}
70+
5971
//
60-
// Called by the Temperature ISR to
72+
// Called by the Temperature or Stepper ISR to
6173
// apply accumulated babysteps to the axes.
6274
//
6375
static inline void task() {

Marlin/src/module/stepper.cpp

+42-3
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ uint32_t Stepper::advance_divisor = 0,
217217

218218
#endif // LIN_ADVANCE
219219

220+
#if ENABLED(INTEGRATED_BABYSTEPPING)
221+
uint32_t Stepper::nextBabystepISR = BABYSTEP_NEVER;
222+
#endif
223+
220224
int32_t Stepper::ticks_nominal = -1;
221225
#if DISABLED(S_CURVE_ACCELERATION)
222226
uint32_t Stepper::acc_step_rate; // needed for deceleration start point
@@ -1358,16 +1362,32 @@ void Stepper::isr() {
13581362
if (!nextAdvanceISR) nextAdvanceISR = advance_isr(); // 0 = Do Linear Advance E Stepper pulses
13591363
#endif
13601364

1365+
#if ENABLED(INTEGRATED_BABYSTEPPING)
1366+
const bool do_babystep = (nextBabystepISR == 0); // 0 = Do Babystepping (XY)Z pulses
1367+
if (do_babystep) nextBabystepISR = babystepping_isr();
1368+
#endif
1369+
13611370
// ^== Time critical. NOTHING besides pulse generation should be above here!!!
13621371

13631372
if (!nextMainISR) nextMainISR = block_phase_isr(); // Manage acc/deceleration, get next block
13641373

1374+
#if ENABLED(INTEGRATED_BABYSTEPPING)
1375+
if (do_babystep) // Avoid ANY stepping too soon after baby-stepping
1376+
NOLESS(nextMainISR, (BABYSTEP_TICKS) / 8) // FULL STOP for 125µs after a baby-step
1377+
1378+
if (nextBabystepISR != BABYSTEP_NEVER) // Avoid baby-stepping too close to axis Stepping
1379+
NOLESS(nextBabystepISR, nextMainISR / 2) // TODO: Only look at axes enabled for baby-stepping
1380+
#endif
1381+
13651382
// Get the interval to the next ISR call
13661383
const uint32_t interval = _MIN(
1367-
nextMainISR // Time until the next Stepper ISR
1384+
nextMainISR // Time until the next Pulse / Block phase
13681385
#if ENABLED(LIN_ADVANCE)
13691386
, nextAdvanceISR // Come back early for Linear Advance?
13701387
#endif
1388+
#if ENABLED(INTEGRATED_BABYSTEPPING)
1389+
, nextBabystepISR // Come back early for Babystepping?
1390+
#endif
13711391
, uint32_t(HAL_TIMER_TYPE_MAX) // Come back in a very long time
13721392
);
13731393

@@ -1384,6 +1404,10 @@ void Stepper::isr() {
13841404
if (nextAdvanceISR != LA_ADV_NEVER) nextAdvanceISR -= interval;
13851405
#endif
13861406

1407+
#if ENABLED(INTEGRATED_BABYSTEPPING)
1408+
if (nextBabystepISR != BABYSTEP_NEVER) nextBabystepISR -= interval;
1409+
#endif
1410+
13871411
/**
13881412
* This needs to avoid a race-condition caused by interleaving
13891413
* of interrupts required by both the LA and Stepper algorithms.
@@ -2043,6 +2067,16 @@ uint32_t Stepper::block_phase_isr() {
20432067

20442068
#endif // LIN_ADVANCE
20452069

2070+
#if ENABLED(INTEGRATED_BABYSTEPPING)
2071+
2072+
// Timer interrupt for baby-stepping
2073+
uint32_t Stepper::babystepping_isr() {
2074+
babystep.task();
2075+
return babystep.has_steps() ? BABYSTEP_TICKS : BABYSTEP_NEVER;
2076+
}
2077+
2078+
#endif
2079+
20462080
// Check if the given block is busy or not - Must not be called from ISR contexts
20472081
// The current_block could change in the middle of the read by an Stepper ISR, so
20482082
// we must explicitly prevent that!
@@ -2511,7 +2545,10 @@ void Stepper::report_positions() {
25112545
// MUST ONLY BE CALLED BY AN ISR,
25122546
// No other ISR should ever interrupt this!
25132547
void Stepper::babystep(const AxisEnum axis, const bool direction) {
2514-
cli();
2548+
2549+
#if DISABLED(INTEGRATED_BABYSTEPPING)
2550+
cli();
2551+
#endif
25152552

25162553
switch (axis) {
25172554

@@ -2594,7 +2631,9 @@ void Stepper::report_positions() {
25942631
default: break;
25952632
}
25962633

2597-
sei();
2634+
#if DISABLED(INTEGRATED_BABYSTEPPING)
2635+
sei();
2636+
#endif
25982637
}
25992638

26002639
#endif // BABYSTEPPING

Marlin/src/module/stepper.h

+16
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ class Stepper {
329329
static bool LA_use_advance_lead;
330330
#endif
331331

332+
#if ENABLED(INTEGRATED_BABYSTEPPING)
333+
static constexpr uint32_t BABYSTEP_NEVER = 0xFFFFFFFF;
334+
static uint32_t nextBabystepISR;
335+
#endif
336+
332337
static int32_t ticks_nominal;
333338
#if DISABLED(S_CURVE_ACCELERATION)
334339
static uint32_t acc_step_rate; // needed for deceleration start point
@@ -383,6 +388,17 @@ class Stepper {
383388
FORCE_INLINE static void initiateLA() { nextAdvanceISR = 0; }
384389
#endif
385390

391+
#if ENABLED(INTEGRATED_BABYSTEPPING)
392+
// The Babystepping ISR phase
393+
static uint32_t babystepping_isr();
394+
FORCE_INLINE static void initiateBabystepping() {
395+
if (nextBabystepISR == BABYSTEP_NEVER) {
396+
nextBabystepISR = 0;
397+
wake_up();
398+
}
399+
}
400+
#endif
401+
386402
// Check if the given block is busy or not - Must not be called from ISR contexts
387403
static bool is_block_busy(const block_t* const block);
388404

Marlin/src/module/temperature.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
#include "stepper.h"
7070
#endif
7171

72-
#if ENABLED(BABYSTEPPING)
72+
#if ENABLED(BABYSTEPPING) && DISABLED(INTEGRATED_BABYSTEPPING)
7373
#include "../feature/babystep.h"
7474
#endif
7575

@@ -3010,7 +3010,7 @@ void Temperature::tick() {
30103010
// Additional ~1KHz Tasks
30113011
//
30123012

3013-
#if ENABLED(BABYSTEPPING)
3013+
#if ENABLED(BABYSTEPPING) && DISABLED(INTEGRATED_BABYSTEPPING)
30143014
babystep.task();
30153015
#endif
30163016

0 commit comments

Comments
 (0)