15.8 NC/CNC Machines & CNC Part Programming
Key Takeaways
- NC/CNC machines and CNC programming are named explicitly in the Machining bullet of the CIL Mechanical Paper-II syllabus.
- G codes are preparatory functions that control machine motion and modes, while M codes are miscellaneous functions that control auxiliary equipment such as the spindle and coolant.
- In absolute programming with G90 all coordinates are measured from the program origin, whereas in incremental programming with G91 each coordinate is measured from the previous point.
- G02 specifies clockwise circular interpolation and G03 anticlockwise, with the arc centre normally given by the incremental I, J and K offsets from the start point.
NC, CNC and DNC
| Term | Meaning |
|---|---|
| NC | Numerical Control; the programme is fixed in hardware or on punched tape, and editing requires a new tape |
| CNC | Computer Numerical Control; a dedicated computer holds the programme, which can be edited, stored and re-run at the machine |
| DNC | Direct or Distributed Numerical Control; a central computer serves programmes to several machines over a network |
The decisive advance of CNC over NC is programme storage and editing at the machine, together with the ability to hold canned cycles, tool offsets and compensation tables in memory.
Point-to-point versus continuous path
| Control | Behaviour | Application |
|---|---|---|
| Point-to-point (positional) | Moves to a position; the path between points is unimportant | Drilling, tapping, spot welding, punching |
| Continuous path (contouring) | The path itself is controlled at every instant | Milling, turning, profiling, flame cutting |
Open versus closed loop
| Loop | Character |
|---|---|
| Open loop | Stepper motors; no position feedback; cheaper; position assumed from pulse count; a missed step is undetected |
| Closed loop | Servo motors with encoders or linear scales providing feedback; errors are detected and corrected; used on all production machines |
Axis Conventions
The right-hand rule fixes the axes:
- Z is always along the spindle axis, with positive Z moving the tool away from the workpiece.
- X is the longest axis of motion perpendicular to Z, normally horizontal.
- Y completes the right-handed set.
- Rotary axes A, B and C rotate about X, Y and Z respectively.
On a lathe, X is the diametral direction, and X coordinates are conventionally programmed as diameters rather than radii — a persistent source of error for those moving between milling and turning.
A fundamental convention: motion is always described as the movement of the tool relative to the workpiece, even when the machine physically moves the table instead.
Interpolation
| Type | Code | Path |
|---|---|---|
| Rapid positioning | G00 | Fastest available; path not guaranteed straight; never cutting |
| Linear interpolation | G01 | Straight line at programmed feed rate |
| Circular, clockwise | G02 | Arc |
| Circular, anticlockwise | G03 | Arc |
| Helical, parabolic, cubic | Varies | Advanced controllers |
Rapid moves must never be used for cutting: the path between two points under G00 may be a dog-leg rather than a straight line, and the feed is uncontrolled.
G Codes and M Codes
G codes are preparatory functions that set the machine's mode of operation. M codes are miscellaneous functions that switch auxiliary equipment. Keeping the two categories apart is the most common single question on this topic.
Common G codes
| Code | Function |
|---|---|
| G00 | Rapid traverse |
| G01 | Linear interpolation |
| G02 / G03 | Circular interpolation, clockwise / anticlockwise |
| G04 | Dwell |
| G17 / G18 / G19 | Plane selection: XY / XZ / YZ |
| G20 / G21 | Inch / metric units |
| G28 | Return to machine reference (home) |
| G40 / G41 / G42 | Cutter compensation cancel / left / right |
| G43 / G49 | Tool length compensation on / cancel |
| G54 - G59 | Work coordinate systems |
| G90 / G91 | Absolute / incremental positioning |
| G94 / G95 | Feed per minute / feed per revolution |
| G96 / G97 | Constant surface speed / constant spindle rpm |
| G80 | Cancel canned cycle |
| G81 / G83 / G84 | Drilling / peck drilling / tapping cycles |
Common M codes
| Code | Function |
|---|---|
| M00 / M01 | Programme stop / optional stop |
| M03 / M04 / M05 | Spindle clockwise / anticlockwise / stop |
| M06 | Tool change |
| M08 / M09 | Coolant on / off |
| M30 | Programme end and rewind |
| M98 / M99 | Subprogramme call / return |
Absolute Versus Incremental
This distinction produces more programming errors than any other.
| Mode | Code | Coordinates measured from |
|---|---|---|
| Absolute | G90 | The fixed programme origin (work zero) |
| Incremental | G91 | The previous point |
To move through the points (10,10), (30,10) and (30,40):
| Absolute (G90) | Incremental (G91) |
|---|---|
| X10 Y10 | X10 Y10 |
| X30 Y10 | X20 Y0 |
| X30 Y40 | X0 Y30 |
Absolute programming is safer because an error at one point does not propagate; in incremental mode every subsequent position inherits the error. Absolute is therefore standard practice, with incremental reserved for repeated patterns and subprogrammes.
Circular Interpolation and I, J, K
An arc requires the endpoint and the centre. The centre is normally specified by I, J and K, which are the incremental offsets from the arc start point to the arc centre, along X, Y and Z respectively.
To cut a quarter circle of radius 20 anticlockwise from (20,0) to (0,20) with centre at the origin:
G03 X0 Y20 I-20 J0
The I value of minus 20 is the X distance from the start point (20,0) to the centre (0,0). Many controllers also accept an R word giving the radius directly, which is simpler but ambiguous for arcs greater than 180 degrees, where the sign of R selects between the two possible arcs.
Cutter Radius Compensation
The programmer wants to describe the finished part profile, not the path of the cutter centre, which lies one cutter radius away. Cutter compensation lets the controller make that offset automatically using a radius value stored in an offset table.
| Code | Meaning |
|---|---|
| G41 | Compensation left of the direction of travel |
| G42 | Compensation right of the direction of travel |
| G40 | Cancel compensation |
Left and right are judged looking along the direction of tool travel. For conventional climb milling of an external profile, G41 is usual.
The practical benefit is decisive: if the cutter is reground to a smaller diameter, or a different cutter is fitted, only the offset value in the table changes — the programme itself is untouched. The same mechanism is used to leave a finishing allowance, simply by entering an offset larger than the true radius.
Tool length compensation (G43) works similarly in Z, storing each tool's length offset so that programmes can be written to the part datum irrespective of how far each tool protrudes from its holder.
Canned Cycles
A canned cycle replaces many blocks with one. For drilling a series of holes, instead of programming rapid down, feed to depth, rapid out for every hole:
G81 X__ Y__ Z-25 R2 F100
where Z is the final depth, R the retract plane and F the feed. Subsequent X and Y coordinates repeat the cycle at each new position until cancelled by G80.
| Cycle | Purpose |
|---|---|
| G81 | Simple drilling |
| G82 | Drilling with dwell at the bottom, for a flat-bottomed counterbore |
| G83 | Peck drilling, retracting to clear chips; essential for deep holes |
| G84 | Tapping |
| G85 | Boring, feeding out |
A Complete Worked Programme
Milling a 60 by 40 mm rectangular pocket profile, 5 mm deep, with a 10 mm diameter end mill, work zero at the lower left corner of the profile:
O0001 (programme number)
G21 G90 G94 G17 (metric, absolute, feed/min, XY plane)
G54 (work coordinate system)
T01 M06 (tool 1, end mill 10 mm)
S1200 M03 (spindle 1200 rpm clockwise)
G43 H01 Z50 (tool length offset, safe height)
G00 X-15 Y-15 (rapid to approach point clear of part)
Z5 (rapid to clearance plane)
G01 Z-5 F100 (feed to depth)
G41 D01 X0 Y0 (cutter comp left, approach profile)
G01 X60 F200 (cut along bottom edge)
Y40 (cut up right edge)
X0 (cut along top edge)
Y0 (cut down left edge)
G40 X-15 Y-15 (cancel compensation, move clear)
G00 Z50 (retract)
M05 (spindle stop)
M09 (coolant off)
G28 G91 Z0 (return to home in Z)
M30 (programme end and rewind)
Note the structural discipline: safety block first setting units and modes, approach clear of the part before engaging compensation, cancel compensation clear of the part, and retract before stopping the spindle. Those four habits prevent the great majority of crashes.
Programming Methods
| Method | Description |
|---|---|
| Manual | G code written by hand; suits simple prismatic parts |
| Conversational | Menu-driven at the machine control; fast for shop-floor programming |
| CAM (computer aided) | Toolpath generated from the CAD model and post-processed; essential for sculptured surfaces |
For a maintenance workshop producing one-off spares from drawings, manual and conversational programming usually suffice. For die and mould work or three-dimensional surfaces, CAM is not optional.
In CNC programming, the difference between G codes and M codes is that:
Under G91 incremental programming, each coordinate is measured from:
In circular interpolation, the words I, J and K normally specify:
The principal practical advantage of cutter radius compensation is that: