3dSynth

Tutorials / Custom G-code

Open in the interactive tutorials reader

---
title: Tutorial 16 Parametric Thinking (Manual)
description: Writing code that can be reused anywhere. The "Stamp" concept.
---

Tutorial 16 Parametric Thinking (Manual)

1. Lesson Header

2. Concept Introduction

Hard-coded vs. Portable.

Up until now, we have largely used Absolute Positioning (G90).
- G1 X10 Y10 -> "Go to the specific coordinate 10,10."

This is great for precision, but terrible for reuse. If you want to print that same square at X50 Y50, you have to recalculate every single coordinate and rewrite the lines.

Parametric Thinking is the shift from "Where am I?" to "What am I doing?"
Instead of defining a square by its corners (10,10, 20,10, ...), we define it by its actions:
"Move Right 10, Move Back 10, Move Left 10, Move Front 10."

If you define the shape using Relative Positioning (G91), the code becomes a "Stamp". You can move the nozzle to any starting location (using G90), and then "stamp" the shape (using G91).

3. Machine State Explanation

Context Switching.

To be effective, we must switch modes frequently.
1. Transport Mode (G90): Move the machine to the start position of the feature.
2. Local Mode (G91): Execute the feature relative to that start position.
3. Restoration: Return to G90 so the next transport move is predictable.

4. Command Breakdown

Review:
- G90: Absolute. The grid is the bed.
- G91: Relative. The grid is "Here".

The "Subroutine" Pattern (Manual):
Since standard G-code doesn't have "Functions" (like Python or C++), we simulate them by writing a block of code that we can copy-paste.

5. Minimal Working Example

The "Box Stamp"

; --- SETUP ---
G28
G1 Z0.3

; --- POSITION 1 ---
G90             ; Absolute
G0 X20 Y20      ; Transport to Spot A

; --- THE STAMP (Copy this block) ---
G91             ; Switch to Relative
G1 X10 E1       ; Draw Right
G1 Y10 E1       ; Draw Back
G1 X-10 E1      ; Draw Left
G1 Y-10 E1      ; Draw Front (Return to start)
G90             ; Safety: Back to Absolute
; -----------------------------------

; --- POSITION 2 ---
G0 X60 Y60      ; Transport to Spot B

; --- THE STAMP (Paste) ---
G91
G1 X10 E1
G1 Y10 E1
G1 X-10 E1
G1 Y-10 E1
G90
; -------------------------

Notice the code inside "THE STAMP" is identical. We didn't calculate X70 or Y70.

6. Visual Representation

Interactive preview is available in the interactive reader.

The "Shape" is a portable object.

7. Build Exercise

Task: Create a "Cross" (+) stamp.
- Size: 10mm arms.
- Print it at three locations: (30,30), (80,30), and (55, 80).

The Cross Logic (Relative):
Start at center.
1. Up 5, Down 10, Up 5 (Vertical bar).
2. Left 5, Right 10, Left 5 (Horizontal bar).

...

Solution:

; Setup
G28
G1 Z0.3

; --- Spot 1 ---
G90
G0 X30 Y30
; [STAMP START]
G91
G1 Y5 E0.5   ; Up
G1 Y-10 E1   ; Down (Full length)
G1 Y5 E0.5   ; Back to center
G1 X-5 E0.5  ; Left
G1 X10 E1    ; Right (Full length)
G1 X-5 E0.5  ; Back to center
G90
; [STAMP END]

; --- Spot 2 ---
G90
G0 X80 Y30
; [PASTE STAMP HERE]
G91
G1 Y5 E0.5
G1 Y-10 E1
G1 Y5 E0.5
G1 X-5 E0.5
G1 X10 E1
G1 X-5 E0.5
G90

; --- Spot 3 ---
G90
G0 X55 Y80
; [PASTE STAMP HERE]
...

8. Deep Insight Section

Drift and Accumulation Error.
The danger of G91 is that if you don't end exactly where you started, the next move will be offset.
In the Cross example, we went Y+5, Y-10, Y+5. Net change: 0.
If we had typed Y+5.1 by mistake, we would end up 0.1mm off center.
If we paste that stamp 100 times, the 100th cross would be shifted by 10mm!

Best Practice: Always end your relative block by returning to 0,0 (relative to start). Or, issue a G90 command immediately after the block to "re-anchor" the machine to the absolute grid.

9. Common Failure Modes

  1. Forgetting G90: If you finish your relative stamp and try to move to the next position G0 X100 without switching back to G90, the machine will try to move 100mm away from where it is, likely crashing into the frame.
  2. E-Axis Confusion: If you use M83 (Relative Extrusion), you don't need to worry about E. If you use M82 (Absolute Extrusion), G91 might affect E depending on firmware settings. Always use M83 when doing manual parametric work.

10. Real-World Application

Step-and-Repeat.
Industrial manufacturing uses this constantly. A "Pallet" of parts is just one part program repeated with a coordinate offset.
In G-code, we can use G54, G55 (Work Offsets) to handle this professionally, but G91 is the quick-and-dirty way to do it on any 3D printer.

11. Final Clean Version

Save this as lesson16.gcode.

; Lesson 16 - The Grid of Crosses
G21
G90
M83 ; Relative Extrusion is CRITICAL here
G28
G1 Z0.3

; --- Definition of the Stamp (Mental) ---
; Center -> Up 5 -> Down 10 -> Center -> Left 5 -> Right 10 -> Center

; --- Position 1 (Bottom Left) ---
G90
G0 X40 Y40
G91 ; Begin Relative
G1 Y5 E0.5
G1 Y-10 E1
G1 Y5 E0.5
G1 X-5 E0.5
G1 X10 E1
G1 X-5 E0.5
G90 ; End Relative

; --- Position 2 (Bottom Right) ---
G90
G0 X80 Y40
G91 ; Begin Relative
G1 Y5 E0.5
G1 Y-10 E1
G1 Y5 E0.5
G1 X-5 E0.5
G1 X10 E1
G1 X-5 E0.5
G90 ; End Relative

; --- Position 3 (Top Center) ---
G90
G0 X60 Y80
G91 ; Begin Relative
G1 Y5 E0.5
G1 Y-10 E1
G1 Y5 E0.5
G1 X-5 E0.5
G1 X10 E1
G1 X-5 E0.5
G90 ; End Relative

G28 X0 Y0

12. Stretch Challenge

Challenge: Create a "Nested Box" stamp.
A 20mm box with a 10mm box inside it.
Paste it at X50 Y50.

Answer Key:

G90
G0 X50 Y50
G91
; Outer Box (20x20)
G1 X10 E1   ; Right
G1 Y10 E1   ; Back
G1 X-20 E2  ; Left (Full width)
G1 Y-20 E2  ; Front (Full width)
G1 X20 E2   ; Right (Full width)
G1 Y10 E1   ; Back to Middle-Right edge... wait, this is getting complex.
; Simpler: Start at Bottom-Left corner relative to center
G0 X-10 Y-10 ; Move to corner
G1 X20 E2
G1 Y20 E2
G1 X-20 E2
G1 Y-20 E2
; Inner Box
G0 X5 Y5 ; Move in
G1 X10 E1
G1 Y10 E1
G1 X-10 E1
G1 Y-10 E1
G90