Hello Guest it is March 28, 2024, 06:12:05 PM

Author Topic: MPG strange behavior  (Read 2837 times)

0 Members and 1 Guest are viewing this topic.

Re: MPG strange behavior
« Reply #10 on: April 14, 2018, 01:45:56 AM »
It always bugged me there wasn't an easy way to relay 1 message in the PLC script.   I came up with this idea about 2 weeks ago.   It works pretty good for what I'm doing.   Hopefully it can help others out as well.  

I was inspired by needing to find a way to run a function in the PLC script once enable was pressed the very first time.   Did you know there is a register that keeps track of how many times you enable/disable Mach?   Pretty cool.  
I used the first instance but it kept looping.   So I changed the register to 2 once I was finished running the function, that solved the looping.
This clicked for messages. Use Registers... VARS#
« Last Edit: April 14, 2018, 01:47:27 AM by Cbyrdtopper »
Chad Byrd
Re: MPG strange behavior
« Reply #11 on: April 14, 2018, 02:13:23 AM »
Check page 1 for more info
Chad Byrd
Re: MPG strange behavior
« Reply #12 on: April 16, 2018, 10:15:54 AM »
Craig,
Here is a test I have set up on my computer at work.
I have a button to toggle Input10 on and off.  I added a message to it.  It runs in the PLC Script and only logs 1 message per button press.

     local inst = mc.mcGetInstance()
     local MessageVar = mc.mcCntlGetPoundVar(inst, 400)
     local Input10 = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT10)
     local hSig = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT10)
     local State = mc.mcSignalGetState(hSig)
     if State == 1 then
         scr.SetProperty("TestTextBox", "Value", "On")
         if MessageVar == 0 then
             mc.mcCntlSetLastError(inst, "Input 10 is on.")
             mc.mcCntlSetPoundVar(inst, 400, 1)
         end
     else
         scr.SetProperty("TestTextBox", "Value", "Off")
         mc.mcCntlSetPoundVar(inst, 400, 0)
     end
« Last Edit: April 16, 2018, 10:17:32 AM by Cbyrdtopper »
Chad Byrd

Offline mark4

*
  •  167 167
    • View Profile
Re: MPG strange behavior
« Reply #13 on: May 08, 2018, 12:13:40 PM »
you guys went way over my head so i programmed it into the screen load and plc script with a few modifications

screen load
--2CNC433--Mach4 4381
pageId = 0
screenId = 0
testcount = 0
machState = 0
machStateOld = -1
machEnabled = 0
machWasEnabled = 0
inst = mc.mcGetInstance()

---------------------------------------------------------------
-- Signal Library
---------------------------------------------------------------
SigLib = {
[mc.OSIG_MACHINE_ENABLED] = function (state)
    machEnabled = state;
    ButtonEnable()
end,

[mc.ISIG_INPUT0] = function (state)
    if (state == 1) then
        wx.wxMessageBox('Way Oiler Empty Add Oil')
    end
end,

[mc.ISIG_INPUT1] = function (state)
    if (state == 1) then   
        CycleStart()
    end
end,
[mc.ISIG_INPUT2] = function (state)
    if (state == 1) then
        mc.mcCntlFeedHold (0)
    end
end,
[mc.ISIG_INPUT3] = function (state)
    if (state == 1) then   
        CycleStop()
    end
end,
[mc.ISIG_INPUT4] = function (state)
    if (state == 1) then   
        mc.mcCntlEStop(0)
        wx.wxMessageBox('Vfd Fault')   
    end
end,
[mc.ISIG_INPUT5] = function (state)
    if (state == 1) then   
       mc.mcCntlEStop(0)
       wx.wxMessageBox('Drive Fault')
    end
end,
[mc.ISIG_INPUT9] = function (state)
    if (state == 1) then   
        AxisSelect()
    end
end,
[mc.ISIG_INPUT10] = function (state)
    if (state == 1) then   
        AxisSelect()
    end
end,
[mc.ISIG_INPUT11] = function (state)
    if (state == 1) then   
        AxisSelect()
    end
end,
[mc.ISIG_INPUT12] = function (state)
    if (state == 1) then   
        AxisSelect()
    end
end,
[mc.ISIG_INPUT13] = function (state)
    if (state == 1) then   
        AxisSelect()
    end
end,
[mc.ISIG_INPUT14] = function (state)
    if (state == 1) then   
        IncSelect()
    end
end,
[mc.ISIG_INPUT15] = function (state)
    if (state == 1) then   
        IncSelect()
    end
end,
[mc.ISIG_INPUT16] = function (state)
    if (state == 1) then   
        IncSelect()
    end
end,


and this is my plc modifications

--2CNC433--Mach4 4381
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
---------------------------------------------------------------
--Axis Selection
---------------------------------------------------------------
function AxisSelect()
    local hsigInput9, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT9)
    local NoneSelected, rc = mc.mcSignalGetState(hsigInput9)
    local hsigInput10, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT10)
    local Xselection, rc = mc.mcSignalGetState(hsigInput10)
    local hsigInput11, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT11)
    local Yselection, rc = mc.mcSignalGetState(hsigInput11)
    local hsigInput12, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT12)
    local Zselection, rc = mc.mcSignalGetState(hsigInput12)
    local hsigInput13, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT13)
    local Aselection, rc = mc.mcSignalGetState(hsigInput13)
    if NoneSelected == 1 then
        mc.mcMpgSetAxis(inst, 0, -1)
        --mc.mcCntlSetLastError(inst, "No Axis Selected")
    elseif Xselection == 1 then
        mc.mcMpgSetAxis(inst, 0, 0)
        --mc.mcCntlSetLastError(inst, "X Selected")
    elseif Yselection == 1 then
        mc.mcMpgSetAxis(inst, 0, 1)
        --mc.mcCntlSetLastError(inst, "Y Selected")
    elseif Zselection == 1 then
        mc.mcMpgSetAxis(inst, 0, 2)
        --mc.mcCntlSetLastError(inst, "Z Selected")
    elseif Aselection == 1 then
        mc.mcMpgSetAxis(inst, 0, 3)
        --mc.mcCntlSetLastError(inst, "A Selected")
    end
end
---------------------------------------------------------------
--Increment Selection
---------------------------------------------------------------
function IncSelect()
    local hsig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT14)
    local Step01, rc = mc.mcSignalGetState(hsig) -- increment .01
    local hsig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT15)
    local Step001, rc = mc.mcSignalGetState(hsig) -- increment .001
    local hsig, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT16)
    local Step0001, rc = mc.mcSignalGetState(hsig) -- increment .0001
    if Step01 == 1 then
        mc.mcMpgSetInc(inst, 0, .010)
        --mc.mcCntlSetLastError(inst, "X1 Selected")
    elseif Step001 == 1 then
        mc.mcMpgSetInc(inst, 0, .001)
        --mc.mcCntlSetLastError(inst, "X10 Selected")
    elseif Step0001 == 1 then
        mc.mcMpgSetInc(inst, 0, .0001)
        --mc.mcCntlSetLastError(inst, "X100 Selected")
    end
end
-------------------------------------------------------
--Run Lube
-------------------------------------------------------
local inst = mc.mcGetInstance()
local hsig,rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
if (machState == 0)then
    mc.mcSignalSetState(hsig,0)
else
    mc.mcSignalSetState(hsig,1)
end

now i get this error
signal script error
lua:error while running chunk
[string ""]:75:attempt to call global 'AxisSelect' (a nil function)
stack traceback
[string ""]:75: in function '?'
[string ""]:706: in function <[string ""] 703>
correct the error and restart

i also started a help ticket but here can help more people
as i said this worked fine i switched and switched and played with it. it was faster and more responsive.
then i boot up today and it gives me this error which i dont have the foggiest idea how to find much less solve.
thank you
mark
Re: MPG strange behavior
« Reply #14 on: May 08, 2018, 01:54:37 PM »
Functions go in the screen load script.  You have your functions in the PLC Script.
Chad Byrd

Offline mark4

*
  •  167 167
    • View Profile
Re: MPG strange behavior
« Reply #15 on: May 08, 2018, 04:20:35 PM »
thank you that was the boost i needed it works fine now.
mark