Fanuc Custom Macro B Programming Guide: Variables, Conditional Branches, and WHILE Loops
1. Why Professional CNC Programmers Rely on Custom Macro B
Standard G-code programs are strictly static: coordinates are hardcoded, and any dimension change requires regenerating CAM toolpaths from scratch. Fanuc Custom Macro B removes these constraints on CNC controllers (from 0i-MF/TF up to 30i/31i/32i Series) by enabling: 1. Real-time Arithmetic: Trigonometric (SIN, COS, ATAN), square root (SQRT), and rounding functions (ROUND, FIX, FUP). 2. Conditional Decision Making: Dynamic branching using `IF [...] GOTO ...` based on offsets, probe triggers, or part dimensions. 3. Loop Iterations: Utilizing `WHILE [...] DO n` to mill surfaces or drill repetitive hole patterns with minimal code length.
2. Variable Classification in Fanuc Macro B
| Variable Range | Classification | Persistence | Typical Application |
|---|---|---|---|
| #0 | Null / Empty | Always empty (Read-only) | Checking if argument was passed (e.g. IF [#1 EQ #0]) |
| #1 to #33 | Local Variables | Cleared on program end (M30/M99) | Receiving arguments from G65 calls (e.g. G65 P9010 A50.0) |
| #100 to #199 | Common Variables (Volatile) | Reset upon CNC power down | Sharing temporary calculation values between main and subprograms |
| #500 to #999 | Common Variables (Non-volatile) | Retained in battery-backed SRAM | Part counting, tool life tracking, and fixture coordinate offsets |
| #1000 and above | System Variables | Direct CNC hardware/internal state access | Current machine positions (#5021-#5024), active tool offsets, alarms |
3. Mathematical and Logical Operators
Fanuc Macro B syntax requires conditional expressions to be enclosed in square brackets `[...]`. Standard comparison operators include: - EQ (Equal to): e.g., `IF [#1 EQ 10] GOTO 100` - NE (Not Equal to): e.g., `IF [#1 NE #0] GOTO 200` - GT (Greater Than): e.g., `IF [#1 GT 50.0] GOTO 300` - GE (Greater Than or Equal to) - LT (Less Than) - LE (Less Than or Equal to) - AND / OR / XOR: Logical operators for compounding conditions.
4. Practical Code Example: Bolt Hole Circle (PCD Macro)
O9010 (CUSTOM BOLT HOLE CIRCLE MACRO)
(CALL BY: G65 P9010 X0 Y0 R50.0 N8 A0 Z-15.0 F120)
(#24 = X-CENTER, #25 = Y-CENTER, #18 = PCD RADIUS)
(#14 = NUMBER OF HOLES, #1 = START ANGLE)
(#26 = DRILL DEPTH, #9 = FEEDRATE)
IF [#14 LE 0] GOTO 999 (CHECK INVALID HOLE COUNT)
IF [#18 LE 0] GOTO 999 (CHECK INVALID RADIUS)
#101 = 0 (CURRENT HOLE INDEX)
#102 = 360.0 / #14 (ANGLE STEP BETWEEN HOLES)
WHILE [#101 LT #14] DO 1
#103 = #1 + [#101 * #102] (CALCULATE CURRENT ANGLE)
#104 = #24 + [#18 * COS[#103]] (CALCULATE X POSITION)
#105 = #25 + [#18 * SIN[#103]] (CALCULATE Y POSITION)
G90 G00 X#104 Y#105 (POSITION OVER HOLE)
G81 Z#26 R3.0 F#9 (DRILL CYCLE)
G80 (CANCEL CANNED CYCLE)
#101 = #101 + 1 (INCREMENT HOLE COUNTER)
END 1
G90 G00 Z50.0
M99 (RETURN TO MAIN PROGRAM)
N999 #3000 = 1 (ALARM: INVALID PCD ARGUMENTS)
M995. Real-Time Probing with Skip Function (G31) and System Variables
One of the most potent applications of Macro B is automated part centering using touch trigger probes with G31 (Skip Function). When probe contact occurs, the controller halts motion immediately and latches the exact machine coordinate into system variables: - #5061: X-axis coordinate at skip signal - #5062: Y-axis coordinate at skip signal - #5063: Z-axis coordinate at skip signal Macro B logic can calculate the center point between dual-side contacts and automatically write the result directly into Work Coordinate System G54 (#5221 for X, #5222 for Y), eliminating manual dial indicator setup.
Warning: Floating Point Rounding and Infinite Loops
Never use strict equality `EQ` when evaluating floating-point loop termination conditions. Minute binary rounding errors (e.g. 9.9999999 vs 10.0000000) can cause the loop termination test to fail, triggering an Infinite Loop and hanging the CNC control. Best Practice: Always use inequality checks (`GE` or `LE`) and verify variable increments inside the loop body.
- DO/END nesting is strictly limited to 3 levels (DO 1, DO 2, DO 3).
- Parameter No. 6000 Bit 0 allows Single Block execution through Macro statements for safe dry-run validation.
- Do not indiscriminately overwrite #500-#520, as some machine tool builders (e.g. Mazak, Brother) reserve them for ATC and pallet changer logic.