Passing persist variable using "Script-> action-> Program"

Moderators: TomKerekes, dynomotion

Post Reply
Roamin
Posts: 25
Joined: Tue Dec 13, 2022 10:34 pm

Passing persist variable using "Script-> action-> Program"

Post by Roamin » Thu Apr 20, 2023 12:56 pm

Hi Tom,

Long time no talk, hope all is well.

I'm trying to make custom buttons that will pass a parameter to my tool changer routine. While milling , the g-code command "M6 T1" will pass the "1" to my program without issue.

Now using the custom button , I was under the impression that the "param" box would be the value of the VAR but it doesn't seem to work.

I set the parameters to the Action function like on the picture, but when calling the tool changer, the value isn't passed properly and instead of "3" as in the example ,it seems to use an uninitiated value of -10000000. What am I doing wrong? Anyway I can pass a value (from 1 to 16) to the function using the action call?

Thanks for the help!
Attachments
toolparam.png
toolparam.png (7.61 KiB) Viewed 3418 times

User avatar
TomKerekes
Posts: 2676
Joined: Mon Dec 04, 2017 1:49 am

Re: Passing persist variable using "Script-> action-> Program"

Post by TomKerekes » Thu Apr 20, 2023 5:17 pm

Hi,

The value is passed as a 32-bit float value and can be accessed with

Code: Select all

	int Button = *(float *)&persist.UserData[PERSIST_VAR]; // button code pushed
Is that how you are accessing it ?
Regards,

Tom Kerekes
Dynomotion, Inc.

Roamin
Posts: 25
Joined: Tue Dec 13, 2022 10:34 pm

Re: Passing persist variable using "Script-> action-> Program"

Post by Roamin » Thu Apr 20, 2023 5:51 pm

#define TOOL_VAR 9

int *Tool = &persist.UserData[TOOL_VAR];

printf("Tool : %d\n", *Tool); // Displays value of the tool to change to (From M06 TXX g-code)

// Logic to calculate rotation count and direction from Last_Tool and Tool.
if (*Last_Tool > *Tool)
{
...
}

(*Last_Tool is set by reading a file on the PC)

I'm not sure how I would go about to combine your example of "Button" and my use of "*Tool". Remember , I am not a coder by trade so my skills are somewhat limited.

Should I simply duplicate my Tool_Changer.c code and adapt your "Button" in the code that is called from the button presses and keep my original code the way it is , and works , for when G-code changes the tool? Or , do you see a way I could write the code only once , combining both variables? Something like "If button is pressed , set variable this way , else set it the original method" ?

Thanks for the prompt response!

User avatar
TomKerekes
Posts: 2676
Joined: Mon Dec 04, 2017 1:49 am

Re: Passing persist variable using "Script-> action-> Program"

Post by TomKerekes » Thu Apr 20, 2023 11:01 pm

You might read it assuming it is an integer set by M6 and if it is invalid then assume it is a float set by a button. Something like:

Code: Select all

	int Tool = persist.UserData[TOOL_VAR];  // assume integer
	
	if (Tool <= 0 || Tool >= 10) // invalid Tool Number?
		Tool = *(float *)&persist.UserData[TOOL_VAR];  // yes, must be float 
		
	printf("Tool : %d\n", Tool); // Displays value of the tool to change to (From M06 TXX g-code)

	// Logic to calculate rotation count and direction from Last_Tool and Tool.
	if (*Last_Tool > Tool)
	{
	...
	}
Note your original code assumes that Tool is a pointer to an integer. So its value is obtained by de-referencing the pointer with *Tool to get the value. But in the above code Tool is a simple integer so use Tool everywhere. A pointer is useful if you need to change the original value, but I doubt if that is ever required.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply