Skip to content

Changing $10x steps/mm at a non-zero position corrupts the parser position — next move travels the wrong distance #1006

Open

Description

@holla2040

Summary

Changing $100/$101/$102 (steps/mm) while the machine is at a non-zero position leaves gc_state.position (the parser/planner position) inconsistent with sys.position and the reported MPos. The next move is planned from the corrupted value and travels the wrong distance — the error grows with distance from machine zero.

sync_position() is called before the new steps_per_mm is installed, so it converts the freshly rescaled step count using the old calibration.

Affected code

settings.c, set_axis_setting() — current master:

case Setting_AxisStepsPerMM:
    if((status = validate_pulse_width(settings.axis[idx].max_rate, value, settings.steppers.pulse_microseconds)) == Status_OK) {
        if(settings.axis[idx].steps_per_mm > 0.0f && settings.axis[idx].steps_per_mm != value) {
            float comp = value / settings.axis[idx].steps_per_mm;
            sys.position[idx] *= comp;
            sys.home_position[idx] *= comp;
            sys.probe_position[idx] *= comp;
            sys.tlo_reference[idx] *= comp;
            sync_position();                        // <-- still the OLD steps_per_mm
        }
        settings.axis[idx].steps_per_mm = value;    // <-- installed only here
    }
    break;

sync_position() (gcode.h) expands to plan_sync_position(); system_convert_array_steps_to_mpos(gc_state.position, sys.position), and system_convert_array_steps_to_mpos() (system.c) does:

position[idx] = steps[idx] / settings.axis[idx].steps_per_mm;

plan_sync_position() only memcpys the step array, so it is unaffected by the ordering — the problem is entirely in the mm conversion.

Why the result is wrong

Let S₀ = old steps/mm, Sₙ = new steps/mm, and P = the true step position.

Scaling sys.position by comp = Sₙ/S₀ is clearly deliberate: it keeps MPos unchanged across a recalibration, since MPos = (P·Sₙ/S₀)/Sₙ = P/S₀. That part works.

But sync_position() runs one line too early, so it computes

gc_state.position = (P·Sₙ/S₀)/S₀ = MPos_before · (Sₙ/S₀)

instead of MPos_before. sys.position and the reported MPos are correct; the parser position is off by a factor of Sₙ/S₀.

A subsequent G91 move of d mm therefore changes MPos by

MPos_before · (1 − Sₙ/S₀) + d

instead of d. At machine zero the term vanishes, which is why this is easy to miss.

Reproduction

No special hardware needed — it is visible purely in grblHAL's own reported position. On a 2000 steps/mm axis:

(move to Y = 8.000)
?           -> <Idle|MPos:0.000,8.000,...>
$101=1000   -> ok
?           -> <Idle|MPos:0.000,8.000,...>     (MPos correctly unchanged)
$J=G91 G21 Y-3.000 F1000
?           -> <Idle|MPos:0.000,1.000,...>     (moved 7 mm, not 3 mm)

Predicted by the formula: 8·(1 − 1000/2000) + (−3) = −7.

Measured on my bench (SAME5x driver, ClearPath servos): 7000 steps were emitted, which at the motor's true 2000 steps/mm is 3.5 mm of physical rotation for a 3 mm command. In a longer randomized test the same defect produced 25,227 steps ≈ 12.6 mm of rotation for a nominal 5.903 mm jog from MPos.Y = 38.648.

On a machine with real travel and no soft limits armed, a large enough MPos_before can drive an axis into a hard stop.

Suggested fix

Install the new value before converting, keeping the existing behaviour for the first-set / unchanged-value path:

         case Setting_AxisStepsPerMM:
             if((status = validate_pulse_width(settings.axis[idx].max_rate, value, settings.steppers.pulse_microseconds)) == Status_OK) {
                 if(settings.axis[idx].steps_per_mm > 0.0f && settings.axis[idx].steps_per_mm != value) {
                     float comp = value / settings.axis[idx].steps_per_mm;
                     sys.position[idx] *= comp;
                     sys.home_position[idx] *= comp;
                     sys.probe_position[idx] *= comp;
                     sys.tlo_reference[idx] *= comp;
+                    settings.axis[idx].steps_per_mm = value;   // sync_position() reads this
                     sync_position();
-                }
-                settings.axis[idx].steps_per_mm = value;
+                } else
+                    settings.axis[idx].steps_per_mm = value;
             }
             break;

I'm happy to open a PR if the approach looks right.

While here

sync_position() in gcode.h is an unbraced two-statement macro:

#define sync_position() plan_sync_position(); system_convert_array_steps_to_mpos (gc_state.position, sys.position)

so if(cond) sync_position(); guards only the first statement. All current call sites appear to be plain statements, so nothing is broken today, but wrapping it in do { ... } while(0) would remove the trap.

Environment

  • grblHAL core: current master (verified against the settings.c, gcode.h, system.c on master at time of filing); also reproduces on the pinned 306bf68d, build 20260726.
  • Driver: SAME5x / Teknic ClearCore (out-of-tree), 4 axes, no kinematics (KINEMATICS_API not defined).
  • $20=0, unhomed, no limit switches fitted.

The defect is in core settings.c and independent of the driver — any driver without KINEMATICS_API should show it.

Activity

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