Skip to content

Error checking changes state: a refused G93/G94/G95 block zeroes gc_state.feed_rate #1027

Open

Description

@yiivon

Core: d7aaee3 (build 20260923); the same code on current master 6902052.

What happens

gc_execute_block() writes to the parser state during error checking, in step 2 "Set feed rate mode"
(gcode.c:2386):

// Switching to G94 or G95 from G93, so don't push last state feed rate.
if(gc_block.modal.feed_mode != gc_state.modal.feed_mode)
    gc_state.feed_rate = 0.0f;

This runs before the checks that can still refuse the block — the missing F word in G93 right below it, and
everything after. When the block is refused, gc_state.modal.feed_mode is unchanged but the feed rate is gone.
The comment at the top of the function says the opposite should hold: the parser block "will only be used and
executed after successful error-checking".

Consequence: after a refused G93 G1 X2 (error 22, F word missing), a sender that clears the error and goes on
finds the earlier F undefined: the next G1/G2/G81 without an F word is refused with error 22 as well, although
the program set F before and never left G94. Check mode ($C) shows it most: a sender that lists every error of a
program reports a run of "feed rate undefined" errors after the one real mistake. The same holds for any refused
block that switches G94 ↔ G95 ↔ G93.

Reproduction

In check mode (or any mode), clearing each error with an empty line; the same on the Simulator (91eda77 with #25,
core d7aaee3):

$C                  ok
G21 G90 G94         ok
G1 X1 F100          ok
G93 G1 X2           error:22 (correct: G93 needs F in the block)
                    ok (clears the error)
G94                 ok
G2 X10 Y0 I0 J0     error:22 - expected error:33 (the full circle with I0 J0 is the real problem)
                    ok
G1 X3               error:22 - expected ok: F100 was never replaced

Without the refused G93 G1 X2 line the same session gives error:33 and ok for the last two lines.

Found by comparing, line by line, a check run that carries on after each error with one where each refused line is
removed and the program checked again (grblHAL_validator from the Simulator repository and an emulator running the
unmodified core agree on the latter: 42 refused lines in 4 programs, each also with CR LF line ends, all the same but an M4 the validator's null spindle cannot run; the two runs differ only after the refused
G93 block).

Proposed fix

Leave gc_state alone; only the value pushed into the block depends on the mode change. For accepted blocks the
result is the same (gc_state.feed_rate = gc_block.values.f at execution, step 3):

-        // Switching to G94 or G95 from G93, so don't push last state feed rate.
-        if(gc_block.modal.feed_mode != gc_state.modal.feed_mode)
-            gc_state.feed_rate = 0.0f;
+        // Switching between G93, G94 and G95: don't push the last state feed rate, it is undefined in the new mode.
+        // Nothing in gc_state changes here - the block may still be refused below.
+        bool feed_mode_changed = gc_block.modal.feed_mode != gc_state.modal.feed_mode;
 ...
-        } else if(!gc_block.words.f)
+        } else if(!gc_block.words.f && !feed_mode_changed)
             gc_block.values.f = gc_state.feed_rate; // Push last state feed rate

With it, the carry-on run and the remove-and-recheck run agree on every line of the same programs, and the
emulator's motion property tests (planner, holds, soft limits, homing) are unchanged.

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