You are on page 1of 6

Course: Reference:

Mechanical Engineering 4Z03 Computer Numerical Control G-Codes

Introduction Most Computer Numerical Control (CNC) drilling, milling, and turning machines controllers adhere to the RS-274 CNC standard. CAD/CAM software typically writes the machine control codes into an intermediate cutter location data (CL-DATA) format. A post processor, tailored to the specific machine capabilities, then converts the CL-DATA format to the language understood by the machine tool controller. For more detailed information, refer to the references listed at the end of the document. CNC Command Lines Computer Numerical Controllers (CNCs) accept program input line by line. consists of a sequence of <address letter><numerical value> pairs. Comments are enclosed in parenthesis. For example (This is a comment.) Program Number Every CNC program begins with the O address letter that specifies the program number. Usually program numbers are restricted to the range 1-9999. No other address letter can appear on the same line as the program number. The physical end of the program is denoted by a line containing only the % (percent) character. For example, a complete CNC program (that accomplishes nothing), number 43, would be written O43 (This is program 43.) % Line Number Optionally, each command line can begin with a number, prefixed with the N address letter. Spindle Speed Machine tools are usually equipped with a rotating spindle which turns the cutting tool (milling) or turns the part (lathe). To specify the rotational speed in revolutions per minute, use the S address letter. For example, to specify 2300 rpm, code S2300 Note that the S address letter only specifies the spindle speed. To begin actual rotation, an M address letter must be specified. For clockwise rotation, specify M3. For counterclockwise rotation, specify M4. To stop rotation, specify M5. To stop the spindle at page 1 of 6 Mechanical Engineering 4Z03 Winter 2007 Each line

Computer Numerical Control G-Codes a repeatable orientation, specify M19. The M19 code is used to orient the spindle in preparation for an automatic tool change, when using boring bars, and when using touch trigger probes. For safety, never specify the S address letter while the spindle is rotating. The M codes are widely used for Programmable Logic Control (PLC) functions such as spindle rotation, tool changing, opening and closing of access doors, pallet changing, coolant on and off, etc. Tool Specification Many machine tools support automatic exchange of cutting tools using the T address letter. This indexes the carousel (milling) or turret (turning) to the correct position in preparation for tool exchange. The actual tool exchange occurs when the M6 code is encountered. Cutting Fluid (Coolant) Cutting fluid or coolant is widely used. This is controlled using M codes. To obtain mist coolant, code M7. To obtain flood coolant, code M8. To shut off coolant, code M9. Program End Each program should end with the M30 code. This instructs the CNC to complete execution and return to the first line of the program. For example, the following program loads tool number 7 into the spindle, begins spindle rotation at 500 rpm (clockwise), begins flood coolant, stops coolant, stops and orients the spindle, and ends execution: O43 (This is program 43.) T7M6 S500M3 M7 M9 M19 M30 % Units CNCs generally accept length input in either inch (G70) or millimeter (G71) mode. This is specified at the very beginning of the program. Axis Letters For 3-axis linear, orthogonal axis machines, the axis are labeled X, Y, Z. A right hand rule coordinate system is always used. Units are as previously specified using the G70 or G71 code. The resulting motion depends on which G address letters are in effect (see below). To avoid ambiguity, a decimal point should always be specified, even for integer values. For example, to specify an X value of 21, code X21. rather than X21 (without the .). Mechanical Engineering 4Z03 Winter 2007 page 2 of 6

Computer Numerical Control G-Codes Absolute/Incremental Mode With respect to the current position, axis motion can be specified either in absolute coordinates (G90) or incremental coordinates (G91). The absolute mode is more commonly used. Incremental mode most often is used with subroutines. Point to Point (Rapid) Motion Point to point motion (sometimes referred to as rapid) motion is specified using the G0 code. This causes the specified axes to move from the current position to the specified new position (G90 mode), or incrementally with respect to the current position (G91 mode). Assume, for example, that the machine is currently located at position (X,Y,Z)=(12.0,14.3,8.4). To move from this position to (X,Y,Z)=(15.0,14.3,10.0), code G90G0X15.Z10. or G91G0X3.Z1.6 Note that the motion is not interpolated. That is, the path from the current to the new position is not guaranteed to be a straight line. The speed of motion (feed rate) is determined by parameters permanently stored within the CNC memory. To obtain interpolated straight line motion at a specified feed rate, use the G1 code and F address letter (see below). Return to Machine Home Position Every machine has a primary reference or home position. To program a return to home position, code G28. The home position is normally where tool exchanges occur, and hence it is common to code G28 T7M6 Reset Coordinate System At power up, the machine zero position is identical to the machine home position. This is inconvenient for part programming with CAD/CAM systems, since it will not be known in advance where a part will be located on the machine. To resolve this, the G92 code can be used to reset the coordinate system registers. The typical procedure is to: 1. Power up and home the machine. 2. Set inch G70 or millimeter G71 mode. 3. Execute G92X0.Y0.Z0. 4. Manually move the machine to where the part coordinate system origin is to be located. page 3 of 6 Mechanical Engineering 4Z03 Winter 2007

Computer Numerical Control G-Codes Record the current machine coordinates as (mpx,mpy,mpz). 5. Return the machine to the home position using G28. 6. Execute G92X-mpxY-mpyZ-mpz. Note that the negative of the recorded coordinates are used at this step. The remainder of the CNC program can now be written in part coordinates. Drilling Canned Cycle Drilling holes is a common machining operation, both for metal parts, and applications such as printed circuit boards. The CNC code for drilling is G81. The complete command line is of the form G81XxvalYyvalZzvalRrvalFfvalTtval The machine will move in point to point (rapid) motion from the current position to the position (xval,yval,rval). It then feeds along the Z axis from the position (xval,yval,rval) to the position (xval,yval,zval) at feed rate fval. For inch (G70) mode, the feed rate is specified in inches per minute. For millimeter (G71) mode, the feed rate is specified in millimeters per minute. When the position (xval,yval,zval) is reached, motion stops for a dwell period of tval seconds. Rapid motion back to the position (xval,yval,rval) then occurs. Omitted address letters cause the previously specified value to be used. The default dwell is zero seconds. This sequence is repeated each time a new line containing an axis letter is encountered, until the G80 (end of canned cycle) code is encountered. Coding for a complete drilling example program is shown below. First, the command line is coded, immediately followed by a descriptive comment. O43 (specify the beginning of program number 43) N1G71 (specify millimetre units) N2G90 (specify absolute mode) N3G28 (return to home position) N4G92X345.Y280.Z450. (reset coordinate system for part programming) N5T1M6 (load tool 1 into the spindle) N6S2000M3 (begin clockwise spindle rotation at 2000 rpm) N7M7 (flood coolant on) N8G0X50.8Y25.4Z1.0 Mechanical Engineering 4Z03 Winter 2007 page 4 of 6

Computer Numerical Control G-Codes (move tool to position 50.8,25.4,1.0 at rapid rate) N9G81X50.8Y25.4Z-40.0R1.F200.T1. (drill at 200 mm/min to Z=-40., dwell 1 s, retract to Z=1.) N10G80Z10. (cancel drilling cycle, moves to Z=10.) N11X101.6 (move tool to 101.6,25.4,10. at rapid rate) N12G81Z-20.R1.T0. (drill at 200 mm/min to Z=-20., zero dwell, retract to Z=1. N13G80Z10. (cancel drilling cycle, moves to Z=10.) N14M9 (coolant off) N15M5 (spindle stop) N16G28 (return to home position) N17M30 (end of program) % Linear Interpolation The ability to simultaneously move more than one axis in coordinated motion is the principal benefit of numerical control. A common CNC operation is milling along a straight line, at a specified feed rate. This is accomplished using the G1 code. A typical command line is G1XxvalYyvalZzvalFfval In G90 (absolute mode), the machine moves from its current position to (xval,yval,zval)at feed rate fval. In G91 (incremental mode), the machine moves from its current position by a delta amount (xval,yval,zval). The feed rate units are inches per minute (G70) mode or millimeters per minute (G71) mode. For example, if the machine is currently located at position (200.,300.,430.), then the absolute mode command lines to move to position (240.,280.,430.) at a feed rate of 350 millimetres per minute are G71 G90 G1X240.Y280.F350. The equivalent incremental mode command lines are G71 G91 G1X40.Y-20.F350. page 5 of 6 Mechanical Engineering 4Z03 Winter 2007

Computer Numerical Control G-Codes Note that, in either case, because the coordinates do not change, the Z address letter is not required. Circular Interpolation Although a circular motion can always be approximated by a sequence of short linear moves, the frequency with which holes, fillets, etc. are machined led CNC designers to also include circular interpolation in controllers. Within an individual command line, many controllers are restricted to motion within a single circle quadrant. This will be assumed in the following discussion. Circular interpolation must occur within one of the three principal planes. To specify the XY plane, code G17. To specify the ZX plane, code G18. To specify the YZ plane, code G19. Clockwise motion (from the positive Y axis towards the positive X axis in the XY plane) is specified using G2. Counterclockwise motion is specified using G3. The center of the circle is specified relative to the current position. Use the I address letter for the X axis, the J address letter for the Y axis, and the K address letter for the Z axis. For example, to machine along a counterclockwise arc in the XY plane centered at position (10.,15.,5.), beginning at (20.,15.,5.), and ending at (10.,25.,5.) code G90G17G3X10.Y25.I-10.J0. To machine along a clockwise arc centered at (10.,15.,5.), (17.071,22.071,5.), and ending at (20.,15.,5.) code G90G17G2X20.Y15.I-7.071J-7.071 Feed rates can be specified using Ffval as described for linear interpolation. Cutter Length and Radius Compensation For flexibility, it is desirable to delay specification of the exact cutter dimensions until just before execution of the NC program. The codes G40, G41, G42, G43, G44, and G49 are used for this purpose. Details are available in other references. References [1] Y. Koren, Computer Control of Manufacturing Systems, McGraw-Hill, TS176.K6515 1983. [2] I. Zeid, CAD/CAM Theory and Practice, McGraw-Hill, TS155.6.Z45 1991. beginning at

Mechanical Engineering 4Z03 Winter 2007

page 6 of 6