Hello Guest it is March 28, 2024, 11:40:26 AM

Author Topic: Macro - Pause until input triggered  (Read 8329 times)

0 Members and 1 Guest are viewing this topic.

Macro - Pause until input triggered
« on: September 26, 2016, 08:14:44 PM »
Is there a simple way to pause motion until an input is triggered within a macro? 

I would like to ensure my Z axis returns home before the macro ends because after the macro would be an X-Y move. This is a cylinder where I have output 1 activate the "up" direction and output 0 activate the "down" direction. 

Something like this:

1. activate output_1 (up)
2. wait for Z home switch to trigger
3. end macro.

What is the best way to code step 2 (wait for input) in a Macro?

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Macro - Pause until input triggered
« Reply #1 on: September 27, 2016, 02:40:49 AM »
Take a look at the api docs for mcSignalWait located in the mach4 docs directory.

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Macro - Pause until input triggered
« Reply #2 on: September 27, 2016, 11:09:54 AM »
Thanks.  I gave it a try by adding it to the end of the macro.  For some reason it does not wait for the "Z home" signal before moving on to the next line of the Gcode program ( a G0 X/Y axis positioning move ).   I also tried WAIT_MODE_LOW and it did the same thing.  I was hoping the machine would stop all movement until the Z axis reached the home position. Any Ideas what I am doing wrong?

Code: [Select]
function M100()
   
    local inst = mc.mcGetInstance()
    local input_20 = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT20) -- Get handle for input 20
    local input_20_State = mc.mcSignalGetState(input_20) -- Get input 20 state
    local z_home_sig = mc.mcSignalGetHandle(inst, mc.OSIG_ZHOME) -- Get handle for Z axis home switch
   

    if input_20 == 0 then
        wx.wxMessageBox("Could not locate signal!")

    else

        if input_20_State == 1 then                   --If input_20_State equals 1, then the signal is active.
            wx.wxMessageBox("Signal is already active!")

        else                                                   --If input_20_State does not equal 1, then the signal is inactive.

            mc.mcSignalSetState(input_20, 1)
            mc.mcSignalWait(inst, z_home_sig, WAIT_MODE_HIGH, 20)   

        end
    end
end

if (mc.mcInEditor() == 1) then
    M100()
end

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Macro - Pause until input triggered
« Reply #3 on: September 27, 2016, 11:32:59 AM »
Your trying to wait for a signal handle not a signal id like mc.OSIG_ZHOME

Try something like this but signal should be the INPUT of your Z Home

Code: [Select]
mc.mcSignalWait(inst, mc.ISIG_MOTOR0_PLUS, WAIT_MODE_HIGH, 20)
DazTheGas
New For 2022 - Instagram: dazthegas
Re: Macro - Pause until input triggered
« Reply #4 on: September 27, 2016, 11:59:15 AM »
Hmmm....I changed the signal to "mc.ISIG_MOTOR3_HOME"  which is mapped to the Z axis home switch and it still just moves on to the next line in the main G-code program?



Code: [Select]

function M100()
    
    local inst = mc.mcGetInstance()
    local input_20 = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT20) -- Get handle for input 20
    local input_20_State = mc.mcSignalGetState(input_20) -- Get input 20 state
    local z_home_sig = mc.ISIG_MOTOR3_HOME -- Get signal for Z axis home switch
  


    if input_20 == 0 then
        wx.wxMessageBox("Could not locate signal!")

    else

        if input_20_State == 1 then        
            wx.wxMessageBox("Signal is already active!")

        else    --If input_20_State does not equal 1, then the signal is inactive.
          
            mc.mcSignalSetState(input_20, 1)
            mc.mcSignalWait(inst, z_home_sig, WAIT_MODE_HIGH, 20)    
        end
    end
end

if (mc.mcInEditor() == 1) then
    M100()
end

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Macro - Pause until input triggered
« Reply #5 on: September 27, 2016, 12:54:37 PM »
In the default screens of the later dev versions the ref all home buttons run the RefAllHome function as a coroutine. This was modified to provide an example for those interested in doing something similar. It will reference all axis and (only after motion is finished) pop up a message box saying referencing is complete. You may be able to do something similar to get the results your looking for.

Code: [Select]
--Button script
--RefAllHome()
wait = coroutine.create (RefAllHome) --Run the RefAllHome function as a coroutine named wait.

--Screen Load Script
---------------------------------------------------------------
-- Ref All Home() function.
---------------------------------------------------------------
function RefAllHome()
    mc.mcAxisDerefAll(inst)  --Just to turn off all ref leds
    mc.mcAxisHomeAll(inst)
    coroutine.yield() --yield coroutine so we can do the following after motion stops
    ----See ref all home button and plc script for coroutine.create and coroutine.resume
    wx.wxMessageBox('Referencing is complete')
end

--PLC Script
-------------------------------------------------------
--  Coroutine resume
-------------------------------------------------------
if (wait ~= nil) and (machState == 0) then --wait exist and state == idle
local state = coroutine.status(wait) --Get the status of the coroutine named wait
    if state == "suspended" then --Coroutine named wait is suspended
        coroutine.resume(wait) --Resume coroutine named wait
    end
end
;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!
Re: Macro - Pause until input triggered
« Reply #6 on: September 27, 2016, 02:36:03 PM »
Well a gave it a go as and I get a lua error in the macro "Error while running chunk"  it doesn't seem to want to run my function (GrindCycle) in the Screenload script from within the Macro(M100)?

Here is what I have now:

Add to the ScreenLoad script:

Code: [Select]

--My GrindCycle function starts here:
   
function GrindCycle()

    local inst = mc.mcGetInstance()
    local input_20 = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT20) -- Get handle for input 20
    local input_20_State = mc.mcSignalGetState(input_20) -- Get input 20 state
   
    if input_20 == 0 then
        wx.wxMessageBox("Could not locate signal!")

    else

        if input_20_State == 1 then --If input_20_State equals 1, then grind cycle active signal is High.
            wx.wxMessageBox("Signal is already active!")

        else    --If input_20_State does not equal 1, then the grind cycle signal is inactive.
            mc.mcSignalSetState(input_20, 1) -- set grind cycle signal active
            mc.mcSignalWait(inst, z_home_sig, WAIT_MODE_HIGH, 20) -- wait for z to get home
            coroutine.yield () --yield coroutine so we can do the following after motion stops
            wx.wxMessageBox("Grind Cycle Complete")
        end
    end
end

--My GrindCycle function ends here:


In the M100 macro:

Code: [Select]

function M100()
    wait = coroutine.create (GrindCycle) --Run the GrindCycle function as a coroutine named wait.
end

if (mc.mcInEditor() == 1) then
    M100()
end


Added to the PLC script:

Code: [Select]

-------------------------------------------------------
--  Coroutine resume
-------------------------------------------------------
if (wait ~= nil) and (machState == 0) then --wait exist and state == idle
local state = coroutine.status(wait) --Get the status of the coroutine named wait
    if state == "suspended" then --Coroutine named wait is suspended
        coroutine.resume(wait) --Resume coroutine named wait
    end
end


Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Macro - Pause until input triggered
« Reply #7 on: September 27, 2016, 02:58:57 PM »
Cant remember of top of my head but think macros and gcode run in a different lua instance than the gui??

DazTheGas
New For 2022 - Instagram: dazthegas

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Macro - Pause until input triggered
« Reply #8 on: September 27, 2016, 03:24:06 PM »
Yup, that is right Daz........ my bad.
;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!
Re: Macro - Pause until input triggered
« Reply #9 on: September 27, 2016, 04:29:36 PM »
Is there a function to inhibit motion?  I looked through the API but didn't see one. If there is, maybe I could inhibit motion somehow until the Z home signal goes high?  The Z would still move home even if all motion is inhibited since it is a pneumatic cylinder and mach4 only controls up/down via output_0 and output_1.
« Last Edit: September 27, 2016, 04:31:23 PM by beezerlm »