neuralcad / docs /wiki /gcode-reference.md
CallMeDaniel's picture
docs: add CNC/CAM knowledge wiki and slicer/preview design spec
265686c
# G-code Reference for CNC Milling
## Motion Codes (Modal Group 1)
Modal codes remain active until replaced by another code from the same group.
| Code | Function | Parameters |
|------|----------|------------|
| G00 | Rapid positioning (max speed, no cutting) | X Y Z |
| G01 | Linear interpolation (cutting feed) | X Y Z F |
| G02 | Circular interpolation, clockwise | X Y Z I J K / R, F |
| G03 | Circular interpolation, counter-clockwise | X Y Z I J K / R, F |
## Arc Interpolation (G02/G03)
Two formats for specifying arcs:
**Center Offset (I, J, K)**:
- I = X-offset from current position to arc center
- J = Y-offset from current position to arc center
- K = Z-offset (for helical arcs)
- In XY plane (G17): uses I, J
- In XZ plane (G18): uses I, K
- In YZ plane (G19): uses J, K
**Radius (R)**:
- R > 0: arc < 180 degrees
- R < 0: arc > 180 degrees
Example:
```gcode
G02 X10 Y0 I5 J0 F200 ; CW arc to (10,0), center offset (5,0)
G03 X0 Y10 R10 F200 ; CCW arc to (0,10), radius 10
```
## Plane Selection (Modal Group 2)
| Code | Function |
|------|----------|
| G17 | XY plane (default for milling) |
| G18 | ZX plane |
| G19 | YZ plane |
## Distance Mode (Modal Group 3)
| Code | Function |
|------|----------|
| G90 | Absolute positioning |
| G91 | Incremental positioning |
## Units (Modal Group 6)
| Code | Function |
|------|----------|
| G20 | Inch mode |
| G21 | Millimeter mode |
## Canned Drilling Cycles (Modal Group 1)
| Code | Function | Key Params |
|------|----------|-----------|
| G80 | Cancel canned cycle | -- |
| G81 | Simple drilling | X Y Z R F L |
| G82 | Spot drill with dwell | X Y Z R P F L |
| G83 | Deep-hole peck drilling (full retract) | X Y Z R Q F L |
| G84 | Right-hand tapping | X Y Z R F L |
| G85 | Boring, feed out | X Y Z R F L |
| G86 | Boring, spindle stop, rapid out | X Y Z R F L |
| G73 | High-speed peck (chip break, short retract) | X Y Z R Q F L |
Parameters:
- R = retract plane (Z height for rapid between holes)
- Q = peck depth increment
- P = dwell time at bottom (milliseconds)
- L = repeat count
## Tool Compensation
| Code | Function |
|------|----------|
| G40 | Cancel cutter radius compensation |
| G41 | Cutter radius compensation left |
| G42 | Cutter radius compensation right |
| G43 | Tool length compensation positive |
| G49 | Cancel tool length compensation |
## Work Coordinate Systems
| Code | Function |
|------|----------|
| G54 | Work coordinate system 1 (default) |
| G55-G59 | Work coordinate systems 2-6 |
| G92 | Set position (coordinate system offset) |
## M-Codes (Miscellaneous Functions)
| Code | Function |
|------|----------|
| M0 | Program pause (operator resume) |
| M1 | Optional stop |
| M2 | Program end |
| M3 | Spindle on, clockwise |
| M4 | Spindle on, counter-clockwise |
| M5 | Spindle stop |
| M6 | Tool change |
| M7 | Mist coolant on |
| M8 | Flood coolant on |
| M9 | Coolant off |
| M30 | Program end and rewind |
## Feed Rate and Spindle
| Letter | Function |
|--------|----------|
| F | Feed rate (mm/min or in/min) |
| S | Spindle speed (RPM) |
| T | Tool number (e.g., T1, T2) |
| N | Line number (optional) |
## Complete G-code Program Example
```gcode
%
O0001 (POCKET MILLING EXAMPLE)
G21 G90 G17 (mm, absolute, XY plane)
G54 (work coordinate system 1)
(TOOL 1 - 6MM ENDMILL)
T1 M6 (select tool 1, tool change)
S18000 M3 (spindle 18000 RPM, clockwise)
G43 H1 (tool length compensation)
(RAPID TO START POSITION)
G00 X0 Y0 Z10 (rapid above workpiece)
(POCKET - FIRST PASS)
G00 Z2 (rapid to clearance height)
G01 Z-2 F200 (plunge to depth at feed rate)
G01 X40 F800 (cut along X)
G01 Y30 (cut along Y)
G01 X0 (cut back)
G01 Y0 (cut back to start)
(POCKET - SECOND PASS)
G01 Z-4 F200 (plunge deeper)
G01 X40 F800
G01 Y30
G01 X0
G01 Y0
(RETRACT AND END)
G00 Z50 (rapid retract)
M5 (spindle off)
M9 (coolant off)
G28 (return to reference point)
M30 (program end)
%
```
## Modal vs Non-Modal
**Modal codes** stay active: G00, G01, G02, G03, G17-G19, G20/G21, G40-G43, G54-G59, G80-G89, G90/G91
**Non-modal codes** execute once: G04 (dwell), G28 (return to home), G92 (set position)
## Controller Dialects
| Feature | LinuxCNC | Grbl | FANUC | Haas |
|---------|----------|------|-------|------|
| Arc format | I/J/K and R | I/J/K and R | I/J/K and R | I/J/K and R |
| Canned cycles | Full G73-G89 | None | Full G73-G89 | Full G73-G89 |
| Tool change | M6 | Manual | M6 | M6 |
| Subroutines | O-codes | None | M98/M99 | M97/M98 |
| Variables | #1-#999 | None | #1-#999 | #1-#999 |
| Peck drilling | G83 | Manual | G83 | G83 |