Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: dhelf on August 06, 2019, 06:14:46 PM

Title: Button script
Post by: dhelf on August 06, 2019, 06:14:46 PM
I am slowly learning to write the macros I used in Mach3 in lua. I have successfully written 6 of them and when running in the zerobrane editor and watching the diagnostics screen, everything does what it is suppose to. (simulation, not hooked up to machine) I named the macro M127.mcs ok no problem. I want to call the script from a button, in mach3 I just called the actual M127, I assume in Mach4 I should write the script to a button with screen editor? So created a button, in the screen editor, then copy and pasted my M127 script I wrote in to the button script. I did delete the function m127() saved screen. The issue is it will not run the script from the button, at least it will not in its entirety. What am I doing wrong? Is it possible to call a Macro from a button?
Much thanks
Dan
Title: Re: Button script
Post by: joeaverage on August 06, 2019, 08:52:10 PM
Hi,

Quote
Is it possible to call a Macro from a button?

Not quite. Remember, a button is in the GUI and is a part of the GUI chunk. A macro on the other hand is part of the
Gcode interpreter chunk. One chunk or the other runs. It is not possible for a GUI button to call a function that is
in the other chunk.

You have done a good thing, writing it as a macro and testing it out with the editor.

What you need to do is extract the function and call it some other name than m127(), maybe MyFunction(). Note you can
delete the:
if (mc.mcInEditor()==1) then
   m127()
end
from the function.

Now put the function and all its code in the screen load script. Change the called function in the button script to MyFunction().

Thus when you hit the button, being in the GUI chunk, the button script will search for MyFunction() and it will find it in the screenload
script which is also in the same chunk.

Craig
Title: Re: Button script
Post by: dhelf on August 07, 2019, 03:53:23 PM
Thank you.
So I changed my button script to
loadFunction()

I then edited my screen load script, and added a function. This is what was added:

function loadFunction()
 local inst = mc.mcGetInstance()
local Stock = mc.mcCntlGetPoundVar(inst, 1050)
local length = (Stock * 12)
local move = (330 - length)
local offset = mc.mcCntlGetPoundVar(inst, 1049)



mc.mcCntlGcodeExecute(inst,"G53 G01 z -0.125 F50\nG53 G00 X 1.00\nG53 G01 A 2.00 F1800")




mc.mcAxisHome(inst,3)
wx.wxMilliSleep(1000)



local osig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
 mc.mcSignalSetState(osig, 1);

wx.wxMessageBox( "Click Ok once material is on Lift Cylinders")
local osig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
 mc.mcSignalSetState(osig1, 1);


wx.wxMessageBox("Verify Cylinders are fully raised and  area is clear for machine movement")

 wx.wxMilliSleep(500)
mc.mcCntlGcodeExecute(inst, "G53 G01 x " .. move .."\n G53 G01 A" .. offset)


 mc.mcSignalSetState(osig, 0);
wx.wxMilliSleep(500)

 

wx.wxMilliSleep(500)
local osig2 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
 mc.mcSignalSetState(osig2, 1);
wx.wxMilliSleep(500)
local osig3 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3)
 mc.mcSignalSetState(osig3, 1);
wx.wxMilliSleep(500)
local osig4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
 mc.mcSignalSetState(osig4, 1);

wx.wxMilliSleep( 5000)
 mc.mcSignalSetState(osig3, 0);
wx.wxMilliSleep(500)
 mc.mcSignalSetState(osig4, 0);
wx.wxMilliSleep(1000)
 mc.mcSignalSetState(osig2, 0);
wx.wxMilliSleep(500)
 mc.mcSignalSetState(osig1, 0);


end

It works, however I am missing something on how to control the timing of it all. It does not wait for the movement at begining to complete before proceeding. I read that you can not use Gcodecontrolexecutewait, as I did in my macro. I also read the gcode should be on one single line which I did.
What am I doing incorrect?
Thanks
Dan
Title: Re: Button script
Post by: Bill_O on August 07, 2019, 05:16:15 PM
Use mc.mcCntlGcodeExecuteWait not Gcodecontrolexecutewait.
Bill
Title: Re: Button script
Post by: dhelf on August 07, 2019, 05:47:59 PM
Thanks
I tried that, the issue I have is the screen is not updated during the mc.mcCntlGcodeExecuteWait
Being this function takes a bit of time, and with no axis dro's or output status leds changing it appears it is locking up. It actual runs the code I am just looking for a way to run this code and the screen is updated by clicking a button,
Title: Re: Button script
Post by: Chaoticone on August 07, 2019, 06:15:10 PM
If it can all be done in Gcode do it all as one string of gcode and do not use wait. It will be executed in order.

If you have to use api calls you might want to consider using a coroutine. See the ref all home button, function in screen load script and PLC of wx6.set.

I would get rid of those sleeps too. Rule of thumb is if you have to use sleep you need to rethink your code.

Maybe you can do it all in a macro (say 127). Then when you want it to run from a button the buttons clicked script looks something like this.

local inst = mc.mcGetInstance()
mc.mcCntlMdiExecute(inst, "M127")
Title: Re: Button script
Post by: dhelf on August 07, 2019, 06:32:03 PM
I will give that a try. The sleeps are really not for the code, they are for a delay as the outputs are activating air cylinders and we can not have the next output activated till the cylinder has finished its stroke.
Title: Re: Button script
Post by: dhelf on August 07, 2019, 06:40:13 PM
Ok, I changed the button script to:
local inst = mc.mcGetInstance()
mc.mcCntlMdiExecute(inst, "M127")
It runs about 1/2 the code then stops? But if I load the M127.mcs in the zerobrane editor and run it completes with no problem?

Thanks to everyone for the help
Title: Re: Button script
Post by: Chaoticone on August 07, 2019, 06:57:25 PM
Quote
The sleeps are really not for the code, they are for a delay as the outputs are activating air cylinders and we can not have the next output activated till the cylinder has finished its stroke.

If that is the case then you should have sensors that tell you when the cylinders have finished their stroke. And the way to hold it up until it does is to use the mc.mcSignalWait api and wait for that confirmation signal to go to whatever state it goes to confirming it has finished it's stroke.

Put some checks in you macro to find out where it is breaking.........
mc.mcCntlSetLastError(inst, "Step one finished, cylinder 1 should be retracted")
mc.mcCntlSetLastError(inst, "Step two finished, cylinder 2 should be retracted") etc.
Title: Re: Button script
Post by: dhelf on August 07, 2019, 08:17:40 PM
Thank you. I have full intentions of adding end of stroke sensors. I am doing everything I can to keep it simple as mach4 is totally new to me. Have over 32 air cylinders so did not want to confuse myself any further ;)
Title: Re: Button script
Post by: Chaoticone on August 08, 2019, 10:44:34 AM
No problem, i understand. But do yourself a favor and go ahead and do it right while your doing. If not you will spend more time doing it. Likely much, much more time.

Learn how to do it right for one and you know how to do it right for all.
Title: Re: Button script
Post by: dhelf on August 08, 2019, 02:14:26 PM

I am a bit overwhelmed with all the scripts, I thought I could make it more simple and go from there, but after seeing what goes in to a script you are 100% correct, going to start rewriting them this evening. Wish me luck! Going to need some forum assistance with my odd homing cycle this weekend.
Thank god for this forum and helpful people like yourself.