3dSynth

Tutorials / Custom G-code

Open in the interactive tutorials reader

---
title: Tutorial 25 Machine State as a Model
description: Thinking like the firmware.
---

Tutorial 25 Machine State as a Model

1. Lesson Header

2. Concept Introduction

The Printer is a State Machine.
G-code is Modal. This means a command changes the state of the machine, and that state persists until changed again.
If you send G1 F3000, every subsequent move will be at 3000mm/min until you send a new F command.
The "State" includes:
- Current Position (X, Y, Z, E)
- Coordinate Mode (G90/G91)
- Extrusion Mode (M82/M83)
- Units (G20/G21)
- Temperature (M104/M140)
- Fan Speed (M106)
- Feedrate (F)

3. Machine State Explanation

The "Context" Problem.
When you paste a snippet of G-code into the middle of a file, it inherits the state of the previous lines.
If the previous line was G91 (Relative) and your snippet assumes G90 (Absolute), your printer will crash.

Safe State Initialization:
Always start a block with "Safety Codes":
G21 (mm), G90 (Abs), M83 (Rel Extrusion).

4. Command Breakdown

5. Minimal Working Example

The Modal Failure.

; Setup
G1 F1000
G1 X10

; Block 1
G1 X20 ; Moves at F1000

; Block 2 (Fast Travel)
G0 X0 F9000 ; Changes F to 9000

; Block 3 (Print)
G1 X10 E1 ; OOPS! This moves at F9000!
; The extruder can't keep up. The filament strips.

6. Visual Representation

Interactive preview is available in the interactive reader.

7. Build Exercise

Task: Debug the "Mystery File".
I will provide a broken G-code snippet. You must identify why it fails based on missing state commands.

Snippet:

G1 X10 Y10 E1
G1 X20 Y10 E1
G1 X20 Y20 E1
G1 X10 Y20 E1

Scenario: The user ran a "Retraction Test" before this that left the Extruder in Absolute Mode (M82) and the Position in Relative Mode (G91).
Result:
- G1 X10... (Relative) -> Moves 10mm away from current spot (Maybe off bed!).
- E1 (Absolute) -> Extruder tries to go to position "1". If it was at "100", it retracts 99mm!

Fix: Add G90 and M83 at the top.

8. Deep Insight Section

G92 (Set Position).
You can force a state change without moving.
G92 E0 tells the printer "Pretend the Extruder is at 0 right now."
This is crucial for Absolute Extrusion (M82) to prevent the E value from growing to infinity (float precision errors).

9. Common Failure Modes

  1. The "Crazy Extruder": Printing a file sliced for Absolute Extrusion (M82) on a machine set to Relative (M83). The extruder spins wildly trying to reach "E5000".
  2. The "Z-Crash": A script assumes Z is at 0, but the previous print ended at Z=200.

10. Real-World Application

Tool Change Scripts.
When switching from Tool 0 to Tool 1, you must save the state of T0 (Position, Temp), switch, and then restore.
Firmware usually handles offsets, but you must handle the logic (e.g., "Don't hit the clamp while switching").

11. Final Clean Version

The "Safe Block" Template.
Use this wrapper for any manual G-code.

; --- SAFE START ---
G21 ; Millimeters
G90 ; Absolute Positioning
M83 ; Relative Extrusion
G92 E0 ; Reset Extruder to 0
; ------------------

; Your Code Here
G1 F1000
G1 X10 Y10 Z0.2 E1

; --- SAFE END ---
G92 E0 ; Reset E again just in case
; ----------------

12. Stretch Challenge

Challenge: Write a G-code file that swaps modes intentionally to create a glitch art effect.
1. Move X in Absolute.
2. Move Y in Relative.
3. Move E in Absolute.
4. Repeat.
This creates a "drift" where the pattern walks across the bed.