Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: mcardoso on March 07, 2019, 09:13:55 AM
-
Finally broke the ice and made my first LUA script (I have several others that don't work right now). Thought I would share. If you see anything that could be made cleaner, please let me know.
Description of Operation: Power drawbar is a 2 position air cylinder connected to a 5 port, 4 way, 3 position, center exhausting valve. The operator interface is a momentary N.O. pushbutton. While the pushbutton is pressed and held, the drawbar is extended and the tool released. When the button is released, the cylinder retracts and once the cylinder is retracted all outputs turn off to depressurize the cylinder.
In the Mach 4 screen load LUA script, I added the following code:
--------------------------------------
-- PDB Timer Code --
--------------------------------------
PDBTimerPanel = wx.wxPanel (wx.Null, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize )
--This line creates a Window which has properties exposed to Win10. This window will not appear on screen, but the Windows event will be tied to it.
PDBTimer = wx.wxTimer(PDBTimerPanel)
--This line creates the actual timer 'PDBTimer' which will be referenced throughout the code
PDBTimerPanel:Connect(wx.wxEVT_TIMER,
function(event)
local PDBRetract, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2) -- Is mapped to ESS PDB Retract
mc.mcSignalSetState(PDBRetract, 0)
mc.mcCntlSetLastError(inst, "PDB Retracted")
PDBTimer:Stop()
end)
In the Signals LUA script, I added the following code:
---------------------------------------------------------------
-- Power Drawbar Pushbutton
---------------------------------------------------------------
if (sig == mc.ISIG_INPUT3) and (state == 1) then
local PDBExtend, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
mc.mcSignalSetState(PDBExtend, 1)
mc.mcCntlSetLastError(inst, "PDB Retracted")
end
if (sig == mc.ISIG_INPUT3) and (state == 0) then
local PDBExtend, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
local PDBRetract, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
mc.mcSignalSetState(PDBExtend, 0)
mc.mcSignalSetState(PDBRetract, 1)
PDBTimer:Start(500)
mc.mcCntlSetLastError(inst, "PDB Timer Started")
end
The basis for this code was DazTheGaz's Youtube video on wxTimer (Thank you!):
https://www.youtube.com/watch?v=nkCx2xdoPEw&feature=youtu.be
Future modifications will be to automatically retract the cylinder when Mach 4 loads, to save the position of the cylinder to a register, and to interlock spindle operation so the spindle cannot start while the drawbar is extended.
-
Thanks for sharing. It is always nice to have a piece of working code that others can adapt or extend.
RT
-
Happy to! I've found most of the code posted here is people asking for help with non-working code. I'll try to post anything I get working even if it is silly.
-
Hi Mcardoso,
While it might be "silly" from a coder's point of view,
it could very well be a "lifesaver" from a machinist's point of view.
Please feel free to post what you come up with.
Mike
-
Sorry guys!!!
Caught that I had uploaded an older version of the code with a few typos. Same concept, but use this instead:
Screen Load Script:
--------------------------------------
-- PDB Timer Code --
--------------------------------------
TimerPanel = wx.wxPanel (wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize )
--This line creates a Window which has properties exposed to Win10. This window will not appear on screen, but the Windows event will be tied to it.
Timer = wx.wxTimer(TimerPanel)
--This line creates the actual timer 'PDBTimer' which will be referenced throughout the code
TimerPanel:Connect(wx.wxEVT_TIMER,
function(event)
local PDBRetract, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2) -- Is mapped to ESS PDB Retract
mc.mcSignalSetState(PDBRetract, 0)
mc.mcCntlSetLastError(inst, "PDB Retracted")
Timer:Stop()
end)
Signal Script:
---------------------------------------------------------------
-- Power Drawbar Pushbutton
---------------------------------------------------------------
if (sig == mc.ISIG_INPUT3) and (state == 1) then
local PDBExtend, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
mc.mcSignalSetState(PDBExtend, 1)
mc.mcCntlSetLastError(inst, "PDB Retracted")
end
if (sig == mc.ISIG_INPUT3) and (state == 0) then
local PDBExtend, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
local PDBRetract, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
mc.mcSignalSetState(PDBExtend, 0)
mc.mcSignalSetState(PDBRetract, 1)
Timer:Start(500)
mc.mcCntlSetLastError(inst, "PDB Timer Started")
end