Hello Guest it is April 18, 2024, 07:51:17 AM

Author Topic: Signal Scripts  (Read 13776 times)

0 Members and 1 Guest are viewing this topic.

Signal Scripts
« on: May 03, 2014, 09:11:43 PM »
Well Now that you guys have been having fun with scripts I wanted to share a fun one with you.

In the PLC script I have this:
Code: [Select]
SigLib = {
    --Enable
    [mc.OSIG_MACHINE_ENABLED] = function (state)
        machEnabled = state;
    end,
    --HeadUp
    [mc.ISIG_INPUT1] = function (on_off)--mc.ISIG_INPUT1
        if( on_off==1 ) then
            -- On
        else
            -- Off
        end
    end,
    --HeadDn
    [mc.ISIG_INPUT2] = function (state)--mc.ISIG_INPUT2
        if( state == 1 ) then
            -- On
        else
            -- Off Call A function here if you wanted
        end
    end,
  [mc.ISIG_INPUT3] = function (state)--mc.ISIG_INPUT3 --Used as an enable button
        if( state == 1 ) then
            local inst= mc.mcGetInstance();
            local reg = mc.mcSignalGetHandle(inst, mc.OSIG_MACHINE_ENABLED)
            state = mc.mcSignalGetState(reg);
            if(state == 1)then
                state = 0
            else
                state = 1
            end
            mc.mcCntlEnable(inst, state);
        end
    end,
    [mc.ISIG_INPUT4] = function (state)--mc.ISIG_INPUT2 button to do feed hold
        if( state == 1 ) then
            local inst= mc.mcGetInstance();
            mc.mcCntlFeedHold(inst)
        end
    end,
    [mc.ISIG_INPUT5] = function (state)--mc.ISIG_INPUT2
        if( state == 1 ) then
            CycleStart() --Run my cycle start function
        end
    end
}

Then in the signal script I do this:
Code: [Select]
if(SigLib[sig] ~= nil)then
SigLib[sig](state);
end

The magic of this is your functions get called when the state changes on the input! So you can make inputs do things on your screen .. for example you could enable the control, Swap pages, do a cycle start, ANYTHING.. I use this in my screen to interface my PLC to the screen so I can have buttons do things on the screen.

Event based programing :) By adding the function calls to a Lua table the speed is very fast to jump into the index in the table.

Hope that helps you all to make your dream screen!

Thanks
Brian
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com
Re: Signal Scripts
« Reply #1 on: May 04, 2014, 08:12:01 AM »
After adding and a restart I receive this Error message.

had to comment this out to get the enable button to work again.

--if(SigLib[sig] ~= nil)then
--SigLib[sig](state);
--end
« Last Edit: May 04, 2014, 08:28:56 AM by Ya-Nvr-No »
Re: Signal Scripts
« Reply #2 on: May 04, 2014, 08:49:44 AM »
if you rem that out you will not call the signal table I made :)
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com
Re: Signal Scripts
« Reply #3 on: May 04, 2014, 08:52:49 AM »
"had to comment this out to get the enable button to work again."

Nothing works if I dont rem them out
Re: Signal Scripts
« Reply #4 on: May 04, 2014, 10:23:10 AM »
Here is my complete Lua script
other than the rem lines, please enlighten me, what causes your Signal Script not to work for me?
when rem's are removed and restarted the error message shows.
The Green Disable button only flashes and does not visually change states when pressed.

This ability to see everything, is priceless, and think their file should be posted when having issues.

So that everyone knows; the "Lua Script" (found under the Operator Menu) has every script for everything in your selected running screen.set
Re: Signal Scripts
« Reply #5 on: May 04, 2014, 12:34:03 PM »
If I just change screen sets to one that does not have your script in it, I still have the same issue until I restart Mach4. Then the Enable/Disable buttons works correct again and the emulation keys work. Makes me think it is tied in to the machine.ini file too.

Offline poppabear

*
  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
Re: Signal Scripts
« Reply #6 on: May 04, 2014, 01:16:53 PM »
That is because in:

if(SigLib[sig] ~= nil)then
SigLib[sig](state);
end

sig (that is in SigLib[sig]), needs to be replaces with the "Signal" your testing against, i.e.

sig = one of the following in this instance:

[mc.OSIG_MACHINE_ENABLED],
[mc.ISIG_INPUT1],
[mc.ISIG_INPUT2],
[mc.ISIG_INPUT3],           
[mc.ISIG_INPUT4],
or
[mc.ISIG_INPUT5]

Tables in lua are Key/value pairs, in this case the "Key" is your sig name, further the "Key" as used here is also a mc var as well so it is pulling double duty.

Scott

fun times
Re: Signal Scripts
« Reply #7 on: May 04, 2014, 01:43:35 PM »

I see the sig and the state it is looking for,
Have keyboard emulation signal Input 5 setup as function key F2
Just cant get by the startup error to try it out for the cycle start button.
when the script is not running I can trigger the output led I created so I know that's working.

Offline poppabear

*
  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
Re: Signal Scripts
« Reply #8 on: May 04, 2014, 01:46:21 PM »
post YOUR code, that you are running in the PLC script
fun times
Re: Signal Scripts
« Reply #9 on: May 04, 2014, 01:57:32 PM »
I posted everything above in the text file

but here is just the PLC script

Code: [Select]
local inst = mc.mcGetInstance();
local rc = 0;
testcount = testcount + 1
machState, rc = mc.mcCntlGetState(inst);
local inCycle = mc.mcCntlIsInCycle(inst);

--mc.mcCntlSetLastError(inst, 'machWasEnabled = ' .. tostring (machWasEnabled) .. 'machEnabled = ' .. tostring(machEnabled));

if ((machWasEnabled == 0) and (machEnabled == 1)) then
    --mc.mcCntlSetLastError(inst, 'Enable!');
    scr.SetProperty('btnEnable', 'Label', 'Disable');
    scr.SetProperty('btnEnable', 'Bg Color', '#FF0000');

    --Enable controls!
elseif ((machWasEnabled == 1) and (machEnabled == 0)) then
    --mc.mcCntlSetLastError(inst, 'Disable!');
    scr.SetProperty('btnEnable', 'Label', 'Enable');
    scr.SetProperty('btnEnable', 'Bg Color', '');

    --Diaable controls!
    scr.SetProperty('CycleStart', 'Enabled', '0');

    enableBlink = 0;
    enableBlinkCount = 0;
end

--Blink the enable button.
if (machEnabled == 0) then
    enableBlinkCount = enableBlinkCount + 1;
    --mc.mcCntlSetLastError(inst, 'enableBlinkCount = ' .. tostring(enableBlinkCount));
    if (enableBlinkCount == 10) then
        enableBlink = not enableBlink;
        --mc.mcCntlSetLastError(inst, 'enableBlink = ' .. tostring(enableBlink));
        if (enableBlink == true) then
            scr.SetProperty('btnEnable', 'Bg Color', '#00FF00');
        elseif(enableBlink == false) then
            scr.SetProperty('btnEnable', 'Bg Color', '');
        end
        enableBlinkCount = 0;
    end
end

if ((machState ~= machStateOld) or (machEnable ~= machWasEnabled)) then
    local enaCycleStart = ((machState == mc.MC_STATE_IDLE) or (machState == mc.MC_STATE_HOLD)
            or (machState == mc.MC_STATE_FRUN_PROBE_FH)
            or (machState == mc.MC_STATE_FRUN_THREAD_FH)
            or (machState == mc.MC_STATE_FRUN_MACROH)
            or (machState == mc.MC_STATE_MRUN_FH)
            or (machState == mc.MC_STATE_FRUN_FH)
            or (machState == mc.MC_STATE_MRUN_PROBE_FH)
            or (machState == mc.MC_STATE_MRUN_THREAD_FH)
            or (machState == mc.MC_STATE_MRUN_MACROH)
            or (machState == mc.MC_STATE_FRUN_SINGLE_BLOCK))
    enaCycleStart = (enaCycleStart and (machEnabled == 1))

    local enaStop = (machState ~= mc.MC_STATE_IDLE) and (machEnabled == 1) and (machState ~= mc.MC_STATE_JOG)
    local enaFeedHold = (machState ~= mc.MC_STATE_IDLE) and (machEnabled == 1) and (machState ~= mc.MC_STATE_JOG)
    local enaReset = (machEnabled == 1) and (machState == mc.MC_STATE_IDLE)
    local enaJog = ((machState == mc.MC_STATE_IDLE) or (machState == mc.MC_STATE_HOLD)
            or (machState == mc.MC_STATE_FRUN_PROBE_FH)
            or (machState == mc.MC_STATE_JOG)
            or (machState == mc.MC_STATE_FRUN_THREAD_FH)
            or (machState == mc.MC_STATE_FRUN_MACROH)
            or (machState == mc.MC_STATE_FRUN_MACROH_JOG)
            or (machState == mc.MC_STATE_MRUN_FH)
            or (machState == mc.MC_STATE_FRUN_FH)
            or (machState == mc.MC_STATE_MRUN_PROBE_FH)
            or (machState == mc.MC_STATE_MRUN_THREAD_FH)
            or (machState == mc.MC_STATE_FRUN_FH_JOG)
            or (machState == mc.MC_STATE_MRUN_MACROH)
            or (machState == mc.MC_STATE_MRUN_MACROH_JOG))
    enaJog = (enaJog and (machEnabled == 1))
   
    if (enaCycleStart) then
        scr.SetProperty('btnCycleStart', 'Enabled', '1');
    else
        scr.SetProperty('btnCycleStart', 'Enabled', '0');
    end

    if (enaStop) then
        scr.SetProperty('btnStop', 'Enabled', '1');
    else
        scr.SetProperty('btnStop', 'Enabled', '0');
    end

    if (enaFeedHold) then
        scr.SetProperty('btnFeedHold', 'Enabled', '1');
    else
        scr.SetProperty('btnFeedHold', 'Enabled', '0');
    end

    if (enaReset) then
        scr.SetProperty('btnReset', 'Enabled', '1');
    else
        scr.SetProperty('btnReset', 'Enabled', '0');
    end

--    if (enaJog) then
--        scr.SetProperty('pnlJog', 'Enabled', '1');
--        JogEnable();
--    else
--        scr.SetProperty('pnlJog', 'Enabled', '0');
--    end
end

if (machEnabled ~= machWasEnabled) then
    scr.SetProperty('btnRefAll', 'Enabled', tostring(machEnabled));
    scr.SetProperty('btnRefAll2', 'Enabled', tostring(machEnabled));
    scr.SetProperty('btnGotoZero', 'Enabled', tostring(machEnabled));
    scr.SetProperty('tabJogging', 'Enabled', tostring(machEnabled));
    if (machEnabled == 1) then
        AxisEnable();
    end
end

SigLib = {
    --Enable
    [mc.OSIG_MACHINE_ENABLED] = function (state)
        machEnabled = state;
    end,
    --HeadUp
    [mc.ISIG_INPUT1] = function (on_off)--mc.ISIG_INPUT1
        if( on_off==1 ) then
            -- On
        else
            -- Off
        end
    end,
    --HeadDn
    [mc.ISIG_INPUT2] = function (state)--mc.ISIG_INPUT2
        if( state == 1 ) then
            -- On
        else
            -- Off Call A function here if you wanted
        end
    end,
  [mc.ISIG_INPUT3] = function (state)--mc.ISIG_INPUT3 --Used as an enable button
        if( state == 1 ) then
            local inst= mc.mcGetInstance();
            local reg = mc.mcSignalGetHandle(inst, mc.OSIG_MACHINE_ENABLED)
            state = mc.mcSignalGetState(reg);
            if(state == 1)then
                state = 0
            else
                state = 1
            end
            mc.mcCntlEnable(inst, state);
        end
    end,
    [mc.ISIG_INPUT4] = function (state)--mc.ISIG_INPUT2 button to do feed hold
        if( state == 1 ) then
            local inst= mc.mcGetInstance();
            mc.mcCntlFeedHold(inst)
        end
    end,
    [mc.ISIG_INPUT5] = function (state)--mc.ISIG_INPUT2
        if( state == 1 ) then
            CycleStart() --Run my cycle start function
        end
    end
}

--local CurrentLine = scr.GetProperty('droCurrent', 'Value', mc.mcCntlGetGcodeLineNbr);
local TotalLines = mc.mcCntlGetGcodeLineCount(inst);
local GcodeCurrentLine = mc.mcCntlGetGcodeLineNbr(inst);
local GcodePercent = (GcodeCurrentLine/TotalLines) * 100;

--scr.SetProperty('droCurrent', 'Value', tostring(CurrentLine));
scr.SetProperty('droTotal', 'Value', tostring(TotalLines+1));
scr.SetProperty('droPercent', 'Value', tostring(GcodePercent));

local Xval = mc.mcAxisGetPos(inst,0);
local Yval = mc.mcAxisGetPos(inst,1);
local Zval = mc.mcAxisGetPos(inst,2);

getAngle(Xval,Yval);
scr.SetProperty('droXY', 'Value', tostring(angle));
getAngle(Xval,Zval);
scr.SetProperty('droXZ', 'Value', tostring(angle));
getAngle(Yval,Zval);
scr.SetProperty('droYZ', 'Value', tostring(angle));

getHypot(Xval,Yval);
scr.SetProperty('droXYhyp', 'Value', tostring(Hyp));
getHypot(Xval,Zval);
scr.SetProperty('droXZhyp', 'Value', tostring(Hyp));
getHypot(Yval,Zval);
scr.SetProperty('droYZhyp', 'Value', tostring(Hyp));

--This is the last thing we do.  So keep it at the end of the script!
machStateOld = machState;
machWasEnabled = machEnabled;