G-code
Language standard
24min
this chapter describes the input language, rs274/ngc the rs274/ngc language is based on lines of code each line (a "block") may include commands to a machining center to do several things lines of code may be collected in a file to make a program a typical line of code consists of an optional line number at the beginning followed by one or more "words " a word consists of a letter followed by a number (or something that evaluates to a number) a word may either give a command or provide an argument to a command for example, g1 x3 is a valid line of code with two words g1 is a command meaning "move in a straight line at the programmed feed rate," and x3 provides an argument value (the value of x should be 3 at the end of the move) most rs274/ngc commands start with either g or m (for miscellaneous) the words for these commands are called "g codes" and "m codes " the rs274/ngc language does not have an indicator for the start of a program it has two commands ( m2 or m30 ), either of which ends a program system parameters/variables in the rs274/ngc language view, a machining center maintains 5999 numerical parameters they can be accessed by #1 #5999 the specific parameters with a dedicated function are listed in the table below other parameters in the range of 1 5999 are free to use in your g code program some of them have a special function and are read only; see the table below parameter number purpose 1 26 used for parameters when overriding m functions in the g code there is e g m999 x100 s1000 and you have in your macro cnc sub m999 msg “this is my m999 x=”#24” s=”#19 endsub inside the subroutine, the given x and s parameters are at #24 and #19 #1 #26 = a – z parameter value values are negative 1e10 if not provided with m999 in this example 27 4999 free to use, note that 4995 – 4999 are used by the tool length measurement function under user button 2 z#4996 safe height for tool measurement above the fixed tool setter x#4997 y#4998 fixed tool setter position z#4999 chuck height, or zero length tool height, chuck just touches the fixed tool setter at this height z#4995 is used for the tool setter height for zeroing z under user button 1 4000 4999 free to use, persistent, see above some of these are used by tool length measurement and zeroing 5001 5006 position of x – c, interpreter position = work position 5008 current tool number 5009 current tool radius 5010 current tool z offset (length + zdelta) 5011 new tool number during tool change (m6) 5012 current tool x offset (x offset + xdelta) 5013 current g43 z offset (z offset + zdelta) 5014 current g43 x offset (x offset + xdelta) 5015 5050 used in tool change sub routine 5051 5056 probe position x c in machine coordinates 5061 5066 probe position x c in work coordinates 5067 1 if the probe is triggered after g38 2 , 0 otherwise 5068 current probe value 5069 handwheel counter 5070 spindle rate in rev/second 5071 5076 position of x c, interpreter position without offsets = machine position 5081 5086 probe position x c in joint coordinates 5101 5106 mca negative limit x c 5111 5116 mca positive limit x c 5121 5126 home position x c 5131 5133 tca negative limit x z 5141 5143 tca positive limit x z 5150 active kinematics type 1 trivial 2 4 ax acylinder (y > a mapping) 3 virtual c 4 17 system reserved 18 30 custom 1 – custom 12 5151 zhc is active 5152 1 if the spindle is on, 0 if the spindle is off 5161 5166 g28 home x c 5181 5186 g30 home x c 5190 g68 rotation method (0 = off, 1 = on) 5191 5193 g68/g51 rotation point x c 5194 g68 rotation angle xy 5195 g68 rotation angle yz 5196 g68 rotation angle xz 5200 0 if g51 scaling is off, 1 if g51 scaling is on 5204 g51 scaling factor x 5205 g51 scaling factor y 5206 g51 scaling factor z (always 1 0) 5211 5216 g92 offset x c 5220 coordinate system number 5221 5226 coordinate system 1 x c 5241 5246 coordinate system 2 x c 5261 5266 coordinate system 3 x c 5281 5286 coordinate system 4 x c 5301 5306 coordinate system 5 x c 5321 5326 coordinate system 6 x c 5341 5346 coordinate system 7 x c 5361 5366 coordinate system 8 x c 5381 5386 coordinate system 9 x c 5390 spindle selection 0 = m90, 1 = m91, 2 = m92 5391 5393 alternative spindle offset x z 5394 5397 spindle speed max m90 m93 5230, 5250, 5270, 5290, 5310, 5330, 5350, 5370 reserved for rotation coordinate system 5380 simulation mode, 0 controller connected, 1 simulation mode 5397 running mode, 0 running, 1 rendering use e g if you have g38 2 movements in your macro file because during rendering g38 2 always runs until the given end point which may give incorrect tool measurement results always take care that the tool table contains (approx ) correct tool data 5398 return value of dlgmsg (1 = ok, 1 = cancel) 5399 return value for m55 , m56 5401 5499 tool z offset (length) for tools 1 99 5501 5599 tool diameter for tools 1 99 5601 5699 tool x offset (for lathe) for tools 1 99 5701 5799 tool orientation (for turning) for tools 1 99 5801 5899 tool x delta due to wear 5901 5999 tool z delta due to wear parameters in italics are read only tool data tool id zoffset (length) xoffset (for turning) diameter orientation 1 1 9 2 1 9 99 1 9 tool orientation for lathes when the g18 plane (x z) is selected, special lathe tool radius compensation can be used (g41, g42) depending on the tool orientation and tool radius an extra offset is applied the blue crosses show the radius center of the tool the green crosses show the controlled point depending on the tool orientation for orientation 9 there is no offset compensation for orientation 2, the compensation in x is –tool radius, in z also –tool radius coordinate systems in the rs274/ngc language view, a machining center has an absolute coordinate system and nine program coordinate systems you can set the offsets of the nine program coordinate systems using g10 l2 pn (n is the number of the coordinate system) with values for the axes in terms of the absolute coordinate system you can select one of the nine systems by using g54, g55, g56, g57, g58, g59, g59 1, g59 2, or g59 3 it is not possible to select the absolute coordinate system directly you can offset the current coordinate system using g92 or g92 3 this offset will then apply to all nine program coordinate systems this offset may be canceled with g92 1 or g92 2 you can make straight moves in the absolute machine coordinate system by using g53 with either g0 or g1 data for coordinate systems is stored in parameters, see the previous section during initialization, the coordinate system is selected that is specified by parameter 5220 a value of 1 means the first coordinate system (the one g54 activates), a value of 2 means the second coordinate system (the one g55 activates), and so on it is an error for the value of parameter 5220 to be anything but a whole number between one and nine the g code are described in detail in section 3 6 format of a line a permissible line of input rs274/ngc code consists of the following, in order, with the restriction that there is a maximum (currently 256) to the number of characters allowed on a line an optional line number any number of words, parameter settings, and comments any input not explicitly allowed is illegal and will cause the interpreter to signal an error spaces and tabs are allowed anywhere on a line of code and do not change the meaning of the line, except inside comments this makes some strange looking input legal the line g0x +0 12 34y 7 is equivalent to g0 x+0 1234 y7 , for example blank lines are allowed in the input they are to be ignored input is case insensitive line number a line number is the letter n followed by an integer (with no sign) between 0 and 99999 written with no more than five digits (000009 is not ok, for example) line numbers may be repeated or used out of order, although normal practice is to avoid such usage line numbers may also be skipped, and that is normal practice a line number is not required to be used but must be in the proper place if used word a word is a letter other than n followed by a real value words may begin with any of the letters shown in table 3 2 the table includes n for completeness, even though, as defined above, line numbers are not words several letters (i, j, k, l, p, and r) may have different meanings in different contexts letter meaning d tool radius compensation number f feed rate g general function (see table 3 4) h tool length offset index i x axis offset for arcs x offset in g87 canned cycle j y axis offset for arcs y offset in g87 canned cycle k z axis offset for arcs z offset in g87 canned cycle l number of repetitions in canned cycles key used with g10 m miscellaneous function (see table 3 6) n line number p dwell time in canned cycles dwell time with g4 key used with g10 q feed increment in g83 canned cycle r arc radius, clear z distance in canned cycle s spindle speed t tool selection x x axis of machine y y axis of machine z z axis of machine a a axis of machine b b axis of machine c c axis of machine a real value is some collection of characters that can be processed to come up with a number a real value may be an explicit number (such as 341 or 0 8807), a parameter value, an expression, or a unary operation value definitions of these follow immediately processing characters to come up with a number is called "evaluating" an explicit number evaluates itself number the following rules are used for (explicit) numbers in these rules, a digit is a single character between 0 and 9 a number consists of (1) an optional plus or minus sign, followed by (2) zero to many digits, followed, possibly, by (3) one decimal point, followed by (4) zero to many digits provided that there is at least one digit somewhere in the number there are two kinds of numbers integers and decimals an integer does not have a decimal point in it; a decimal does numbers may have any number of digits, subject to the limitation on line length only about seventeen significant figures will be retained, however (enough for all known applications) a non zero number with no sign as the first character is assumed to be positive notice that initial (before the decimal point and the first non zero digit) and trailing (after the decimal point and the last non zero digit) zeros are allowed but not required a number written with initial or trailing zeros will have the same value when it is read as if the extra zeros were not there numbers used for specific purposes in rs274/ngc are often restricted to some finite set of values or some to some range of values in many uses, decimal numbers must be close to integers; this includes the values of indexes (for parameters and carousel slot numbers, for example), m codes, and g codes multiplied by ten a decimal number that is supposed to be close to an integer is considered close enough if it is within 0 0001 of an integer parameter value a parameter number is the pound character # followed by an integer value between 1 and 5399 the value of the parameter is whatever number is stored in the numbered parameter the # character takes precedence over other operations, so that, for example, \[#1+2] means the number found by adding 2 to the value of parameter 1, not the value found in parameter 3 of course, #\[1+2] does mean the value found in parameter 3 the # character may be repeated; for example ##2 means the value of the parameter whose index is the (integer) value of parameter 2 examples using a parameter \#100 = 1 1234 \#101 = \[#100 + 1] g1 f\[#100] x\[#101 + 10] expressions and binary operations an expression is a set of characters starting with a left bracket \[ and ending with a balancing right bracket ] in between the brackets are numbers, parameter values, mathematical operations, and other expressions an expression may be evaluated to produce a number the expressions on a line are evaluated when the line is read, before anything on the line is executed an example of an expression is \[1 + acos\[0] \[#3 \[4 0/2]]] binary operations appear only inside expressions nine binary operations are defined there are four basic mathematical operations addition (+), subtraction ( ), multiplication ( ), and division (/) there are three logical operations non exclusive or (or), exclusive or (xor), and logical and (and) the eighth operation is the modulus operation (mod) the ninth operation is the "power" operation ( ) of raising the number on the left of the operation to the power on the right the binary operations are divided into three groups the first group is power the second group is multiplication, division, and modulus the third group is addition, subtraction, logical non exclusive or, logical exclusive or, and logical and if operations are strung together (for example in the expression \[2 0 / 3 1 5 5 5 / 11 0] ), operations in the first group are to be performed before operations in the second group, and operations in the second group before operations in the third group if an expression contains more than one operation from the same group (such as the first / and in the example), the operation on the left is performed first thus, the example is equivalent to \[((2 0 / 3) 1 5) (5 5 / 11 0)] , which simplifies to \[1 0 0 5] , which is 0 5 the logical operations and modulus are to be performed on any real numbers, not just on integers the number zero is equivalent to logical false, and any non zero number is equivalent to logical true unary operation value a unary operation value is either atan followed by one expression divided by another expression (for example atan\[2]/\[1+3] ) or any other unary operation name followed by an expression (for example sin\[90] ) the unary operations are abs (absolute value), acos (arc cosine), asin (arc sine), atan (arc tangent), cos (cosine), exp (e raised to the given power), fix (round down), fup (round up), ln (natural logarithm), round (round to the nearest whole number), sin (sine), sqrt (square root), and tan (tangent) arguments to unary operations which take angle measures ( cos , sin , and tan ) are in degrees values returned by unary operations which return angle measures ( acos , asin , and atan ) are also in degrees the fix operation rounds towards the left (less positive or more negative) on a number line, so that fix\[2 8] =2 and fix\[ 2 8] = 3, for example the fup operation rounds towards the right (more positive or less negative) on a number line; fup\[2 8] = 3 and fup\[ 2 8] = 2, for example parameter setting a parameter setting is the following four items one after the other (1) a pound character # , (2) a real value which evaluates to an integer between 1 and 5399, (3) an equal sign = , and (4) a real value for example, #3 = 15 is a parameter setting meaning "set parameter 3 to 15 " a parameter setting does not take effect until after all parameter values on the same line have been found for example, if parameter 3 has been previously set to 15 and the line #3=6 g1 x#3 is interpreted, a straight move to a point where x equals 15 will occur and the value of parameter 3 will be 6 comments and messages printable characters and white space inside parentheses are a comment a left parenthesis always starts a comment the comment ends at the first right parenthesis found thereafter once a left parenthesis is placed on a line, a matching right parenthesis must appear before the end of the line comments may not be nested; it is an error if a left parenthesis is found after the start of a comment and before the end of the comment here’s an example of a line containing a comment g80 m5 (stop motion) comments do not cause a machining center to do anything a comment contains a message if msg, appears after the left parenthesis and before any other printing characters variants of msg, which include white space and lowercase characters are allowed the rest of the characters before the right parenthesis are considered to be a message messages should be displayed on the message display device comments not containing messages need not be displayed there item repeats a line may have any number of g words, but two g words from the same modal group may not appear on the same line a line may have zero to four m words two m words from the same modal group may not appear on the same line for all other legal letters, a line may have only one word beginning with that letter if a parameter setting of the same parameter is repeated on a line, #3=15 #3=6 for example, only the last setting will take effect if more than one comment appears on a line, only the last one will be used; each of the other comments will be read and its format will be checked, but it will be ignored thereafter putting more than one comment on a line is expected to be very rare item order the three types of items whose order may vary on a line (as given at the beginning of this section) are word, parameter setting, and comment imagine that these three types of items are divided into three groups by type the first group (the words) may be reordered in any way without changing the meaning of the line if the second group (the parameter settings) is reordered, there will be no change in the meaning of the line unless the same parameter is set more than once in this case, only the last modification of the parameter will take effect for example, after line #3=15 #3=6 has been interpreted, the value of parameter 3 will be 6 if the order is reversed to #3=6 #3=15 and the line is interpreted, the value of parameter 3 will be 15 if the third group (the comments) contains more than one comment and is reordered, only the last comment will be used if each group is kept in order or reordered without changing the meaning of the line, then the three groups may be interleaved in any way without changing the meaning of the line for example, the line g40 g1 #3=15 (foo) #4= 7 0 has five items and means exactly the same thing in any of the 120 possible orders (such as #4= 7 0 g1 #3=15 g40 (foo) ) for the five items commands and machine modes in rs274/ngc, many commands cause a machining center to change from one mode to another, and the mode stays active until some other command changes it implicitly or explicitly such commands are called "modal" for example, if coolant is turned on, it stays on until it is explicitly turned off the g codes for motion are also modal if a g1 (straight move) command is given on one line, for example, it will be executed again on the next line if one or more axis words are available on the line, unless an explicit command is given on that next line using the axis words or canceling motion "non modal" codes have an effect only on the lines on which they occur for example, g4 (dwell) is non modal modal groups modal commands are arranged in sets called "modal groups", and only one member of a modal group may be in force at any given time in general, a modal group contains commands for which it is logically impossible for two members to be in effect at the same time like measure in inches vs measure in millimeters a machining center may be in many modes at the same time, with one mode from each modal group being in effect the modal groups are shown in the following tables modal groups for g codes name g codes group group 1 g0, g1, g2, g3, g38 2, g76, g80, g81, g82, g83, g84, g85, g86, g87, g88, g89 motion group 2 g17, g18, g19 plane selection group 3 g90, g91 distance mode group 5 g93, g94 feed rate mode group 6 g20, g21 units group 7 g40, g41, g42 cutter radius compensation group 8 g43, g49 tool length offset group 10 g98, g99 return mode in canned cycles group 12 g54, g55, g56, g57, g58, g59, g59 1, g59 2, g59 3 coordinate system selection group 13 g61, g61 1, g64 path control mode group 14 g68, g69 xy plane rotation modal groups for m codes name m codes group group 4 m0, m1, m2, m30, m60 stopping group 5 m54, m55, m56, m64, m65, m66 aux and general purpose i/o group 6 m6 tool change group 7 m3, m4, m5 spindle turning group 8 m7, m8, m9 coolant group 9 m48, m49, m50, m51, m52 enable/disable feed and speed override switches group 10 m90, m91, m92, m95, m97 select standard or alternate spindle or touch probe or camera offset, m90 = standard enable thc m20, m21 thc on | thc off (torch height control) a axis clamp m26, m27 clamp on | clamp off non modal groups name m codes group group 0 g4, g10, g28, g30, g53, g92, g92 1, g92 2, g92 3 for several modal groups, when a machining center is ready to accept commands, one member of the group must be in effect there are default settings for these modal groups the default values are automatically in effect when the machining center is turned on or otherwise re initialized group 1 is a group of g codes for motion one of these is always in effect that one is called the current motion mode it is an error to put a g code from group 1 and a g code from group 0 on the same line if both of them use axis words if an axis word using g code from group 1 is implicitly in effect on a line (by having been activated on an earlier line), and a group 0 g code that uses axis words appears on the line, the activity of the group 1 g code is suspended for that line the axis word using g codes from group 0 are g10, g28, g30, and g92