G-code - Example Program

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.)

Sample
Line Code Description
% (Demarcates the start and end of a program. Originally indicated the start and end of tape feed on NC machines, generally but not always required to be present on newer machines.)
O4968 (Sample face and turn program)
N01 M216 (Turn on load monitor)
N02 G20 G90 G54 D200 G40 (Inch units. Absolute mode. Call work offset values. Moving coordinate system to the location specified in the register D200. Cancel any existing tool radius offset.)
N03 G50 S2000 (Set maximum spindle speed rev/min - preparing for G96 CSS coming soon)
N04 M01 (Optional stop)
N05 T0300 (Index turret to tool 3. Clear wear offset (00).)
N06 G96 S854 M42 M03 M08 (Constant surface speed, 854 sfm, select spindle gear, start spindle CW rotation, turn on the coolant flood)
N07 G41 G00 X1.1 Z1.1 T0303 (Call tool radius offset. Call tool wear offset. Rapid feed to a point about 0.100" from the end of the bar and 0.050" from the side)
N08 G01 Z1.0 F.05 (Feed in horizontally until the tool is standing 1" from the datum i.e. program Z-zero)
N09 X-0.002 (Feed down until the tool is slightly past center, thus facing the end of the bar)
N10 G00 Z1.1 (Rapid feed 0.1" away from the end of the bar - clear the part)
N11 X1.0 (Rapid feed up until the tool is standing at the finished OD)
N12 G01 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)
N13 G00 X1.1 M05 M09 (Clear the part, stop the spindle, turn off the coolant)
N14 G91 G28 X0 (Home X axis - return to machine X-zero passing through no intermediate X point )
N15 G91 G28 Z0 (Home Z axis - return to machine Z-zero passing through no intermediate Z point )
N16 G90 M215 (Return to absolute mode. Turn off load monitor)
N17 M30 (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 jobs of the load monitor are various:
    1. Prevent machine damage in the event of tool breakage or a programming mistake.
      1. This is especially important because it allows safe "lights-out machining", in which the operators set up the job and start it running during the day, then go home for the night, leaving the machines running and cutting parts during the night. Because no human is around to hear, see, or smell a problem such as a broken tool, the load monitor serves an important sentry duty. When it senses overload condition, which semantically suggests a dull or broken tool, it commands a stop to the machining. Technology is available nowadays to send an alert to someone remotely (e.g., the sleeping owner, operator, or owner-operator) if desired, which can allow them to come intercede and get production going again, then leave once more. This can be the difference between profitability or loss on some jobs, because lights-out machining reduces labor hours per part.
    2. Warn of a tool that is becoming dull and needs to be replaced or sharpened. Thus an operator who is busy tending multiple machines will be told by a machine, essentially, "Hey, pause what you're doing over there, and come attend to a need over here."
  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. Fortunately, 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.

Read more about this topic:  G-code

Famous quotes containing the word program:

    But one day he met a man who was a whole lot badder,
    And now he’s dead, and we ain’t none the sadder.
    —Administration in the State of Texa, U.S. public relief program (1935-1943)

    The twelve Cells for Incorrigibles ... are also carved out of the solid rock hill. On the walls of one of the cells human “liberty” is clearly inscribed, with the “liberty” in significant quotation marks.
    —Administration in the State of Ariz, U.S. public relief program (1935-1943)