Dynamic M-Functions in G-Code

Hello Community

Customer want to set a dynamic M-Function in G-Code from BnR.

For example: M[MyVar] → MyVar should be dynamic, that he could cyclically change the M-Function

Our assumption was first “M=MyVar” but it did not work with the error «unexpected Assignment character»

We tried the following settings for this (with the specific error output)
M=MyVar → unexpected Assignment character
M(MyVar) → unexpected End of line
M[MyVar] → Call to unknown function Name: M
M{MyVar} → unexpected Open bracket for indexing
M<<MyVar → unexpected TKG_STRING_CONCAT

Does anybody know the code we have to write for this?

Thanks a lot in advance and have a good one

Hello

You could solve this by using Structured Text instead of G-Code. In Structured Text there is the function SetM(Index), which is exactly what you need.

You can wrap this function and make it available also in G-Code, by making a Structured Text Program containing a wrapper function and then calling this wrapper function from G-Code.

The file structure for the G-Code on the user partition would look like this:

image

STWrapper.st

In the STWrapper.st file you can then define functions written in ST which will be also available in G-Code:

(* All functions in this file should be globally available after loading *)
#pragma SCOPE IP_GLOBAL

(* Wrapper so the SetM() function can be called from G-Code *)
FUNCTION SetMFunc : NONE
	VAR_INPUT
		Idx : UINT;
	END_VAR
	SetM(Idx);
END_FUNCTION
CallVariableMFunc.cnc

In the G-Code of CallVariableMFunc.cnc you have to Call the STWrapper.st once, so the functions are available to the interpreter. This can be done with the L <File> command and it will not make any movement. Afterwards you can then call all the functions, which were defined in the STWrapper.st.

(****************************************************************)
(Call M function by index from variable in G-Code)
(****************************************************************)

(The following line is required to make the functions known to the interpreter)
L STWrapper.st 

def UINT MFunIdx1 = 301
def UINT MFunIdx2 = 302

SetMFunc[MFunIdx1] (will call M301)
G4 10
G0 X100 X50
SetMFunc[MFunIdx2] (will call M302)

(End of program)
M30

If you need these functions in all G-Code programs, instead of the L <File> command you could also load the STWrapper.st file with the function block MC_BR_LoadProgram() in the PLC code.

1 Like