Skip to content

Feed hold: resumed block re-initialises its Bresenham counters, a minor axis can lose one step #1023

Open

Description

@yiivon

Core: master d7aaee3 (build 20260923); stepper.c on master today is identical.

What happens

When a feed hold stops a block midway and the cycle is resumed, the stepper ISR treats the rest of the block as a new block and re-initialises the Bresenham counters to step_event_count / 2. If the minor axis happened to stop exactly on a Bresenham tie (its counter half a step from its next step), that step is never produced: the block ends one step short on that axis, and the machine stays one step off for the rest of the job (MPos keeps counting the steps that were commanded, so it shows the true position; the error is between the two).

Cause (stepper.c, master):

  • line 576, segment buffer empty: st.exec_block = NULL; — st.exec_block doubles as the "stepping active" flag (line 459 if(st.exec_block)), and st_wake_up() zeroes st.dir_out relying on the new-block branch to set the direction outputs again, so it cannot simply stay set;
  • line 491: if(st.exec_block != st.exec_segment->exec_block) — after the hold this compares against NULL, so the first segment of the resumed block runs the new-block code, including the counter reset at line 528.

grbl 1.1 compared block indices here and kept the counters (its comment: "The stepper path should continue exactly as if nothing has happened").

Reproduction

Deterministic, in a simulator that runs the unmodified core (a 16 MHz emulated MCU, MC-1 digital twin, 800 steps/mm). A move with an X:Y step ratio of 2:1 (G53 G1 X-90 Y-95 F600 from X-100 Y-100), a feed hold at 40 different moments during the move, resume at Hold:0:

  • 23 of the 40 holds end the move with Y one step short (−1.25 µm); the other 17 end exactly. With a "generic" ratio (X10 Y7.3219) none of 40 holds loses a step, as the tie is rarely hit.
  • Property test over random programs with 1–4 feed holds each: about 1 case in 10 ends one step off on a minor axis (never more than one step per hold per axis).

On a real machine the effect is one step per hold at most — small, but it is a systematic error that the planner's position does not know about.

Proposed fix

Keep clearing st.exec_block, remember the block that was executing when the buffer ran empty, and skip the counter reset when the next segment belongs to that same block:

static st_block_t *st_resume_block = NULL; // block that was executing when the segment buffer ran empty
...
                if(st.exec_block != st_resume_block)      // not a block resumed after a feed hold
                st.counter.x = st.counter.y = st.counter.z ... = st.step_event_count >> 1;
                st_resume_block = NULL;
...
            cycles_per_tick = 0;
            st_resume_block = st.exec_block;
            st.exec_block = NULL;
...
    memset(&st, 0, sizeof(stepper_t));   // st_reset()
    st_resume_block = NULL;

Verified in the simulator: the 2:1 sweep ends on the same step as without a hold in 40 of 40 cases, the random-program property passes 60 of 60, all other regression tests unchanged. I have not run it on hardware.

Patch against master (stepper.c):

--- a/stepper.c
+++ b/stepper.c
@@ -92,6 +92,7 @@
 // main program. Pointers may be planning segments or planner blocks ahead of what being executed.
 static plan_block_t *pl_block;     // Pointer to the planner block being prepped
 static st_block_t *st_prep_block;  // Pointer to the stepper block data being prepped
+static st_block_t *st_resume_block = NULL; // Block that was executing when the segment buffer ran empty
 static st_block_t st_hold_block;   // Copy of stepper block data for block put on hold during parking
 
 // Segment preparation data struct. Contains all the necessary information to compute new segments
@@ -523,7 +524,9 @@
                     st.exec_block->message = NULL;
                 }
 
-                // Initialize Bresenham line and distance counters
+                // Initialize Bresenham line and distance counters,
+                // unless the block is resumed after the segment buffer ran empty (feed hold)
+                if(st.exec_block != st_resume_block)
                 st.counter.x = st.counter.y = st.counter.z
                 #ifdef A_AXIS
                   = st.counter.a
@@ -544,6 +547,7 @@
                   = st.counter.w
                 #endif
                   = st.step_event_count >> 1;
+                st_resume_block = NULL;
             }
 
 #if MAX_AMASS_LEVEL > 1
@@ -573,6 +577,7 @@
             }
 
             cycles_per_tick = 0;
+            st_resume_block = st.exec_block;
             st.exec_block = NULL;
             system_set_exec_state_flag(EXEC_CYCLE_COMPLETE); // Flag main program for cycle complete
 
@@ -753,6 +758,7 @@
 
     memset(&prep, 0, sizeof(prep));
     memset(&st, 0, sizeof(stepper_t));
+    st_resume_block = NULL;
 
 #if MAX_AMASS_LEVEL > 1
     // amass.level[0]: Normal operation. No AMASS. No upper cutoff frequency. Starts at amass.level[1] cutoff frequency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions