Getting familiar with CNC programing

1.What is CNC programming?

CNC stands for Computerized Numerical Control and a CNC machine is a machine computerized and numerically controlled. When we work on a material on the CNC machine, we use G-code and M-code for programming.

CNC programming sounds difficult like a killer phrase for non-skilled operators because of many codes and enigmatic rules. However, once you understand the rules, it is not so hard to understand.

Programing codes are developed by several CNC controller manufactures such as FANUC, Siemens, Heidenhain and their systems are divided into lathe system and machining system. All of the manufactures use G-code and M-code so you might be misled into a thought that the string of the written programs means same command and order, but actually the command could be different even though they use same codes. You need to be sure which CNC controller is equipped with a machine you use before you start writing a program.

Nakamura’s machines are equipped with FANUC controller, and in this column, we explain about how to activate a program in an easy-to-understand manner using FANUC CNC programming codes as an example.

FANUC controller

2.G-code and M-code

CNC programming is composed of languages called G-code and M-code. Basic codes are presented in two digits from 00 to 99, but depending on the configuration of a machine, up to three digits may be used.

2-1.G-code

As preparatory function, G-code is mainly used in manufacturing command, represented in numbers following sign G.

G-code has two types: one-shot codes (also called “non-modal”) that are valid only once, and modal codes that remain valid until another code in a same group is commanded. Don’t worry. There is no need to be panic about the groups. This grouping is aimed to avoid from interfering each command. For example, if you state “G00: Positioning”, you cannot write “G01: Cutting feed” in a same line because a machine cannot follow both commands at the same time. Same as this condition, you cannot state “G02: Circular interpolation in clockwise” and “G03: Circular interpolation in counter clockwise” in a line because the machine cannot execute them simultaneously. These opposing codes are enclosed within one group to prevent from canceling commands each other.

The basic G-codes are about 70 in number from G00 to G99, but we do not usually use them all every time. Only if you remember frequently used codes, you can write a program.

Here are the often-used 16 G-codes that you’d better remember.

G00 Positioning (Rapid traverse)
G01 Linear interpolation (Cutting feed)
G02 Circular interpolation CW
G03 Circular interpolation CCW
G04 Dwell
G28 Automatic return to reference point
G40 Tool nose radius compensation cancel
G41 Tool nose radius compensation left
G42 Tool nose radius compensation right
G50 Coordinate system setting/Max. spindle speed setting
G54 Workpiece coordinate system 1 selection
G92 Thread cutting cycle
G96 Constant surface speed control
G97 Constant surface speed control cancel
G98 Per minute feed
G99 Per revolution feed

G-codes can be written in a same line if each code belongs to different groups.

2-2.M-code

M-code is miscellaneous function that is used in auxiliary command, calling for vary machine action. They are represented in numbers following sing M.

For example, M03 is a command for “Spindle start in forward direction”, and M08 is for “Coolant start”. Since M-code is aimed at activating a machine, the programming codes are ruled by machine builders rather than CNC controller manufactures. Therefore, the codes called for action vary from machine builder to builder.

Imagine if you already had a machine produced by Builder A, then you purchased a new machine from Builder B. Both machines adopted FANUC controller so you tried the same M-code digits used for the old machine to the new machine to state the same commands, but you came across to failure. This is because even though the M-code digits are same, the commands are different. The commands are customized to machine builders separately.

There are about 80 M-codes designated from M00 to M99 fundamentally, but same as G-code, you do not need to use them all.

Let’s take a look at the often-used 22 M-codes with Nakamura machines. Please note that those M-codes are not compatible to other builder’s machines and customized only for Nakamura machines.

M00 Program stop
M01 Optional stop
M03 Spindle start (forward direction)
M04 Spindle start (reverse direction)
M05 Spindle stop
M08 Coolant start (high pressure coolant)
M09 Coolant stop (low/high pressure coolant)
M10 Chuck close
M11 Chuck open
M20 Fixed air blow ON
M23 Chamfering ON
M24 Chamfering OFF
M30 Memory mode operation reset & restart
M41 Spindle selection (C-axis OFF)
M86 Spindle lock ON
M87 Spindle lock OFF
M88 Rotary tool spindle start (forward direction)
M89 Rotary tool spindle start (reverse direction)
M90 Rotary tool spindle stop
M91 C-axis selection (C-axis connection)
M98 Sub-program call
M99 End of sub program

As you see, there are about 80 M-codes but minimally required are about 22. Please note that only one M-code can be commanded per program line, not multiple M-codes per line.

3.Coordinate system (Machine zero and Workpiece zero)

CNC lathe has two zero points. One is machine zero which is the standard for the machine, and the other is workpiece zero which can be freely decided by the person who create a program.

In a programming process, defining the workpiece zero point is important. Normally, the workpiece zero is set to the point where the machining end face and the center of a spindle intersect. The position of the workpiece zero is represented by coordinates of X0 and Z0. For X-axis, set the distance between the edge of a cutting tool and the center of a spindle to the shape correction, and for Z-axis, set the distance between the machine zero and the tip of a cutting tool to the shape correction. Since the machining end face is commonly set as Z0, a cutting tool moves toward minus direction when the machine cuts a workpiece chucked on the main spindle.

For machining on the sub-spindle, set G-code “G55: Workpiece coordinate system 2 selection” and calculate its coordinate based on it. The concept of the direction that the cutting tool moves is same as it is working with the main spindle: minus direction and plus direction. Therefore, when the machine works on the sub-spindle and the cutting tool moves in Z-axis, it moves toward plus direction.

4.Summary

As you see, you do not have to be afraid of programming. There is no need to remember all G-codes and M-codes. More importantly is understanding what the workpiece zero because if you set this point wrongly, all the programming you made will be ruined. It is good to mark the workpiece zero on your drawing before starting programing. CNC programming will work out in good shape when the intention of the user is conveyed to the machine correctly in the end.

In the next column, we will discuss about the basic composition of a CNC program, absolute coordinates vs relative coordinates, and coordinate calculation.