Hello Guest it is March 28, 2024, 12:48:08 PM

Author Topic: First Successful LUA script, Power Drawbar with a timer  (Read 1245 times)

0 Members and 1 Guest are viewing this topic.

First Successful LUA script, Power Drawbar with a timer
« 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:

Code: [Select]
--------------------------------------
-- 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:

Code: [Select]
---------------------------------------------------------------
-- 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.
Re: First Successful LUA script, Power Drawbar with a timer
« Reply #1 on: March 08, 2019, 11:27:05 AM »
Thanks for sharing.  It is always nice to have a piece of working code that others can adapt or extend.

RT
Re: First Successful LUA script, Power Drawbar with a timer
« Reply #2 on: March 08, 2019, 11:42:51 AM »
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.
Re: First Successful LUA script, Power Drawbar with a timer
« Reply #3 on: March 09, 2019, 05:26:51 AM »
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
We never have the time or money to do it right the first time, but we somehow manage to do it twice and then spend the money to get it right.
Re: First Successful LUA script, Power Drawbar with a timer
« Reply #4 on: March 10, 2019, 08:18:52 PM »
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:

Code: [Select]
--------------------------------------
-- 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:

Code: [Select]
    ---------------------------------------------------------------
    -- 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