Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: RecceDG on July 02, 2021, 01:38:35 PM

Title: Why doesn't this work? (FEED HOLD state)
Post by: RecceDG on July 02, 2021, 01:38:35 PM
Code: [Select]
function InputFeedHold()

-- Momentary pushbutton, so we get TWO signals with each button press:
        -- an "ON" and an "OFF"
        -- So check state and ignore the "OFF"

local hHoldButton
local HoldButtonState
hHoldButton, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2)      -- signal 2
HoldButtonState, rc = mc.mcSignalGetState(hHoldButton)

if (HoldButtonState == 1) then

mc.mcCntlSetLastError(inst, "Control Panel Feed Hold PRESS")

-- If we are running a program, pause
-- If we are already paused, stop
local Running
local Paused
Running, rc = mc.mcCntlIsInCycle(inst)
Paused, rc = mc.mcCntlFeedHoldState(inst)

if (Running) then -- We are running, so do something
if (Paused) then -- We are paused, so stop
CycleStop()
mc.mcCntlSetLastError(inst, "Control Panel CYCLE STOP")
else -- Pause
rc = mc.mcCntlFeedHold(inst)            -- Should this maybe be FeedHold()?
mc.mcCntlSetLastError(inst, "Control Panel FEED HOLD")
end
else
-- Spurious button press
mc.mcCntlSetLastError(inst, "Not running or paused - IGNORE ME")
end
else
mc.mcCntlSetLastError(inst, "Control Panel Feed Hold RELEASE")
end
end

The idea here is that push FEED HOLD once (a physical button) should activate FEED HOLD, and pushing FEED HOLD a second time (while already paused) should CYCLE STOP.

The code above triggers CYCLE STOP

The test for Running works, the test for Paused does not.

What have i missed?
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: Bill_O on July 02, 2021, 02:46:43 PM
I could be wrong but i think you need to put a value for Running and Paused.

You have;
if (Paused) then

I think you need;
if (Paused=1) then

Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: Cbyrdtopper on July 02, 2021, 03:07:37 PM
Just want to watch this thread.  I tried to do this when Mach4 was just a tiny baby and I didn't know anything about LUA  HAHA
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: RecceDG on July 02, 2021, 03:50:23 PM
OK, you are right - it seems that LUA doesn't follow the same idioms that some other languages do. It needs an explicit equality check.

But not done yet - it seems that Running, rc = mc.mcCntlIsInCycle(inst) may not be the right test.That appears to test if a *file* is running, not that the machine is active (so it doesn't work for MDI programs).

Code: [Select]
--------------------------------------------------------------
-- Control Panel Feed Hold
-- One push for Feed Hold, two pushes for Cycle Stop
---------------------------------------------------------------

function InputFeedHold()

-- Momentary pushbutton, so we get TWO signals with each button press:
        -- an "ON" and an "OFF"
        -- So check state and ignore the "OFF"

local hHoldButton
local HoldButtonState
hHoldButton, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2)      -- signal 2
HoldButtonState, rc = mc.mcSignalGetState(hHoldButton)

if (HoldButtonState == 1) then

mc.mcCntlSetLastError(inst, "Control Panel Feed Hold PRESS")

-- If we are running a program, pause
-- If we are already paused, stop
local Running
local Paused
Running, rc = mc.mcCntlIsInCycle(inst)
Paused, rc = mc.mcCntlFeedHoldState(inst)

if (Paused == 1) then -- We are paused, so stop
CycleStop()
-- rc = mc.mcCntlFeedHold(inst) -- TODO This is wrong, but I want hold not stop right now
mc.mcCntlSetLastError(inst, "Control Panel CYCLE STOP")
elseif (Running == 1) then -- We are running, so do something

rc = mc.mcCntlFeedHold(inst)            -- Should this maybe be FeedHold()?
mc.mcCntlSetLastError(inst, "Control Panel FEED HOLD")
else
-- Spurious button press
mc.mcCntlSetLastError(inst, "Not running or paused - IGNORE ME")
end
else
mc.mcCntlSetLastError(inst, "Control Panel Feed Hold RELEASE")
end

end


This will CycleStop if the machine is in feed hold (via the GUI) but doen't respond to a button press feed hold; it follows the IGNORE ME path.

CONFIRMED - works perfectly if I load a gcode from *file*, but MDI fails.

There doesn't seem to be an API call that returns true if an MDI file is running?
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: jbuehn on July 02, 2021, 05:45:35 PM
Can you get the state with mc.mcCntlGetState(inst) and check you're not in an idle state (mc.MC_STATE_IDLE)? Or in newer version of Mach4 there's an output signal mc.OSIG_MACHINE_IDLE.
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: RecceDG on July 02, 2021, 08:09:09 PM
Is there a doc somewhere that lists the possible machine states and their aliases?
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: jbuehn on July 02, 2021, 10:35:17 PM
I don't think it's in the docs, but this post has the possible return values from mc.mcCntlGetState(inst)

https://www.machsupport.com/forum/index.php?topic=41409.msg271498#msg271498
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: RecceDG on July 02, 2021, 10:40:27 PM
FRUN is “run from file” and MRUN is “run from MDI”?
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: jbuehn on July 02, 2021, 10:44:35 PM
Yes
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: joeaverage on July 03, 2021, 12:10:21 AM
Hi,
there is another listing for the return values and quite a few other ENUMS etc:

https://www.machsupport.com/forum/index.php?topic=40051.0 (https://www.machsupport.com/forum/index.php?topic=40051.0)

Craig
Title: Re: Why doesn't this work? (FEED HOLD state)
Post by: RecceDG on July 03, 2021, 04:31:07 PM
OK, that did it.

Thanks everyone.