Hello Guest it is November 14, 2019, 09:09:04 PM

Author Topic: how to use screen "CycleStop" in macro script  (Read 112 times)

0 Members and 1 Guest are viewing this topic.

how to use screen "CycleStop" in macro script
« on: October 12, 2019, 09:19:44 AM »
I posted this in the wrong forum earlier...

I have been running a dual table 5 axis router now for a few years on Mach4.  I have a macro that looks for an input signal from a PLC telling Mach if that table is ready to run.  the problem I am dealing with is the macro is a simple loop, looking at the Mach input controlled by the PLC and looping if it isn't on.  I need another way to get it out of the loop.  I wanted to use the screen button "Cycle Stop" since 99.9% of the time we are just stopping the program at the end of a run, but can't figure out how to "see" when it is pressed.  I built and coded the machine years ago, and am not a programmer, so sorry if this one is a simple solution.  My logic skills are good, my scripting/syntax skills need work!

Here is the working script:

local Yready = mc.mcSignalGetHandle (inst, mc.ISIG_INPUT5);

   repeat
         local ytable = mc.mcSignalGetState(Yready);         
   until ytable==1

here is what I want to add in red:

local Yready = mc.mcSignalGetHandle (inst, mc.ISIG_INPUT5);
local CycleStopScreen = not sure what goes here
   repeat
         local ytable = mc.mcSignalGetState(Yready);
        local cyclestop = not sure what goes here        
   until ytable==1 or cyclestop==1

I am just not sure how to tag the screen button, or the function.  I have tried a couple ways but it hasn't been working.  Maybe I am approaching it all wrong as well.
I am sure I can add an output signal to the Cycle Stop function that I could then look at, but figure I have to be able to see that screen button being pushed since Mach recognizes it, so there must be something there to directly tag

Thanks in advance for any help.
Re: how to use screen "CycleStop" in macro script
« Reply #1 on: October 14, 2019, 10:23:12 AM »
I see what you're trying to do, but there is a better way.

Mach 4 offers the ability to respond to a change of state of a signal. This happens in the background and doesn't require a user script to be looping to check for it. It acts something like this: "If I see a change in state of a button, run this function".

So rather than looping, looking for your "OK to run" input, you can check the signal state before you start (perhaps as part of the cycle start button) and then just respond to a change in state.

Let's look at what this might look like in code. First off, we need to place the function call. I have done this in the Screen Load Script, but it can also be done in the Signals Script.
Code: [Select]
SigLib = {
    [mc.ISIG_INPUT5] = function (state)   --Change the input number
        SystemOKChange()                  --This is called if there is a change with the input state
    end
}

This code will call a function SystemOKChange() whenever INPUT5 (change this to what you need) changes state.

Next we need to create this function (put it at the end of your screen load script). The is called when the script changes state, but we don't yet know what the new state is. To get this, we need the handle (pointer to memory address of that input signal) and then query Mach 4 for the signal state

Code: [Select]
function SystemOKChange()
    local hSysOK
    local SysOK
   
    hSysOK, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT5)     --Change Input #
    SysOK, rc = mc.mcSignalGetState(hSysOK)
       
    if (SysOK == 0) then       --Invert this logic if needed here
        mc.mcCntlEStop(inst)  --Activate the EStop signal in Mach
        mc.mcCntlSetLastError(inst, "System OK Input Lost: ESTOP Active")   --Show a message in the Screen Set
    else
        mc.mcCntlSetLastError(inst, "System OK!")  --Show a message in the Screen Set
    end 
end

Now with this code, Mach isn't spending time in a loop looking for a change in your input. Because of this, all of your on-screen controls should continue to work fine. You can expand on that SigLib{} list to add additional inputs.

Finally, we don't want your Cycle Start Button to work if the input is OFF. To do this, we remove the property binding from the cycle start button to CycleStart() and add our own button pressed script.

Code: [Select]
local inst = mc.mcGetInstance()

    local hSysOK
    local SysOK
   
    hSysOK, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT5)     --Change Input #
    SysOK, rc = mc.mcSignalGetState(hSysOK)

    if (SysOK == 1) then       --Invert this logic if needed here
            mc.mcCntlCycleStart(inst)
    else
            mc.mcCntlSetLastError(inst, "Cannot Start, System OK Input is OFF")   --Show a message in the Screen Set
    end
Re: how to use screen "CycleStop" in macro script
« Reply #2 on: October 14, 2019, 12:20:10 PM »
That is great to know.  I definitely plan on looking into how to use that.  I did get around what I originally looked at doing.  Messier, but works.

I added an output to the Cycle stop function in the load screen script.
In my "run control" macro I looked for that output as a second escape from the loop.
this worked.  so now it will exit the macro when Cycle Stop is pressed.

I think your desciption has much better oppurtunity to make it all cleaner, so I will definitely be looking into what you described.
Re: how to use screen "CycleStop" in macro script
« Reply #3 on: October 14, 2019, 03:07:00 PM »
Happy to help:)

Just found a good write up from JoeAverage on this matter.

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

Read his "SignalScript2.pdf" attachment