CNC Program Structure and Manual ProgrammingIntroductionComputer numerical control (CNC) programming converts a machining process into a precisely ordered set of instructions. These instructions specify tool movement, spindle operation, feed rate, coordinate systems, cutting conditions, auxiliary functions, and program control. Although controllers differ in syntax and available cycles, most industrial systems use a closely related ISO-style vocabulary based on address letters, numerical values, preparatory functions (G codes), and miscellaneous functions (M codes). A competent programmer must do more than memorize codes. The programmer must understand coordinate systems, modal behavior, machine limitations, tooling, workholding, cutting conditions, offsets, verification, and safe operating practice. This chapter develops a controller-neutral method for constructing and reading manual CNC programs. Examples use common ISO conventions, but actual machine documentation always takes precedence. Learning ObjectivesBy the end of this chapter, the learner should be able to: - explain the structure of a CNC block and a complete program;
- distinguish addresses, words, blocks, sequence numbers, G codes, and M codes;
- describe modal and non-modal commands;
- construct safe-start blocks and document program intent;
- apply absolute and incremental programming correctly;
- specify linear and circular interpolation;
- calculate and command spindle speed and feed rate;
- use work offsets, tool offsets, and cutter compensation appropriately;
- identify controller-dependent syntax and verify programs safely;
- analyze a complete illustrative machining program.
1. The CNC Program as a Control DocumentA CNC program is both a machine-control document and a manufacturing record. It communicates a sequence of states and actions to the controller. A typical program contains: - program identification;
- initialization or safe-start commands;
- tool selection and offset activation;
- spindle and coolant commands;
- approach and cutting motions;
- retract and tool-change actions;
- program-end commands.
The controller interprets the program one block at a time. A block normally represents one line of coded information and may contain several words. A word consists of an address letter followed by a numerical value, for example X25.0, F120, or G01. 1.1 Program, block, word, and address| Element | Meaning | Example |
|---|
| Program | Complete set of instructions for a part or operation | O0100 | | Block | One line or executable record | N20 G01 X25.0 F120 | | Word | Address plus value | X25.0 | | Address | Letter identifying the function | X | | Value | Numerical data associated with the address | 25.0 | | Comment | Human-readable documentation | (ROUGH PROFILE) |
Common addresses include X, Y, and Z for axes; A, B, and C for rotary axes; F for feed; S for spindle speed; T for tool selection; H for tool-length offset; D for cutter-radius offset; N for sequence number; G for preparatory functions; and M for miscellaneous functions. The meaning of an address can vary by controller and machine type. A block may be written as: N20 G01 X25.000 Y10.000 F120 Here, N20 is a sequence number, G01 commands linear interpolation, X25.000 and Y10.000 specify the endpoint, and F120 specifies feed rate. Whether F120 means millimetres per minute or another unit depends on the active feed mode and machine configuration. 1.2 Sequence numbers and program orderSequence numbers identify blocks and can assist with editing, inspection, and restart procedures. They do not necessarily determine execution order: the controller normally executes blocks in their stored order. Sequence numbers should therefore be logical and unique enough to support troubleshooting. Numbering by tens, such as N10, N20, and N30, leaves room for later insertion. Comments should identify the part, operation, material, tools, datum, units, revision, and important assumptions. Documentation reduces setup errors and supports traceability. 2. Preparatory and Miscellaneous Functions2.1 Preparatory functionsG codes define geometrical, coordinate, motion, or machining modes. Common examples are shown below. Exact meanings and group assignments must be confirmed in the controller manual. | Function | Common purpose |
|---|
| G00 | Rapid positioning | | G01 | Linear interpolation at programmed feed | | G02 | Clockwise circular interpolation | | G03 | Counterclockwise circular interpolation | | G17/G18/G19 | Select XY, ZX, or YZ plane | | G20/G21 | Inch or metric units | | G40 | Cancel cutter-radius compensation | | G41/G42 | Left/right cutter-radius compensation | | G43 | Apply positive tool-length compensation, commonly with H address | | G49 | Cancel tool-length compensation | | G54–G59 | Work-coordinate-system selection on many controls | | G90 | Absolute programming | | G91 | Incremental programming | | G94/G95 | Feed per minute/feed per revolution on many controls | | G80 | Cancel canned cycle |
These are representative conventions, not universal commands. For example, the available work-offset range, tool compensation format, and cancellation behavior may differ. 2.2 Miscellaneous functionsM codes control auxiliary machine functions. Typical examples include M03 for clockwise spindle rotation, M04 for counterclockwise rotation, M05 for spindle stop, M06 for tool change, M08 and M09 for coolant on and off, M00 for programmed stop, M01 for optional stop, and M30 for end and rewind. Some functions are executed immediately; others are synchronized with motion or interpreted according to the machine builder’s ladder logic. An M code should never be assumed to be harmless. For example, a spindle command without a verified speed limit or a coolant command near an open electrical enclosure can create hazards. Operators must understand the machine’s implementation. 3. Modal Behavior and Safe-State ControlA modal command remains active until replaced or cancelled. For example, after G01 is activated, subsequent coordinate blocks may continue to command feed motion without repeating G01. G90, G21, G54, feed mode, plane selection, and compensation states are also commonly modal. Non-modal commands affect only the block in which they occur. A dwell command, a program stop, and some probing or coordinate commands are often non-modal, although controller behavior must be checked. Modal behavior improves compactness but creates risk. A forgotten modal state can cause an apparently simple block to execute differently from its author’s intention. Good programming practice makes important states explicit at tool changes and operation boundaries. A generic milling safe-start block may be: G17 G21 G40 G49 G54 G80 G90 This selects the XY plane, metric units, cancels cutter compensation, cancels tool-length compensation, selects a work offset, cancels canned cycles, and selects absolute programming. A machine may require additional commands such as feed-mode selection, spindle-direction cancellation, or a particular rapid-mode convention. A safe-start block is therefore a controlled initialization strategy, not a universal magic line. Safe-state principles include: - establish units before dimensional commands;
- establish the coordinate system before positioning;
- cancel compensation before tool changes or non-cutting moves when appropriate;
- cancel canned cycles before ordinary positioning;
- retract to a verified safe location before changing tools;
- avoid relying on an unknown previous modal state;
- test the first execution using single-block, reduced rapid, and graphics modes where available.
4. Coordinate Systems and Offsets4.1 Machine, work, and tool coordinatesThe machine coordinate system is established by the machine reference or homing procedure. The work coordinate system locates the part relative to the machine. A work offset such as G54 stores the relationship between the programmed part datum and the machine coordinate system. Tool offsets compensate for differences between the programmed tool and the actual tool. On a milling machine, tool-length compensation accounts for the tool’s measured length. Cutter-radius compensation accounts for the effective radius or diameter of the cutting tool when the programmed path represents the finished profile. The programmer must distinguish three ideas: - programmed position: the coordinate written in the program;
- machine position: the physical location in machine coordinates;
- compensated position: the controller-adjusted location after applying offsets.
A wrong work offset can shift an otherwise correct program by a large distance. Offset entry is therefore a setup responsibility requiring independent verification. 4.2 Absolute and incremental programmingIn absolute mode, each coordinate describes the endpoint relative to the active work datum. In incremental mode, each coordinate describes the displacement from the current position. Suppose the tool is at X10.0\ Y5.0 and must move to X30.0\ Y12.0. In absolute mode the command is: G90 G01 X30.0 Y12.0 F100 In incremental mode the displacement is: G91 G01 X20.0 Y7.0 F100 The arithmetic is: ∆X = Xf − Xi = 30.0 − 10.0 = 20.0 mm ∆Y = Yf − Yi = 12.0 − 5.0 = 7.0 mm Absolute programming is often easier to audit for part geometry. Incremental programming can be useful for repeated patterns and portable subroutines, but it is more vulnerable to accumulated mistakes. A mode change should be isolated and documented. 5. Interpolation and Motion Commands5.1 Rapid positioningG00 commands positioning at a machine-controlled rapid rate. It is not a cutting feed command. The path between the start and endpoint may not be a straight line in the geometrical sense, because axes can move at different rates. Therefore, the tool must be clear of the workpiece, fixture, and other obstructions throughout the rapid move. A safe approach normally consists of separate axis moves or a verified diagonal move in a clearance region. Never use G00 to enter material unless the machine builder and process have explicitly justified it. 5.2 Linear interpolationG01 commands a coordinated straight-line move at the programmed feed. If both X and Y change, the controller synchronizes the axes so the tool follows a straight line in the selected plane. The path length for a two-axis move is: L = √((Xf − Xi)2 + (Yf − Yi)2) where L is path length and coordinates use consistent units. For a move from (10, 5) to (30, 12): L = √((20)2 + (7)2) = √449 ≈ 21.19 mm This geometric length does not by itself determine machining time unless the programmed feed and controller interpolation behavior are known. 5.3 Circular interpolationG02 and G03 command clockwise and counterclockwise arcs in the active plane. An arc may be specified using a radius R or center offsets, commonly I, J, and K. The meaning of clockwise and counterclockwise depends on the viewed positive direction of the selected plane; the programmer must not infer it from a two-dimensional sketch without checking the plane convention. Center-offset programming is often preferred for technical clarity because it identifies the arc center relative to the start point. A valid arc requires geometrically consistent start point, endpoint, center, direction, and radius. Controllers may reject an arc if the endpoint is outside tolerance or if an ambiguous full-circle format is used. 5.4 Plane selectionThe active plane determines the two axes used for circular interpolation. G17 commonly selects XY, G18 ZX, and G19 YZ. Plane selection should be explicit before arcs, especially in multi-axis or mixed-operation programs. 6. Spindle Speed, Feed, and Cutting ConditionsSpindle speed is commonly commanded with S. For turning or milling, cutting speed, tool diameter, and spindle speed are related by: Vc = (π D n) / 1000 where Vc is cutting speed in metres per minute, D is tool or workpiece diameter in millimetres, and n is spindle speed in revolutions per minute. Rearranging gives: n = (1000 Vc) / (π D) The formula assumes diameter-based cutting speed, consistent units, and an ideal relationship without speed limits, load compensation, or slip. The commanded value must be limited by the machine, tool, workpiece, and process. For a 10 mm milling cutter at a recommended cutting speed of 150 m/min: n = (1000 × 150) / (π × 10) ≈ 4775 rpm The programmer may select a practical value such as 4750 rpm if permitted by the machine and tooling. For milling, feed per minute can be estimated from feed per tooth: F = fz z n where F is feed rate in millimetres per minute, f_z is feed per tooth in millimetres per tooth, z is the number of effective teeth, and n is spindle speed in revolutions per minute. For fz = 0.05\ mm/tooth, z = 4, and n = 4750 rpm: F = 0.05 × 4 × 4750 = 950 mm/min The result is a starting calculation, not a guarantee of process capability. Tool material, workpiece material, radial and axial engagement, coolant, rigidity, chip evacuation, and machine dynamics must also be considered. In turning, feed per revolution is frequently used. If feed is specified per revolution, its numerical interpretation differs from feed per minute. Activating the wrong feed mode can produce a severe overfeed or underfeed. Every program should make the intended feed mode clear and verify the controller’s convention. 7. Tool Changes, Compensation, and Operation StructureA typical milling operation sequence is: - stop or safely manage the spindle;
- retract to a tool-change position;
- cancel unsuitable compensation or cycles;
- select and change the tool;
- activate the measured tool-length offset;
- position above a verified approach point;
- start spindle and coolant;
- feed into the cut;
- machine the feature;
- retract and end the operation.
A representative, controller-neutral pattern is: (--- OPERATION 1: FACE ---)
T01 M06
G17 G21 G40 G49 G54 G80 G90
G00 Z50.0
G43 H01 Z50.0
S4750 M03
M08
G00 X-5.0 Y-5.0
G01 Z-0.5 F150
G01 X105.0 F950
G00 Z50.0
M09
M05 This is illustrative rather than production-ready. The correct tool-change command, safe Z height, offset convention, spindle orientation, and coolant behavior depend on the machine. The example also assumes that the work offset, tool offset, cutter, stock, and clearance have been verified. Cutter compensation is especially important when the programmed path represents a part boundary. Compensation permits the programmer to define the nominal contour while the controller offsets the tool centerline. Compensation should be activated on a lead-in move and cancelled on a lead-out move where possible. Activating compensation directly on a geometrically unsuitable block can cause alarms or an unexpected path. 8. Program Documentation and VerificationA professional program documents intent rather than merely encoding motion. Useful comments include: - part number and revision;
- material and stock size;
- datum definition;
- units and programming mode;
- tool list and offset numbers;
- operation purpose;
- cutting-condition assumptions;
- inspection requirements;
- revision history.
Verification should occur at several levels: Syntax verification: confirm that addresses, decimal formats, code groups, and program delimiters are accepted. Geometric verification: compare the simulated tool path with the drawing, including approach, retract, arcs, and compensation. Machine verification: confirm travel limits, spindle range, tool length, workholding, coolant, guarding, and offset values. Process verification: confirm that feeds, speeds, depth of cut, chip load, and tool engagement are suitable. A simulation cannot always reproduce machine-specific behavior such as servo following error, fixture interference, incorrect offset entry, tool pullout, or a nonstandard M code. Dry runs and controlled first-off inspections remain necessary. 9. Worked Program AnalysisThe following illustrative program machines a rectangular path. It is intentionally generic and must be adapted to a particular controller. %
O0100 (RECTANGULAR PROFILE - REV A)
(UNITS: MM; DATUM: LOWER-LEFT TOP FACE)
(T01: 10 MM END MILL; H01: TOOL LENGTH)
N10 G17 G21 G40 G49 G54 G80 G90
N20 T01 M06
N30 G00 Z50.0
N40 G43 H01 Z50.0
N50 S4750 M03
N60 M08
N70 G00 X5.0 Y5.0
N80 G01 Z-2.0 F150
N90 G01 X95.0 F950
N100 Y45.0
N110 X5.0
N120 Y5.0
N130 G00 Z50.0
N140 M09
N150 M05
N160 G49
N170 G53 Z0.0
N180 M30
% The analysis is as follows. N10 establishes the plane, units, compensation cancellation, work offset, cycle cancellation, and absolute mode. N20 changes to tool T01. N30 and N40 position and apply tool-length compensation. N50 and N60 start the spindle and coolant. N70 moves rapidly to a clearance point, and N80 feeds to cutting depth. N90–N120 form the rectangular path using modal G01 behavior: G01 need not be repeated on each line because it remains active. N130 retracts in rapid mode. N140 and N150 stop coolant and spindle. N160 cancels tool-length compensation. N170 uses a machine-coordinate move convention often associated with G53, but this command is highly controller-dependent and must be verified. N180 ends and rewinds the program. Potential concerns include the absence of cutter-radius compensation, the chosen clearance height, whether the first horizontal move is safe at the selected depth, and whether the tool can reach the corners without violating the specified geometry. These are process questions, not merely syntax questions. 10. Applied Case: Correcting a First-Article Profile ErrorBackgroundThe following is a fictional illustrative case involving a three-axis machining cell producing an aluminium mounting plate. The drawing requires a rectangular pocket and four drilled holes. The first article showed a pocket displaced 8 mm in X, although the internal dimensions were correct. ProblemThe program geometry was checked in simulation and appeared correct. The machine operator reported that the tool moved to the expected coordinates, yet the pocket was shifted relative to the part edges. AnalysisThe programmer and setup technician compared the drawing datum, probing record, and active work offset. The program used G54 and absolute programming. The measured work offset had been entered using the right-hand stock corner, while the program documentation defined the lower-left corner as the datum. The difference between the two reference locations was 8 mm in X. No change to the pocket geometry was required. The error was a coordinate-system definition and setup-verification failure. The program also lacked a prominent comment describing the physical datum location. Approach and solutionThe team: - stopped production and quarantined the first article;
- re-established the datum using the drawing and setup sheet;
- corrected the G54 value after independent verification;
- added a datum sketch and explicit coordinate note to the program header;
- introduced a prove-out checklist requiring confirmation of work offset, tool offsets, and first rapid position;
- ran the program in graphics, single-block, and reduced-rapid modes;
- inspected the second article before release.
OutcomeThe second article met the positional and dimensional requirements. The organization retained the program structure but revised its documentation and setup control. The case demonstrates that a syntactically correct program can produce an incorrect part when datum management is weak. Learning points- Work offsets are part of the manufacturing system, not invisible administrative data.
- Program comments should identify the physical datum in language understandable to the setup technician.
- Simulation verifies programmed geometry but may not verify real offset entry.
- Independent checks are especially important when a coordinate system is re-established.
11. Limitations, Safety, and Contemporary PracticeManual programming is transparent and useful for simple prismatic features, turning profiles, fixtures, edits, and troubleshooting. Its limitations become significant for complex free-form surfaces, large feature counts, simultaneous multi-axis motion, and frequent design changes. CAM systems, postprocessors, digital twins, and probing routines can improve productivity, but they introduce their own risks: incorrect postprocessor settings, unverified machine definitions, stale tool libraries, and false confidence in simulation. Artificial intelligence may assist with code explanation, documentation, anomaly detection, or parameter suggestions. It should not be treated as an autonomous authority for machine motion. AI-generated code must be checked against the controller manual, drawing, tooling, offsets, machine envelope, and simulation. Responsibility for safe release remains with qualified personnel. Relevant governance practices include revision control, access control for production programs, audit trails, approved postprocessors, operator training, and retention of inspection records. Programs should never be edited casually at the machine without recording the change and evaluating its effect on quality and safety. Best-Practice ChecklistBefore releasing a manual CNC program, confirm: - the correct controller and machine syntax;
- program number, revision, and comments;
- units, plane, absolute/incremental mode, and feed mode;
- work offset and physical datum;
- tool numbers, tool lengths, diameters, and wear values;
- spindle direction, speed, and maximum permitted speed;
- feed rates and cutting-condition assumptions;
- compensation activation and cancellation;
- clearance, retract, and tool-change positions;
- coolant and auxiliary-function behavior;
- simulation, dry run, single-block, and first-article inspection plans.
Chapter SummaryCNC programs are structured collections of blocks containing addresses, values, preparatory functions, miscellaneous functions, motion commands, offsets, and documentation. Modal commands remain active until changed or cancelled, so safe-start blocks and explicit operation boundaries are essential. Absolute and incremental programming describe positions in different ways and must not be mixed without deliberate control. G00, G01, G02, and G03 define common motion types, while plane selection determines the interpretation of circular interpolation. Spindle speed may be estimated from cutting speed and diameter, and milling feed may be estimated from feed per tooth, tooth count, and spindle speed; these calculations require process and machine validation. Work offsets and tool offsets connect the virtual program to the physical setup and are frequent sources of error. Professional programming includes comments, revision control, simulation, machine verification, controlled prove-out, and first-article inspection. Controller manuals and machine-builder documentation take precedence over generic ISO-style conventions. Manual programming remains a valuable engineering skill because it develops understanding of motion, process logic, and machine behavior even when CAM and AI tools are used. Key TermsAddress: A letter identifying the function of a CNC word. Block: One executable line or record in a CNC program. Cutter compensation: Controller adjustment of the tool path for cutter radius or diameter. Feed rate: The commanded rate of cutting motion. G code: Preparatory function controlling motion, modes, or machining behavior. Incremental programming: Programming in which coordinates specify displacement from the current position. Interpolation: Coordinated axis movement used to generate a path. Machine coordinate system: Coordinate system established by the machine reference system. M code: Miscellaneous function controlling auxiliary machine actions. Modal command: A command that remains active until replaced or cancelled. Non-modal command: A command that applies only to its current block or immediate action. Offset: Stored dimensional data used to relate programmed geometry to actual tools or work. Program: Complete collection of instructions for a machining operation or part. Safe-start block: Initialization block that establishes known modal states. Sequence number: Identifier assigned to a program block. Tool-length compensation: Correction for the measured length of a tool. Work offset: Stored transformation locating the programmed datum on the machine. Word: An address and its associated numerical value. Review Questions5 Short Answer Questions- What is the difference between a block and a word in a CNC program?
- Why is modal behavior both useful and potentially hazardous?
- State two purposes of a safe-start block.
- Distinguish machine coordinates from work coordinates.
- What is the practical difference between G00 and G01?
5 Long Answer Questions- Explain the structure of an ISO-style CNC program from identification through program end.
- Compare absolute and incremental programming, including a worked coordinate example.
- Explain how work offsets, tool-length offsets, and cutter compensation affect the actual tool path.
- Discuss the verification stages required before releasing a manual CNC program.
- Explain the relationship among cutting speed, spindle speed, tool diameter, feed per tooth, and feed rate.
3 Analytical / Application Questions- A program is correct in simulation but produces a part shifted in X while all internal dimensions remain correct. Develop a diagnostic procedure and identify the most likely causes.
- Design a safe operation structure for a milling tool that faces a block, including initialization, tool change, compensation, spindle and coolant control, cutting motion, retract, and program end. State which commands must be verified against the controller manual.
- A 12 mm cutter has a recommended cutting speed of 180 m/min, four effective teeth, and a feed per tooth of 0.04 mm/tooth. Calculate a starting spindle speed and feed rate, then discuss at least four reasons why the values may require adjustment before production.
Multiple Choice Questions with AnswersQuestion 1Which item consists of an address letter followed by a numerical value? A. A program B. A word C. A machine datum D. A tool offset Correct Answer: B. A word Question 2What is the principal risk of an unrecognized modal state? A. The controller cannot store comments B. A later block may execute using an unintended mode C. The spindle must always stop D. The work offset is automatically deleted Correct Answer: B. A later block may execute using an unintended mode Question 3Which command commonly selects absolute programming? A. G40 B. G54 C. G90 D. G94 Correct Answer: C. G90 Question 4What is the usual purpose of G01? A. Rapid positioning B. Linear interpolation at programmed feed C. Spindle stopping D. Canned-cycle cancellation Correct Answer: B. Linear interpolation at programmed feed Question 5For a 10 mm cutter and cutting speed of 150 m/min, what spindle speed is approximately required? A. 477 rpm B. 1,500 rpm C. 4,775 rpm D. 47,750 rpm Correct Answer: C. 4,775 rpm Question 6In the milling feed relation F = fz z n, what does fz represent? A. Tool diameter B. Feed per tooth C. Number of spindle revolutions D. Cutting speed Correct Answer: B. Feed per tooth Question 7Which offset locates the programmed part datum relative to the machine? A. Work offset B. Tool-length offset C. Cutter-radius offset D. Wear-only offset Correct Answer: A. Work offset Question 8Why should G00 generally not be used to enter material? A. It always reverses spindle direction B. Its path and rate are intended for non-cutting positioning C. It cancels the work offset D. It activates cutter compensation Correct Answer: B. Its path and rate are intended for non-cutting positioning Question 9What is a major purpose of program comments? A. To increase spindle torque B. To replace tool offsets C. To document datum, tools, assumptions, and revision information D. To force circular interpolation Correct Answer: C. To document datum, tools, assumptions, and revision information Question 10A pocket is shifted but its internal dimensions are correct. Which cause is especially likely? A. Incorrect work-offset datum B. Excessive comment length C. Correct feed-mode selection D. A cancelled coolant command Correct Answer: A. Incorrect work-offset datum
|