Skip to content

Conversation

ThomasToka
Copy link
Contributor

@ThomasToka ThomasToka commented Jul 28, 2025

Description

This PR introduces two powerful runtime features for bed leveling in Marlin:

  • DYNAMIC_MARGINS — Enables live configuration of the bed probing area, dynamically adapted to probe offsets and machine constraints.
  • DYNAMIC_TRAMMING — Automatically generates assisted tramming points based on the current margins and probe offset, removing the need for static configuration or recompilation.

These features improve flexibility, safety, and usability for leveling and tramming — especially on machines with limited probe reach or off-center probes.


  • Runtime control of probing area via M421:

    • Example: M421 L10 R10 F15 B20 sets probing margins directly. Keep L R the same if you want a centered mesh.
  • Auto-correcting margin enforcement:

    • Each value provided to M421 is validated and adjusted upward if it is smaller than the minimum safe margin required by the probe offset and bed geometry.
    • For example:
      • If the minimum front margin is 25 mm due to probe offset, then both M421 F0 and M421 F15 will result in F25.
    • Ensures that the probe will never be commanded outside reachable areas. A minimum margin of 10 mm is always enforced, or higher if required based on the user’s input and physical limits.
    • M421 L0 R0 F0 B0 triggers full auto-recalculation of all margins based on current probe offsets and usable bed area.
  • Optional margin reset:

    • M421 Dresets the margins to their defaults (PROBING_MARGIN_LEFT,PROBING_MARGIN_RIGHT,PROBING_MARGIN_FRONT, PROBING_MARGIN_BACK)
  • Fully integrated into:

    • Unified Bed Leveling (UBL): mesh probing, interpolation, and display.
    • Bilinear Leveling: mesh creation, probing boundaries, and M420 V output.
  • Safe and dynamic mesh access:

    • All probing and interpolation logic clamps access to within valid bounds.
    • Prevents out-of-bounds errors or unreachable probe moves.
    • supports positive and negative probe offsets
    • respects HOME_OFFSETS if enabled
  • Enables automatic generation of assisted tramming points (G35) based on the current margins.

  • Removes the need to statically define TRAMMING_POINT_XY when DYNAMIC_MARGINS is enabled.

  • Reacts in real time to updated margins — no rebuild required.

  • Works seamlessly with ASSISTED_TRAMMING.

  • Adds feature flags:

    • DYNAMIC_MARGINS
    • DYNAMIC_TRAMMING
  • Saves and loads dynamic margin configuration from/to EEPROM.

  • Integrates margin and tramming state into persistent printer settings.

  • Modified to retrieve margin-aware tramming points if DYNAMIC_TRAMMING is active.

  • Assisted tramming logic now generates points from current margins if DYNAMIC_TRAMMING is enabled.

  • Fallbacks to static TRAMMING_POINT_XY only when dynamic margins are disabled.

  • includes M421_report if DYNAMIC_MARGINS are enabled.

  • Configuration_adv.h: Adds feature flags DYNAMIC_MARGINS, DYNAMIC_TRAMMING.

  • UBL:

    • ubl.cpp, ubl.h, ubl_g29.cpp, ubl_motion.cpp: Updated to support margin-aware mesh logic and probing bounds.
  • Bilinear:

    • bbl.cpp, bbl.h: Respect dynamic margins during mesh generation, probing, and display.
  • Tramming:

    • Assisted tramming points are calculated dynamically from current margins.

M421 L15 R15 F10 B10  ; Manually define all probing margins (reaccounting if to low)
or
M421 L15 R15 ; Manually set L R conditionally.
or
M421 L0 R0 F0 B0      ; Auto-calculate margins from probe offset and bed size

If the requested margin is too small (e.g., F0), the system will automatically enforce a larger minimum, such as F25, depending on the probe offset and physical constraints of the printer.


start
PowerUp
Marlin bugfix-2.1.x
echo: Last Updated: 2025-06-29 | Author: (none, default config)
echo: Compiled: Jul 28 2025
echo: Free Memory: 0  PlannerBufferBytes: 1536
echo:EEPROM version mismatch (EEPROM= Marlin=V90)
echo:Hardcoded Default Settings Loaded
[7] echo:busy: processing
X:107.00 Y:107.00 Z:5.00 E:0.00 Count X:8560 Y:8560 Z:2000
ok
echo:; Linear Units:
echo:  G21 ; (mm)
echo:; Temperature Units:
echo:  M149 C ; Units in Celsius
echo:; Filament settings (Disabled):
echo:  M200 S0 D1.75
echo:; Steps per unit:
echo:  M92 X80.00 Y80.00 Z400.00 E500.00
echo:; Max feedrates (units/s):
echo:  M203 X2000.00 Y2000.00 Z5.00 E1000.00
echo:; Max Acceleration (units/s2):
echo:  M201 X3000.00 Y3000.00 Z100.00 E10000.00
echo:; Acceleration (units/s2) (P<print-accel> R<retract-accel> T<travel-accel>):
echo:  M204 P3000.00 R3000.00 T3000.00
echo:; Advanced (B<min_segment_time_us> S<min_feedrate> T<min_travel_feedrate> J<junc_dev>):
echo:  M205 B20000.00 S0.00 T0.00 J0.01
echo:; Home offset:
echo:  M206 X0.00 Y0.00 Z0.00
echo:; Auto Bed Leveling:
echo:  M420 S0 Z10.00 ; Leveling OFF
echo:; Dynamic Margins:
echo:  M421 L45.00 R45.00 F45.00 B45.00 ; Margins in mm
echo:; Material heatup parameters:
echo:  M145 S0 H180.00 B70.00 F0
echo:  M145 S1 H240.00 B110.00 F0
echo:; Hotend PID:
echo:  M301 P22.20 I1.08 D114.00
echo:; Z-Probe Offset:
echo:  M851 X10.00 Y10.00 Z0.00 ; (mm)
ok

M421 L0 R0 F0 B0

  ! L margin too small (0 < 10). L set to: 10

  ! F margin too small (0 < 10). F set to: 10

  ! R margin too small (0 < 10). R set to: 10

  ! B margin too small (0 < 10). B set to: 10

ok

Changing probe offsets to X=-31.5 Y-41.8, and recalculating min margins (X_MAX_POS=242, Y_MAX_POS=235, Bedsize 235x235):

M851 X-31.5 Y-41.8

M421 L0 R0 F0 B0

[2] ok
  ! L margin too small (0 < 10). L set to: 10

  ! F margin too small (0 < 10). F set to: 10

  ! R margin too small (0 < 25). R set to: 25

  ! B margin too small (0 < 42). B set to: 42

ok

  • ✅ Cartesian platforms (KINEMATIC not tested)
  • M421 with both manual and automatic values
  • ✅ Correct auto-correction behavior for undersized margins
  • ✅ Optional margin reset via M421 D
  • ✅ UBL and Bilinear mesh generation within validated probing zones
  • M420 V output clipped to valid mesh area
  • G35 assisted tramming following the current probing area
  • ✅ Safe operation across all margin combinations
  • ✅ Safe operation with positive and negative probe offsets
  • ✅ respects HOME_OFFSETS if enabled

Many printers have probes that are offset from the nozzle or have restricted reach. Without this feature, users had to manually adjust and recompile constants like PROBING_MARGIN or TRAMMING_POINT_XY.

These enhancements:

  • Eliminate the need to recompile when changing probing margins or tramming points.
  • Adapt automatically to the hardware layout of the printer.
  • Improve safety and usability during leveling and tramming.

This implementation is isolated behind the DYNAMIC_MARGINS and DYNAMIC_TRAMMING feature flags and maintains full backward compatibility with existing configurations. It has been tested on Cartesian platforms. Kinematic (e.g., CoreXY, Delta) support is untested and can be addressed in follow-up work.

Feedback and review are welcome!

DIFFS

Added 08/10/2025:

Edit: I think this is also one part of the functionality that is known as "adaptive mesh leveling" in Orca Slicer where the probed area can be scaled down to the print object size. Should do this job also pre print in a matching sliced gcode:

M421 L{adaptive_bed_mesh_min[10]} R{adaptive_bed_mesh_max[225]} F{adaptive_bed_mesh_min[10]} B{adaptive_bed_mesh_max[225]}

@ThomasToka
Copy link
Contributor Author

ThomasToka commented Jul 28, 2025

I will take care in a follow up of the failed build checks.

EDIT7: ok this looks how it should now. I think i am ready. force pushed and updated the diff

@ThomasToka ThomasToka force-pushed the DYNAMIC_MARGINS_DYNAMIC_TRAMMING branch 15 times, most recently from c7cfe9c to 96f68ff Compare July 29, 2025 12:41
This PR introduces two powerful runtime features for bed leveling in Marlin:

- **`DYNAMIC_MARGINS`** — Enables live configuration of the bed probing area, dynamically adapted to probe offsets and machine constraints.
- **`DYNAMIC_TRAMMING`** — Automatically generates assisted tramming points based on the current margins and probe offset, removing the need for static configuration or recompilation.

These features improve flexibility, safety, and usability for leveling and tramming — especially on machines with limited probe reach or off-center probes.

---

- **Runtime control of probing area via `M421`:**
  - Example: `M421 L10 R10 F15 B20` sets probing margins directly. Keep L R the same if you want a centered mesh.

- **Auto-correcting margin enforcement:**
  - Each value provided to `M421` is **validated and adjusted upward** if it is smaller than the minimum safe margin required by the probe offset and bed geometry.
  - For example:
    - If the minimum front margin is 25 mm due to probe offset, then both `M421 F0` and `M421 F15` will result in `F25`.
  - Ensures that the probe will **never be commanded outside reachable areas**. A **minimum margin of 10 mm** is always enforced, or higher if required based on the user’s input and physical limits.
  - `M421 L0 R0 F0 B0` triggers full auto-recalculation of all margins based on current probe offsets and usable bed area.

- **Optional margin reset:**
  - `M421 D`resets the margins to their defaults (PROBING_MARGIN_LEFT,PROBING_MARGIN_RIGHT,PROBING_MARGIN_FRONT, PROBING_MARGIN_BACK)

- **Fully integrated into:**
  - **Unified Bed Leveling (UBL)**: mesh probing, interpolation, and display.
  - **Bilinear Leveling**: mesh creation, probing boundaries, and `M420 V` output.

- **Safe and dynamic mesh access:**
  - All probing and interpolation logic clamps access to within valid bounds.
  - Prevents out-of-bounds errors or unreachable probe moves.
  - supports positive and negative probe offsets
  - respects HOME_OFFSETS if enabled

- Enables automatic generation of assisted tramming points (`G35`) based on the **current margins**.
- Removes the need to statically define `TRAMMING_POINT_XY` when `DYNAMIC_MARGINS` is enabled.
- Reacts in real time to updated margins — no rebuild required.
- Works seamlessly with `ASSISTED_TRAMMING`.

- Adds feature flags:
  - `DYNAMIC_MARGINS`
  - `DYNAMIC_TRAMMING`

- Saves and loads dynamic margin configuration from/to EEPROM.
- Integrates margin and tramming state into persistent printer settings.

- Modified to retrieve margin-aware tramming points if `DYNAMIC_TRAMMING` is active.
- Assisted tramming logic now generates points from current margins if `DYNAMIC_TRAMMING` is enabled.
- Fallbacks to static `TRAMMING_POINT_XY` only when dynamic margins are disabled.

- includes M421_report if DYNAMIC_MARGINS are enabled.

- `Configuration_adv.h`: Adds feature flags `DYNAMIC_MARGINS`, `DYNAMIC_TRAMMING`.
- **UBL**:
  - `ubl.cpp`, `ubl.h`, `ubl_g29.cpp`, `ubl_motion.cpp`: Updated to support margin-aware mesh logic and probing bounds.
- **Bilinear**:
  - `bbl.cpp`, `bbl.h`: Respect dynamic margins during mesh generation, probing, and display.
- **Tramming**:
  - Assisted tramming points are calculated dynamically from current margins.

---

```gcode
M421 L15 R15 F10 B10  ; Manually define all probing margins (reaccounting if to low)
or
M421 L15 R15 ; Manually set L R conditionally.
or
M421 L0 R0 F0 B0      ; Auto-calculate margins from probe offset and bed size
```

If the requested margin is too small (e.g., `F0`), the system will automatically **enforce a larger minimum**, such as `F25`, depending on the probe offset and physical constraints of the printer.

---

```
start
PowerUp
Marlin bugfix-2.1.x
echo: Last Updated: 2025-06-29 | Author: (none, default config)
echo: Compiled: Jul 28 2025
echo: Free Memory: 0  PlannerBufferBytes: 1536
echo:EEPROM version mismatch (EEPROM= Marlin=V90)
echo:Hardcoded Default Settings Loaded
[7] echo:busy: processing
X:107.00 Y:107.00 Z:5.00 E:0.00 Count X:8560 Y:8560 Z:2000
ok
echo:; Linear Units:
echo:  G21 ; (mm)
echo:; Temperature Units:
echo:  M149 C ; Units in Celsius
echo:; Filament settings (Disabled):
echo:  M200 S0 D1.75
echo:; Steps per unit:
echo:  M92 X80.00 Y80.00 Z400.00 E500.00
echo:; Max feedrates (units/s):
echo:  M203 X2000.00 Y2000.00 Z5.00 E1000.00
echo:; Max Acceleration (units/s2):
echo:  M201 X3000.00 Y3000.00 Z100.00 E10000.00
echo:; Acceleration (units/s2) (P<print-accel> R<retract-accel> T<travel-accel>):
echo:  M204 P3000.00 R3000.00 T3000.00
echo:; Advanced (B<min_segment_time_us> S<min_feedrate> T<min_travel_feedrate> J<junc_dev>):
echo:  M205 B20000.00 S0.00 T0.00 J0.01
echo:; Home offset:
echo:  M206 X0.00 Y0.00 Z0.00
echo:; Auto Bed Leveling:
echo:  M420 S0 Z10.00 ; Leveling OFF
echo:; Dynamic Margins:
echo:  M421 L45.00 R45.00 F45.00 B45.00 ; Margins in mm
echo:; Material heatup parameters:
echo:  M145 S0 H180.00 B70.00 F0
echo:  M145 S1 H240.00 B110.00 F0
echo:; Hotend PID:
echo:  M301 P22.20 I1.08 D114.00
echo:; Z-Probe Offset:
echo:  M851 X10.00 Y10.00 Z0.00 ; (mm)
ok

M421 L0 R0 F0 B0

  ! L margin too small (0 < 10). L set to: 10

  ! F margin too small (0 < 10). F set to: 10

  ! R margin too small (0 < 10). R set to: 10

  ! B margin too small (0 < 10). B set to: 10

ok
```

Changing probe offsets to X=-31.5 Y-41.8, and recalculating min margins (X_MAX_POS=242, Y_MAX_POS=235, Bedsize 235x235):

```
M851 X-31.5 Y-41.8

M421 L0 R0 F0 B0

[2] ok
  ! L margin too small (0 < 10). L set to: 10

  ! F margin too small (0 < 10). F set to: 10

  ! R margin too small (0 < 25). R set to: 25

  ! B margin too small (0 < 42). B set to: 42

ok

```

- ✅ Cartesian platforms (KINEMATIC not tested)
- ✅ `M421` with both manual and automatic values
- ✅ Correct auto-correction behavior for undersized margins
- ✅ Optional margin reset via `M421 D`
- ✅ UBL and Bilinear mesh generation within validated probing zones
- ✅ `M420 V` output clipped to valid mesh area
- ✅ `G35` assisted tramming following the current probing area
- ✅ Safe operation across all margin combinations
- ✅ Safe operation with positive and negative probe offsets
- ✅ respects HOME_OFFSETS if enabled

---

Many printers have probes that are offset from the nozzle or have restricted reach. Without this feature, users had to manually adjust and recompile constants like `PROBING_MARGIN` or `TRAMMING_POINT_XY`.

These enhancements:
- Eliminate the need to recompile when changing probing margins or tramming points.
- Adapt automatically to the hardware layout of the printer.
- Improve safety and usability during leveling and tramming.

---

This implementation is isolated behind the `DYNAMIC_MARGINS` and `DYNAMIC_TRAMMING` feature flags and maintains full backward compatibility with existing configurations. It has been tested on **Cartesian platforms**. Kinematic (e.g., CoreXY, Delta) support is untested and can be addressed in follow-up work.

Feedback and review are welcome!

in the PR
@ThomasToka ThomasToka force-pushed the DYNAMIC_MARGINS_DYNAMIC_TRAMMING branch from 96f68ff to 46c5060 Compare July 29, 2025 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant