search

BA1

NC PROGRAM

G-codes M-codes 



Contents

 [hide]

[edit]Specific codes

G-codes are also called preparatory codes, and are any word in a CNC program that begins with the letter "G". Generally it is a code telling the machine tool what type of action to perform, such as:
  • rapid move
  • controlled feed move in a straight line or arc
  • series of controlled feed moves that would result in a hole being bored, a workpiece cut (routed) to a specific dimension, or a decorative profile shape added to the edge of a workpiece.
  • set tool information such as offset.
There are other codes; the type codes can be thought of like registers in a computer.

[edit]Letter addresses

Some letter addresses are used only in milling or only in turning; most are used in both. Bold below are the letters seen most frequently throughout a program.
VariableDescriptionCorollary info
AAbsolute or incremental position of A axis (rotational axis around X axis)
BAbsolute or incremental position of B axis (rotational axis around Y axis)
CAbsolute or incremental position of C axis (rotational axis around Z axis)
DDefines diameter or radial offset used for cutter compensation
EPrecision feedrate for threading on lathes
FDefines feed rate
GAddress for preparatory commandsG commands often tell the control what kind of motion is wanted (eg, rapid positioning, linear feed, circular feed, fixed cycle) or what offset value to use.
HDefines tool length offset
IDefines arc size in X axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.
JDefines arc size in Y axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles.
KDefines arc size in Z axis for G02 or G03 arc commands. Also used as a parameter within some fixed cycles, equal to L address (which see).
LDefines number of repetitions ("loops") of a fixed cycle at each position.Assumed to be 1 unless programmed with another integer. Sometimes the K address is used instead of L. With incremental positioning (G91), a series of equally spaced holes can be programmed as a loop rather than as individual positions.
MMiscellaneous functionAction code, auxiliary command; descriptions vary. Many M-codes call for machine functions, which is why people often say that the "M" stands for "machine", although it was not intended to.
NLine number in programOptional, so often omitted.
OProgram nameFor example, O4501.
PWith G04, defines dwell time value. Also serves as a variable in some canned cycles, representing dwell times or other variables. Also used to call subroutines.
QPeck increment in canned cyclesFor example, G73, G83 (peck drilling cycles)
RDefines size of arc radius or defines retract height in canned cycles
SDefines speed, either spindle speed or surface speed depending on modeData type = integer. In G97 mode (which is usually the default), an integer after S is interpreted as a number of rev/min (rpm). In G96 mode (CSS), an integer after S is interpreted as surface speed—sfm (G20) or m/min (G21). See also Speeds and feeds.
TTool selectionTo understand how the T address works and how it interacts (or not) with M06, one must study the various methods, such as lathe turret programming, ATC fixed tool selection, ATC random memory tool selection, the concept of "next tool waiting", and empty tools. Programming on any particular machine tool requires knowing which method that machine uses.
UIncremental axis parallel to X axis (typically only lathe group A controls)In these controls, X and U obviate G90 and G91, respectively.
VIncremental axis parallel to Y axisVery rarely used. Most lathes that use U and W don't have a Y-axis, so they don't use V.
WIncremental axis parallel to Z axis (typically only lathe group A controls)In these controls, Z and W obviate G90 and G91, respectively.
XAbsolute or incremental position of X axis. Also defines dwell time on some machines (instead of "P" or "U").
YAbsolute or incremental position of Y axis
ZAbsolute or incremental position of Z axisThe main spindle's axis of rotation often determines which axis of a machine tool is labeled as Z.

[edit]List of G-codes for milling

CodeDescriptionCorollary info
G00Rapid positioning
G01Linear interpolation
G02CW circular interpolation
G03CCW circular interpolation
G04DwellTakes an address for dwell period (may be X, U, or P)
G05.1 Q1.Ai Nano contour control
G05 P10000HPCC
G07Imaginary axis designation
G09Exact stop check
G10/G11Programmable Data input/Data write cancel
G12CW full-circle interpolationFixed cycle for ease of programming 360° circular interpolation with blend-radius lead-in and lead-out. Not standard on Fanuc controls.
G13CCW full-circle interpolationFixed cycle for ease of programming 360° circular interpolation with blend-radius lead-in and lead-out. Not standard on Fanuc controls.
G17XY plane selection
G18ZX plane selection
G19YZ plane selection
G20Programming in inchesSomewhat uncommon except in USA and (to lesser extent) Canada and UK. However, in the global marketplace, competence with both G20 and G21 always stands some chance of being necessary at any time. The usual minimum increment in G20 is one ten-thousandth of an inch (0.0001"), which is a larger distance than the usual minimum increment in G21 (one thousandth of a millimeter, .001 mm, that is, one micrometre). This physical difference sometimes favors G21 programming.
G21Programming in millimeters (mm)Prevalent worldwide. However, in the global marketplace, competence with both G20 and G21 always stands some chance of being necessary at any time.
G28Return to home position (machine zero, aka machine reference point)Takes X Y Z addresses which define the intermediate point that the tool tip will pass through on its way home to machine zero. They are in terms of part zero (aka program zero), NOT machine zero.
G30Return to secondary home position (machine zero, aka machine reference point)Takes a P address specifying which machine zero point is desired, if the machine has several secondary points (P1 to P4). Takes X Y Z addresses which define the intermediate point that the tool tip will pass through on its way home to machine zero. They are in terms of part zero (aka program zero), NOT machine zero.
G31Skip function (used for probes and tool length measurement systems)
G33Constant pitch threading
G34Variable pitch threading
G40Tool radius compensation offKills G41 or G42.
G41Tool radius compensation leftGiven righthand-helix cutter and M03 spindle direction, G41 corresponds to climb milling. Takes an address (D or H) that calls an offset register value for radius.
G42Tool radius compensation rightGiven righthand-helix cutter and M03 spindle direction, G42 corresponds to conventional milling. Takes an address (D or H) that calls an offset register value for radius.
G43Tool height offset compensation negativeTakes an address, usually H, to call the tool length offset register value. The value is negative because it will be added to the gauge line position. G43 is the commonly used version (vs G44).
G44Tool height offset compensation positiveTakes an address, usually H, to call the tool length offset register value. The value is positive because it will be subtracted from the gauge line position. G44 is the seldom-used version (vs G43).
G45Axis offset single increase
G46Axis offset single decrease
G47Axis offset double increase
G48Axis offset double decrease
G49Tool length offset compensation cancelKills G43 or G44.
G50Define the maximum spindle speed
G52Local coordinate system (LCS)
G53Machine coordinate system
G54 to G59Work coordinate systems (WCSs)Have largely replaced position register (G92).
G54.1 P1 to P48Extended work coordinate systemsUp to 48 more WCSs besides the 6 provided as standard by G54 to G59. Note floating-point extension of G-code data type (formerly all integers). Other examples have also evolved (e.g., G84.2). Modern controls have the hardware to handle it.
G73Peck drilling cycle - high-speed (NO full retraction from pecks)Retracts only as far as a clearance increment (system parameter). For when chipbreaking is the main concern, but chip clogging of flutes is not.
G74Tapping cycle - lefthandthread, M04 spindle direction
G76Fine boring canned cycle
G80Cancel canned cycleKills all cycles such as G73, G83, G88, etc. Z-axis returns either to Z-initial level or R-level, as programmed (G98 or G99, respectively).
G81Simple drilling cycleNo dwell
G82Drilling cycle with dwellDwells at hole bottom (Z-depth) for the number of milliseconds specified by the P address. Good for when hole bottom finish matters.
G83Peck drilling cycle (full retraction from pecks)Returns to R-level after each peck. Good for clearing flutes of chips.
G84Tapping cycle -righthand thread, M03 spindle direction
G84.2Tapping cycle -righthand thread, M03 spindle direction - rigid toolholder
G90Absolute programmingPositioning defined with reference to part zero
G91Incremental programmingPositioning defined with reference to previous position
G92Position register (programming of vector from part zero to tool tip)If the operator was not allowed to overwrite G92 X Y Z values, then the programmer was effectively mandating that the part be placed on the table with perfect precision in a certain machine-referenced XYZ point. If programmer and/or management were ignorant of setup realities, this was very inefficient. WCSs (G54 et al) are a newer way to relate the part origin to the machine origin.
G94Feedrate per minute
G95Feedrate per revolution
G96Constant surface speed
G97Constant spindle speed
G98Return to initial Z level in canned cycle
G99Return to R level in canned cycle

[edit]List of G-codes for turning

FANUC G Codes for turning
CodeDescription
G00Rapid positioning
G01Linear interpolation
G02CW circular interpolation
G03CCW circular interpolation
G04Dwell
G09Exact stop check
G10/G11Programmable Data input/Data write cancel
G20Programming in inches
G21Programming in mm
G28Return to home position (machine zero, machine reference point)
G30Return to secondary home position (machine zero, machine reference point)
G40Tool radius compensation off
G41Tool radius compensation left
G42Tool radius compensation right
G50Define the maximum spindle speed OR position register
G53Machine coordinate system
G54 to G59Work coordinate systems
G54.1 P1 to P48Extended work coordinate systems
G80Cancel canned cycle
G90Absolute programming (type B and C systems; type A uses U/W address instead)
G91Incremental programming (type B and C systems; type A uses U/W address instead)
G92Position register (type B and C systems; type A uses G50)
G96Constant surface speed (CSS)
G97Constant spindle speed (that is, CSS cancel)
G98Feedrate per minute (type A)
G99Feedrate per revolution (type A)

[edit]List of M-codes for milling

CodeDescriptionCorollary info
M00Compulsory stop(non-optional—machine will always stop upon reaching M00 in the program execution)
M01Optional stop(machine will only stop at M01 if operator has pushed the optional stop button)
M02End of program(no return to program top; may or may not reset register values)
M03Spindle on (CW rotation)
M04Spindle on (CCW rotation)
M05Spindle stop
M06Automatic tool change (ATC)To understand how the T address works and how it interacts (or not) with M06, one must study the various methods, such as lathe turret programming, ATC fixed tool selection, ATC random memory tool selection, the concept of "next tool waiting", and empty tools. Programming on any particular machine tool requires knowing which method that machine uses.
M07Coolant on (mist)
M08Coolant on (flood)
M09Coolant off
M10Pallet clamp on
M11Pallet clamp off
M19Spindle orientationSpindle orientation is more often called within cycles (automatically) or during setup (manually), but it is also available under program control via M19. The abbreviation OSS (oriented spindle stop) may be seen in reference to an oriented stop within cycles.
M21Mirror, X-axis
M22Mirror, Y-axis
M23Mirror OFF
M30End of program with return to program top
M48Feedrate override allowed
M49Feedrate override NOT allowedThis rule is also called (automatically) within tapping cycles, where feed is precisely correlated to speed. Same with spindle speed override and feed hold button.
M60Automatic pallet change (APC)
M98Subprogram call
M99Subprogram endUsually placed at end of subprogram, where it returns execution control to the M98 point of the main program.

[edit]List of M-codes for turning

CodeDescriptionCorollary info
M00Compulsory stop(non-optional—machine will always stop upon reaching M00 in the program execution)
M01Optional stop(machine will only stop at M01 if operator has pushed the optional stop button)
M02End of program(no return to program top; may or may not reset register values)
M03Spindle on (CW rotation)
M04Spindle on (CCW rotation)
M05Spindle stop
M06Automatic tool change (ATC)(many lathes do not use M06 because the T address itself indexes the turret)
M07Coolant on (mist)
M08Coolant on (flood)
M09Coolant off
M19Spindle orientation
M21Tailstock forward
M22Tailstock backward
M23Thread gradual pullout ON
M24Thread gradual pullout OFF
M30End of program with return to program top
M41Gear select - gear 1
M42Gear select - gear 2
M43Gear select - gear 3
M44Gear select - gear 4
M48Feedrate override allowed
M49Feedrate override NOT allowedThis rule is also called (automatically) within tapping cycles, where feed is precisely correlated to speed. Same with spindle speed override and feed hold button.
M98Subprogram call
M99Subprogram endUsually placed at end of subprogram, where it returns execution control to the M98 point of the main program. Can also be used in main program with block skip for endless loop of main program on bar work (until operator toggles block skip)

[edit]Example program

This is a generic program that demonstrates the use of G-Code to turn a 1" diameter X 1" long part. Assume that a bar of material is in the machine and that the bar is slightly oversized in length and diameter and that the bar protrudes by more than 1" from the face of the chuck. (Caution: This is generic, it might not work on any real machine! Pay particular attention to point 5 below.)
Tool Path for program
Sample
LineCodeDescription
O4968(Sample face and turn program)
N01M216(Turn on load monitor)
N02G20 G90 G54 G40(Inch units. Absolute mode. Call work offset values. Cancel any existing tool radius offset.)
N03G50 S2000(Set maximum spindle speed rev/min - preparing for G96 CSS coming soon)
N04M01(Optional stop)
N05T0303 M6(Select tool 3 from the carousel, use tool offset values located in line 3 of the program table, index the turret to select new tool)
N06G96 S854 M42 M03 M08(Variable speed cutting, 854 sfm, select spindle gear, start spindle CW rotation, turn on the coolant flood)
N07G41 G00 X1.1 Z1.1(Call tool radius offset. Rapid feed to a point 0.1" from the end of the bar and 0.05" from the side)
N08G01 Z1.0 F.05(Feed in horizontally until the tool is standing 1" from the datum ie program Z-zero)
N09X-0.002(Feed down until the tool is slightly past center, thus facing the end of the bar)
N10G00 Z1.1(Rapid feed 0.1" away from the end of the bar - clear the part)
N11X1.0(Rapid feed up until the tool is standing at the finished OD)
N12G01 Z0.0 F.05(Feed in horizontally cutting the bar to 1" diameter all the way to the datum, feeding at 0.050" per revolution)
N13G00 X1.1 M05 M09(Clear the part, stop the spindle, turn off the coolant)
N14G91 G28 X0(Home X axis - return to machine X-zero passing through no intermediate X point [incremental X0])
N15G91 G28 Z0(Home Z axis - return to machine Z-zero passing through no intermediate Z point [incremental Z0])
N16G90 M215(Return to absolute mode. Turn off load monitor)
N17M30(Program stop, rewind to beginning of program)
%
Several points to note:
  1. There is room for some programming style, even in this short program. The grouping of codes in line N06 could have been put on multiple lines. Doing so may have made it easier to follow program execution.
  2. Many codes are "modal", meaning that they stay in effect until they are cancelled or replaced by a contradictory code. For example, once variable speed cutting (CSS) had been selected (G96), it stayed in effect until the end of the program. In operation, the spindle speed would increase as the tool neared the center of the work in order to maintain a constant surface speed. Similarly, once rapid feed was selected (G00), all tool movements would be rapid until a feed rate code (G01, G02, G03) was selected.
  3. It is common practice to use a load monitor with CNC machinery. The load monitor will stop the machine if the spindle or feed loads exceed a preset value that is set during the set-up operation. The job of the load monitor is to prevent machine damage in the event of tool breakage or a programming mistake. On small or hobby machines, it can warn of a tool that is becoming dull and needs to be replaced or sharpened.
  4. It is common practice to bring the tool in rapidly to a "safe" point that is close to the part - in this case 0.1" away - and then start feeding the tool. How close that "safe" distance is, depends on the skill of the programmer and maximum material condition for the raw stock.
  5. If the program is wrong, there is a high probability that the machine will crash, or ram the tool into the part under high power. This can be costly, especially in newer machining centers. It is possible to intersperse the program with optional stops (M01 code) which allow the program to be run piecemeal for testing purposes. The optional stops remain in the program but they are skipped during the normal running of the machine. Thankfully, most CAD/CAM software ships with CNC simulators that will display the movement of the tool as the program executes. Many modern CNC machines also allow programmers to execute the program in a simulation mode and observe the operating parameters of the machine at a particular execution point. This enables programmers to discover semantic errors (as opposed to syntax errors) before losing material or tools to an incorrect program. Depending on the size of the part, wax blocks may be used for testing purposes as well.
  6. For pedagogical purposes, line numbers have been included in the program above. They are usually not necessary for operation of a machine, so they are seldom used in industry. However, if branching or looping statements are used in the code, then line numbers may well be included as the target of those statements (e.g. GOTO N99).
  7. Some machines do not allow multiple M codes in the same line.

[edit]Programming environments

G-code's programming environments have evolved in parallel with those of general programming—from the earliest environments (e.g., writing a program with a pencil, typing it into a tape puncher) to the latest environments that stack CAD, CAM, and richly featured G-code editors. (G-code editors are analogous to XML editors, using colors and indents semantically [plus other features] to aid the user in ways that basic text editors can't. CAM packages are analogous to IDEs in general programming.)
Two high-level paradigm shifts have been (1) abandoning "manual programming" (with nothing but a pencil or text editor and a human mind) for CAM software systems that generate G-code automatically via postprocessors (analogous to the development of visual techniques in general programming), and (2) abandoning hardcoded constructs for parametric ones (analogous to the difference in general programming between hardcoding a constant into an equation versus declaring it a variable and assigning new values to it at will). Macro (parametric) CNC programming uses human-friendly variable names, relational operators, and loop structures much as general programming does, to capture information and logic with machine-readable semantics. Whereas older manual CNC programming could only describe particular instances of parts in numeric form, parametric CAM programming describes abstractions which can be flowed with ease into a wide variety of instances. The difference is analogous to creating text as bitmaps versus using character encoding and glyphs, or to the way that HTML passed through a phase of using content markup for presentation purposes, then matured toward the CSS model. In all of these cases, a higher layer of abstraction was introduced in order to pursue what was missing semantically.
STEP-NC reflects the same theme, which can be viewed as yet another step along a path that started with the development of machine tools, jigs and fixtures, and numerical control, which all sought to "build the skill into the tool". Recent developments of G-code and STEP-NC aim to build the information and semantics into the tool.

1 ความคิดเห็น: