G-Code Programming

The AeroScript programming language includes Aerotech G-code dialect, which is based on the RS-274 G-code standard. You can use the Aerotech G-code dialect to configure and do different types of motion on the controller.

Most processes that you can do with the Aerotech G-code dialect, you can do by calling AeroScript functions. But there are some processes that you can only do with the Aerotech G-code dialect. You can also create custom G-codes and M-codes to use in your program.

The AeroScript example program that follows uses the Aerotech G-code dialect with a controller that has an X and Y axis.

G90 // Configure the specified target positions as absolute
G76 // Configure time units as seconds.
G00 // Configure the task to operate in rapid point-to-point motion mode.

// Move axes X and Y to the absolute position (5, 10)
// with each axis moving at a rate of 100 units/second.
X5 Y10 XF100 YF100 G01 // Configure the task to operate in coordinated linear motion mode F50 // Configure the feedrate of coordinated motion to be 50 units/second // Move the X and Y axes such that their path creates a square N10 X5 Y20 N20 X-5 Y20 N30 X-5 Y10 N40 X5 Y10

G-Code Program Lines

A G-code program line in AeroScript can include:

  • An optional N-code. For more information, refer to the N-Codes section of this topic.
  • Zero or more G-codes. For more information, refer to the G-Codes section of this topic.
  • An optional M-code. For more information, refer to the M-Codes section of this topic.
  • An optional AeroScript comment at the end of the line.

G-Code Features

Most of the Aerotech G-code dialect is not case sensitive, so upper-case letters and lower-case letters have the same meaning. For example, G1 and g1 have the same meaning when you specify them on a G-code program line. The only case-sensitive part of the Aerotech G-code dialect is the name of an axis that is used by the controller. For example, if you have a controller with an axis named X and an axis named x, then the program lines that follow do motion on different axes.

G1 X10
G1 x10

Leading zeros are permitted in most Aerotech G-code dialect if you specify an integer literal value. For example, G1 and G01 have the same meaning when you specify them on a G-code program line.

Most of the Aerotech G-code dialect is not whitespace sensitive, and you can have zero, one, or more spaces between G-code letters and numerical values. For example, the program lines that follow have the same meaning.

G1X10F20
G1 X10 F20
G 1 X 10 F 20

If you specify two or more G-code commands on one AeroScript program line, the commands are usually executed in the order that follows:

  1. Modal G-code words (G17, G71, G90, and so on).
  2. F, E, S, and T commands, in that order (F100, T10, and so on).
  3. Spindle on or off (M3, M4, M5).
  4. Cutter compensation commands (G40, G41, G42, G43, G44, G143, G144, and G149).
  5. Motion mode commands (G0, G1, G2, and G3).
  6. Motion commands (x10, Y-10, and so on).
  7. Control M-codes (M0, M1, M2, M30, M47, and so on).

G-Code and Exponential Notation

AeroScript has special rules when parsing G-code programs. This is because of the E command in the G-code dialect, support for constant floating-point values that are specified with exponential notation, and whitespace insensitivity in the G-code dialect.

A floating-point value specified with exponential notation must be delimited on both its left and right sides to prevent conflicts with the E command in G-code programming. The examples that follow show how exponential notation and the E command work.

// E commands
E100E 100
				
// Exponential notation
$myVar = 1e10

// Move with Y = 1, E command = 10
G1 Y1E10

// Move with Y = 1, E command = 10
G1 Y1 E10

// Move with Y = 1E10 (no E command)
G1 Y 1E10

// Move with Y = 1, E command = 10
G1 Y 1 E 10

// Exponential notation can be enforced by using delimiter characters
G1 Y(1E10)G1 Y=1E10

G-Code and Hexadecimal Notation

There are exceptions when specifying hexadecimal literals in a G-code program.

A hexadecimal literal must be delimited on both its left and right sides to prevent conflicts with an axis called either X or x that is directly followed by an integer value in G-code. The examples that follow show how hexadecimal notation and G-code work.

// G-code
X10

// Hexadecimal notation
$myVar = 0x10

// Move with Y = 0, X = 10
G1 Y0X10

// Move with Y = 0, X = 10
G1 Y0 X10

// Move with Y = 0x10 (16, no move on X)
G1 Y 0X10

// Move with Y = 0, X = 10
G1 Y 0 X 10

// Hexadecimal notation can be enforced by using delimiter characters
G1 Y(0X10)G1 Y=0X10

E Command

The E command specifies the feedrate for dependent axes when doing coordinated motion. Coordinated motion includes the motion types that follow:

  • G1, MoveLinear()
  • G2, MoveCw()
  • G3, MoveCcw()
  • G12 and G13

An E command can be specified on its own program line, or it can be specified on the same program line as G-code motion.

// Specified on its own line
E100
 
// Specified as part of a move
G1 Y50 E100

An E command must be specified with a constant value or with a numeric expression.

// Specified with a constant feedrate
E100
 
// Specified with a numeric expression feedrate
E($dependentSpeed)

The dependent feedrate is used for coordinated moves that contain only dependent axes. If a coordinated move contains only dominant axes, or a mixture of dominant and dependent axes, then the move will use the dominant feedrate (the F command).

F Command

The F command specifies the feedrate for dominant axes when performing coordinated motion. Coordinated motion includes the following types of motion:

  • G1, MoveLinear()
  • G2, MoveCw()
  • G3, MoveCcw()
  • G12 and G13

An F command can be specified on its own program line, or it can be specified on the same program line as G-code motion.

// Specified on its own line
F100

 
// Specified as part of a G-code move

G41 G1 X10 Y10 F100

An F command must be specified with a constant value or with a numeric expression.

// Specified with a constant feedrate
F100

 
// Specified with a numeric expression feedrate
F($mySpeed * 0.1)

The dominant feedrate is used for coordinated moves that contain only dominant axes or a mixture of dominant and dependent axes. If a coordinated move contains only dependent axes, then the move will use the dependent feedrate (the E command).

G-Codes

G-codes are used to configure and do various types of motion on the controller.

G-codes must be specified with a numeric integer constant. Preceding zeros can be used in the numeric value, making both of the G-codes that follow the same:

// These G-codes are equivalent:
G1 X10
G01 X10

Refer to the list of Supported G-Codes for more information.

I/J/K Offsets

The I, J, and K offsets are only applicable with the G2, G3, G12, and G13 G-codes.

M-Codes

M-codes are used for miscellaneous functionality on the controller.

M-codes must be specified with a numeric integer constant. Preceding zeros are allowed to be used in the integer value. The following two M-codes are equivalent:

  • M2
  • M02

Refer to the list of M-Codes for more information.

N-Codes

N-codes label a line or a block of G-code. N-codes are optional on G-code lines. If an N-code is specified on a program line that contains G-code, then it must be specified before all other G-codes on the same line.

N-codes must be specified with a positive numeric integer constant. Preceding zeros are allowed in the numeric value.

  • N01G1X10
  • N02G1X15
// N-codes can be used as an alternative to labels in G-code.
// Use a label to branch to a specific location in G-code

goto
myLabel
 
G1 Y1.3
G1 Y2.2
 
myLabel:
G1 Y3.5

 
// Use an N-code to branch to a specific location in G-code
goto
N03
 
N01G1X2.5
N02G1X3.3
N03G1X7.5

P Command

The P command has different meanings depending upon the context in which it is specified. A P command cannot be specified in its own program, and must be specified on the same line as G2, G3, G12, or G13 G-code motion, or with the G4 G-code.

  • When specified with the G4 G-code, the numeric value of the P command specifies the amount of time that the G4 command delays execution of the task.

  • When specified with G2, G3, G12, or G13 G-code motion, the numeric value of the P command specifies the absolute starting angle for the G2 or G3 arc move.

The numeric value specified as part of the P command can be a numeric constant or a numeric expression.

G4 P0.1
G4 P$dwellTime
G2 P108 Q-18 R1.5

Q Command

The Q command specifies the absolute ending angle for a G2, G3, G12, or G13 arc move. A Q command cannot be specified on its own line and must on the same line as G2, G3, G12, or G13 G-code motion.

The numeric value specified as part of a Q command can be a numeric constant or a numeric expression.

R Command

The R command specifies the radius in an arc move (G2, G3, G12, and G13 G-codes). The R command cannot be on its own line. It must be on the same line as a G2, G3, G12, or G13 G-code motion.

The numeric value specified as part of an R command can be a numeric constant or a numeric expression.

G2 X10 Y0 R5

S Command

The S command specifies the speed of the spindle that is active on the current task.

T Command

Use the T command with custom T-codes to make a user-defined tool selection routine in AeroScript.

IMPORTANT: If you want to use T commands in the Aerotech G-code dialect, you must define one custom T-code in an AeroScript program or library. If no custom T-code is defined or two or more custom T-codes are defined, a compiler error will occur.

For more information about how to use custom T-codes, see Custom T-Codes .

Custom G-Codes and M-Codes

You can add custom G-codes and M-codes to the G-codes and M-codes in the Aerotech G-code dialect. With custom G-codes and M-codes, you can use G-code syntax to execute a custom AeroScript routine.

Defining Custom G-Codes and M-Codes

Use the syntax that follows to define a custom G-code or M-code.

gcode G<integer>
    <body>
end
gcode G<integer>.<integer>
    <body>
end
gcode M<integer>
    <body>
end
gcode M<integer>.<integer>
    <body>
end

If you define a custom G-code or M-code by using the G<integer> or M<integer> syntax, the integer value must be in the range of 0 to 4,294,967,295. If you define a custom G-code or M-code by using the G<integer>.<integer> or M<integer>.<integer> syntax, the two integer values must be in the range of 0 to 65,535.

The body of the custom G-code or M-code must include the set of AeroScript instructions that the controller executes when the custom G-code or M-code is called. The example that follows shows the definitions of custom G-codes and custom M-codes.

gcode G300
    MoveLinear(X, 10)
end

gcode G300.1 
    MoveLinear(X, -10)
end
						
gcode M450
    Enable([A, B, C])
    Home([A, B, C])
end

gcode M2.1
    Abort(X)
    WaitForMotionDone(X)
    Disable(X)
    ProgramExit()
end

The G-code or M-code that you specify with the gcode keyword cannot be the same as a G-code or M-code in the Aerotech G-code dialect or a custom G-code or M-code that is defined in your program. You can use the #define preprocessor directive to redefine a G-code or M-code in the Aerotech G-code dialect or a custom G-code or M-code that is defined in your program. The example that follows shows the definitions of custom G-codes and custom M-codes that redefine G-codes and M-codes in the Aerotech G-code dialect.

// Redefine G4 to dwell for the number of milliseconds specified by the P argument.
// For example, the G4 P1.0 command will dwell for 1 millisecond instead of 1 second.
#define G4 G1004
gcode G1004
    Dwell(argument(P) / 1000.0)
end

// Redefine M2 to stop the program on the task specified by the P argument.
// For example, the M2 P2 command will stop the program running on Task 2.
#define M2 M1002
gcode M1002
    ProgramStop(argument(P))
end

Custom G-codes and M-codes can also be added to libraries. These are created the same way as library functions. See Libraries for more information. The example that follows shows how to add a custom G-code to a library.

library gcode G500
    Enable([X, Y])
    Home([X, Y])
    MoveLinear([X, Y], [10, 10])
end

G-codes and M-codes that redefine a G-code or M-code from the Aerotech G-code dialect are only available in AeroScript source files where the macro that you defined with the #define preprocessor directive is included.

To make a text replacement macro available to other AeroScript files, you must use the #include preprocessor directive to include the source file where the macro is defined. But, to use the #include preprocessor directive to include an AeroScript source file, the source file must only contain comments and preprocessor directives. Because of this, the custom G-code or M-code block must be defined in a separate AeroScript library, which must be imported by the AeroScript source files where you want to use the G-code or M-code.

The example that follows shows how you could use the #include preprocessor directive and an AeroScript library file to make a redefined G-code or M-code available to other AeroScript source files.

// MyCustomGCodeDefines.ascript
#define G4 G1004

// MyCustomGCodeBlocks.ascriptlib
library gcode G1004
	Dwell(argument(P) / 1000.0)
end

// MyCustomGCodeProgram.ascript
#include "MyCustomGCodeDefines.ascript"
import "MyCustomGCodeBlocks.a1lib" as static

G4 P500

For more information on using preprocessor directives like #define and #include, refer to the Preprocessor page.

You can make redefined G-codes and M-codes available to all AeroScript programs and libraries on the controller without using the #include directive or any explicit import statements with the Program Automation Module. To do this, use the procedure that follows.

Using Custom G-Codes and M-Codes

To use custom G-codes and M-codes in an AeroScript program, specify the G-codes and M-codes on a program line. The example that follows shows how to use the AeroScript procedure defined by the custom M450 M-code from the previous example.

M450

You must specify custom G-codes and M-codes first on a program line. If you specify a custom G-code or M-code on a program line, then you cannot specify a different G-code or M-code on the same line.

Passing Arguments to Custom G-Codes and M-Codes

You can pass one or more arguments when you use a custom G-code or M-code. Each argument that you specify must be a pair that has a name and a real value. The name can be the name of an axis or one of these letters: E, F, I, J, K, P, Q, R, S, T. The example that follows uses a custom M412 M-code with arguments.

M412 P10.5 Q21.3 R46.2

The AeroScript code in the body of a custom G-code or M-code can access the arguments that were passed to that G-code or M-code when it is used.

argument() Function

Use the built-in argument() function to get the real value from the argument that has a name.

gcode M500
    $rglobal[0] = argument(X)
end

// Sets $rglobal[0] to 15.5
M500 X15.5

A task error will occur if you call the argument() function with a name that does not match any of the arguments that were specified with the G-code or M-code.

gcode M600
    $rglobal[0] = argument(X)
end

// M600 expects an argument named X, so a task error will occur
M600

defined() Function

Use the built-in defined() function to see if the argument that has a given name was specified with the G-code or M-code. You can use this function to prevent the task error that occurs if you call the argument() function with a name that does not match the name of one of the specified arguments.

gcode M200
    if defined(F)
        $rglobal[5] = argument(F)
    else
        // Argument F was not specified
    end
end

Custom T-Codes

You can add custom T-codes to the Aerotech G-code dialect. Use custom T-codes to create a custom AeroScript routine that you can execute by using the T command. You can also use custom T-codes to make an implementation of a tool selection routine together with a custom tool table definition. For an example of how to use custom T-codes, see the Tool Table Example Program.

IMPORTANT: If you want to use T commands in the Aerotech G-code dialect, you must define one custom T-code in an AeroScript program or library. If no custom T-code is defined or two or more custom T-codes are defined, a compiler error will occur.

Defining Custom T-Codes

To define a custom T-code, use the syntax that follows.

gcode T
    <body>
end

The body of the custom T-code must include the set of AeroScript instructions that the controller will execute when the custom T-code is used. The example that follows shows you how to define a custom T-code body.

gcode T
    Home(X)
    Disable(X)
end

You can also add custom T-codes to libraries. These are created with the same method as library functions. See Libraries for more information. The example that follows shows you how to add a custom T-code to a library.

library gcode T
    Home(X)
    Disable(X)
end

If a custom T-code is currently defined in your program or in an imported library, you cannot define more custom T-codes. Thus, you can have only one custom T-code in scope at a time in any AeroScript program. Because of this, you must be careful when you define a T-code in an auto-import library. When you do this, it causes the T-code to become accessible to all of the AeroScript programs on the controller. It also prevents you from defining more custom T-codes in any of the AeroScript programs on the controller.

IMPORTANT: Use Program Automation to load only one compiled library that defines a custom T-code onto the controller. If you try to load two or more compiled libraries that have a custom T-code, it will cause an error to occur when you try to reset the controller. To stop this error from occurring, you must remove the compiled libraries from Program Automation.

Using Custom T-Codes

To use custom T-codes in an AeroScript program, you must specify a G-code T-letter and an integer argument on a program line. The example that follows shows you how to use the AeroScript procedure that is defined by the custom T-code in the previous example:

T10

It is not necessary for the specified integer argument to be an integer literal. The argument can be any integer expression. The example that follows shows more ways that you can use a custom T-code:

T(5+5)
T=10
T$iglobal[0]

If you want to call different AeroScript code when you specify an argument, use the #define preprocessor directive to redefine the T command for that argument. The example that follows shows you how to redefine T20 so that it does not call a custom a custom T-code:

#define T20 Enable(Y)

gcode T
    // ...
end

// T20 will call Enable(Y).
T20
// T10 will call the custom T-code body.
T10

You can specify custom T-codes in any order that is relative to other G-code commands on a program line.

IMPORTANT: Do not specify T-codes on the same line as:

  • Custom G-codes or M-codes
  • Other T-codes
  • G-codes that must be specified on their own program line

If you do this, a compiler error will occur.

For more information about the execution order of two or more G-code commands on a program line, see G-Code Commands.

Accessing Integer Arguments Passed to Custom T-Codes

You must use custom T-codes with only one integer argument. To access the integer argument, use the built-in argument() function with the special G-code letter T syntax.

gcode T
    var $toolNum as integer = argument(T)

    if $toolNum <= 10
        $iglobal[0] = $toolNum
    elseif $toolNum <= 50
        $iglobal[1] = $toolNum
    else
        $iglobal[2] = $toolNum
    end
end

// $iglobal[0] will be equal to 10 after this program line.
T10
// $iglobal[1] will be equal to 25 after this program line.
T25
// $iglobal[2] will be equal to 99 after this program line.
T99

When you use a custom T-code body, it must include a call to the argument() function with the G-code letter T. You must use this call with T because it is the only method you can use to access the required integer argument.

IMPORTANT: If a custom T-code body has a call to the argument() function that uses a G-code letter that is not T, a compiler error will occur. If a custom T-code body does not contain a call to the argument() function with T, a compiler warning will occur.

To see if the integer argument is currently defined, use the defined() function in a custom T-code body. This function gives you more information for custom G-codes and M-codes than it does for custom T-codes. This occurs because custom T-codes must be used with the integer argument. If you use a T-code without an integer argument that follows, then a compiler error will occur. If the body of the custom T-code was used, the integer argument will be defined.

If you use a custom T-code body with a call to the defined() function, it must use the G-code letter T.

IMPORTANT: If a custom T-code body has a call to the defined() function that uses a G-code letter that is not T, a compiler error will occur.

Program Example: G-Code Specification Rules

// E commands
E100E 100
				
// Exponential notation
$myVar = 1e10

// Move with Y = 1, E command = 10
G1 Y1E10

// Move with Y = 1, E command = 10
G1 Y1 E10

// Move with Y = 1E10 (no E command)
G1 Y 1E10

// Move with Y = 1, E command = 10
G1 Y 1 E 10

// Exponential notation can be enforced by using delimiter characters
G1 Y(1E10)G1 Y=1E10