Parametric Programming in CNC: Using Variables, Expressions, and Logic
Parametric programming transforms your CNC code into a smart, adaptive system. Instead of hardcoding values, you use variables, formulas, and logic to write flexible programs that adapt to part sizes, repeat patterns, and even make decisions.
Whether you’re optimizing part families or building automated macros, mastering parametric G-code will elevate your CNC skills to a new level.
🔣 What Is Parametric Programming?
It allows you to:
- Replace fixed numbers with variables
- Perform math operations and logic conditions
- Create loops, branches, and custom cycles
- Reuse code dynamically for different inputs
🔢 Basic Variable Syntax
| Syntax | Description | Example |
|---|---|---|
#100 | Local variable | #100 = 25 |
#500–#999 | Common variables (persist) | #500 = 10 |
#<_name> | System variables (read-only or special) | #<_X> |
#[...] | Indirect address | #[#100] = 5 |
🧮 Arithmetic Operators
#101 = 10 + 5 ; Addition → #101 = 15
#102 = 20 - 4 ; Subtraction → #102 = 16
#103 = 3 * 4 ; Multiplication → #103 = 12
#104 = 16 / 4 ; Division → #104 = 4
#105 = 7 MOD 3 ; Remainder → #105 = 1
🔍 Built-in Math Functions
| Function | Description |
|---|---|
ABS[#] | Absolute value |
SQRT[#] | Square root |
SIN[#], COS[#] | Trigonometric functions |
ROUND[#] | Round to nearest integer |
FIX[#] | Truncate toward 0 |
🧠 Conditional Logic
IF [#100 GT 10] THEN #101 = 1
IF [#100 EQ 20] GOTO 500
| Operator | Meaning |
|---|---|
EQ | Equal to |
NE | Not equal |
GT | Greater than |
LT | Less than |
GE | ≥ |
LE | ≤ |
🔁 Loops & Repeats
#101 = 0
WHILE [#101 LT 5] DO1
G81 X[#101*10] Y0 Z-10 R2 F100
#101 = #101 + 1
END1
- Repeats the drilling cycle at X=0,10,20,30,40
📦 Practical Example: Parametric Bolt Hole Circle
#1 = 50 ; Diameter
#2 = 6 ; Number of holes
#3 = 0 ; Hole counter
WHILE [#3 LT #2] DO1
#4 = COS[#3*360/#2]*[#1/2]
#5 = SIN[#3*360/#2]*[#1/2]
G81 X#4 Y#5 Z-10 R2 F100
#3 = #3 + 1
END1
💡 Automatically drills #2 holes in a circular pattern of #1 mm diameter.
🎛️ Macro Call with Arguments (Custom Cycles)
O9001 (Subprogram)
#1 = #1 + 10
G81 X#1 Y#2 Z-#3 R2 F100
M99
Main Program:
M98 P9001 L1 (Call macro with preset arguments)
You can also pass values via arguments depending on controller capabilities.
⚠️ Common Mistakes
| Mistake | Result |
|---|---|
| Using undefined variable | Error or unexpected behavior |
| Division by zero | Alarm or crash |
| Logic error in loops | Infinite loop |
| Indirect addressing misuse | Accessing invalid memory |
✅ Use Cases for Parametric Programming
- Family-of-parts machining
- Automated part probing and offsets
- Tool life and break detection routines
- Fixture-specific macros
- Custom canned cycles
🧭 Summary
Parametric programming is more than just “math in G-code” — it’s the gateway to:
- Automation
- Customization
- Efficiency
- Adaptability
By mastering it, you move from manual programming to creating intelligent CNC logic that adapts, optimizes, and scales.
Leave a comment