Subprograms

Function: Call a subprogram

Frequently recurring tasks, such as pocket milling, hole drilling, and tool changing, can be swapped out to G code subprograms and called from there. During a call, parameters can be passed to the subprogram. The data types BOOL, LREAL, and STRING are permitted for this purpose.

Note

If you use subprograms, then you have to use the function blocks SMC_ReadNCFile2 and SMC_NCInterpreter instead of SMC_ReadNCFile and SMC_NCDecoder.

Note

Subprograms work in the online decoder only (not in the CNC editor).

Each subprogram is stored in a separate file. These files are saved in one or more subdirectories on the controller. They have to have the file extension .cnc. The file name must correspond to the name of the subprogram and be lowercase. Example: Subprogram name "Drill" -> file name drill.cnc.

The POU SMC_ReadNCFile2 has an input aSubProgramDirs : ARRAY[0..4] OF STRING(174). Up to five subdirectories can be specified beyond this. They are scanned in the given order. If there are subprograms with the same name in multiple directories, then the subprogram is found that has the directory with the lowest index in the array. The subprogram name is converted to lowercase.

Example

aSubProgramDirs = ['subprograms/user', 'subprograms/system', '']

The subprogram DrillA1 is scanned first in the file subprograms/user/drilla1.cnc. If this file does not exist, then the search continues in subprograms/system/drilla1.cnc.

Note

Subprograms cannot be created offline in the CNC editor.

The maximum nesting depth of subprogram calls is limited to 14.

Syntax for the call

Subprogram calls are special blocks in the G code that consist of a block number and a call only. No additional words are permitted.

N<block number> <Name> <BracketOpen> <ActualParamList> <BracketClosed>

<ActualParamList> ::= Empty | <ActualParamListNotEmpty>
<ActualParamListNotEmpty> ::= <ActualParamValue> | <ActualParamValue>, <ActualParamListNotEmpty>

<Name>

Valid IEC identifier with minimum 3 and maximum 80 characters. It must correspond to the file name (without extension) where the subprogram is defined. Uppercase or lowercase does not matter for subprogram names.

The following keywords are invalid for names: RETURN, IF, ELSE, END_IF, CASE, END_CASE, FOR, END_FOR, WHILE, END_WHILE, REPEAT, UNTIL, END_REPEAT.

<ActualParamList>

There has to be exactly the same number of parameter values specified as the subprogram defines (see "Syntax for the declaration"). The type of each parameter value must agree with the declaration.

<BracketOpen>/<BracketClosed>

For reasons of compatibility, braces are used instead of parentheses in default settings for SMC_ReadNCFile2 and subprogram calls and declarations. Parentheses are valid in G code for comments.

The function block SMC_ReadNCFile2 has a mode (bParenthesesAsComments input) where parentheses are not comments. Instead, multiline comments are opened and closed with (* and *), respectively. In this new mode, both braces and parentheses can be used for subprogram calls and declarations.

<ActualParamValue>

Variable, literal, or any expressions

Example

N10 SUB1()
N20 DRILL(10.0)
N30 SUB2(5, "Text", 2.5)

Syntax for the declaration

A subprogram has to be saved in a separate file. The first line (neither empty nor a comment line) must contain the declaration of the subprogram. The following syntax applies:

SUBPROGRAM <Name> <BracketOpen> <FormalParamList> <BracketClosed> <RESTORE_OPT>
<contents of subprogram>
END_SUBPROGRAM

<FormalParamList> ::= Empty | <FormalParamListNotEmpty>
<FormalParamListNoEmpty> ::= <FormalParam> | <FormalParam> , <FormalParamListNotEmpty>
<FormalParam> ::= <ParamName> : <ParamType>
<ParamName>   ::= #[a-zA-Z0-9_]+

<ParamType> ::= LREAL | BOOL | STRING ; string with maximum length of 255 bytes
<RESTORE_OPT> ::= RESTORE_MODES

<ParamName>

The length of the name can be a maximum of 80 characters (not including the prefix #).

RESTORE_MODES

If this keyword is specified, then the following modal states are restored when returning to the calling program (set to the value that it had at the call):

  • Active G code

  • Relative/Absolute mode (G90/G91, G98/G99)

  • Circular plane and 2D/3D mode

  • Decoder coordinate system (including scaling)

  • Feed rates (path and additional axes), rapid traverse rate, maximum accelerations and decelerations (path and additional axes)

  • Feature flags and general parameters (G38)

  • Tool radius (D word)

  • Tool offsets (G43)

  • S profile (S word)

The following modal states are not restored:

  • Current position of the decoder/interpreter as well as the current cardinal spline state

  • State (on/off) of the path preprocessing POUs (G40-G43, G50-G52, G60-G61, G70-G71)

  • Modulo setting of the additional axes (PA, PB, …)

Note: Regardless of this keyword, the implicit counter variables (G36, G37) are restored when returning from the subprogram to the calling program.

Examples

SUBPROGRAM SUB1()                 ; no formal parameters
SUBPROGRAM DRILL(#depth : LREAL)
SUBPROGRAM SUB2(#a : LREAL, #b : STRING, #c : LREAL)
SUBPROGRAM SRM1() RESTORE_MODES

Syntax for the return

The return is done either at the end of the subprogram text (before the END_SUBPROGRAM) or explicitly with the following syntax: N<block number> RETURN.

Using the formal parameters in the subprogram

The values of the formal parameters can be accessed in the subprogram by #<ParamName>.

The number of formal parameters is limited to 21.

Example

SUBPROGRAM SUB(#Param1 : LREAL)
N10 G01 X#Param1

Display of the call stack

The interpreter (SMC_NCInterpreter) has an output that contains the 10 top active programs/subprograms: aActivePrograms : ARRAY[0..9] OF STRING. The first entry (aActivePrograms[0]) is the currently interpreted program/subprogram. The second entry (aActivePrograms[1]) is the calling program/subprogram and so on. If there is no calling program, then the corresponding string is empty.

The call stack can also be displayed at interpolation time. The SMC_DisplayNCCallstack POU shows the active programs/subprograms in the same format as the interpreter, only later (namely when the movement is run). In doing so, the interpreter output CallstackInfo (SMC_NCCallstackInfo) and the interpolator are passed to it as VAR_IN_OUT variables. SMC_NCCallstackInfo stores all call stack changes, including the corresponding SMC_GeoInfo object number, in a ring buffer. At this time, this restricts the number of storable call stack changes between the interpretation time and the interpolation time to 128. Because the ring buffer does not meet multitasking criteria, SMC_DisplayNCCallstack has to be called from the interpreter task.

The sample program CNC_Subprogram.project shows an example of the display of the call stack at interpolation time.

See also