Hello Guest it is March 28, 2024, 02:48:27 PM

Author Topic: Mach4 Signal Script  (Read 4959 times)

0 Members and 1 Guest are viewing this topic.

Mach4 Signal Script
« on: November 16, 2016, 02:44:46 PM »
I'm having issues calling the SigLib in the screen load script and for the life of me I don't know what's going on.  I have searched these forums and gone through the scripting PDF that is installed with the software( which needs to be fixed!! ) but can't find the answer.  Here's what I am wanting to do:
I want to assign a key on the keyboard to perform the same task as the "Button Jog Mode" Button. If I press it, the state will change from "Continuous" to "Incremental" or vice versa.

Here is what I have done so far:

1. Assigned a new input inside of the keyboard plugin and then assigned it to input 63 per the "Quicky 2" video.
2. Verified that the input is working in the diagnostics log.
3. Added the following code to the Screen Load script. It is truncated for simplicity.
Code: [Select]
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
    machEnabled = state;
    ButtonEnable()
end,

[mc.ISIG_INPUT0] = function (state)
   
end,

[mc.ISIG_INPUT63] = function (state)
    if (state == 1) then   
        --CycleStart()
        ButtonJogModeToggle()
    --else
        -- Do something else
        --mc.mcCntlFeedHold (0)
    end

end,
}

4. Added the following code to the Screen Load script.
Code: [Select]
if (SigLib[mc.ISIG_INPUT63] ~= nil) then
    SigLib[mc.ISIG_INPUT63](state);
end

If I put the information into the PLC script like in the video instead of in the screen load and signal scripts, then the state of the button changes continuously while the button is held down.  Please help me as I have searched and cannot find an exact, consice answer.  Thank you for looking.
« Last Edit: November 16, 2016, 02:47:16 PM by sour kraut »
Re: Mach4 Signal Script
« Reply #1 on: November 17, 2016, 10:46:04 AM »


---------------------------------------------------------------
-- Button Jog Mode Toggle() function.
---------------------------------------------------------------
function ButtonJogModeToggle()
    local cont = mc.mcSignalGetHandle(inst, mc.OSIG_JOG_CONT);
    local jogcont = mc.mcSignalGetState(cont)
    local inc = mc.mcSignalGetHandle(inst, mc.OSIG_JOG_INC);
    local joginc = mc.mcSignalGetState(inc)
    local mpg = mc.mcSignalGetHandle(inst, mc.OSIG_JOG_MPG);
    local jogmpg = mc.mcSignalGetState(mpg)
   
    if (jogcont == 1) then
        mc.mcSignalSetState(cont, 0)
        mc.mcSignalSetState(inc, 1)
        mc.mcSignalSetState(mpg, 0)       
    else
        mc.mcSignalSetState(cont, 1)
        mc.mcSignalSetState(inc, 0)
        mc.mcSignalSetState(mpg, 0)
    end

end

In the PLC script you need to leave the default code that looks for changes to any inputs.

Russ

Re: Mach4 Signal Script
« Reply #2 on: November 17, 2016, 11:05:12 AM »
So I need to move the whole function ButtonJogModeToggle() code from the screen load script to the PLC script?  I can do this but I don't understand why the signal script doesn't recognize input 63 and pass the signal to the signal library in the screen load script like every other post says it should?  Luckily I was able to get this very feature to work in the  PMC just not 5 minutes ago but I wanted to do it in LUA to learn the code better.
Re: Mach4 Signal Script
« Reply #3 on: November 17, 2016, 11:23:16 AM »
No the function stays in the signal load script, the default PLC script that came in MACH4 has the code that grabbed all the input signals.

Russ
Re: Mach4 Signal Script
« Reply #4 on: November 17, 2016, 11:59:58 AM »
 I did add the code to reference the input but then it toggled continuously as I held the key down. Besides that, I haven't change the PLC script any. I can't find anything in the PLC script that references any IO signals.  I was under the impression that when the input changed states, this triggered the signal script to run, and then because the input changed states, the signal script called the signal library inside of the screen load script.   I guess I'm just not understanding why I need code in the PLC and what code that is.

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

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

-------------------------------------------------------
--  Cycle time label update
-------------------------------------------------------
--Requires a static text box named "CycleTime" on the screen
if (machEnabled == 1) then
local cycletime = mc.mcCntlGetRunTime(inst, time)
scr.SetProperty("CycleTime", "Label", SecondsToTime(cycletime))
end

-------------------------------------------------------
--  Set Height Offset Led
-------------------------------------------------------
local HOState = mc.mcCntlGetPoundVar(inst, 4008)
if (HOState == 49) then
    scr.SetProperty("ledHOffset", "Value", "0")
else
    scr.SetProperty("ledHOffset", "Value", "1")
end

-------------------------------------------------------
--  Set Spindle Ratio DRO
-------------------------------------------------------
local spinmotormax = scr.GetProperty('droSpinMotorMax', 'Value');   
local rangemax = scr.GetProperty('droRangeMax', 'Value');   
local ratio = (rangemax / spinmotormax);   
scr.SetProperty('droRatio', 'Value', tostring(ratio));

-------------------------------------------------------
--  Set Feedback Ratio DRO Updated 5-30-16
-------------------------------------------------------
local range, rc = mc.mcSpindleGetCurrentRange(inst)
local fbratio, rc = mc.mcSpindleGetFeedbackRatio(inst, range)
scr.SetProperty('droFeedbackRatio', 'Value', tostring(fbratio))

-------------------------------------------------------
--  PLC First Run
-------------------------------------------------------
if (testcount == 1) then --Set Keyboard input startup state
    local iReg = mc.mcIoGetHandle (inst, "Keyboard/Enable")
    mc.mcIoSetState(iReg, 1) --Set register to 1 to ensure KeyboardInputsToggle function will do a disable.
    KeyboardInputsToggle()
   
    --scr.SetProperty('btnCycleStart', 'Label', 'Cycle Start\nGcode')
    prb.LoadSettings()

---------------------------------------------------------------
-- Set Persistent DROs.
---------------------------------------------------------------

    DROTable = {
[1000] = "droJogRate",
[1001] = "droSurfXPos",
[1002] = "droSurfYPos",
[1003] = "droSurfZPos",
    [1004] = "droInCornerX",
    [1005] = "droInCornerY",
    [1006] = "droInCornerSpaceX",
    [1007] = "droInCornerSpaceY",
    [1008] = "droOutCornerX",
    [1009] = "droOutCornerY",
    [1010] = "droOutCornerSpaceX",
    [1011] = "droOutCornerSpaceY",
    [1012] = "droInCenterWidth",
    [1013] = "droOutCenterWidth",
    [1014] = "droOutCenterAppr",
    [1015] = "droOutCenterZ",
    [1016] = "droBoreDiam",
    [1017] = "droBossDiam",
    [1018] = "droBossApproach",
    [1019] = "droBossZ",
    [1020] = "droAngleXpos",
    [1021] = "droAngleYInc",
    [1022] = "droAngleXCenterX",
    [1023] = "droAngleXCenterY",
    [1024] = "droAngleYpos",
    [1025] = "droAngleXInc",
    [1026] = "droAngleYCenterX",
    [1027] = "droAngleYCenterY",
    [1028] = "droCalZ",
    [1029] = "droGageX",
    [1030] = "droGageY",
    [1031] = "droGageZ",
    [1032] = "droGageSafeZ",
    [1033] = "droGageDiameter",
    [1034] = "droEdgeFinder",
    [1035] = "droGageBlock",
    [1036] = "droGageBlockT"
    }

-- ******************************************************************************************* --
--  _   _   _  __          __             _____    _   _   _____   _   _    _____   _   _   _  --
-- | | | | | | \ \        / /     /\     |  __ \  | \ | | |_   _| | \ | |  / ____| | | | | | | --
-- | | | | | |  \ \  /\  / /     /  \    | |__) | |  \| |   | |   |  \| | | |  __  | | | | | | --
-- | | | | | |   \ \/  \/ /     / /\ \   |  _  /  | . ` |   | |   | . ` | | | |_ | | | | | | | --
-- |_| |_| |_|    \  /\  /     / ____ \  | | \ \  | |\  |  _| |_  | |\  | | |__| | |_| |_| |_| --
-- (_) (_) (_)     \/  \/     /_/    \_\ |_|  \_\ |_| \_| |_____| |_| \_|  \_____| (_) (_) (_) --
--                                                                                             --
-- The following is a loop. As a rule of thumb loops should be avoided in the PLC Script.      --
-- However, this loop only runs during the first run of the PLC script so it is acceptable.    --
-- ******************************************************************************************* --                                                         

    for name,number in pairs (DROTable) do -- for each paired name (key) and number (value) in the DRO table
        local droName = (DROTable[name]) -- make the variable named droName equal the name from the table above
        --wx.wxMessageBox (droName)
        local val = mc.mcProfileGetString(inst, "PersistentDROs", (droName), "NotFound") -- Get the Value from the profile ini
        if(val ~= "NotFound")then -- If the value is not equal to NotFound
            scr.SetProperty((droName), "Value", val) -- Set the dros value to the value from the profile ini
        end -- End the If statement
    end -- End the For loop
    ---------------------------------------------------

end
-------------------------------------------------------

--if mc.mcSignalGetState (mc.mcSignalGetHandle (inst, mc.ISIG_INPUT63)) == 1 then
--    -- mcSignalWait(inst, ISIG_INPUT63, WAIT_MODE_Low, 0)
--    mc.mcCntlFeedHold (inst)
--else
--    --  Do something else
--end

--This is the last thing we do.  So keep it at the end of the script!
machStateOld = machState;
machWasEnabled = machEnabled;
Re: Mach4 Signal Script
« Reply #5 on: November 17, 2016, 12:49:15 PM »
My mistake, this is the code I am talking about this should be in the signal script, this ensures if a signal state changes it updates the code in the screen load page.  you don't need to touch the PLC page.


if SigLib[sig] ~= nil then
    SigLib[sig](state);
end
Re: Mach4 Signal Script
« Reply #6 on: November 17, 2016, 01:04:52 PM »
Let me see if I'm understanding you correctly. I do not need to change the [sig] in the code you mention to a specific input?  The code should read exactly as it originally was created?

so YES to this in the Signal script:

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

and NO to this in the Signal script:

if SigLib[mc.ISIG_INPUT63] ~= nil then
    SigLib[mc.ISIG_INPUT63](state);
end
 

I'll give that a try and see if that works.  Thanks.
Re: Mach4 Signal Script
« Reply #7 on: November 17, 2016, 01:58:35 PM »
That worked perfectly!  Thank you very much for your help.  Now on to new frustrations!
Re: Mach4 Signal Script
« Reply #8 on: November 17, 2016, 02:13:54 PM »
Trust me there is a whole bunch more frustration coming your way, as you learn how to navigate Mach4.  I have been working to get all this stuff working for months, and jump for joy if someone responds and helps me along the way, because there is very little documentation available.

Russ