Combining G-Code and ST

A GST-Program

<g-code>
<g-code>
! <st-code>
<g-code>
<g-code>
{
<st-code>
<st-code>
! <g-code>
<st-code>
<st-code>
}
<g-code>
<g-code>

A GST-file consists of sequences of G-code and sequences of ST-code that can be interleaved as shown above. Each program starts in G-code mode. The mode can be switched to ST for one line using an exclamation mark (‘!’). The ST-mode ends at the end of line automatically.

As an alternative a block of ST-code can be defined using curly braces (‘{‘…’}’). This notation is more practical to define a long sequence of ST-code in a GST-program. Within the ST-block the G-code mode can be entered for one line using the exclamation mark. Thereby, the G-code mode ends at the end of line automatically.

G-Code Block

<address><value> <address>=<G-Expression> <address>{<ST-Expression>}

A line of G-code is called a block. It consists of a sequence of words. A word is a combination of an address (e.g. G or X) and a value. A value can be defined by a literal (e.g. 2.54), by a G-expression (e.g. 2*foo+1) or by an ST-expression (e.g. sin(foo**2)-1).

G-Code Expression

<address>=a+b-c*d/e

The result of the expression is used as the value of the word. The four basic arithmetic operations (‘+’, ‘-’, ‘*’, ‘/’) can be used in a G-expression. They are evaluated as expected, i.e. all operations are left-associative and ‘*’, ‘/’ have a higher precedence than ‘+’, ‘-’. Variables that have been declared in ST can also be used in a G-expression (with respect to their scope).

All computations are performed using type LReal (64-bit floating point according to IEEE 754). The value of an ST-variable is implicitly converted to type LReal according to the conversion rules of ST. If a type (e.g. STRING) cannot be converted, an error is reported.

Combining G-Code and ST 1:

RESTRICTION:

ST-variables that contain a number in their name (e.g. x0) cannot be used in a G-expression to avoid confusion with a G-Code like X0. This limitation does not apply to ST-expressions.

Combining G-Code and ST 2:

RESTRICTION:

Array variables, struct variables and objects cannot be used in a G-expression. This limitation does not apply to ST-expressions.

Combining G-Code and ST 3:

RESTRICTION:

Parentheses are not allowed in a G-expression as they are used to denote comments in G-Code. For the same reason function calls are not available. These limitations do not apply to ST-expressions.

Embedded ST-Expression

<address>{<ST-Expression>}

The result of the ST-expression is used as the value of the word. It must be convertible to LReal. Basically, an ST-expression is ST-Code that could be placed on the right hand side of an assignment. Other ST-Code (e.g. an ST-statement) is not allowed. However, extensive computations can be encapsulated in an ST-function that is then called in the ST-expression.

Combining G-Code and ST 4:

An ST-expression should not have side effects, since the evaluation order of ST-expressions is generally undefined and may change in the future. Besides, this style of programming employing side effects is a bad programming style. For instance, an ST-expression should not call a function that contains G-Code.

Example:

G00 X0 Y0 Z0
! VAR   i : INT;   END_VAR
G01 Z-1 F6000
{
FOR i := 1 TO 5 DO
!G01 X=i*10 Y{ (i MOD 2) *10 }
END_FOR;
}
Combining G-Code and ST 5:

Figure “ExampleExpressions”.

Suppression of G-Code Blocks

/<n> <G-Code block>

The execution of a G-Code block can be suppressed conditionally. If ‘/<n>’ is prefixed and the n-th bit in an internal disable mask is set, the block is suppressed (not executed). The disable mask can be set by the PLC and by the ST-function disableMaskSet. If n is omitted, it has 0 value by default. [See section Suppression of G-Code Blocks.]