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.
If you use subprograms, then you have to use the function blocks SMC_ReadNCFile2
and SMC_NCInterpreter
instead of SMC_ReadNCFile
and SMC_NCDecoder
.
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
.
Subprograms cannot be created offline in the CNC editor.
The maximum nesting depth of subprogram calls is limited to 14.
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>
|
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: |
|
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. |
|
For reasons of compatibility, braces are used instead of parentheses in default settings for
The function block |
|
Variable, literal, or any expressions |
Example
N10 SUB1()
N20 DRILL(10.0)
N30 SUB2(5, "Text", 2.5)
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
|
The length of the name can be a maximum of 80 characters (not including the prefix |
|
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):
The following modal states are not restored:
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
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
.
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