Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: mark4 on April 12, 2018, 12:35:53 AM

Title: MPG strange behavior
Post by: mark4 on April 12, 2018, 12:35:53 AM
hello
i have a strange problem this is my setup. mach4,Ethernet smooth stepper, intel nuc with windows 10, ace 22 plc modbus over usb.
so the only job the plc has is to control two rotary switches one for axis X,Y,Z and A and one for X1,X10,X100 increment.
sometimes the axis will be set to say Y and X will jog. when you see this behavior all you need to do is flip the switch to one of
the other axises and back and it will jog on the proper axis. this usually happens after homing. the problem is i haven't been able to
successfully recreate the problem on demand so i cant tell whats wrong. if i were to guess i think it looses its way and doesn't look at the switch again if that makes sense. i am going to attach my screen load script please look at my switches and see if there is a problem. also i need to have an off position for the axis rotary switch and am not sure how to program that so when all switches are off mpg is off and no jog or the arrow buttons are active either would work for my setup.
thank you
mark
Title: Re: MPG strange behavior
Post by: Cbyrdtopper on April 12, 2018, 08:19:01 AM
Your code, to me, looks fine.  When I set up MPGs I usually put something in the PLC script that way I know it is going to read my switches.  Sometimes,
I've noticed having switches in one position, I need to "update" the signal, like you have had to, so I put the function in the PLC script to always update.
You could do the same here, since you set up two functions to switch the Axis and Increment.  Just take out your messages, otherwise it will put the message into the log thousands and thousands of times, and then put your two functions right into the PLC script. 
No more code than you have, you won't notice any difference in the speed of the PLC run. 

------------------------
--Mark 4-12-18
--MPG Settings
AxisSelect()
IncSelect()
------------------------
Title: Re: MPG strange behavior
Post by: mark4 on April 13, 2018, 11:21:27 PM
thanks for the reply i think i follow you but where would the
------------------------
--Mark 4-12-18
--MPG Settings
AxisSelect()
IncSelect()
------------------------
go. does that make lua look in the plc script for the functions.
mark

Title: Re: MPG strange behavior
Post by: joeaverage on April 13, 2018, 11:44:11 PM
Hi,
I think what Chad means is you put the function calls in the PLC script so they get repeated but the function definitions can stay in the screen load script. All functions in the screen load script are available to be called from
any where in the GUI.

As you have written it the function AxisSelect() gets called only when ISIG_INPUT10 through ISIG_INPUT13 are changed as monitored by the signal library. Chad reccommendes that they be called at every pass
of the PLC script. In fact you could delete ISIG_INPUT10 thru ISIG_INPUT13 from the signal library, the PLC will run dozens of times per second so having the input signals monitored is somewhat redundant.

Chad also mentions taking out the messages so the log does not becomes dominated by endless repetitions of  confirmation of AxisSelect() function calls. I'm just not quite sure how he means to do that...
if your still listening Chad I'd like to know how you do that.

Craig
Title: Re: MPG strange behavior
Post by: Cbyrdtopper on April 14, 2018, 12:44:16 AM
Hey Craig,
What would you like to know? 
Title: Re: MPG strange behavior
Post by: joeaverage on April 14, 2018, 12:47:26 AM
Hi Chad,
just wondering how you do this, I've put functions in the PLC script before and just about made the log unusable as a result.

Quote
Just take out your messages, otherwise it will put the message into the log thousands and thousands of times, and then put your two functions right into the PLC script. 

Craig
Title: Re: MPG strange behavior
Post by: Cbyrdtopper on April 14, 2018, 12:49:50 AM
You mean, how to send out only 1 message in the log?
Title: Re: MPG strange behavior
Post by: joeaverage on April 14, 2018, 01:33:40 AM
Hi,
yeah that would be cool.

Craig
Title: Re: MPG strange behavior
Post by: Cbyrdtopper on April 14, 2018, 01:39:52 AM
All my stuff is at work, so I will have to double check Monday to be sure I have this correct.   But I can give you the overview.
I use #VARS .  Since there are hundreds we can use that aren't being used.   I choose a section of them to be "message vars"

For example.  
MPGX = mc.mcSignalGetHandle(instead, mc.ISIG_INPUT1)
MPGXState = mc.mcSignalGetState(MPGX)

if MPGX == 1 then
    MPG set to x
    if VARS# == 0 then
        Message. -- message first ,then set VARS# to 1
        Set VARS to 1
    end
else
Set VARS# to 0 --resets to 0
end

I'll check my syntax again Monday.  But I think you get the gist.
Let me know.
Title: Re: MPG strange behavior
Post by: Cbyrdtopper on April 14, 2018, 01:40:45 AM
I'm typing on my phone.   Excuse all the syntax errors.
Title: Re: MPG strange behavior
Post by: Cbyrdtopper 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#
Title: Re: MPG strange behavior
Post by: Cbyrdtopper on April 14, 2018, 02:13:23 AM
Check page 1 for more info
Title: Re: MPG strange behavior
Post by: Cbyrdtopper 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
Title: Re: MPG strange behavior
Post by: mark4 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
Title: Re: MPG strange behavior
Post by: Cbyrdtopper on May 08, 2018, 01:54:37 PM
Functions go in the screen load script.  You have your functions in the PLC Script.
Title: Re: MPG strange behavior
Post by: mark4 on May 08, 2018, 04:20:35 PM
thank you that was the boost i needed it works fine now.
mark