Introduction to CNC programming for CNC machine tools [G-code]

If your work or hobby is related to CNC machines or 3D printers, it is important for you to understand what G-code is and how it works. In this tutorial, we will learn the basics of G-code language and common G-code commands, and explain how these G-code commands work.

Test your G-Code commands with this free online emulator .

1. What is G-code?

G-code is a programming language for CNC (Computer Numerical Control) machine tools. G-code refers to Geometric Code, that is, geometric code. We use this language to tell machines what to do or how to do something. G-code commands instruct the machine where to move, how fast to move, and the path to follow.

For CNC machines, the cutting tool is driven by these G-code commands to cut the material in a specific path to obtain the desired shape.

Similarly, with a 3D printer, G-code commands instruct the machine to deposit material layer by layer, forming precise geometric shapes.

2. How to read the G-code command?

When you first look at a G-code file, it might seem rather complicated, but it's actually not that hard to understand.

insert image description here

If you look closely at the code, you can see that most lines of code have the same structure. It looks like the "complex" part of G-code is mostly numbers, and those numbers are Cartesian coordinates.

Let's take a look at one line of code and explain how it works.

G01 X247.951560 Y11.817060 Z-1.000000 F400.000000

The row has the following structure:

G#x##Y#Z## F##
  • The first is the G-code command, which in the above example is G01, which means "move in a straight line to a specific position".
  • We declare the X, Y and Z coordinates of the location to move to.
  • Finally, with the F value we set the feedrate, the speed at which the move is performed.

To summarize, the code G01 X247.951560 Y11.817060 Z-1.000000 F400.000000asks the CNC machine tool to move linearly from the current position to the coordinates X247.951560, Y11.817060 and Z-10000000 at a speed of 400 mm/min.

Note that the unit is mm/min, because in the previous G-code example we used the command G21 to set the unit to mm. If you want the units in inches, you can use the G20 command instead.

3. Common G-code commands

Now that we understand how to read a line of G-code code, we can learn the most important or commonly used G-code commands. We will go through a few examples to understand how each G-code command works, and by the end of this tutorial, we will be able to fully understand how G-code works, how to read it, how to modify it, and be able to write our own G-code programs .

3.1 G00 - quick positioning

The G00 command moves the machine from the current position to the specified coordinates at maximum speed. The machine will move all axes simultaneously so that the stroke is completed at the same time. The result is a straight line movement to the new location point.

insert image description here

G00 is a non-cutting motion whose purpose is to quickly move the machine to the desired position to start some kind of work, such as cutting or printing.

3.2 G01 – linear interpolation

The G01 command instructs the machine to move in a straight line at the set speed. We specify the final position with X, Y, and Z values, and the velocity with the F value. The CNC controller calculates (interpolates) the coordinates of the intermediate points to pass through to obtain the straight line. While these G-code commands are simple, intuitive and easy to understand, behind the scenes the CNC performs thousands of calculations per second to make these movements.

insert image description here

Unlike G00 commands, which are used only for positioning, G01 commands are used when the machine is performing its main task. For example, a machine tool cuts material in a straight line, or a 3D printer
extrudes material in a straight line.

3.3 G02–clockwise circular interpolation

The G02 command asks the machine to move clockwise in a circular pattern. It has the same concept as the G01 command and is used when performing the appropriate machining process. In addition to the end point parameter, here we also need to define the center of rotation, or the distance between the starting point of the arc and the center point of the arc. The start point is actually the end point of the previous command or the current point.

For better understanding, we will add the G02 command after the G01 command in the previous example.

insert image description here

So, in the example, first we use the G01 command to move the machine to point X5, Y12. Now, this will be the starting point for the G02 command. Through the X and Y parameters of the G02 command, we set the end point. Now, in order to reach the end point with a circular motion or arc, we need to define its center point. We do this using the I and J parameters. The values ​​of I and J are relative to the start point or the end point of the previous command. So, to get the center point of X5 and Y7, we need an offset of 0 along the X axis and -5 along the Y axis.

Of course, we could set the center point elsewhere and we would get a different arc, ending at the same end point. Below is an example:

insert image description here

So here, we still have the same end point as the previous example i.e. (X10, Y7), but the center point is now at a different location (X0, Y2).
So we get a wider arc.

3.4 G00, G01, G02 Example - Manual G Code Programming

Let's take a look at a simple CNC milling example using these three main G-code commands, G00, G01 and G02.

insert image description here

To get the path for the shape shown in the image above, we need to follow the G-code command:

G00 X5 Y5                ; point B
G01 X0 Y20 F200          ; point C
G01 X20 Y0               ; point D
G02 X10 Y-10 I0 J-10     ; point E
G02 X-4 Y-8 I-10 J0      ; point F
G01 X-26 Y-2             ; point B

The first G00 command quickly brings the machine from its initial position to point B (5, 5). From here, we "cut" using the G01 command at a feed rate of 200. We can notice here that to go from point B (5, 5) to point C (5, 25), we use the X and Y values ​​relative to the starting point B. So +20 units in the Y direction will get us to point C (5, 25). Actually, it depends on whether we interpret the coordinates as absolute or relative. We will explain this in a later section.

Once we reach point C (5, 25), another G01 command is used to reach point D (25, 25). Then, we use the G02 command (circular motion) to reach point E (35, 15), and the middle point is (25, 15). Actually, for the next G02 command, we have the same center point (25, 15) to reach point F (31, 7). It should be noted, however, that the I and J parameters are different from the previous commands because we offset the center from the last end point or point E. We complete the entire path with another G01 command, which returns us from point F (31, 7) to point B (5, 5).

Above is the G-code program we wrote to make this shape. However, it should be noted that this is not a complete G-code program, because several more basic commands are still missing. We will write a complete G-code program in a later example.

3.5 G03–Counterclockwise circular interpolation

Like G02, the G03 command instructs the machine to move in a circular pattern, the difference being that G03 is a counterclockwise motion. All other functions and rules are the same as G02 command.

insert image description here

Using these three main G-code commands, G01, G02 and G03, we can theoretically generate paths of arbitrary shapes. You may now be wondering how this is possible, but this is actually a really simple task with computers and CAM software. Yes, we do sometimes make G-code programs by hand, but most of the time, we use simpler and safer software to generate G-code programs.

Anyway, continue to explain the commonly used commands and implement a real G-code example before the end of the tutorial.

3.6 G20/G21 - Unit Selection

The G20 and G21 commands define the G-code units, inches or millimeters.

  • G20 = inches
  • G21 = mm

We need to note that the units must be set at the beginning of the program. If no unit is specified, the CNC will consider the default value set by the previous program.

3.7 G17/G18/G18 - Working Face Selection

With these G-code commands, we select the working plane of the machine:

  • G17–XY plane
  • G18–XZ plane
  • G19–YZ plane

insert image description here

The default for most CNC machines is G17, but the other two can also be used to achieve specific motions.

3.8 G28 – return home

The G28 command asks that the machine will move to its reference point or home position. To avoid collisions we can include an intermediate point with X, Y and Z parameters. The tool will pass through the reference point before going to it.G28 X##Y##Z##

insert image description here

The home location can be defined with the command G28.1 X# Y # Z #.

3.9 G90/G91 - Positioning Mode

Using the G90 and G91 commands, we tell the machine how to interpret the coordinate values. G90 is absolute mode, and G91 is relative mode.

In absolute mode, the positioning of the tool is always relative to an absolute or zero point. So the command G01 X10 Y5 will move to the exact point (10, 5), regardless of the previous position.

In relative mode, the positioning of the tool is relative to the last point. So, if the machine is currently at point (10, 10), command G01 X10 Y5 to point the tool to point (20, 15). This mode is also known as "incremental mode".

3.10 More commands and rules

The G-code commands we explained above are the most common, but there are many more such as cutter compensation, scaling, work coordinate system, etc.

In addition to G-code, the M-code command is also required to generate a truly complete G-code program. Here are some common M-code commands:

  • M00 – program stop
  • M02 – end of program
  • M03 – Spindle On – Clockwise
  • M04 – Spindle On – CCW
  • M05 - spindle stop
  • M06 – Tooling Changes
  • M08 – Enable Flood Colant
  • M09 – Disable Flood Colant
  • M30 - end of program

Additional commands for 3D printers:

  • M104 – Start Extrusion Heating
  • M109 – wait until extruder reaches T0
  • M140 – Start baseplate heating
  • M190 – wait for bottom plate to reach T0
  • M106 – Set Fan Speed

Some of these commands require appropriate parameters. For example, when turning on the spindle with the M03 command, we can use the S parameter to set the spindle speed. So the M30 S1000 will turn on the spindle at 1000 RPM.

We can also notice that many of the codes are modal, meaning they remain active until canceled or replaced by another code. For example, suppose we have a linear cutting motion G01 X5 Y7 F200 code. If the next action is still linear cutting, we can directly input the X and Y coordinates without writing G01 in front.

G01 X5 Y7 F200
X10 Y15
X12 Y20
G02 X5 Y5 I0 J-5
X3 Y6 I-2 J0

The same applies to the feed rate parameter F. You don't have to include it on every line unless you want to change its value.

In some G-code files, you can also see "N##" in front of the command, which is a number for the line or block of code, which helps to identify a specific line of code when an error occurs in a large program.

4. G-code program example

After learning the above content, now we can manually make a real G-code program. Here is an example:

insert image description here

%
G21 G17 G90 F100
M03 S1000
G00 X5 Y5                 ; point B
G01 X5 Y5 Z-1             ; point B
G01 X5 Y15 Z-1            ; point C
G02 X9 Y19 Z-1 I4 J0      ; point D
G01 X23 Y19 Z-1           ; point E
G01 X32 Y5 Z-1            ; point F
G01 X21 Y5 Z-1            ; point G
G01 X21 Y8 Z-1            ; point H
G03 X19 Y10 Z-1 I-2 J0    ; point I
G01 X13 Y10 Z-1           ; point J
G03 X11 Y8 Z-1 I0 J-2     ; point K
G01 X11 Y5 Z-1            ; point L
G01 X5 Y5 Z-1             ; point B
G01 X5 Y5 Z0
G28  X0 Y0
M05
M30
%

Description of the G-code program:

  1. Code initialization. This character (%) is always present at the beginning and end of the program.
  2. Safety Line: Set program parameters such as metric system (in millimeters), XY plane, absolute positioning, feedrate at 100 in/min.
  3. Rotate clockwise at 1000 RPM.
  4. Quickly navigate to B (5, 5).
  5. Control the movement at the same position, but lower the tool to -1.
  6. Linear cutting motion position C (5, 15).
  7. Clockwise circular motion to point D (9, 19) with center point (9, 15).
  8. Linear cut to point E (23, 19).
  9. Cut linearly to point F(32,5).
  10. Continue cutting straight to point G(21,5).
  11. Continue cutting straight to point H (21, 8).
  12. Counterclockwise circular interpolation to position I (19, 10), with center point at (19, 8).
  13. Cut linearly to point J(13,10).
  14. Cut circularly counterclockwise to position K (11, 8), with the center point at (13, 8).
  15. Linear cut to position L(11,5).
  16. Final linear cutting movement to position B (5, 5).
  17. Lift the tool.
  18. return home.
  19. The spindle is switched off.
  20. The main program ends.

Here is this code in the Universal G-code Sender software ready to be sent to our CNC:

insert image description here

So, using these main G-code commands explained above, we write a complete G-code program. Of course, this example is simple, for more complex shapes we will definitely need to use CAM software. Here is an example of a complex G-code program for cutting a horse:

insert image description here

The above code has about 700 lines, but was auto-generated with Inkscape. The result is as follows:

insert image description here


Original Link: G-code CNC Machine Tool Programming Tutorial—BimAnt

Guess you like

Origin blog.csdn.net/shebao3333/article/details/118240830#comments_26728520