Hello Guest it is April 25, 2024, 07:21:04 AM

Author Topic: Button script  (Read 2241 times)

0 Members and 1 Guest are viewing this topic.

Offline dhelf

*
  •  10 10
    • View Profile
Button script
« 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
Re: Button script
« Reply #1 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline dhelf

*
  •  10 10
    • View Profile
Re: Button script
« Reply #2 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

Offline Bill_O

*
  •  563 563
    • View Profile
Re: Button script
« Reply #3 on: August 07, 2019, 05:16:15 PM »
Use mc.mcCntlGcodeExecuteWait not Gcodecontrolexecutewait.
Bill

Offline dhelf

*
  •  10 10
    • View Profile
Re: Button script
« Reply #4 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,

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Button script
« Reply #5 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")
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!

Offline dhelf

*
  •  10 10
    • View Profile
Re: Button script
« Reply #6 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.

Offline dhelf

*
  •  10 10
    • View Profile
Re: Button script
« Reply #7 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

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Button script
« Reply #8 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.
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!

Offline dhelf

*
  •  10 10
    • View Profile
Re: Button script
« Reply #9 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 ;)