CNC PART PROGRAMMING
CNC programming refers to converting a 3D CAD (computer-
aided design) model into a set of machine-readable
instructions that include: which cutting tools to use, the feed
rate, and where to move the tool to create the final part.
CNC part programming involves a series of coded
instructions that are required to produce a part.
The program controls the machine tool movements and
controls auxiliary functions including spindle, coolant, and
rotation.
The instructions may include numbers, letters, and symbols
arranged in functional format blocks.
CNC PART PROGRAMMING
CNC programming allows manufacturers to create
parts automatically while controlling the process
manually.
Manufacturers can use CNC machines to transform
raw materials into finished products with great
precision and in large batches.
Hence the methods of part programming can be of two
types depending upon the two techniques as below :
(a) Manual part programming, and
(b) Computer aided part programming.
THE BASIC CNC CODE?
G-code is the simple programming language for CNC
machines. It consists of lines of code that are organized in
blocks.
Each block controls one CNC machining operation, such
as a cutting operation with a specific tool.
Each line in a block is labeled with the letter N and a
number
G-code is a variation of an alphanumeric pattern. A
standard G-code command conforms to the following
logic:
N## G## X## Y## Z## F## S## T## M##
THE BASIC CNC CODE?
To understand G-code, you need to know how a program is formatted. G-
code programs keep to the following structure.
Program header: This starting G-code basically tells the machine how to
prepare itself for the upcoming job. Common aspects include stock
dimensions, offsets, and planes.
Program blocks: Also called the body of the program, this is where the
main part of the job is described, including axis moves, speeds, feeds, and
tool changes. Codes following the logic described in the previous section go
here. (Most errors also appear here.)
End of program: This part tells the machine that the work has ended so it
can retract to a safe position and turn everything off.
THE BASIC CNC CODE?
STRUCTURE OF AN NC PART
PROGRAM:
Commands are input into the controller in units called blocks or
statements.
Block Format:
1. Fixed sequential format
2. Tab sequential format
3. Word address format
Structure of an NC Part Program:
1. FIXED SEQUENTIAL FORMAT
0050 00 +0025400 +0012500 +0000000 0000 00
0060 01 +0025400 +0012500 -0010000 0500 08
0070 00 +0025400 +0012500 +0000000 0000 09
2. Tab sequential format
0050 TAB 00 TAB +0025400 TAB +0012500 TAB +0000000 TAB
0060 TAB 01 TAB TAB TAB -0010000 TAB 0500 TAB 08
0070 TAB 00 TAB TAB TAB -0000000 TAB 0000 TAB 09
3. Word address format
N50 G00 X25400 Y125 Z0 F0
N60 G01 Z-10000 F500 M08
N70 G00 Z0 M09
WORD-ADDRESS CODING
Example CNC Program
N5 G90 G20 Each instruction to the machine consists of a letter
N10 M06 T3 followed by a number.
N15 M03 S1250
Each letter is associated with a specific type of action
N20 G00 X1 Y1
or piece of information needed by the machine.
N25 Z0.1
N30 G01 Z-0.125 F5
N35 X3 Y2 F10
N40 G00 Z1 Letters used in Codes
N45 X0 Y0 N,G,X,Y,Z,A,B,C,I,J,K,F,S,T,R,M
N50 M05
N55 M30
THE BASIC CNC CODE?
G00 Rapid traverse G40 Cutter compensation – cancel
G01 Linear interpolation G41 Cutter compensation – left
G02 Circular interpolation, CW G42 Cutter compensation- right
G03 Circular interpolation, CCW G70 Inch format
G04 Dwell G71 Metric format
G08 Acceleration G74 Full-circle programming off
G09 Deceleration G75 Full-circle programming on
G17 X-Y Plane G80 Fixed-cycle cancel
G18 Z-X Plane G81-G89 Fixed cycles
G19 Y-Z Plane G90 Absolute dimensions
G20 Inch Units (G70) G91 Incremental dimensions
G21 Metric Units (G71)
M CODES
M are miscellaneous functions to turn things on
and off, flood coolant, air blast, close machine
doors, and more.
Every machine has different M commands, so
make sure to read your machine’s manual
carefully.
M CODES
M00 Program stop
M01 Optional program stop
M02 Program end
M03 Spindle on clockwise
M04 Spindle on counterclockwise
M05 Spindle stop
M06 Tool change
M08 Coolant on
M09 Coolant off
M10 Clamps on
M11 Clamps off
M30 Program stop, reset to start
N CODES
Gives an identifying number for each block of
information.
It is generally good practice to increment each
block number by 5 or 10 to allow additional
blocks to be inserted if future changes are
required.
N is the line number the program is in and it
ensures that the program follows a logical order.
X,Y, AND Z CODES
X, Y, and Z codes are used to specify the coordinate
axis.
Number following the code defines the coordinate at
the end of the move relative to an incremental or
absolute reference point.
X Y Z commands are used to define X, Y, and Z
coordinates. The Z-axis is the “scariest” to program
and the one we recommend being extra careful with,
as crashing a machine spindle is always an expensive
fix. This is where simulating comes in handy. (More
on that later.)
COORDINATE SYSTEMS
F, S, AND T CODES
F stands for feed rate, which is basically how fast the machine
should move from point to point. It’s usually expressed in
inches/minute or mm/minute.
S is spindle speed, which determines how fast the spindle should
rotate in rotations per minute (rpm).
T is used for tool selection; most CNC mills now have 10 or
more tools ready to use on the machine.
I and J are used for arcs. The X and Y coordinates of where the
arc ends are needed as well as the X and Y distances from the arc
start point to the arc center point.
G-CODE EXAMPLE:
Take a look at the following G-code example:
N2 G01 X10 Y20 Z5 F300 S3000 T1
Breaking it down, we can determine that
the program is in line 2;
the spindle is going to perform a feed move (G01) from (X0, Y0, Z0), the starting position,
to (X10, Y20, Z5);
the selected tool is tool 1 (T1);
the feed speed is 300 mm/min (F300); and the spindle rpm is 3000 (S3000).
CNC PROGRAMMING BASICS
Program structure
Every program consists of:
1.Program Start
The program start is the program
number. The program number begins
with character/letter O.
2.Program Contents NC blocks
3.Program End
M30 for a main program
COMMAND STRUCTURE
BLOCK FORMAT SAMPLE BLOCK
N135 G01 X1.0 Y1.0 Z0.125 F5
Restrictions on CNC blocks
Each may contain only one tool move
Each may contain any number of non-tool move G-codes
Each may contain only one feedrate
Each may contain only one specified tool or spindle speed
The block numbers should be sequential
Both the program start flag and the program number must be
independent of all other commands (on separate lines)
The data within a block should follow the sequence shown in the
above sample block
X G00 运 动 轨 迹
B C
G00 Rapid motion
G00 X_ Y_ ; G01 运 动 轨 迹
The tool in the point A, rapidly
56
A
moves to point C.
43
G90 G00 X117 Y65; O Z
G91 G00 X85 Y31;
The motion track is ABC. 32
63
G01 X117 Y65 linear interpolation 117
motion
Its trajectory due to the specific CNC system varies
(5000-10000mm/min), Uncertain trajectory.
Feed rate is invalid for G00
To prevent tool collision ,G00 is only used when the tool is
high enough.
20
G01 Linear interpolation
G01 X_ Y_ Z_ F_;
X G00 运 动 轨 迹
B C
G01 运 动 轨 迹
56
A
43
O Z
32
63
117
21
G01 Linear interpolation
X G00 运 动 轨 迹
G01 X_ Y_ Z_ F_;
G90 G01 X117 Y65 F100; B C
G91 G01 X85 Y31 F100;
G01 运 动 轨 迹
56
A
43
O Z
32
63
117
22
G02,G03 circular interpolation
G02 Clockwise motion; G03 Counter Clockwise motion.
Format 1: G02 (G03) X_ Y_Z_I_J_K _ F;
x,y ,z are the value of arc end point;
I,J,K are the incremental value of the center.
F is feed rate.
I is related to the X axis, J is related to the y axis, and K
is related
to the Z axis.
Format2: G02 (G03) X_ Y_Z_R_ F;
x,y ,z are the value of arc end point;
R: the radius value of the arc.
if the arc is smaller than 180°,the R is plus.
if the arc is bigger than 180°,the R is minus.
The whole circle isn’t used R to express.
23
Format 2: G02 (G03) X_ Y_ I_ J_ F_;
I, J, K is corresponding to X, Y, Z
The I, J, K value is the distance and direction
from starting point to the center of the arc.
Absolute mode :
G00 X200.0 Y40.0 ;
G90 G03 X140.0 Y100.0 I-60.0 F300 ; Y
G02 X120.0 Y60.0 I-50.0 ;
or G00 X200.0 Y40. ; B
100
R50
G90 G03 X140.0 Y100.0 R60.0 F300 ;
60
G02 X120.0 Y60.0 R50.0 ; C R60
40
A
X
O 90 120 140 200
200
24
Format 2: G02 (G03) X_ Y_ I_ J_ F_;
I, J, K is corresponding to X, Y, Z
The I, J, K value is the distance and direction
from starting point to the center of the arc.
Incremental mode :
G91 G03 X-60.0 Y60.0 I-60.0 F300 ;
G02 X-20.0 Y-40.0 I-50.0 ; Y
G91 G03 X-60.0 Y60.0 R60.0 F300 ;
G02 X-20.0 Y-40.0 R50.0 ; B
100
R50
60 R60
C
40
A
X
O 90 120 140 200
200
25
G28 Automatic return to reference point
G29 Return from reference point
G28 X_ Y_ Z_ ;
The tool rapidly moves to middle point( X, Y,
Z ) ,then return to reference point.
G29 X_ Y_ Z_ ;
The tool from reference point
rapidly moves to middle point,
then moves to point ( X, Y, Z ) .
26
G28 Automatic return to reference point
G29 Return from reference point
Example:
A(100,170) B(200,270) C(500,100)
G91 G28 X100.0 Y100.0;
M06;
G29 X300.0 Y170;
M06: Stops the program and change
tool
27
G43, G44, G49 tool length compensation
G43 Tool length positive compensation
G44 Tool length negative compensation
实 T03
G49 Tool length cancellation 际
移 T03
Setting up tool length compensation 动
量
程
G43 (G44) G01 (G00) Z_ H_ ; 序
给
定 刀
Cancel tool length compensation 值 具
偏
G49 G01 (G00) Z_ ; 置
量
H00~H99: The number of tool length
compensation. It is usually kept as the same
number as the tool magazine number.(tool
Magazine 1 will use H code 1(H01)).
Thorough MDI inputting the tool length
compensation value to the address H of register.
The value of H00 is always 0.
28
G04 Dwell for a certain time ( Stopping to
feed , the spindle remaining turn )
G04 X_ ; s
G04 P_ ; ms
G91 G01 Z-7. F60 ;
G04 X5. ;(刀具在孔底停留 5s )
G00 Z7. ;
29
换 刀 位 置
Test 1:
05
001
(H01)=50 (H02)=100
φ 20 φ 10 φ 20
X
O
91
91
03
Y
08
06
04
02
20 30
换 刀 位 置
05
001
Test 1:
(H01)=50 (H02)=100
Z
N011 G04 P2000 ;
φ 20
N012 G00 Z3.0 T02 ; O
φ 20 φ 10
X
O0001 ; N013 G91 G28 Z0 M06 ;
91
91
N001 G54 T01 ; N014 S800 M03 ;
03
N002 G91 G28 Z0 M06 ; N015 G43 G00 H02 Z3.0 ;
N003 S600 M03 ; N016 G90 X50.0 Y20.0 ; Y
N004 G90 G00 X20.0 N017 Z3.0 M07 ;
Y60.0 ; N018 G01 Z-35.0 F50 ;
N005 G43 H01 Z3.0 M07 ; N019 G00 Z100.0 M09 ;
N006 G01 Z-19.0 F50 ; N020 M05 ;
08
N007 G04 P2000 ; N021 M30 ;
06
04
N008 G00 Z3.0 ;
02
X
N009 G00 X110.0 Y40.0 ;
20
N010 G01 Z-19.0 F50 ; 50
110
130
31
Home Work 0
32
G41, G42, G40 cutting radius compensation 刀 位 点 轨 迹
G41 Left cutting compensation
零 件 轮 廓 轨 迹
刀 位 点 轨 迹
零 件 轮 廓 轨 迹
G42 Right cutting compensation
G40 Cancel cutting compensation
G41 (G42) G01 (G00) X_ Y_ F_ D_ ;
D00~D99: The number of cutting
Radius compensation.
Thorough MDI inputting the radius
value to the address D in advance.
Cancel cutting radius compensation
G40 G01 (G00) X_ Y_ F_ D;
The value of D00 is always 0.
33
Example:
N01G90 G42 G01 X30.0 Y30.0 F200
D01 ;
g
N02 G01 X110.0 ; Y h
G
N03 G03 X130.0 Y50.0 J20.0 ; f e R10
N04 G01 Y70.0 ; F E
d
N05 G03 X120.0 Y80.0 I-10.0 ; D
07
N06 G01 X50.0 ;
05
C c
N07 X30.0 Y100.0 ; B
i A
N08 Y30.0 ;
a 20 b
N09 G40 G00 X0 Y0 ;
03
100
O X
Φ 10
30
Program zero
34
Exersice
35
Test 2:
O0002 ;
G91 G00 X70. Y45. S800 M03 ;
G43 Z-22. H02 ;
G01 Z-18. F100 M08 ;
G04 X5. ;
G00 Z18. ;
X30. Y-20. ;
G01 Z-33. F100 ;
G00 G49 Z55. M09 ;
X-100. Y-25. ;
M30 ;
36
INFORMATION NEEDED BY A CNC
1. Preparatory Information: units, incremental or absolute positioning
2. Coordinates: X,Y,Z, RX,RY,RZ
3. Machining Parameters: Feed rate and spindle speed
4. Coolant Control: On/Off, Flood, Mist
5. Tool Control: Tool and tool parameters
6. Cycle Functions: Type of action required
7. Miscellaneous Control: Spindle on/off, direction of rotation, stops for part movemen
This information is conveyed to the machine through a set
of instructions arranged in a desired sequence – Program.
List of Best CNC Controller Board Manufacturers in
World
Every machine uses a slightly different type of G-code, so a Fanuc G-
code won’t work on a Haas controller; however, the basics are the
same. Shown below is the list of Best CNC Controller Board
Manufacturers
Fanuc-Japan in World. Haas-America
Mitsubishi-Japan
Huazhog CNC-China
Siemens-German
Heidenhain-German
NUM-France
Mazak-Japan
Bosch Rexroth-German