Machsupport Forum

Mach Discussion => Mach4 General Discussion => Mach4 Plugins => Topic started by: DazTheGas on August 20, 2016, 11:17:57 AM

Title: mcX360 Plugin for Lua
Post by: DazTheGas on August 20, 2016, 11:17:57 AM
Heres a different kind of plugin for you using an X360 controller that populates registry values so you can write your own scripts for it, if you have the registry plugin loaded you can see the values.

I will try and get some video`s on usage as soon as possible but is straight forward, create a lua panel from the screen editor and make sure you select that its hidden and in the code add as an example

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

local X360_PNL = mcLuaPanelParent;


    function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
    end


X360_timer = wx.wxTimer(X360_PNL)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)

    if GetXin("Btn_A") == "1" then
     -- do something after button A is pressed
    end
   
    if GetXin("Btn_B") == "1" then
     -- do something after button B is pressed
    end
 
    end)

    X360_timer:Start(100)

You should get the gist ;-)

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 26, 2016, 06:58:57 PM
Well... First shaky steps in LUA... but... IT WORKS!!
I have acceleration control on ZAxis for jogging... :D >:D ??? >:D ;D ;D ;D ;D ;D

Do you know a better way to do this:
mc.mcJogSetRate(inst, mc.Z_AXIS, tonumber(string.format("%s", rthy)));
because
mc.mcJogSetRate(inst, mc.Z_AXIS, tonumber(rthy));
does not work for me...
...or should i use another method for :" mc.mcRegGetValueString(hreg)".


Anyway here is my Hidden LUA Panel CODE... And i have only run it on the simulator.....
Needs some error handling etc.... adding X,Y and A axis. That will be a start!!

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

local X360_PNL = mcLuaPanelParent;

    function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
    end

X360_timer = wx.wxTimer(X360_PNL)
    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)

    if GetXin("Btn_A") == "1" then
        mc.mcCntlSetLastError(inst, "X360_BTN_A Pressed")
    end

    if GetXin("Btn_X") == "1" then
        mc.mcCntlSetLastError(inst, "X360_BTN_X Pressed")
    end

    local rthy;
    rthy = GetXin("RTH_Y_Val");

    if rthy > "0" then
        mc.mcCntlSetLastError(inst, string.format("X360_RTH_X_UP/%s", rthy));
        mc.mcJogSetRate(inst, mc.Z_AXIS, tonumber(string.format("%s", rthy)));
        mc.mcJogVelocityStart(inst, mc.Z_AXIS, mc.MC_JOG_POS);

    end  

  
    if rthy < "0" then
        mc.mcCntlSetLastError(inst, string.format("X360_RTH_X_DOWN/%s", rthy));
        mc.mcJogSetRate(inst, mc.Z_AXIS, tonumber(string.format("%s", rthy)));
        mc.mcJogVelocityStart(inst, mc.Z_AXIS, mc.MC_JOG_NEG);

    end    

    if rthy == "0" then
        mc.mcCntlSetLastError(inst, "X360_RTH_X_CENTER");
        mc.mcJogSetRate(inst, mc.Z_AXIS, tonumber(string.format("%s", rthy)));
        mc.mcJogVelocityStop(inst, mc.Z_AXIS);
    end
 
    end)

    X360_timer:Start(100)
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 27, 2016, 03:17:14 PM
1:st Version of my script.
Thanks Joakim (Auth of Playstation Controller plugin for mach 3 for security idea :D )

Changes JogRate on different stick position, and requires both RS and LS to be pressed to activate jogging.

I will make it more efficient (codewise) and reduce number of used lines.
I will also be working on E-Stop and Machine enable and some other stuff maybe :P

Feel Free to try it out!
If you have trouble running it check Axis references. In my old profile i have to reference "Z-Axis" and in my new i have to reference "mc.AXIS0".

Good Luck!

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


local X360_PNL = mcLuaPanelParent;


    function GetX360(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
    end


X360_timer = wx.wxTimer(X360_PNL)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)


    if GetX360("Btn_A") == "1" then
--mc.mcJogSetInc(inst, X_AXIS, .010)
--Run MDI
    mc.mcCntlSetLastError(inst, "X360_BTN_A Pressed")
    --wx.wxMessageBox("Do MDI")
    end

    if GetX360("Btn_X") == "1" then
--mc.mcJogSetInc(inst, X_AXIS, .010)
--Run MDI
    mc.mcCntlSetLastError(inst, "X360_BTN_X Pressed")
    --wx.wxMessageBox("Do MDI")
    end

---- CONFIG
    local DeadZone = 25; --Based on controller output value (-99 => 99)
    local MaxJogSpeed = 25; --Percentage of axix max speed
local RTHY_MapToMashineAxis = mc.AXIS2; -- Z-AXIS
local RTHX_MapToMashineAxis = mc.AXIS3; -- A-AXIS
local LTHY_MapToMashineAxis = mc.AXIS0; -- X-AXIS
local LTHX_MapToMashineAxis = mc.AXIS1; -- Y-AXIS

---- RIGHTHAND THUMB Y-AXIS
    local rthy = GetX360("RTH_Y_Val");
if (tonumber(rthy) >= (DeadZone*-1)) and (tonumber(rthy) <= DeadZone) then
        mc.mcCntlSetLastError(inst, "X360_RTH_Y_CENTER_Begin");
        mc.mcJogSetRate(inst, RTHY_MapToMashineAxis, 0);
        mc.mcJogVelocityStop(inst, RTHY_MapToMashineAxis);
        mc.mcCntlSetLastError(inst, "X360_RTH_Y_CENTER_end");
    else
        -- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
        if (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )then
            if rthy > "0" then
                mc.mcCntlSetLastError(inst, string.format("B_X360_RTH_Y_UP (%s)", (((tonumber(rthy)-DeadZone)/(100-DeadZone))*MaxJogSpeed)));
                mc.mcJogSetRate(inst, RTHY_MapToMashineAxis, (((tonumber(rthy)-DeadZone)/(100-DeadZone))*MaxJogSpeed));
                mc.mcJogVelocityStart(inst, RTHY_MapToMashineAxis, mc.MC_JOG_POS);
                mc.mcCntlSetLastError(inst, string.format("E_X360_RTH_Y_UP (%s)", (((tonumber(rthy)-DeadZone)/(100-DeadZone))*MaxJogSpeed)));
            elseif rthy < "0" then
                mc.mcCntlSetLastError(inst, string.format("B_X360_RTH_Y_DOWN (%s)", (((tonumber(rthy)+DeadZone)/(100-DeadZone))*MaxJogSpeed)));
                mc.mcJogSetRate(inst, RTHY_MapToMashineAxis, ((((tonumber(rthy)+DeadZone)/(100-DeadZone))*MaxJogSpeed)*-1));
                mc.mcJogVelocityStart(inst, RTHY_MapToMashineAxis, mc.MC_JOG_NEG);
                mc.mcCntlSetLastError(inst, string.format("E_X360_RTH_Y_DOWN (%s)", (((tonumber(rthy)+DeadZone)/(100-DeadZone))*MaxJogSpeed)));
            end
        end --SECURITY MEASURE
    end -- RIGHTHAND THUMB Y-AXIS

---- RIGHTHAND THUMB X-AXIS
    local rthx = GetX360("RTH_X_Val");
if (tonumber(rthx) >= (DeadZone*-1)) and (tonumber(rthx) <= DeadZone) then
        --mc.mcCntlSetLastError(inst, "X360_RTH_X_CENTER_Begin");
        mc.mcJogSetRate(inst, RTHX_MapToMashineAxis, 0);
        mc.mcJogVelocityStop(inst, RTHX_MapToMashineAxis);
        --mc.mcCntlSetLastError(inst, "X360_RTH_X_CENTER_end");
    else
        -- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
        if (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )then
            if rthx > "0" then
                --mc.mcCntlSetLastError(inst, string.format("B_X360_RTH_X_UP (%s)", (((tonumber(rthx)-DeadZone)/(100-DeadZone))*MaxJogSpeed)));
                mc.mcJogSetRate(inst, RTHX_MapToMashineAxis, (((tonumber(rthx)-DeadZone)/(100-DeadZone))*MaxJogSpeed));
                mc.mcJogVelocityStart(inst, RTHX_MapToMashineAxis, mc.MC_JOG_POS);
                --mc.mcCntlSetLastError(inst, string.format("E_X360_RTH_X_UP (%s)", (((tonumber(rthx)-DeadZone)/(100-DeadZone))*MaxJogSpeed)));
            elseif rthx < "0" then
                --mc.mcCntlSetLastError(inst, string.format("B_X360_RTH_X_DOWN (%s)", (((tonumber(rthx)+DeadZone)/(100-DeadZone))*MaxJogSpeed)));
                mc.mcJogSetRate(inst, RTHX_MapToMashineAxis, ((((tonumber(rthx)+DeadZone)/(100-DeadZone))*MaxJogSpeed)*-1));
                mc.mcJogVelocityStart(inst, RTHX_MapToMashineAxis, mc.MC_JOG_NEG);
                --mc.mcCntlSetLastError(inst, string.format("E_X360_RTH_X_DOWN (%s)", (((tonumber(rthx)+DeadZone)/(100-DeadZone))*MaxJogSpeed)));
            end
        end --SECURITY MEASURE
    end -- RIGHTHAND THUMB X-AXIS

---- LEFTHAND THUMB Y-AXIS
    local lthy = GetX360("LTH_Y_Val");
if (tonumber(lthy) >= (DeadZone*-1)) and (tonumber(lthy) <= DeadZone) then
        --mc.mcCntlSetLastError(inst, "X360_LTH_Y_CENTER_Begin");
        mc.mcJogSetRate(inst, LTHY_MapToMashineAxis, 0);
        mc.mcJogVelocityStop(inst, LTHY_MapToMashineAxis);
        --mc.mcCntlSetLastError(inst, "X360_LTH_Y_CENTER_end");
    else
        -- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
        if (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )then
            if lthy > "0" then
                --mc.mcCntlSetLastError(inst, string.format("B_X360_LTH_Y_UP (%s)", (((tonumber(rthx)-DeadZone)/(100-DeadZone))*MaxJogSpeed)));
                mc.mcJogSetRate(inst, LTHY_MapToMashineAxis, (((tonumber(lthy)-DeadZone)/(100-DeadZone))*MaxJogSpeed));
                mc.mcJogVelocityStart(inst, LTHY_MapToMashineAxis, mc.MC_JOG_POS);
                --mc.mcCntlSetLastError(inst, string.format("E_X360_LTH_Y_UP (%s)", (((tonumber(rthx)-DeadZone)/(100-DeadZone))*MaxJogSpeed)));
            elseif lthy < "0" then
                --mc.mcCntlSetLastError(inst, string.format("B_X360_LTH_Y_DOWN (%s)", (((tonumber(rthx)+DeadZone)/(100-DeadZone))*MaxJogSpeed)));
                mc.mcJogSetRate(inst, LTHY_MapToMashineAxis, ((((tonumber(lthy)+DeadZone)/(100-DeadZone))*MaxJogSpeed)*-1));
                mc.mcJogVelocityStart(inst, LTHY_MapToMashineAxis, mc.MC_JOG_NEG);
                --mc.mcCntlSetLastError(inst, string.format("E_X360_LTH_Y_DOWN (%s)", (((tonumber(rthx)+DeadZone)/(100-DeadZone))*MaxJogSpeed)));
            end
        end --SECURITY MEASURE
    end -- LEFTHAND THUMB Y-AXIS

---- LEFTHAND THUMB X-AXIS
    local lthx = GetX360("LTH_X_Val");
if (tonumber(lthx) >= (DeadZone*-1)) and (tonumber(lthx) <= DeadZone) then
        --mc.mcCntlSetLastError(inst, "X360_LTH_X_CENTER_Begin");
        mc.mcJogSetRate(inst, LTHX_MapToMashineAxis, 0);
        mc.mcJogVelocityStop(inst, LTHX_MapToMashineAxis);
        --mc.mcCntlSetLastError(inst, "X360_LTH_X_CENTER_end");
    else
        -- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
        if (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )then
            if lthx > "0" then
                --mc.mcCntlSetLastError(inst, string.format("B_X360_LTH_X_UP (%s)", (((tonumber(rthx)-DeadZone)/(100-DeadZone))*MaxJogSpeed)));
                mc.mcJogSetRate(inst, LTHX_MapToMashineAxis, (((tonumber(lthx)-DeadZone)/(100-DeadZone))*MaxJogSpeed));
                mc.mcJogVelocityStart(inst, LTHX_MapToMashineAxis, mc.MC_JOG_POS);
                --mc.mcCntlSetLastError(inst, string.format("E_X360_LTH_X_UP (%s)", (((tonumber(rthx)-DeadZone)/(100-DeadZone))*MaxJogSpeed)));
            elseif lthx < "0" then
                --mc.mcCntlSetLastError(inst, string.format("B_X360_LTH_X_DOWN (%s)", (((tonumber(rthx)+DeadZone)/(100-DeadZone))*MaxJogSpeed)));
                mc.mcJogSetRate(inst, LTHX_MapToMashineAxis, ((((tonumber(lthx)+DeadZone)/(100-DeadZone))*MaxJogSpeed)*-1));
                mc.mcJogVelocityStart(inst, LTHX_MapToMashineAxis, mc.MC_JOG_NEG);
                --mc.mcCntlSetLastError(inst, string.format("E_X360_LTH_X_DOWN (%s)", (((tonumber(rthx)+DeadZone)/(100-DeadZone))*MaxJogSpeed)));
            end
        end --SECURITY MEASURE
    end -- LEFTHAND THUMB X-AXIS

    
    end)

    X360_timer:Start(100)
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on November 27, 2016, 04:46:11 PM
Here`s a little snippet of what I use for buttons as remember if you turn on a button you also need to turn it off, that might help you on the way ;-)

Code: [Select]
local XPState = false
local XNState = false
local YPState = false
local YNState = false

    function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
    end


    X360_Panel:Connect(wx.wxEVT_TIMER, function (event)

-- Y++
if GetXin("DPad_UP") == "1" and YPState == false then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGYP), 1)
YPState = true
end
if GetXin("DPad_UP") == "0" and YPState == true then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGYP), 0)
YPState = false
end

-- Y--
if GetXin("DPad_DOWN") == "1" and YNState == false then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGYN), 1)
YNState = true
end
if GetXin("DPad_DOWN") == "0" and YNState == true then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGYN), 0)
YNState = false
end

-- X++
if GetXin("DPad_RIGHT") == "1" and XPState == false then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGXP), 1)
XPState = true
end
if GetXin("DPad_RIGHT") == "0" and XPState == true then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGXP), 0)
XPState = false
end

-- X--
if GetXin("DPad_LEFT") == "1" and XNState == false then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGXN), 1)
XNState = true
end
if GetXin("DPad_LEFT") == "0" and XNState == true then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGXN), 0)
XNState = false
end

end)

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 27, 2016, 05:54:48 PM
I can see that it would be good to avoid hammering the mc.mcSignal...()
Any other point?
Should i use mc.mcSignalSetState instead of mc.mcJogVelocityStart(inst, RTHY_MapToMashineAxis, mc.MC_JOG_POS); ????

Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on November 27, 2016, 06:26:19 PM
By using the mcSignalSetState on the axis it gives me the advantage of using the same routine whether I am in cont or inc mode, by using mcJogVelocityStart you will have to write another routine for mcJogIncStart.

DazTHeGas
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 27, 2016, 09:28:06 PM
GR8! Then it is the way i want it to be for now.
Adding Inc mode will be done later.......maybe.... :P
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 27, 2016, 09:29:58 PM
Some more progress...  ;D

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

---- CONFIG
    local DeadZone = 25 --Based on controller output value (-99 => 99)
    local MaxJogSpeed = 25 --Percentage of axix max speed

    local RTHY_MapToMashineAxis = mc.AXIS2 -- Z-AXIS
    local RTHX_MapToMashineAxis = mc.AXIS3 -- A-AXIS
    local LTHY_MapToMashineAxis = mc.AXIS1 -- Y-AXIS
    local LTHX_MapToMashineAxis = mc.AXIS0 -- X-AXIS
   
    local isPressed_BTN = {false,false,false,false} -- Buttons A,B,X,Y

-- FUNCTIONS
    function GetX360(xinput)
        local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
        return mc.mcRegGetValueString(hreg)
    end

    function EnableMashine(finst,pressed)
        if pressed then
            --I dont want to clear E-Stop on BTN_E-Stop_UP so instead i use X360´s MashineEnable for it...
            mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0);
            mc.mcCntlEnable(finst, true)
        else
            mc.mcCntlSetLastError(finst, "X360_Enabled_Machine")
        end
        return
    end
    function CycleStart(finst,pressed)
        if pressed then
            mc.mcCntlCycleStart(finst)
        else
        end
        return
    end
    function EStop(finst,pressed)
        if pressed then
            mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 1);
        else
            -- Moved to EnableMashine
            --mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0);
        end
        return
    end
   
    function HandleButtonPress(finst,btn,btnNo,func,msg)
        if btn == "1" and isPressed_BTN[btnNo] == false then
            func(finst,true)
            mc.mcCntlSetLastError(finst, msg)
            isPressed_BTN[btnNo] = true
        end
        if btn == "0" and isPressed_BTN[btnNo] == true then
            func(finst,false)
            isPressed_BTN[btnNo] = false
        end
        return
    end

    function HandleThumbInput(finst,th,axis, isSecured,DZ, MaxJS)
        if (tonumber(th) >= (DZ*-1)) and (tonumber(th) <= DZ) then
            mc.mcJogSetRate(finst, axis, 0)
            mc.mcJogVelocityStop(finst, axis);
        else
            if isSecured then
                if tonumber(th) > tonumber(DZ) then
                    mc.mcJogSetRate(finst, axis, (((tonumber(th)-DZ)/(100-DZ))*MaxJS))
                    mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_POS)
                elseif tonumber(th) < (tonumber(DZ)*-1) then
                    mc.mcJogSetRate(finst, axis, ((((tonumber(th)+DZ)/(100-DZ))*MaxJS)*-1))
                    mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_NEG)
                end
            end
        end
        return
    end

X360_timer = wx.wxTimer(X360_PNL)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)

-- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
    local isSecured = (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )

-- HANDLE E-STOP ON BTN_B
    HandleButtonPress(inst,GetX360("Btn_B"),2,EStop,"X360_E-STOP")
-- HANDLE ALL OTHER BUTTONS
    if isSecured then
        -- MASHINE ENABLE ON BTN_X
        HandleButtonPress(inst,GetX360("Btn_X"),3,EnableMashine,"X360_Enabled_Machine")
        -- CYCLE START ON BTN_A
        HandleButtonPress(inst,GetX360("Btn_A"),1,CycleStart,"X360_CycleStart")
    end

-- HANDLE THUMBSTICKS
    HandleThumbInput(inst,GetX360("RTH_Y_Val"),RTHY_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("RTH_X_Val"),RTHX_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("LTH_Y_Val"),LTHY_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("LTH_X_Val"),LTHX_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
   
    end)

    X360_timer:Start(100)
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 27, 2016, 09:47:18 PM
I am having one issue... Pressing the X-Button will only do machine enable the second time i press it..??? Do you know how this can be?
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 28, 2016, 04:16:53 AM
Ok... I added a sleep and it does the trick...
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 28, 2016, 09:01:54 AM
When running the Log Facillity i noticed the script hammers Velocity Stop so i fixed that...

This will likely be "it" for a while. I have some Machine issues to fix... :D

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

---- CONFIG
    local DeadZone = 25 --Based on controller output value (-99 => 99)
    local MaxJogSpeed = 25 --Percentage of axix max speed

    local RTHY_MapToMashineAxis = mc.AXIS2 -- Z-AXIS
    local RTHX_MapToMashineAxis = mc.AXIS3 -- A-AXIS
    local LTHY_MapToMashineAxis = mc.AXIS1 -- Y-AXIS
    local LTHX_MapToMashineAxis = mc.AXIS0 -- X-AXIS
    
    local isPressed_BTN = {false,false,false,false} -- Buttons A,B,X,Y

-- FUNCTIONS
    function GetX360(xinput)
        local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
        return mc.mcRegGetValueString(hreg)
    end

    function EnableMashine(finst,pressed)
        if pressed then
            --I dont want to clear E-Stop on BTN_E-Stop_UP so instead i use X360´s MashineEnable for it...
            mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0);
            wx.wxMilliSleep(100)
            mc.mcCntlEnable(finst, true)
        else
            --mc.mcCntlSetLastError(finst, "X360_Enabled_Machine")
        end
        return
    end
    function CycleStart(finst,pressed)
        if pressed then
            mc.mcCntlCycleStart(finst)
        else
        end
        return
    end
    function EStop(finst,pressed)
        if pressed then
            mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 1);
        else
            -- Moved to EnableMashine
            --mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0);
        end
        return
    end
    
    function HandleButtonPress(finst,btn,btnNo,func,msg)
        if btn == "1" and isPressed_BTN[btnNo] == false then
            func(finst,true)
            mc.mcCntlSetLastError(finst, msg)
            isPressed_BTN[btnNo] = true
        end
        if btn == "0" and isPressed_BTN[btnNo] == true then
            func(finst,false)
            isPressed_BTN[btnNo] = false
        end
        return
    end

    function HandleThumbInput(finst,th,axis, isSecured,DZ, MaxJS)
        if (tonumber(th) >= (DZ*-1)) and (tonumber(th) <= DZ) then
            mc.mcCntlSetLastError(finst, "JogVelocity: "..mc.mcJogGetVelocity(finst, axis))
            if mc.mcJogGetVelocity(finst, axis) == 0 then
            else
                mc.mcJogSetRate(finst, axis, 0)
                mc.mcJogVelocityStop(finst, axis);
            end
        else
            if isSecured then
                if tonumber(th) > tonumber(DZ) then
                    mc.mcJogSetRate(finst, axis, (((tonumber(th)-DZ)/(100-DZ))*MaxJS))
                    mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_POS)
                elseif tonumber(th) < (tonumber(DZ)*-1) then
                    mc.mcJogSetRate(finst, axis, ((((tonumber(th)+DZ)/(100-DZ))*MaxJS)*-1))
                    mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_NEG)
                end
            end
        end
        return
    end

X360_timer = wx.wxTimer(X360_PNL)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)

-- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
    local isSecured = (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )

-- HANDLE E-STOP ON BTN_B
    HandleButtonPress(inst,GetX360("Btn_B"),2,EStop,"X360_E-STOP")
-- HANDLE ALL OTHER BUTTONS
    if isSecured then
        -- MASHINE ENABLE ON BTN_X
        HandleButtonPress(inst,GetX360("Btn_X"),3,EnableMashine,"X360_Enabled_Machine")
        -- CYCLE START ON BTN_A
        HandleButtonPress(inst,GetX360("Btn_A"),1,CycleStart,"X360_CycleStart")
    end

-- HANDLE THUMBSTICKS
    HandleThumbInput(inst,GetX360("RTH_Y_Val"),RTHY_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("RTH_X_Val"),RTHX_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("LTH_Y_Val"),LTHY_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("LTH_X_Val"),LTHX_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    
    end)

    X360_timer:Start(100)
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on November 29, 2016, 03:06:37 AM
Quote
When running the Log Facillity i noticed the script hammers Velocity Stop so i fixed that...

It takes a lot more code but makes a lot of sense to reframe from too many "else" statements, i tend to use "elseif" and give it something to check against, another thing I need to mention is it is an error to quit without stopping the timer, in your screen unload script you need to be placing a timer:Stop() command for the panel

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 29, 2016, 09:11:50 AM
Thanks for that!!

I will make a comment about it in the script and fix it for my panel.

Regarding the "else":s i had it using elseif:s first but there was an error i was hunting down so i removed all elseif:s - I never got arount to fix that.

Maybe if i get some time tonight...
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on November 29, 2016, 02:23:47 PM
Here is the updated script...

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

---- CONFIG

        -- ======================================================================
        -- = Dont forget to add X360_timer:Stop() to your screen unload script  =
        -- ======================================================================

    local DeadZone = 25 --Based on controller output value (-99 => 99)
    local MaxJogSpeed = 25 --Percentage of axix max speed

    local RTHY_MapToMashineAxis = mc.AXIS2 -- Z-AXIS
    local RTHX_MapToMashineAxis = mc.AXIS3 -- A-AXIS
    local LTHY_MapToMashineAxis = mc.AXIS1 -- Y-AXIS
    local LTHX_MapToMashineAxis = mc.AXIS0 -- X-AXIS
   
    local isPressed_BTN = {false,false,false,false} -- Buttons A,B,X,Y

-- FUNCTIONS
    function GetX360(xinput)
        local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
        return mc.mcRegGetValueString(hreg)
    end

    function EnableMashine(finst,pressed)
        if pressed then
            mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0) --I dont want to clear E-Stop on BTN_E-Stop_UP so instead i use X360´s MashineEnable for it... (see E-Stop function)
            wx.wxMilliSleep(100) -- You cannot enable unless E-Stop is cleared so we must wait for mcSignalSetState to complete
            mc.mcCntlEnable(finst, true)
        end
        return
    end
    function CycleStart(finst,pressed)
        if pressed then
            mc.mcCntlCycleStart(finst)
        end
        return
    end
    function EStop(finst,pressed)
        if pressed then
            mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 1)
        --else
            -- Uncomment this if you want E-Stop cleared on button release
            --mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0)
        end
        return
    end
   
    function HandleButtonPress(finst,btn,btnNo,func,msg)
        if btn == "1" and isPressed_BTN[btnNo] == false then
            func(finst,true)
            mc.mcCntlSetLastError(finst, msg)
            isPressed_BTN[btnNo] = true
        elseif btn == "0" and isPressed_BTN[btnNo] == true then
            func(finst,false)
            isPressed_BTN[btnNo] = false
        end
        return
    end

    function HandleThumbInput(finst,th,axis, isSecured,DZ, MaxJS)
        if (tonumber(th) >= (DZ*-1)) and (tonumber(th) <= DZ) then
            if mc.mcJogGetVelocity(finst, axis) ~= 0 then
                mc.mcJogSetRate(finst, axis, 0)
                mc.mcJogVelocityStop(finst, axis);
            end
        elseif isSecured then
            if tonumber(th) > tonumber(DZ) then
                mc.mcJogSetRate(finst, axis, (((tonumber(th)-DZ)/(100-DZ))*MaxJS))
                mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_POS)
            elseif tonumber(th) < (tonumber(DZ)*-1) then
                mc.mcJogSetRate(finst, axis, ((((tonumber(th)+DZ)/(100-DZ))*MaxJS)*-1))
                mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_NEG)
            end
        end
        return
    end

X360_timer = wx.wxTimer(X360_PNL)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)

-- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
    local isSecured = (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )

-- HANDLE E-STOP ON BTN_B
    HandleButtonPress(inst,GetX360("Btn_B"),2,EStop,"X360_E-STOP")
-- HANDLE ALL OTHER BUTTONS (Only X and A are configured)
    if isSecured then
        -- MASHINE ENABLE ON BTN_X
        HandleButtonPress(inst,GetX360("Btn_X"),3,EnableMashine,"X360_Enabled_Machine")
        -- CYCLE START ON BTN_A
        HandleButtonPress(inst,GetX360("Btn_A"),1,CycleStart,"X360_CycleStart")
    end

-- HANDLE THUMBSTICKS
    HandleThumbInput(inst,GetX360("RTH_Y_Val"),RTHY_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("RTH_X_Val"),RTHX_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("LTH_Y_Val"),LTHY_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("LTH_X_Val"),LTHX_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
   
    end)

    X360_timer:Start(100)
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on December 04, 2016, 04:22:44 PM
Updated the script with some nice expo to the thumb input. Now you can almost singlestep the steppermotors while having the same max speed  ;D ;D ;D

Over all I am getting happy with the script. That said i dont like the large Deadzone. I think DazTheGas could fix that in the plugin but on the other hand i think the current values are resonable given the large impresicion of the controller so for the X360 it is the best you can get... If the plugin would support multiple controllers i think this should be configurable but for the X360 it would be just plain dangerous.

Here is the updated script with some expo :P
Code: [Select]
local inst = mc.mcGetInstance()
local X360_PNL = mcLuaPanelParent

---- CONFIG

        -- ======================================================================
        -- = Dont forget to add X360_timer:Stop() to your screen unload script  =
        -- ======================================================================

    local DeadZone = 25 --Based on controller output value (-99 => 99)
    local MaxJogSpeed = 50 --Percentage of axix max speed

    local RTHY_MapToMashineAxis = mc.AXIS2 -- Z-AXIS
    local RTHX_MapToMashineAxis = mc.AXIS3 -- A-AXIS
    local LTHY_MapToMashineAxis = mc.AXIS1 -- Y-AXIS
    local LTHX_MapToMashineAxis = mc.AXIS0 -- X-AXIS
   
    local isPressed_BTN = {false,false,false,false} -- Buttons A,B,X,Y

-- FUNCTIONS
    function GetX360(xinput)
        local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
        return mc.mcRegGetValueString(hreg)
    end

    function EnableMashine(finst,pressed)
        if pressed then
            mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0) --I dont want to clear E-Stop on BTN_E-Stop_UP so instead i use X360´s MashineEnable for it... (see E-Stop function)
            wx.wxMilliSleep(100) -- You cannot enable unless E-Stop is cleared so we must wait for mcSignalSetState to complete
            mc.mcCntlEnable(finst, true)
        end
        return
    end
    function CycleStart(finst,pressed)
        if pressed then
            mc.mcCntlCycleStart(finst)
        end
        return
    end
    function EStop(finst,pressed)
        if pressed then
            mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 1)
        --else
            -- Uncomment this if you want E-Stop cleared on button release
            --mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0)
        end
        return
    end
   
    function HandleButtonPress(finst,btn,btnNo,func,msg)
        if btn == "1" and isPressed_BTN[btnNo] == false then
            func(finst,true)
            mc.mcCntlSetLastError(finst, msg)
            isPressed_BTN[btnNo] = true
        elseif btn == "0" and isPressed_BTN[btnNo] == true then
            func(finst,false)
            isPressed_BTN[btnNo] = false
        end
        return
    end

    function HandleThumbInput(finst,th,axis, isSecured,DZ, MaxJS)
        if (tonumber(th) >= (DZ*-1)) and (tonumber(th) <= DZ) then
            if mc.mcJogGetVelocity(finst, axis) ~= 0 then
                mc.mcJogSetRate(finst, axis, 0)
                mc.mcJogVelocityStop(finst, axis);
            end
        elseif isSecured then
            if tonumber(th) > tonumber(DZ) then
                mc.mcJogSetRate(finst, axis, ((((tonumber(th)-DZ)*(tonumber(th)-DZ))/((100-DZ)*(100-DZ)))*MaxJS))
                mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_POS)
            elseif tonumber(th) < (tonumber(DZ)*-1) then
                mc.mcJogSetRate(finst, axis, ((((tonumber(th)+DZ)*(tonumber(th)+DZ))/((100-DZ)*(100-DZ)))*MaxJS))
                mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_NEG)
            end
        end
        return
    end

X360_timer = wx.wxTimer(X360_PNL)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)

-- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
    local isSecured = (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )

-- HANDLE E-STOP ON BTN_B
    HandleButtonPress(inst,GetX360("Btn_B"),2,EStop,"X360_E-STOP")
-- HANDLE ALL OTHER BUTTONS (Only X and A are configured)
    if isSecured then
        -- MASHINE ENABLE ON BTN_X
        HandleButtonPress(inst,GetX360("Btn_X"),3,EnableMashine,"X360_Enabled_Machine")
        -- CYCLE START ON BTN_A
        HandleButtonPress(inst,GetX360("Btn_A"),1,CycleStart,"X360_CycleStart")
    end

-- HANDLE THUMBSTICKS
    HandleThumbInput(inst,GetX360("RTH_Y_Val"),RTHY_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("RTH_X_Val"),RTHX_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("LTH_Y_Val"),LTHY_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
    HandleThumbInput(inst,GetX360("LTH_X_Val"),LTHX_MapToMashineAxis,isSecured, DeadZone, MaxJogSpeed)
   
    end)

    X360_timer:Start(100)
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on December 04, 2016, 06:04:53 PM
The return code from the controller (32670) is divided by 330 to give 99, I based this on velocity etc being set by percentage which I believe is the best approach, totally agree with NOT having 2 controllers ;-)

So just a few pointers to help you on your quest, I have downloaded and tested your script but unfortunately it kills any other form of jogging in mach4 - IE jog buttons and even the shuttle. You could do with setting some more  'if' statements.

You dont have to run everything in 1 timer, windows is an event driven os so use what is required.

There are many ways of retrieving a registry try

return mc.mcRegGetValueLong(hreg) instead of converting strings with tostring

Heres a few snippets from my script.


Code: [Select]
local mInst = 0

-- Enable
Xenabled = 0
JogRate = 0
Enable_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
Enable_Timer = wx.wxTimer(Enable_Panel)
-- Velocity
Vel_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
Vel_Timer = wx.wxTimer(Vel_Panel)
-- DPad
DPad_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
DPad_Timer = wx.wxTimer(DPad_Panel)

As you can see its possible to run many timers, these are a few and the only timer that is running const is the enable timer which is looking/waiting for the left trigger, on the left trigger value being over 150 it starts all the other timers and on release stops them.

Code: [Select]
Enable_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("LTR_Val") > 150 and Xenabled == 0 then
JogRate = mc.mcJogGetRate(mInst, mc.X_AXIS)
EStop_Timer:Start(50)
DPad_Timer:Start(100)
Vel_Timer:Start(100)
LTH_Timer:Start(100)
RTH_Timer:Start(100)
BTN_Timer:Start(100)
Xenabled = 1
elseif GetXin("LTR_Val") < 50 and Xenabled == 1 then
DPad_Timer:Stop()
Vel_Timer:Stop()
EStop_Timer:Stop()
LTH_Timer:Stop
RTH_Timer:Stop()
BTN_Timer:Stop()
Xenabled = 0
mc.mcJogSetRate(mInst, mc.X_AXIS, JogRate)
mc.mcJogSetRate(mInst, mc.Y_AXIS, JogRate)
mc.mcJogSetRate(mInst, mc.Z_AXIS, JogRate)
end
end)
   
As you can see after releasing the left trigger my jograte is returned to what it was before and throughout the script is lots and lots of if and more if`s with the main goal of keeping overheads towards the gui at a minimum when the controller is not in use.

Must be time for a choccy biccy now ;-)

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on December 07, 2016, 05:41:10 AM
Thanks! Im finding this beeing a great way to learn LUA Script and your input is valuable and step by step inspiring me to improve my code and skills. Thanks!

I dont know why i did not think of timers as event timers... in my mind X360_Timer was just a mystical "dont bother now" thingy which i totally overlooked. But i have been thinking of a way to lighten the load the script creates. For me it is not really nessesary since i only use X360 to navigate my machine but for sake of order it should be done.

How is all other jogging killed? Is it me setting zero velocity for safety reasons on all axes (First if statment in HandleThumbInput) when not enabled? That can definitely be improved. ......Just thought about that code for a second and looks like I've been tired when writing that - it´s just wrong... :P
I will get back to my script this weekend (or maybe earlier.. ;P)

LunchTime :P

Daniel

What are your thoughts on the following:
having probing on the D-Pad (BTN_Back for Z and A)
Spindle speed and feedrate on Y+DPad
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on December 10, 2016, 11:33:25 AM
Ok so part 1 of the usage is now uploaded and can be found here http://www.machsupport.com/forum/index.php/topic,33792.0.html

Attached is the latest version of driver.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on December 12, 2016, 05:59:23 PM
What changed in the plugin? Release notes? :P

Im just finishing up my script, now everything is done except probing (waiting for andy @ warp9)
I have spindle speed on DPad now im setting it using the screen...  Is that the right way or is there another way to do it?
My code is cut and paste from the screen SRO "+" button:

Code: [Select]
function SpindleSpeedInc(finst, pressed)
    if pressed then
        local val = scr.GetProperty('slideSRO', 'Value');
        val = tonumber(val) + 5;
        local maxval = scr.GetProperty('slideSRO', 'Max Value')
        if (tonumber(val) >= tonumber(maxval)) then
         val = maxval;
        end
        scr.SetProperty('slideSRO', 'Value', tostring(val));
    end
end
Title: Re: mcX360 Plugin for Lua
Post by: francocncprojects on December 18, 2016, 11:28:23 PM
I need help on a lathe project.  ???  Please see the code below.  After the E-Stop button, I'm trying to toggle between ABS and INC jogging.  Then, I'm trying to change the jog increment.  I'm totally stuck and would appreciate any help I can get.

On the bright side,  I did figure out how to use JogInc=mc.mcJogGetInc(mInst,0) in the dpad buttons. :-\

Code: [Select]
----------------------------------------------------------------------------
--  X_360 Lua by DazTheGas - Last Updated 07/12/16 SDK 3233
----------------------------------------------------------------------------

-----------------Declares

local mInst = mc.mcGetInstance()
local Xenabled = false
local ESState = false
local JogInc = .001
local AIState = false
local CJIState = false
local XPState = false
local XNState = false
local YPState = false
local YNState = false
local ZPState = false
local ZNState = false

-- X360 Init Panel
X360_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
X360_Timer = wx.wxTimer(X360_Panel)
X360_Timer:Start(100)
-- EStop
EStop_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
EStop_Timer = wx.wxTimer(EStop_Panel)
-- DPad
DPad_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
DPad_Timer = wx.wxTimer(DPad_Panel)

-----------------Functions

function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueLong(hreg)
end

-----------------Event Timers

X360_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("LTR_Val") > 150 and Xenabled == false then
        EStop_Timer:Start(50)
        DPad_Timer:Start(100)
Xenabled = true
        mc.mcCntlSetLastError(mInst,"X_360 Running")
elseif GetXin("LTR_Val") < 50 and Xenabled == true then
        EStop_Timer:Stop()
        DPad_Timer:Stop()
Xenabled = false
        mc.mcCntlSetLastError(mInst,"X_360 Stopped")
end
end)

EStop_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("Btn_B") == 1 and ESState == false then
local EStop = mc.mcSignalGetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY))
if (EStop == 1) then
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 0)
else
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 1)
end
ESState = true
end
if GetXin("Btn_B") == 0 and ESState == true then
ESState = false
end
end)

DPad_Panel:Connect(wx.wxEVT_TIMER, function (event)
-- ABS/INC
if GetXin("Btn_X") == 1 and AIState == false then
--How do I active the btnToggleJogMode ?
        mc.mcCntlSetLastError(mInst,"B_Button_Pressed")
        ButtonJogModeToggle()
        AIState = true
end
if GetXin("Btn_X") == 0 and AIState == true then
AIState = false
end
-- Cycle_Jog_Inc
if GetXin("Btn_START") == 1 and CJIState == false then
--How do I active the btnCycleJogInc ?
        mc.mcCntlSetLastError(mInst,"START_Button_Pressed")
        ButtonJogModeToggle()
        CJIState = true
end
if GetXin("Btn_START") == 0 and CJIState == true then
CJIState = false
end
-- -- Y++
-- if GetXin("DPad_UP") == 1 and GetXin("Btn_LS") == 0 and YPState == false then
-- mc.mcJogIncStart(mInst, mc.Y_AXIS, JogInc)
-- YPState = true
-- end
-- if GetXin("DPad_UP") == 0 and GetXin("Btn_LS") == 0 and YPState == true then
-- mc.mcJogIncStop(mInst, mc.Y_AXIS,.1)
-- YPState = false
-- end
-- -- Y--
-- if GetXin("DPad_DOWN") == 1 and GetXin("Btn_LS") == 0 and YNState == false then
-- mc.mcJogIncStart(mInst, mc.Y_AXIS, -1 * JogInc)
-- YNState = true
-- end
-- if GetXin("DPad_DOWN") == 0 and GetXin("Btn_LS") == 0 and YNState == true then
-- mc.mcJogIncStop(mInst, mc.Y_AXIS,.1)
-- YNState = false
-- end
-- X++
if GetXin("DPad_DOWN") == 1 and XPState == false then
JogInc=mc.mcJogGetInc(mInst,0)
        mc.mcJogIncStart(mInst, mc.X_AXIS, JogInc)
XPState = true
end
if GetXin("DPad_DOWN") == 0 and XPState == true then
mc.mcJogIncStop(mInst, mc.X_AXIS,.0001)
XPState = false
end
-- X--
if GetXin("DPad_UP") == 1 and XNState == false then
JogInc=mc.mcJogGetInc(mInst,0)
        mc.mcJogIncStart(mInst, mc.X_AXIS, -1 * JogInc)
XNState = true
end
if GetXin("DPad_UP") == 0 and XNState == true then
mc.mcJogIncStop(mInst, mc.X_AXIS,.0001)
XNState = false
end
    -- Z++
if GetXin("DPad_RIGHT") == 1 and ZPState == false then
JogInc=mc.mcJogGetInc(mInst,2)
        mc.mcJogIncStart(mInst, mc.Z_AXIS, JogInc)
ZPState = true
end
if GetXin("DPad_RIGHT") == 0 and ZPState == true then
mc.mcJogIncStop(mInst, mc.Z_AXIS,.0001)
ZPState = false
end
-- Z--
if GetXin("DPad_LEFT") == 1 and ZNState == false then
JogInc=mc.mcJogGetInc(mInst,2)
        mc.mcJogIncStart(mInst, mc.Z_AXIS, -1 * JogInc)
ZNState = true
end
if GetXin("DPad_LEFT") == 0 and ZNState == true then
mc.mcJogIncStop(mInst, mc.Z_AXIS,.0001)
ZNState = false
end
end)

Thanks!
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on December 20, 2016, 03:17:58 AM
If the function is in your script it should fire....
If you dont you have to add it. Below is cut and paste from default router profile ScreenLoadScript:

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
Title: Re: mcX360 Plugin for Lua
Post by: francocncprojects on December 20, 2016, 12:29:12 PM
Thanks for responding. 

So here is what I've discovered:
1.  I could not get the ButtonJogModeToggle() function call to work within the X30 LUA panel.
2.  I tried pasting the actual code from within the ButtonJogModeToggle() function into the X360 LUA panel and that didn't work either.
3.  I deleted the X360 LUA panel and pasted DAZ's code into the screen PLC Script.  Now, the ButtonJogModeToggle() function call works. ;D
4.  Now, I need to figure out how to get the Xbox 360 controller to activate the btnCycleJogInc button.  It seems like scripted functions are easy to call.  However, there are other things (Actions?) that appear as "Left Up Action" on the screen buttons.  There is an action called "Cycle Jog Increment" that I need to make happen but I don't know how to "script" that?  Any thoughts?

FYI:  My Screen PLC Script is pasted below.

Thanks!

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

-------------------------------------------------------
--  Calculate CSS
-------------------------------------------------------
local NativeUnits, rc = mc.mcCntlGetUnitsDefault(inst)
local units, rc = mc.mcCntlGetUnitsCurrent(inst)
local xDia = mc.mcAxisGetPos(inst, 0)
local dia, rc = mc.mcCntlGetDiaMode(inst)

if (dia == 0) then
 xDia = (xDia * 2)
end

if (NativeUnits == 200) and (units == 210) then
 xDia = (xDia * 25.4)
elseif (NativeUnits == 210) and (units == 200) then
 xDia = (xDia / 25.4)
end

if (units == 200) then --Gcode is in inch mode (G20)
    mc.mcSpindleCalcCSSToRPM(inst, xDia, true)
elseif (units == 210) then --Gcode is in mm mode (G21)
    mc.mcSpindleCalcCSSToRPM(inst, xDia, false)
end

-------------------------------------------------------
--  CSS DRO
-------------------------------------------------------
local FeedRate, SpinRPM, CurrentCSS
FeedRate = scr.GetProperty ("droFeedRate", "Value")
FeedRate = tonumber(FeedRate)
SpinRPM = scr.GetProperty("droSpinRPM", "Value")
SpinRPM = tonumber(SpinRPM)
CurrentCSS = (FeedRate / SpinRPM)
if CurrentCSS ~= LastCSS then --We need to update our user DRO
    LastCSS = CurrentCSS --So we only update DRO when needed. Stops blinking DRO
    if (FeedRate > 0) and (SpinRPM > 0) then
        scr.SetProperty("droConstantSurfaceSpeed", "Value", tostring(CurrentCSS))
    else
        scr.SetProperty("droConstantSurfaceSpeed", "Value", "0.0000")
    end
end

-------------------------------------------------------
--  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

-------------------------------------------------------
--Set current offset x wear
-------------------------------------------------------

local m_currentOffset, XWear, XTT

XWear = scr.GetProperty("droXWearOffset", "Value")
XWear = tonumber(XWear)
m_currentOffset = mc.mcCntlGetPoundVar(inst, mc.SV_CUR_LENGTH_INDEX)
XTT, rc = mc.mcToolGetData(inst, mc.MTOOL_LATHE_X_W, m_currentOffset)
if (XWear ~= XTT) then
    scr.SetProperty("droXWearOffset", "Value", tostring(XTT))
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

-------------------------------------------------------
--Tool Offset Active
-------------------------------------------------------

local offset = scr.GetProperty('currenttool offset','Value');

if (offset == nil) then
 
    scr.SetProperty('tool offset active', 'Fg Color', '#8B8B8B');--Black
    scr.SetProperty('tool offset active', 'Bg Color', '#C0C0C0');--Light Grey

 -- elseif (offset ~= 'droToolNumber') then
 
   
    scr.SetProperty('tool offset active', 'Fg Color', '#8B8B8B');--Black
    scr.SetProperty('tool offset active', 'Bg Color', '#C0C0C0');--RED
 
   
   elseif (offset =='droToolNumber') then
 
    scr.SetProperty('tool offset active', 'Fg Color', '#8B8B8B');--Black
    scr.SetProperty('tool offset active', 'Bg Color', '#FFFF00');--YELLOW
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
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()

    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"
--    [1036] = "droGageBlockT",
--    [1037] = "droProbeTravel",
--    [1038] = "droProbeRate"

    }

--START CUSTOM CODE
----------------------------------------------------------------------------
--  X_360 Lua by DazTheGas - Last Updated 07/12/16 SDK 3233
----------------------------------------------------------------------------

-----------------Declares

local mInst = mc.mcGetInstance()
local Xenabled = false
local ESState = false
local JogInc = .001
local AIState = false
local CJIState = false
local XPState = false
local XNState = false
local YPState = false
local YNState = false
local ZPState = false
local ZNState = false

-- X360 Init Panel
X360_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
X360_Timer = wx.wxTimer(X360_Panel)
X360_Timer:Start(100)
-- EStop
EStop_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
EStop_Timer = wx.wxTimer(EStop_Panel)
-- DPad
DPad_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
DPad_Timer = wx.wxTimer(DPad_Panel)

-----------------Functions

function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueLong(hreg)
end

-----------------Event Timers

X360_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("LTR_Val") > 150 and Xenabled == false then
        EStop_Timer:Start(50)
        DPad_Timer:Start(100)
Xenabled = true
        mc.mcCntlSetLastError(mInst,"X_360 Running")
elseif GetXin("LTR_Val") < 50 and Xenabled == true then
        EStop_Timer:Stop()
        DPad_Timer:Stop()
Xenabled = false
        mc.mcCntlSetLastError(mInst,"X_360 Stopped")
end
end)

EStop_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("Btn_B") == 1 and ESState == false then
local EStop = mc.mcSignalGetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY))
if (EStop == 1) then
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 0)
else
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 1)
end
ESState = true
end
if GetXin("Btn_B") == 0 and ESState == true then
ESState = false
end
end)

DPad_Panel:Connect(wx.wxEVT_TIMER, function (event)
-- ABS/INC
if GetXin("Btn_X") == 1 and AIState == false then
--How do I active the btnToggleJogMode ?
        ButtonJogModeToggle()
        mc.mcCntlSetLastError(mInst,"X_Button_Pressed")
        AIState = true
end
if GetXin("Btn_X") == 0 and AIState == true then
AIState = false
end
-- Cycle_Jog_Inc
if GetXin("Btn_START") == 1 and CJIState == false then
--How do I active the btnCycleJogInc ?
        mc.mcCntlSetLastError(mInst,"START_Button_Pressed")
        CJIState = true
end
if GetXin("Btn_START") == 0 and CJIState == true then
CJIState = false
end
-- -- Y++
-- if GetXin("DPad_UP") == 1 and GetXin("Btn_LS") == 0 and YPState == false then
-- mc.mcJogIncStart(mInst, mc.Y_AXIS, JogInc)
-- YPState = true
-- end
-- if GetXin("DPad_UP") == 0 and GetXin("Btn_LS") == 0 and YPState == true then
-- mc.mcJogIncStop(mInst, mc.Y_AXIS,.1)
-- YPState = false
-- end
-- -- Y--
-- if GetXin("DPad_DOWN") == 1 and GetXin("Btn_LS") == 0 and YNState == false then
-- mc.mcJogIncStart(mInst, mc.Y_AXIS, -1 * JogInc)
-- YNState = true
-- end
-- if GetXin("DPad_DOWN") == 0 and GetXin("Btn_LS") == 0 and YNState == true then
-- mc.mcJogIncStop(mInst, mc.Y_AXIS,.1)
-- YNState = false
-- end
-- X++
if GetXin("DPad_DOWN") == 1 and XPState == false then
JogInc=mc.mcJogGetInc(mInst,0) / 2.
        mc.mcJogIncStart(mInst, mc.X_AXIS, JogInc)
XPState = true
end
if GetXin("DPad_DOWN") == 0 and XPState == true then
mc.mcJogIncStop(mInst, mc.X_AXIS,.0001)
XPState = false
end
-- X--
if GetXin("DPad_UP") == 1 and XNState == false then
JogInc=mc.mcJogGetInc(mInst,0) / 2.
        mc.mcJogIncStart(mInst, mc.X_AXIS, -1 * JogInc)
XNState = true
end
if GetXin("DPad_UP") == 0 and XNState == true then
mc.mcJogIncStop(mInst, mc.X_AXIS,.0001)
XNState = false
end
    -- Z++
if GetXin("DPad_RIGHT") == 1 and ZPState == false then
JogInc=mc.mcJogGetInc(mInst,2)
        mc.mcJogIncStart(mInst, mc.Z_AXIS, JogInc)
ZPState = true
end
if GetXin("DPad_RIGHT") == 0 and ZPState == true then
mc.mcJogIncStop(mInst, mc.Z_AXIS,.0001)
ZPState = false
end
-- Z--
if GetXin("DPad_LEFT") == 1 and ZNState == false then
JogInc=mc.mcJogGetInc(mInst,2)
        mc.mcJogIncStart(mInst, mc.Z_AXIS, -1 * JogInc)
ZNState = true
end
if GetXin("DPad_LEFT") == 0 and ZNState == true then
mc.mcJogIncStop(mInst, mc.Z_AXIS,.0001)
ZNState = false
end
end)
--END CUSTOM CODE


-- ******************************************************************************************* --
--  _   _   _  __          __             _____    _   _   _____   _   _    _____   _   _   _  --
-- | | | | | | \ \        / /     /\     |  __ \  | \ | | |_   _| | \ | |  / ____| | | | | | | --
-- | | | | | |  \ \  /\  / /     /  \    | |__) | |  \| |   | |   |  \| | | |  __  | | | | | | --
-- | | | | | |   \ \/  \/ /     / /\ \   |  _  /  | . ` |   | |   | . ` | | | |_ | | | | | | | --
-- |_| |_| |_|    \  /\  /     / ____ \  | | \ \  | |\  |  _| |_  | |\  | | |__| | |_| |_| |_| --
-- (_) (_) (_)     \/  \/     /_/    \_\ |_|  \_\ |_| \_| |_____| |_| \_|  \_____| (_) (_) (_) --
--                                                                                             --
-- 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
-------------------------------------------------------

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

Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on December 20, 2016, 03:02:19 PM
Firstly i must erge you to remove the code from the plc and back into a panel by itself, you will have unlimited problems with your machine and could result in very bad things.

I will write you a small bit of cose to get you on the way.

if wanting to use the buttonjogmode function then instead you can also use the signals for Xplus and Xneg etc

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on December 20, 2016, 03:53:29 PM
You would be better using the signals for jogging.

 
Code: [Select]
DPad_Panel:Connect(wx.wxEVT_TIMER, function (event)

  -- Inc Moves
if GetXin("Btn_X") == 1 and AIState == false then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_JOG_CONT), 0)
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_JOG_INC), 1)
        AIState = true
end
if GetXin("Btn_X") == 0 and AIState == true then
AIState = false
end
-- Cont Moves
if GetXin("Btn_START") == 1 and CJIState == false then
                mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_JOG_INC), 0)
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.OSIG_JOG_CONT), 1)

        CJIState = true
end
if GetXin("Btn_START") == 0 and CJIState == true then
CJIState = false
end
 
-- Y++
if GetXin("DPad_UP") == 1 and YPState == false then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGYP), 1)
YPState = true
end
if GetXin("DPad_UP") == 0 and YPState == true then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGYP), 0)
YPState = false
end

-- Y--
if GetXin("DPad_DOWN") == 1 and YNState == false then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGYN), 1)
YNState = true
end
if GetXin("DPad_DOWN") == 0 and YNState == true then
mc.mcSignalSetState (mc.mcSignalGetHandle (inst, mc.ISIG_JOGYN), 0)
YNState = false
end
end)

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on December 20, 2016, 04:21:46 PM
Just noticed with that code i have used "inst" but needs to be "mInst" to fit with rest of code.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: francocncprojects on December 20, 2016, 06:53:54 PM
DAZ - you have been EXTREMELY helpful.  Thank You! 

So far, I've been able to "borrow" enough of your code to get up and running - see below. 

I eagerly await your next YouTube video so I can learn how to get the joysticks to work.  Beyond that, the only other thing I'd like have is the ability to change the jog feed rate from the XBox 360 controller. 

Thanks!

Code: [Select]
----------------------------------------------------------------------------
--  X_360 Lua by DazTheGas - Last Updated 07/12/16 SDK 3233
----------------------------------------------------------------------------

-----------------Declares

local mInst = mc.mcGetInstance()
local Xenabled = false
local ESState = false
local JogInc = .001
local CState = false
local FHState = false
local RSState = false
local AIState = false
local CJIState = false
local XPState = false
local XNState = false
local YPState = false
local YNState = false
local ZPState = false
local ZNState = false
local isinc = false

-- X360 Init Panel
X360_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
X360_Timer = wx.wxTimer(X360_Panel)
X360_Timer:Start(100)
-- EStop
EStop_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
EStop_Timer = wx.wxTimer(EStop_Panel)
-- DPad
DPad_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
DPad_Timer = wx.wxTimer(DPad_Panel)

-----------------Functions

function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueLong(hreg)
end

-----------------Event Timers

X360_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("LTR_Val") > 150 and Xenabled == false then
        EStop_Timer:Start(50)
        DPad_Timer:Start(50) -- was 100
Xenabled = true
        mc.mcCntlSetLastError(mInst,"X_360 Running")
elseif GetXin("LTR_Val") < 50 and Xenabled == true then
        EStop_Timer:Stop()
        DPad_Timer:Stop()
Xenabled = false
        mc.mcCntlSetLastError(mInst,"X_360 Stopped")
end
end)

EStop_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("Btn_B") == 1 and ESState == false then
local EStop = mc.mcSignalGetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY))
if (EStop == 1) then
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 0)
else
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 1)
end
ESState = true
end
if GetXin("Btn_B") == 0 and ESState == true then
ESState = false
end
end)

DPad_Panel:Connect(wx.wxEVT_TIMER, function (event)
-- Inc Moves
if GetXin("Btn_X") == 1 and AIState == false then
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.OSIG_JOG_CONT), 0)
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.OSIG_JOG_INC), 1)
        isinc = true
        AIState = true
end
if GetXin("Btn_X") == 0 and AIState == true then
        AIState = false
end
-- Cont Moves
if GetXin("Btn_START") == 1 and CJIState == false then
        mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.OSIG_JOG_INC), 0)
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.OSIG_JOG_CONT), 1)
        isinc = false
        CJIState = true
end
if GetXin("Btn_START") == 0 and CJIState == true then
        CJIState = false
end
-- Cycle_Start
if GetXin("Btn_A") == 1 and CState == false then
--CycleStart()
        --mc.mcCntlCycleStart (mInst)
            local rc;
            local tab, rc = scr.GetProperty("MainTabs", "Current Tab")
            local tabG_Mdione, rc = scr.GetProperty("nbGCodeMDI1", "Current Tab")
            --local tabG_Mditwo, rc = scr.GetProperty("nbGCodeMDI2", "Current Tab")
            --wx.wxMessageBox("tab == " .. tostring(tab) )
            --See if we have to do an MDI command
           
            if ((tonumber(tab) == 0 and tonumber(tabG_Mdione) == 1)) then --or (tonumber(tab) == 2 and tonumber(tabG_Mditwo) == 1 )) then
                local state = mc.mcCntlGetState(mInst);
                if (state == mc.MC_STATE_MRUN_MACROH) then
                    mc.mcCntlCycleStart(mInst);
                    mc.mcCntlSetLastError(mInst, "Do Cycle Start");
                else
                    if (tonumber(tab) == 0) then 
                        scr.ExecMdi('mdi1');
                        mc.mcCntlSetLastError(mInst, "Do MDI 1");
                    end
                end
            else
                --Do CycleStart
                mc.mcCntlSetLastError(mInst, "Do Cycle Start");
                mc.mcCntlCycleStart(mInst);
                --wx.wxMessageBox("Do Cycle Start")       
            end
        mc.mcCntlSetLastError(mInst,"A_Button_Pressed")
        CState = true
end
if GetXin("Btn_A") == 0 and CState == true then
CState = false
end
-- Feed_Hold
if GetXin("Btn_Y") == 1 and FHState == false then
mc.mcCntlFeedHold(mInst)
        mc.mcCntlSetLastError(mInst,"Y_Button_Pressed")
        FHState = true
end
if GetXin("Btn_Y") == 0 and FHState == true then
FHState = false
end
-- Jog_Percent
if GetXin("Btn_RS") == 1 and RSState == false then
        mc.mcCntlSetLastError(mInst,"RS_Button_Pressed")
        local CurInc = mc.mcProfileGetInt(mInst,"Preferences","JogIncSet",0) --make a new setting in profile to hold our current inc number
        local SetInc = mc.mcProfileGetDouble(mInst,"Preferences","JogInc"..CurInc,0.00) -- fetch our inc value from the profile JogInc + 2 say = [Preferences][JogInc2]

        CurInc = CurInc + 1 -- increment our CurInc by 1
        if CurInc > 10 then CurInc = 1 end -- if our CurInc is greater than 10 then go back to 1
       
        mc.mcJogSetInc(mInst, mc.X_AXIS, SetInc) -- set inc for axis
        mc.mcJogSetInc(mInst, mc.Y_AXIS, SetInc) -- set inc for axis
        mc.mcJogSetInc(mInst, mc.Z_AXIS, SetInc) -- set inc for axis

        mc.mcProfileWriteInt(mInst,"Preferences","JogIncSet",CurInc) --set our profile to new CurInc

        RSState = true
end
if GetXin("Btn_RS") == 0 and RSState == true then
RSState = false
end
-- -- Y++
-- if GetXin("DPad_UP") == 1 and GetXin("Btn_LS") == 0 and YPState == false then
-- mc.mcJogIncStart(mInst, mc.Y_AXIS, JogInc)
-- YPState = true
-- end
-- if GetXin("DPad_UP") == 0 and GetXin("Btn_LS") == 0 and YPState == true then
-- mc.mcJogIncStop(mInst, mc.Y_AXIS,.1)
-- YPState = false
-- end
-- -- Y--
-- if GetXin("DPad_DOWN") == 1 and GetXin("Btn_LS") == 0 and YNState == false then
-- mc.mcJogIncStart(mInst, mc.Y_AXIS, -1 * JogInc)
-- YNState = true
-- end
-- if GetXin("DPad_DOWN") == 0 and GetXin("Btn_LS") == 0 and YNState == true then
-- mc.mcJogIncStop(mInst, mc.Y_AXIS,.1)
-- YNState = false
-- end
-- X++
if GetXin("DPad_DOWN") == 1 and XPState == false then
        if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGXP), 1)
        end
        if isinc == true then
            JogInc=mc.mcJogGetInc(mInst,0) / 2.
            mc.mcJogIncStart(mInst, mc.X_AXIS, JogInc)
        end
XPState = true
end
if GetXin("DPad_DOWN") == 0 and XPState == true then
        if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGXP), 0)
        end
        if isinc == true then
            mc.mcJogIncStop(mInst, mc.X_AXIS,.0001)
        end
XPState = false
end
-- X--
if GetXin("DPad_UP") == 1 and XNState == false then
        if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGXN), 1)
        end
        if isinc == true then
            JogInc=mc.mcJogGetInc(mInst,0) / 2.
            mc.mcJogIncStart(mInst, mc.X_AXIS, JogInc * -1.)
        end
XNState = true
end
if GetXin("DPad_UP") == 0 and XNState == true then
        if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGXN), 0)
        end       
        if isinc == true then
            mc.mcJogIncStop(mInst, mc.X_AXIS,.0001)
        end
XNState = false
end

    -- Z++
if GetXin("DPad_RIGHT") == 1 and ZPState == false then
        if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGZP), 1)
        end
        if isinc == true then
            JogInc=mc.mcJogGetInc(mInst,2)
            mc.mcJogIncStart(mInst, mc.Z_AXIS, JogInc)
        end
ZPState = true
end
if GetXin("DPad_RIGHT") == 0 and ZPState == true then
        if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGZP), 0)
        end
if isinc == true then
            mc.mcJogIncStop(mInst, mc.Z_AXIS,.0001)
        end
ZPState = false
end
-- Z--
if GetXin("DPad_LEFT") == 1 and ZNState == false then
if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGZN), 1)
        end
        if isinc == true then
            JogInc=mc.mcJogGetInc(mInst,2)
            mc.mcJogIncStart(mInst, mc.Z_AXIS, -1 * JogInc)
        end
ZNState = true
end
if GetXin("DPad_LEFT") == 0 and ZNState == true then
if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGZN), 0)
        end
        if isinc == true then
            mc.mcJogIncStop(mInst, mc.Z_AXIS,.0001)
        end
        ZNState = false
end
end)
Title: Re: mcX360 Plugin for Lua
Post by: Bx3mE on December 22, 2016, 05:27:10 AM
To handle the Thumbsticks you can check out my code. See the function HandleThumbInput...

Here is my current script:

Code: [Select]
--X360 Controller Script for X360Lua Plugin

-- INITIALIZATION --

local inst = mc.mcGetInstance()

local X360_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
X360_Timer = wx.wxTimer(X360_Panel)
local X360TS_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
X360TS_Timer = wx.wxTimer(X360TS_Panel)
local X360BTN_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
X360BTN_Timer = wx.wxTimer(X360BTN_Panel)
local X360DPAD_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
X360DPAD_Timer = wx.wxTimer(X360DPAD_Panel)


-- CONFIGURATION --

-- ======================================================================
-- = Dont forget to add X360_Timer:Stop() to your screen unload script  =
-- ======================================================================

local DeadZone = 25 --Based on controller output value (-99 => 99)
local MaxJogSpeed = 50 --Percentage of axix max speed

local RTHY_MapToMashineAxis = mc.AXIS2 -- Z-AXIS
local RTHX_MapToMashineAxis = mc.AXIS3 -- A-AXIS
local LTHY_MapToMashineAxis = mc.AXIS1 -- Y-AXIS
local LTHX_MapToMashineAxis = mc.AXIS0 -- X-AXIS

local isPressed_BTN = {false,false,false,false,false,false,false,false} -- Buttons 0-3 = { A,B,X,Y } Buttons 4-7 = { DPAD: UP, DOWN, LEFT, RIGHT }
local thumbSticks = {{"RTH_Y_Val", 1, true, RTHY_MapToMashineAxis}, {"RTH_X_Val", 2, true, RTHX_MapToMashineAxis}, {"LTH_Y_Val", 3, true, LTHY_MapToMashineAxis}, {"LTH_X_Val", 4, true, LTHX_MapToMashineAxis}}  --RegisterName, number, isInDeadZone, MashineAxisMapping
local isSecured = 0

-- FUNCTIONS --

function GetX360(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
end
--------------------------------------------------
function EnableMashine(finst,pressed)
    if pressed then
        mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0) --I dont want to clear E-Stop on BTN_E-Stop_UP so instead i use X360´s MashineEnable for it... (see E-Stop function)
        wx.wxMilliSleep(100) -- You cannot enable unless E-Stop is cleared so we must wait for mcSignalSetState to complete
        mc.mcCntlEnable(finst, true)
    end
    return
end
function CycleStart(finst,pressed)
    if pressed then
        mc.mcCntlCycleStart(finst)
    end
    return
end
function EStop(finst,pressed)
    if pressed then
        mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 1)
    --else
        -- Uncomment this if you want E-Stop cleared on button release
        --mc.mcSignalSetState (mc.mcSignalGetHandle (finst, mc.ISIG_EMERGENCY), 0)
    end
    return
end
--------------------------------------------------
function ProbeYP(finst, pressed)
    if pressed then Probe(finst, "Y", "+") end
    return
end
function ProbeYN(finst, pressed)
    if pressed then Probe(finst, "Y", "-") end
    return
end
function ProbeXP(finst, pressed)
    if pressed then Probe(finst, "X", "+")  end
    return
end
function ProbeXN(finst, pressed)
    if pressed then Probe(finst, "X", "-") end
    return
end
function ProbeZP(finst, pressed)
    if pressed then Probe(finst, "Z", "+") end
    return
end
function ProbeZN(finst, pressed)
    if pressed then Probe(finst, "Z", "-") end
    return
end
function ProbeAP(finst, pressed)
    if pressed then Probe(finst, "A", "+") end
    return
end
function ProbeAN(finst, pressed)
    if pressed then Probe(finst, "A", "-") end
    return
end
function Probe(finst, pressed, axis, direction)
    if pressed then
        mc.mcCntlSetLastError(finst, "Placeholder for Probing "..axis.." axis in "..direction)
    end
    return
end
--------------------------------------------------
function SpindleSpeedInc(finst, pressed)
    if pressed then
        local val = scr.GetProperty('slideSRO', 'Value');
        val = tonumber(val) + 5;
        local maxval = scr.GetProperty('slideSRO', 'Max Value')
        if (tonumber(val) >= tonumber(maxval)) then
         val = maxval;
        end
        scr.SetProperty('slideSRO', 'Value', tostring(val));
    end
end
function SpindleSpeedDec(finst, pressed)
    if pressed then
        local val = scr.GetProperty('slideSRO', 'Value');
        val = tonumber(val) - 5;
        local minval = scr.GetProperty('slideSRO', 'Min Value')
        if (tonumber(val) <= tonumber(minval)) then
         val = minval;
        end
        scr.SetProperty('slideSRO', 'Value', tostring(val));
    end
end
--------------------------------------------------
function FeedRateInc(finst, pressed)
    if pressed then
        local val = scr.GetProperty('slideFRO', 'Value');
        val = tonumber(val) + 10;
        local maxval = scr.GetProperty('slideFRO', 'Max Value')
        if (tonumber(val) >= tonumber(maxval)) then
         val = maxval;
        end
        scr.SetProperty('slideFRO', 'Value', tostring(val));
    end
end
function FeedRateDec(finst, pressed)
    if pressed then
        local val = scr.GetProperty('slideFRO', 'Value');
        val = tonumber(val) - 10;
        local minval = scr.GetProperty('slideFRO', 'Min Value')
        if (tonumber(val) <= tonumber(minval)) then
         val = minval;
        end
        scr.SetProperty('slideFRO', 'Value', tostring(val));
    end
end
--------------------------------------------------

function HandleButtonPress(finst,btn,btnNo,func,msg,strParam1,strParam2)
    if btn == "1" and isPressed_BTN[btnNo] == false then
        func(finst,true,strParam1,strParam2)
        mc.mcCntlSetLastError(finst, msg)
        isPressed_BTN[btnNo] = true
    elseif btn == "0" and isPressed_BTN[btnNo] == true then
        func(finst,false,strParam1,strParam2)
        isPressed_BTN[btnNo] = false
    end
    return
end

function HandleThumbInput(finst,thax,DZ, MaxJS)
    local th = GetX360(thax[1])
    local axis = thax[4]
    if (tonumber(th) >= (DZ*-1)) and (tonumber(th) <= DZ) and thax[2]==0 then
        if mc.mcJogGetVelocity(finst, axis) ~= 0 then
            mc.mcJogSetRate(finst, axis, 0)
            mc.mcJogVelocityStop(finst, axis);
        end
        thax[2] = 1
    else
        if tonumber(th) > tonumber(DZ) then
            mc.mcJogSetRate(finst, axis, ((((tonumber(th)-DZ)*(tonumber(th)-DZ))/((100-DZ)*(100-DZ)))*MaxJS))
            mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_POS)
            thax[2] = 0
        elseif tonumber(th) < (tonumber(DZ)*-1) then
            mc.mcJogSetRate(finst, axis, ((((tonumber(th)+DZ)*(tonumber(th)+DZ))/((100-DZ)*(100-DZ)))*MaxJS))
            mc.mcJogVelocityStart(finst, axis, mc.MC_JOG_NEG)
            thax[2] = 0
        end
    end
    return
end


X360_Timer:Start(50)
-- Main Loop --
X360_Panel:Connect(wx.wxEVT_TIMER,   function (event)
-- HANDLE E-STOP ON BTN_B
    HandleButtonPress(inst,GetX360("Btn_B"),2,EStop,"X360_E-STOP")
-- SECURITY MEASURE -- Require Both triggerbuttons to be pressed...
    local deadMansGrip = (GetX360("Btn_RS") == "1" ) and (GetX360("Btn_LS") == "1" )
-- ENABLE CONTROLLER (Only X and A are configured)
    if deadMansGrip and not isSecured then
        isSecured = true
        X360TS_Timer:Start(100)
        X360BTN_Timer:Start(100)
        X360DPAD_Timer:Start(100)
        mc.mcCntlSetLastError(inst, "Secure grip! - X360 Controller enabled")
    elseif not deadMansGrip and isSecured then -- If dead mans grip is lost then stop the machine
        isSecured = false
        X360TS_Timer:Stop()
        X360BTN_Timer:Stop()
        X360DPAD_Timer:Stop()
        for i=4, 1, -1
        do
            local axis = thumbSticks[i][4]
            --if mc.mcJogGetVelocity(inst, axis) ~= 0 then
            --mc.mcJogSetRate(inst, axis, 0)
            mc.mcJogVelocityStop(inst, axis);
            --end
            thumbSticks[i][3] = true
        end
        mc.mcCntlSetLastError(inst, "Secure grip was lost - X360 Controller disabled")
    end
end)

-- THUMBSTICKS LOOP --
X360TS_Panel:Connect(wx.wxEVT_TIMER,   function (event)
-- HANDLE THUMBSTICKS JOG
    HandleThumbInput(inst, thumbSticks[1], DeadZone, MaxJogSpeed)
    HandleThumbInput(inst, thumbSticks[2], DeadZone, MaxJogSpeed)
    HandleThumbInput(inst, thumbSticks[3], DeadZone, MaxJogSpeed)
    HandleThumbInput(inst, thumbSticks[4], DeadZone, MaxJogSpeed)


end)

-- BUTTONS LOOP --
X360BTN_Panel:Connect(wx.wxEVT_TIMER,   function (event)
-- MASHINE ENABLE ON BTN_X
    HandleButtonPress(inst,GetX360("Btn_X"),3,EnableMashine,"X360_Enabled_Machine")
-- CYCLE START ON BTN_A
    HandleButtonPress(inst,GetX360("Btn_A"),1,CycleStart,"X360_CycleStart")
end)

-- DPAD LOOP --
X360DPAD_Panel:Connect(wx.wxEVT_TIMER,   function (event)
    local StartDownMode = false
    local YDownMode = false
    local Both = false
    local None = false

    local StartDown = (GetX360("Btn_START") == "1" )
    local YDown = (GetX360("Btn_Y") == "1" )

    if YDown and StartDown and not Both then
        Both = true
        None = false
        YDownMode = false
        StartDownMode = false
    elseif not YDown and not StartDown and not None then
        None = true
        Both = false
        YDownMode = false
        StartDownMode = false
    elseif YDown and not YDownMode then
        YDownMode = true
        None = false
        Both = false
        StartDownMode = false
    elseif StartDown and not StartDownMode then
        StartDownMode = true
        FeedAndSpeedMode = false
        None = false
        Both = false
    end

    if None then
    -- SpindleSpeed +
        HandleButtonPress(inst,GetX360("DPad_UP"),4,SpindleSpeedInc,"X360_SpindleSpeed +")
    -- SpindleSpeed -
        HandleButtonPress(inst,GetX360("DPad_DOWN"),5,SpindleSpeedDec,"X360_SpindleSpeed -")
    -- FeedRate +
        HandleButtonPress(inst,GetX360("DPad_RIGHT"),7,FeedRateInc,"X360_FeedRate +")
    -- FeedRate -
        HandleButtonPress(inst,GetX360("DPad_LEFT"),6,FeedRateDec,"X360_FeedRate -")
    elseif StartDownMode then
    -- PROBE DIRECTION Z+
        HandleButtonPress(inst,GetX360("DPad_UP"),4,Probe,"X360_Probe Z+","Z","+")
    -- PROBE DIRECTION Z-
        HandleButtonPress(inst,GetX360("DPad_DOWN"),5,Probe,"X360_Probe Z-","Z","-")
    -- PROBE DIRECTION A+
        HandleButtonPress(inst,GetX360("DPad_RIGHT"),7,Probe,"X360_Probe A+","A","+")
    -- PROBE DIRECTION A-
        HandleButtonPress(inst,GetX360("DPad_LEFT"),6,Probe,"X360_Probe A-","A","-")
    elseif YDownMode then
    -- PROBE DIRECTION Y+
        HandleButtonPress(inst,GetX360("DPad_UP"),4,Probe,"X360_Probe Y+","Y","+")
    -- PROBE DIRECTION Y-
        HandleButtonPress(inst,GetX360("DPad_DOWN"),5,Probe,"X360_Probe Y-","Y","-")
    -- PROBE DIRECTION X+
        HandleButtonPress(inst,GetX360("DPad_RIGHT"),7,Probe,"X360_Probe X+","X","+")
    -- PROBE DIRECTION X-
        HandleButtonPress(inst,GetX360("DPad_LEFT"),6,Probe,"X360_Probe X-","X","-")
    end
end)


Title: Re: mcX360 Plugin for Lua
Post by: francocncprojects on December 22, 2016, 09:47:34 AM
Bx3mE - You and Daz are like super geniuses with this LUA stuff.  I've learned a lot from both of your posts - Thanks!

Your code is really clean and well commented.  I'm studying it but (naturally) I have a few stupid questions:

1.  Where do I place this code?  In the dedicated X360 LUA Panel (per Daz) or in one of the existing scripts for the Screen?
2.  Why does the X360_Timer:Stop() need to be located in the Screen unload script?  Why not in the same place as the rest of the code?
3.  I see a variable "finst" in a lot of the functions - why not just use "inst"?

Once again, thank you for helping me - this is really awesome!
-Franco
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on December 22, 2016, 10:03:22 AM
X360_Timer:Stop() this will be explained in next video, when you start a timer you also need to disable it when shutting down mach4 or this can cause the gui to lock up.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: francocncprojects on December 24, 2016, 11:18:30 PM
Thanks Daz.

I did what you said (placed X360_Timer:Stop() in the Screen Unload Script) but when I do that, I receive the Unload Script Error below:

------------------------------------------
Lua: Error while running chunk
[string ""]:597: attempt to index global 'X360_Timer' (a nil value)
stack traceback:
[string ""]:597: in function <[string ""]:595>

Correct the script error and restart.
______________________________

If I remove the X360_Timer:Stop() from the Screen Unload Script, no more errors.  Do you have any thoughts on that?

Thanks,
-Franco
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on December 25, 2016, 05:43:34 PM
Sometimes I forget when converting from my screenset to the GUI, a long while ago leaving a timer running would cause a fault and still does in my own, the lua panels in the gui are actually c++ classes and prob destroyed inc timer on shutdown so the x360 timer prob no longer exists, although still worth including but like this,

if X360_Timer then X360_Timer:Stop() end

if anything does change at a later date in the core then at least this is already there.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: francocncprojects on December 25, 2016, 07:47:50 PM
Thanks Daz - I'll use that strategy. 

I noticed that the machine can be left in a state where the axis continues to jog even though the safety triggers are released.  This may be due to the strategy I used and may not be an issue for other people.  I included some code after the timer stop commands to prevent any possible movement after the safety triggers are released, just to be safe.

Once again, thanks for your help with this.  I'm not a LUA expert, but I feel like I've learned a lot.

Code: [Select]
----------------------------------------------------------------------------
--  X_360 Lua by DazTheGas - Last Updated 07/12/16 SDK 3233
----------------------------------------------------------------------------

-----------------Declares

local mInst = mc.mcGetInstance()
local Xenabled = false
local ESState = false
local JogInc = .001
local CState = false
local FHState = false
local RSState = false
local AIState = false
local XPState = false
local TXPState = false
local XNState = false
local TXNState = false
local YPState = false
local TYPState = false
local YNState = false
local TYNState = false
local ZPState = false
local TZPState = false
local ZNState = false
local TZNState = false
local islok = false
local DZ = 25 --Dead Zone Based on controller output value (-99 => 99)
local JIState = false
local JDState = false
local Jog_Rate = 0

-- X360 Init Panel
X360_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
X360_Timer = wx.wxTimer(X360_Panel)
-- EStop
EStop_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
EStop_Timer = wx.wxTimer(EStop_Panel)
-- Buttons
Btns_Panel = wx.wxPanel( wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
Btns_Timer = wx.wxTimer(Btns_Panel)

-----------------Functions
--Read XBox Controller Values
function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueLong(hreg)
end
-- X Pos
function jog_XP(xpisinc,xpmove)
    if xpisinc == false then
        mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGXP), xpmove)
    end
    if xpisinc == true then
        if xpmove == 1 then
            JogInc=mc.mcJogGetInc(mInst,0) / 2. -- Diameter Mode for Lathe
            mc.mcJogIncStart(mInst, mc.X_AXIS, JogInc)
        else
            mc.mcJogIncStop(mInst, mc.X_AXIS,.0001)
        end
    end
end
-- X Neg
function jog_XN(xnisinc,xnmove)
    if xnisinc == false then
        mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGXN), xnmove)
    end
    if xnisinc == true then
        if xnmove == 1 then
            JogInc=mc.mcJogGetInc(mInst,0) / 2. -- Diameter Mode for Lathe
            mc.mcJogIncStart(mInst, mc.X_AXIS, JogInc * -1.)
        else       
            mc.mcJogIncStop(mInst, mc.X_AXIS,.0001)
        end
    end
end
-- Z Pos
function jog_ZP(zpisinc,zpmove)
    if zpisinc == false then
        mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGZP), zpmove)
    end
    if zpisinc == true then
        if zpmove == 1 then
            JogInc=mc.mcJogGetInc(mInst,2)
            mc.mcJogIncStart(mInst, mc.Z_AXIS, JogInc)
        else
            mc.mcJogIncStop(mInst, mc.Z_AXIS,.0001)
        end
    end
end
-- Z Neg
function jog_ZN(znisinc,znmove)
    if znisinc == false then
        mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_JOGZN), znmove)
    end
    if znisinc == true then
        if znmove == 1 then
            JogInc=mc.mcJogGetInc(mInst,2)
            mc.mcJogIncStart(mInst, mc.Z_AXIS, -1 * JogInc)
        else
            mc.mcJogIncStop(mInst, mc.Z_AXIS,.0001)
        end
    end
end

-----------------Event Timers
X360_Timer:Start(100)
X360_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("LTR_Val") + GetXin("RTR_Val") > 400 and Xenabled == false then
        EStop_Timer:Start(50)
        Btns_Timer:Start(50) -- was 100
Xenabled = true
        mc.mcCntlSetLastError(mInst,"Pendant Enabeled")
elseif GetXin("LTR_Val") + GetXin("RTR_Val") < 100 and Xenabled == true then
        EStop_Timer:Stop()
        Btns_Timer:Stop()
Xenabled = false
        -- Stop all jogging if safety triggers are released
        jog_XP(isinc,0)
        jog_XN(isinc,0)
        jog_ZP(isinc,0)
        jog_ZN(isinc,0)
        --
        mc.mcCntlSetLastError(mInst,"Pendant Disabled")
end
end)

EStop_Panel:Connect(wx.wxEVT_TIMER, function (event)
if GetXin("Btn_B") == 1 and ESState == false then
local EStop = mc.mcSignalGetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY))
if (EStop == 1) then
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 0)
else
mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 1)
end
ESState = true
end
if GetXin("Btn_B") == 0 and ESState == true then
ESState = false
end
end)

Btns_Panel:Connect(wx.wxEVT_TIMER, function (event)
-- Cont or Inc Jog Mode
if GetXin("Btn_X") == 1 and AIState == false then
        if isinc == false then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.OSIG_JOG_CONT), 0)
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.OSIG_JOG_INC), 1)
            isinc = true
        else
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.OSIG_JOG_INC), 0)
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.OSIG_JOG_CONT), 1)
            isinc = false
        end
        AIState = true
end
if GetXin("Btn_X") == 0 and AIState == true then
        AIState = false
end
-- Cycle_Start
if GetXin("Btn_A") == 1 and CState == false then
--CycleStart()
        --mc.mcCntlCycleStart (mInst)
            local rc;
            local tab, rc = scr.GetProperty("MainTabs", "Current Tab")
            local tabG_Mdione, rc = scr.GetProperty("nbGCodeMDI1", "Current Tab")
            --local tabG_Mditwo, rc = scr.GetProperty("nbGCodeMDI2", "Current Tab")
            --wx.wxMessageBox("tab == " .. tostring(tab) )
            --See if we have to do an MDI command
           
            if ((tonumber(tab) == 0 and tonumber(tabG_Mdione) == 1)) then --or (tonumber(tab) == 2 and tonumber(tabG_Mditwo) == 1 )) then
                local state = mc.mcCntlGetState(mInst);
                if (state == mc.MC_STATE_MRUN_MACROH) then
                    mc.mcCntlCycleStart(mInst);
                    mc.mcCntlSetLastError(mInst, "Do Cycle Start");
                else
                    if (tonumber(tab) == 0) then 
                        scr.ExecMdi('mdi1');
                        mc.mcCntlSetLastError(mInst, "Do MDI 1");
                    end
                end
            else
                --Do CycleStart
                mc.mcCntlSetLastError(mInst, "Do Cycle Start");
                mc.mcCntlCycleStart(mInst);
                --wx.wxMessageBox("Do Cycle Start")       
            end
        --mc.mcCntlSetLastError(mInst,"A_Button_Pressed")
        CState = true
end
if GetXin("Btn_A") == 0 and CState == true then
CState = false
end
-- Feed_Hold
if GetXin("Btn_Y") == 1 and FHState == false then
mc.mcCntlFeedHold(mInst)
        mc.mcCntlSetLastError(mInst,"Feed Hold")
        FHState = true
end
if GetXin("Btn_Y") == 0 and FHState == true then
FHState = false
end
-- Jog_Increase
    if GetXin("Btn_START") == 1 then
        Jog_Rate = Jog_Rate + 2
        if Jog_Rate > 100 then
            Jog_Rate = 100
        end
        mc.mcJogSetRate(mInst,mc.AXIS0,Jog_Rate)
        mc.mcJogSetRate(mInst,mc.AXIS2,Jog_Rate)
        --mc.mcCntlSetLastError(mInst,"Jog Rate Increase")
end
-- Jog_Decrease
    if GetXin("Btn_BACK") == 1 then
        Jog_Rate = Jog_Rate - 2
        if Jog_Rate < 1 then
            Jog_Rate = 1
        end
        mc.mcJogSetRate(mInst,mc.AXIS0,Jog_Rate)
        mc.mcJogSetRate(mInst,mc.AXIS2,Jog_Rate)
        --mc.mcCntlSetLastError(mInst,"Jog Rate Decrease")
end
-- Jog_Increment
if GetXin("Btn_RS") == 1 and RSState == false then
        --mc.mcCntlSetLastError(mInst,"Jog Increment Changed")
        local CurInc = mc.mcProfileGetInt(mInst,"Preferences","JogIncSet",0) --make a new setting in profile to hold our current inc number
        local SetInc = mc.mcProfileGetDouble(mInst,"Preferences","JogInc"..CurInc,0.00) -- fetch our inc value from the profile JogInc + 2 say = [Preferences][JogInc2]

        CurInc = CurInc + 1 -- increment our CurInc by 1
        if CurInc > 10 then CurInc = 1 end -- if our CurInc is greater than 10 then go back to 1
       
        mc.mcJogSetInc(mInst, mc.X_AXIS, SetInc) -- set inc for axis
        mc.mcJogSetInc(mInst, mc.Y_AXIS, SetInc) -- set inc for axis
        mc.mcJogSetInc(mInst, mc.Z_AXIS, SetInc) -- set inc for axis

        mc.mcProfileWriteInt(mInst,"Preferences","JogIncSet",CurInc) --set our profile to new CurInc

        RSState = true
end
if GetXin("Btn_RS") == 0 and RSState == true then
RSState = false
end
-- X++ DPAD
if GetXin("DPad_DOWN") == 1 and XPState == false and islok == false then
        jog_XP(isinc,1)
XPState = true
        islok = true
end
if GetXin("DPad_DOWN") == 0 and XPState == true then
        jog_XP(isinc,0)
XPState = false
        islok = false
end
-- X++ THUMB STICKS
if GetXin("LTH_Y_Val") <= (DZ*-1.) and TXPState == false and islok == false then
        jog_XP(isinc,1)
TXPState = true
        islok = true
end
if GetXin("LTH_Y_Val") > (DZ*-1.) and TXPState == true then
        jog_XP(isinc,0)
TXPState = false
        islok = false
end
-- X-- DPAD
if GetXin("DPad_UP") == 1 and XNState == false and islok == false then
        jog_XN(isinc,1)
XNState = true
        islok = true
end
if GetXin("DPad_UP") == 0 and XNState == true then
        jog_XN(isinc,0)
XNState = false
        islok = false
end
-- X-- THUMB STICKS
if GetXin("LTH_Y_Val") >= (DZ*1.) and TXNState == false and islok == false then
        jog_XN(isinc,1)
TXNState = true
        islok = true
end
if GetXin("LTH_Y_Val") < (DZ*1.) and TXNState == true then
        jog_XN(isinc,0)
TXNState = false
        islok = false
end
    -- Z++ DPAD
if GetXin("DPad_RIGHT") == 1 and ZPState == false and islok == false then
        jog_ZP(isinc,1)
ZPState = true
        islok = true
end
if GetXin("DPad_RIGHT") == 0 and ZPState == true then
        jog_ZP(isinc,0)
ZPState = false
        islok = false
end
-- Z++ THUMB STICKS
if GetXin("RTH_X_Val") >= (DZ*1.) and TZPState == false and islok == false then
        jog_ZP(isinc,1)
TZPState = true
        islok = true
end
if GetXin("RTH_X_Val") < (DZ*1.) and TZPState == true then
        jog_ZP(isinc,0)
TZPState = false
        islok = false
end
-- Z-- DPAD
if GetXin("DPad_LEFT") == 1 and ZNState == false and islok == false then
        jog_ZN(isinc,1)
ZNState = true
        islok = true
end
if GetXin("DPad_LEFT") == 0 and ZNState == true then
        jog_ZN(isinc,0)
        ZNState = false
        islok = false
end
-- Z-- THUMB STICKS
if GetXin("RTH_X_Val") <= (DZ*-1.) and TZNState == false and islok == false then
        jog_ZN(isinc,1)
TZNState = true
        islok = true
end
if GetXin("RTH_X_Val") > (DZ*-1.) and TZNState == true then
        jog_ZN(isinc,0)
TZNState = false
        islok = false
end
end)
Title: Re: mcX360 Plugin for Lua
Post by: TomL21 on February 05, 2017, 05:32:42 PM
Installed and played with this a little bit today. Nice work getting everything into registers Daz, a very smart approach! Runs smooth!

I see you have registers for the rumble, i played with the numbers a little bit but didn't get any feedback from the controller. Be fun to vibrate on alarm or contact with a probe. Just playing games here ;)
Title: Re: mcX360 Plugin for Lua
Post by: TomL21 on February 06, 2017, 12:15:57 PM
Has anyone tried this in the PMC? I played with it for an hour or two this morning. Found a couple little bugs that I forwarded off to the Artsoft gang, but all in all works pretty good. And easy to implement.

If you haven't used the pmc yet just drop these two files in the mach4>pmc folder. Then in the screen editor on the screen properties one of the options is pmc objects. Check the one you want to use and that is it.

I did this with the following functions:
Left joystick X and Y axis jog
DPad X and Y axis jog
Right joystick Z axis jog
Hold left button to enable continuous jogging (must be held down to jog any axis)
Hold right button to enable incremental jogging (must be held down to jog any axis)
Start cycles the jog increment
B is disable
Y is enable
A is cycle start
X is feed hold
Back is cycle stop

One of the bugs was I couldn't enter negative values. So I had to hack the .lua file to get a dead zone on the sticks. You'll see in the ladder of the pmc file the stick values are compared to 0, in the lua file i changed these zeroes to 30 and -30.
Title: Re: mcX360 Plugin for Lua
Post by: TomL21 on February 06, 2017, 12:27:58 PM
And here is the module I whipped up yesterday. A little more basic, I spent most of the time getting some exponential curve on the joysticks. Left and right buttons enable continuous and incremental jog, respectively. Each needs to be held like a deadman switch.

To use this one just load the module in the screen load script and then call the modulename.PLC() function in the plc script. "modulename" is ofcourse whatever variable you assigned the module to.
Title: Re: mcX360 Plugin for Lua
Post by: francocncprojects on February 07, 2017, 10:41:01 PM
Hi.  Stupid question time.  Is the PMC the Mach4 version of the "Brains" in Mach3?  I haven't played with the PMC in Mach4.  When would you go PMC instead of LUA Script?

Thanks!
Title: Re: mcX360 Plugin for Lua
Post by: TomL21 on February 07, 2017, 11:53:17 PM
Short answer, the PMC is a soft PLC in Mach4 that is programmed in ladder logic, and runs on a continuous loop, like a real PLC. So while it's not a version of Brains from Mach3 it is what Brains should have been.

I think the biggest reason for using the PMC over a lua script is your ability to program. If you know ladder, the PMC would make more sense. There is definitely more power and flexibility in lua, but you have to take the time to learn it. If you just want to get something like this up and running, without dealing with adding timers, potentially locking the gui, etc. than the PMC is the way to go. It is just another handy tool that does seem to be a bit under utilized.

Honestly, I've been doing a lot of work in Mach4 for a few years now, and this is the first time I've used the PMC. I saw this was a good opportunity to check it out and it worked great. I'll probably be using it more in the future now!

EDIT: And I should add that after you build the ladder the PMC Editor generates a Lua script that is run on a loop by Mach4, which is why i was able to hack it to get the values I needed. The input filer issue is being fixed, so that should not be necessary in future versions.
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on February 08, 2017, 02:11:33 AM
Didnt notice the rumblers wasnt  working i will have a look at the code and see why.

DazTheGas
Title: mcX360 Plugin for Lua Updated
Post by: DazTheGas on February 08, 2017, 09:26:20 AM
Ok all sorted, they now work in percentage from 0 to 100%

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

function SetXin(xinput, speed) -- 0 - 100%
    local hreg = mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
    mc.mcRegSetValueLong(hreg, speed)
end

SetXin("LRumble", 100)

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: TomL21 on February 14, 2017, 09:50:45 AM
Hahaha. Nice work Daz.

This is becoming a very useful tool for dev, i'm using it for more and more stuff. Thanks for you hard work on this sir!
Title: Re: mcX360 Plugin for Lua
Post by: patpat on May 22, 2017, 03:13:33 AM
I'm new to Mach4 and cant seem to get the mcX360_LUA.rar plugin to work on my computer. When Mach4 is loading the Xbox Controller light flashes and once the screen comes up the message line at the bottom reads "mcX360 Controller Found and Active" but nothing moves when I work the joypad. I do have xinput9_1_0 dll in system32. Am I missing a step?
Thanks for any help on this!
-Pat
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on May 22, 2017, 06:55:26 AM
mcX360 will not configure itself, its a plugin to enable you to write your own scripts in lua to setup the controller how you want it. Take a look at the videos for examples.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: keinert on June 07, 2017, 02:58:15 PM
Hi Daz, I'm setting up a CNC Mill for the very first time and I'm completely new to this. Purchased Mach4 and I'm trying to get a Play Station 3 style (Snakebite brand) game controller to work.

You mentioned that there are videos that show how to write LUA scripts for the mcX360? Where are these videos?
Thanks,
Kevin.

I found Part 1 at:  https://www.youtube.com/watch?v=zMUFlBt61Ic
Is there a part 2?
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on June 07, 2017, 03:31:59 PM
There didnt seem a point of doing a part 2 as the way it works is for anyone to set it up how they want and part 1 was just a pointer to show how to access the values in the registry.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: keinert on June 07, 2017, 03:41:58 PM
OK, I understand, Thanks Daz, you may have answered these questions already but I'll ask again:
1) where can I get the mcX360_LUA.m4pw and mcX360_LUA.sig files? (Do I need them both to run a generic PS3 style game controller?)
2) will these work if my game controller is not for the Xbox360?

Thanks
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on June 07, 2017, 04:52:05 PM
You can get the files from the first post of this thread http://www.machsupport.com/forum/index.php/topic,33121.0.html,
I have never had a PS3 so I couldnt answer if they will work.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: rtaylor22 on August 12, 2017, 06:55:14 PM
Hi there. I first tried the original plugin but then wanted to try the Lua version so I could customize. I feel pretty confident I followed you pretty well watching your video but for some reason I can't figure out, nothing happens when I press the buttons on the controller. I have the plugin active and can see the buttons are working using the register diagnostics. Maybe I'm missing some other setting? Here's what I have so far in the enabled and hidden Lua panel. Any help would be greatly appreciated.

Code: [Select]
-------------------------------------------------------
-- X_360 Lua by DazTheGas
-------------------------------------------------------

-------------------Declares
local mInst = mc.mcGetInstance()
local mInst = mc.mcGetInstance()
local Xenabled = false
local ESSTate = false

-- X360 Init Panel
    X360_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPostion, wx.wxDefaultSize)
    X360_Timer = wx.wxTimer(X360_Panel)
    X360_Timer:Start(100)
-- EStop
    Estop_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPostion, wx.wxDefaultSize)
    Estop_Timer = wx.wxTimer(Estop_Panel)
EStop_Timer:Start(50)

-------------------Functions

function SetXin(xinput)
    local hreg = mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
    mc.mcRegSetValueLong(hreg, speed)
end

-------------------Event Timers

X360_Panel:Connect(wx.wxEVT_TIMER, function (event)
    if GetXin("LTR_Val") > 150 and Xenabled == 0 then
        EStop_Timer:Start(50)
        Xenabled = 1
        mc.mcCntlSetLastError(mInst,"X_360 Running")
    elseif GetXin("LTR_Val") < 50 and Xenabled == 1 then
        EStop_Timer:Stop()
        Xenabled = 0
        mc.mcCntlSetLastError(mInst,"X_360 Stopped")
    end
end)

EStop_Panel:Connect(wx.wxEVT_TIMER, function (event)
    if GetXin("Btn_B") == 1 and ESSTate == false then
        local Estop = mc.mcSignalGetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY))
        if (EStop == 1) then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 0)
        else
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 1)
        end
        ESState = true
    end
    if GetXin("Btn_B") == 0 and ESState == true then
        ESState = false
    end
end)
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on August 13, 2017, 04:04:52 AM
You seem to be missing the function GetXin

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: rhtuttle on November 06, 2017, 04:45:12 PM
First thanks to all that have contributed to this effort on the xBox controller.

I saw that the plugin was already present in the plugins directory so,


What I did:

Watched the video twice.
Enabled the xBox360 plugin. Restarted Mach4.
From 'Diagnostics->RegFile I can see the Btn_B entry change from 0 to one when I press the xBox B
copied the code from Franco (I am using on a lathe as well):
  https://www.machsupport.com/forum/index.php/topic,33121.30.html (https://www.machsupport.com/forum/index.php/topic,33121.30.html)
reply 31

Created the Lua panel and inserted the code
Saved the changes.
Added the on unload script to check to see if the timer is still running.


From the history button I see:  'mcX360 found and active'.

Pressing the B button nothing happens but shows in the RegFile as being changed.

The GetXin function and the X360_Panel:Connect are being constantly called.(Put a wxMessageBox in the function, thanks for the command line \e tip)
Neither the EStop_panel:Connect nor the Btns-Panel:Connect are ever called.

What did I miss?


TIA

RT
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on November 06, 2017, 05:34:10 PM
One thing I would suggest is to start with the most minimum script to test first

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

local X360_PNL = mcLuaPanelParent;


    function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
    end


X360_timer = wx.wxTimer(X360_PNL)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)

    if GetXin("Btn_A") == "1" then
     -- do something after button A is pressed
    end
   
    if GetXin("Btn_B") == "1" then
     -- do something after button B is pressed
    end
 
    end)

    X360_timer:Start(100)

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: rhtuttle on November 07, 2017, 01:36:43 PM
Daz, sage advice as always.

One correction for anyone following this thread:
    if GetXin("Btn_A") == "1" then
should read:       
    if GetXin("Btn_A") == 1 then
   

After rethinking how I want to use the controller I want to toggle the controller on/off (xEnabled) using the start button.

The following code does what I want but the first press shows 'Pendant Disabled' rather than my expected 'Pendant Enabled'.  Where did I err in my code/logic?

Code: [Select]
-----------------Event Timers
X360_Timer:Start(100)
local StartVal=0
X360_Panel:Connect(wx.wxEVT_TIMER, function (event)
-- Toggle xEnabled when Start Button is depressed
  if GetXin("Btn_START") == 1 then
    if StartVal==1 then --more than one start signal received before release
      return
    end
    if xEnabled == false then
--      Btns_Timer:Start(50)
      mc.mcCntlSetLastError(mInst,"Pendant Enabeled")
      xEnabled=true
    else
--      Btns_Timer:Stop()
      mc.mcCntlSetLastError(mInst,"Pendant Disabeled")
      xEnabled=false
    end
    StartVal=1
  else
    StartVal=0
  end
end)

TIA

RT
Title: Re: mcX360 Plugin for Lua
Post by: rhtuttle on November 07, 2017, 02:08:04 PM
Amazing what a little lunch will do for you. 

I had declared local Xenabled=false and was using xEnabled instead
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on November 07, 2017, 04:42:05 PM
Good spot on the   
Code: [Select]
if GetXin("Btn_A") == "1" then
When I first posted some code the GetXin function returned a string

Code: [Select]
function GetX360(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
end

Then after a while it was more beneficial to return an int, so the function was changed to

Code: [Select]
function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueLong(hreg)
end

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: rhtuttle on November 08, 2017, 12:13:45 PM
I've been chasing my tail with trying to change the jog increment for a full day.  To test it without the xBox so as to limit the possible errors I have done the following:
on the jog panel added a DRO and set it to CurrentJogIncr.  When using the 'Incremental Jog Step' button it correctly updates.
I then added a button and in the left up script added the following code which is what I would be doing in the xBox controller:

Code: [Select]
       local mInst=0
        local CurInc = mc.mcProfileGetInt(mInst,"Preferences","JogIncSet",0) --make a new setting in profile to hold our current inc number
        local SetInc = mc.mcProfileGetDouble(mInst,"Preferences","JogInc"..CurInc,0.00) -- fetch our inc value from the profile JogInc + 2 say = [Preferences][JogInc2]

        CurInc = CurInc + 1 -- increment our CurInc by 1
        if CurInc > 10 then CurInc = 1 end -- if our CurInc is greater than 10 then go back to 1
        
        mc.mcJogSetInc(mInst, mc.X_AXIS, SetInc) -- set inc for axis
        mc.mcJogSetInc(mInst, mc.Y_AXIS, SetInc) -- set inc for axis
        mc.mcJogSetInc(mInst, mc.Z_AXIS, SetInc) -- set inc for axis

        mc.mcProfileWriteInt(mInst,"Preferences","JogIncSet",CurInc) --set our profile to new CurInc
        local s=tostring(CurInc)
        mc.mcCntlSetLastError(0,s)

When I use the button the txtJogInc control updates but the DRO does not.  You can see from the error messages that the CurInc is updating but when the 'Incremental Jog Step' button is used it is not synced to the last change.  There seems to be a flaw in the way the dro works or in the way mach checks to see what the current increment has been changed to...or...in my understanding!

Any help?

RT
Title: Re: mcX360 Plugin for Lua
Post by: rhtuttle on November 10, 2017, 05:51:42 PM
Daz,

Everyone always something more. ;D

In Lee's mach3 plugin the thumb sticks had another capability by depressing them straight down.  Would it be possible to expose that as a button click in your next release of the  plugin?

RT
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on November 11, 2017, 08:38:32 AM
It should already be there as Btn_LTH and Btn_RTH but anyway heres a version compiled with latest sdk

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: rhtuttle on November 11, 2017, 05:53:58 PM
Daz, works a charm!

They may have been in the other one but I have never seen a list of all the codes listed anywhere.

Thanks!

RT
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on November 11, 2017, 10:49:36 PM
go into the diagnostic menu and open the the regfile diagnostic window, you will get a full list from the tree and see live data being populated on the tree.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: ysymidi on January 17, 2018, 08:47:47 AM
Hi Daz,

I'm watching your mcx360 yutube part1 and following your code.

But I got stuck...

When I move any AXIS to negative direction, it stuck.

I can't find what's wrong... I do that same as in the video...

I used the latest plugin you posted (dated, 2017-11-11)

Would you please take a look?
(Script, attached)

And... do you have some full fledged mcx360 script?
For me, scripting from null is too hard.... it would be a great help if I have a complete script that I can modify myself.

Title: Re: mcX360 Plugin for Lua
Post by: divana4u on March 17, 2018, 08:33:24 PM
Just curious if any game controller can work, or is an X-Box 360 controller necessary? Wired/wireless? I'm trying to help a friend get his Bridgeport NC mill running again, and I'm lousy with usb generic game controllers, but they don't seem to show up when plugged in. Any help would be appreciated
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on March 18, 2018, 04:19:08 AM
This was made for an XBox controller only.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: hekav on May 11, 2018, 05:53:01 AM
Hi DazTheGas,

I'm using your plugin with TomL21's  XboxPMC and it works. Since I can view the Regs within Diagnostics->RegFile I
don't need the debug output in mach4's last error. Is there a way to turn off the debug messages ?

Helmut
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on May 11, 2018, 12:37:30 PM
Can you be a bit more precise on what messages?

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Klaus on July 10, 2018, 07:17:32 PM
So no script or plugin exist for Mach4 which have a configuration screen like on the Mach3 plugin?
Title: Re: mcX360 Plugin for Lua
Post by: culmy on August 08, 2018, 03:21:29 PM
I have loaded the latest plugin and i can get everything to work except Button A Cycle Start
Rht Shoulder Button A works fine and I can see button A change state in the registry window but it does not perform a cycle start

Typed following into MDI
G1 F50 X1
G1 F50 X0

Press A and nothing
actually press Cycle Start and code exicutes with machine motion

Is there actually a way to edit/see the code in your plugin, i am new to MACH 4 and LUA but have written in C# before i retired
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on August 08, 2018, 04:09:52 PM
The example for the A does not run an mdi, a cycle start in general will start a loaded gcode file, you would need to change to something like

if GetXin("Btn_A") == 1 and A_Btn == false then
      scr.ExecMdi('mdi1')
      A_Btn = true
end

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: culmy on August 08, 2018, 05:55:24 PM
Do you have the code you used in the part 1 video for the xbox controller and is there a part 2 i could not find it
Title: Re: mcX360 Plugin for Lua
Post by: culmy on August 09, 2018, 05:08:54 PM
Below code only works if i disable (Comment out ) the EStop_Timer: Start(50) line
well at least it shows the running/stop stat of the controller
if i leave the timer stop line un commented then it works the first press of the trigger but soon as i release it then nothing anymore
got to be something stupid with the timer definition?????



-- Charley Ulmschneider August 9 2018

-- Declirations
local mInst = mc.mcGetInstance()
local Xenabled = false
local ESState = false

--X360 Init Panel
   X360_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
   X360_Timer = wx.wxTimer(X360_Panel)
   X360_Timer:Start(100)
   
--EStop Init Panel
   EStop_Panel = wx.wxPanel(wx.NULL, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
   EStop_Timer = wx.wxTimer(EStop_Panel)
   
   
   -- Functions
function GetXin(xinput)
      local hreg=mc.mcRegGetHandle(mInst, string.format("mcX360_LUA/%s", xinput))
      return mc.mcRegGetValueLong(hreg)
end
      
-- Event Timers

X360_Panel:Connect(wx.wxEVT_TIMER, function (event)
      if GetXin("LTR_Val")>150 and Xenabled == false then
         
         EStop_Timer:start(50)
         Xenabled=true
         mc.mcCntlSetLastError(mInst,"x_360 Running")
         
      elseif GetXin("LTR_Val")<50 and Xenabled==true then
         
         EStop_Timer:stop(0)
         Xenabled=false
         mc.mcCntlSetLastError(mInst, "x_360 Stopped")
      end
   end)


EStop_Panel:Connect(wx.wxEVT_TIMER, function (event)
      if GetXin("Btn_B") == 1  and ESState == false then
         local EStop = mc.mcSignalGetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY))
         if (EStop ==1) then
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 0)
         else
            mc.mcSignalSetState (mc.mcSignalGetHandle (mInst, mc.ISIG_EMERGENCY), 1)
         end
         ESState = true
      end
      if GetXin("Btn_B") == 0 and ESState == true then
         ESState = false
      end
   end)
Title: Re: mcX360 Plugin for Lua
Post by: culmy on August 09, 2018, 05:11:24 PM
NEVER MIND
Stupid mistake

start and stop instead of Start ans Stop
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on August 10, 2018, 03:35:08 AM
It gets us all at some time Lua is case sensitive

DaTheGas
Title: Re: mcX360 Plugin for Lua
Post by: culmy on August 10, 2018, 10:01:39 AM
another dumb question
where does the code actually get stored, file location. i know i can do a save as and put a copy were i want for safe keeping
but when i create a pannel and add code were is that kept
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on August 10, 2018, 02:57:58 PM
The code is embed into your screenset and is only editable through the screen editor.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: culmy on August 14, 2018, 03:43:29 PM
is there a  way to check the status of a button on the Mach 4 form  ie the Enable/Disable button
i am using the A button on the Xbox controller to toggle the Enable/Disable button but had to resort to a flag (local ToggleState) to see if it has been enabled  by my code
only problem with doing it this way is it assumes that it is in the disabled state to start off


using
mc.mcCntEnable(mIst, 1) to go from disable to enable
and
mc.mcCntEnable(mIst, 0) to go from enabled to disable
it would be nice to test the current state first before setting it to another state
Title: Re: mcX360 Plugin for Lua
Post by: culmy on August 14, 2018, 06:05:04 PM
Figured this one out also.  :)

what is the best way to do a Velocity move in X, Y, and Z axis
currently i am using

mc.mcVelocityStart(mInst, Axis, direction)

but speed is from the Slider on the Mach4 panel and the Jog DRO

So i have to keep changing those values as RTHY and RTHX values change from 0-90 (i have a dead spot -30 to +30
while this works it is messy
is there a better way to do a velocity move
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on August 16, 2018, 07:29:03 AM
Quote
is there a  way to check the status of a button on the Mach 4 form  ie the Enable/Disable button

There are some good examples in the Screen Load Script of the stock wx4 screenset, take a look at the Spindle functions.

I use

Code: [Select]
-- Start Button
if GetXin("Btn_START") == 1 and Start_Btn == false then
        local mcCntlEnable = mc.mcSignalGetState (mc.mcSignalGetHandle (mInst, mc.OSIG_MACHINE_ENABLED))
        if mcCntlEnable == 1 then
            mc.mcCntlEnable(mInst, 0)
        else
            mc.mcCntlEnable(mInst, 1)
        end
Start_Btn = true
    end
    if GetXin("Btn_START") == 0 and Start_Btn == true then
--Release Command if any
Start_Btn = false
    end

Quote
what is the best way to do a Velocity move in X, Y, and Z axis
To set the velocity you just need to take a value from the thumbstic and set mc.mcJogSetRate as a percentage so the further you push the stick the faster it moves.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: mhd on September 19, 2018, 03:28:04 PM
This was made for an XBox controller only.

DazTheGas

First of all, thank you for the good video
You say that the lua you but it only for XBox 360 but I hook my the PS3 controller and check the Register Diagnostics /mcX360_LAU and it gives me a value!!

The problem in my case that when I try to code the software help me with the function. because I'm not that good in coding and I'm learning from your video.

Like when I type (Local or mc.mcRegGetHandle) it the software help me find them

but in case of (mc.mcGetInstance(), Start) there is NO option to select from.


Did I do something wrong?


Best regard
shrooqi

Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on September 19, 2018, 04:52:24 PM
It would be easier to post what code you are using.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: mhd on September 20, 2018, 05:31:35 AM
as I motion that the Register Diagnostics /mcX360_LUA give me a value when I press any bottom

I follow your video step by step and this is the code


 ------- Declaer local---------------
local mInst = mc.mcGetInstance()
local Xenable= 0

-------------- Ps3 Init Panel------------

Ps3_Penal = wx.wxPanel( wx.NULL , wx.wxID_ANY , wx.wxDefaultPosition , wx.wxDefaultSize)
Ps3_Timer = wx.wxTimer (Ps3_Penal)
Ps3_Timer : Start(100)

----------- Function----------------

function GetXin(Xinput)
   local hreg = mc.mcRegGetHandle(mInst , string.format("mcX360_LUA/%s") , Xinput)
   return mc.mcRegGetHandle(hreg)
end

------------Event Timers-------------
Ps3_Penal : Connect (wx.wxEVT_TIMER , function(event)
      
      if GetXin("LTR_Val") > 150 and Xenable == 0 then
         Xenable = 1
         mc.mcCntlSetLastError(mInst , "Ps3 Running")
         
      elseif GetXin ("LTR_Val") < 50 and Xenable == 1 then
         Xenable =0
         mc.mcCntlSetLastError(mInst , "Ps3 Stopped")
         
      end   
      
      
   end   )
Title: Re: mcX360 Plugin for Lua
Post by: mhd on September 22, 2018, 02:37:40 PM
It would be easier to post what code you are using.

DazTheGas



Posts: 7


   

Re: mcX360 Plugin for Lua
« Reply #77 on: September 20, 2018, 04:31:35 AM »
 

as I motion that the Register Diagnostics /mcX360_LUA give me a value when I press any bottom

I follow your video step by step and this is the code


 ------- Declaer local---------------
local mInst = mc.mcGetInstance()
local Xenable= 0

-------------- Ps3 Init Panel------------

Ps3_Penal = wx.wxPanel( wx.NULL , wx.wxID_ANY , wx.wxDefaultPosition , wx.wxDefaultSize)
Ps3_Timer = wx.wxTimer (Ps3_Penal)
Ps3_Timer : Start(100)

----------- Function----------------

function GetXin(Xinput)
   local hreg = mc.mcRegGetHandle(mInst , string.format("mcX360_LUA/%s") , Xinput)
   return mc.mcRegGetHandle(hreg)
end

------------Event Timers-------------
Ps3_Penal : Connect (wx.wxEVT_TIMER , function(event)
     
      if GetXin("LTR_Val") > 150 and Xenable == 0 then
         Xenable = 1
         mc.mcCntlSetLastError(mInst , "Ps3 Running")
         
      elseif GetXin ("LTR_Val") < 50 and Xenable == 1 then
         Xenable =0
         mc.mcCntlSetLastError(mInst , "Ps3 Stopped")
         
      end   
     
     
   end   )
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on September 25, 2018, 09:28:12 AM
Your GetXin function is incorrect

your code
Code: [Select]
function GetXin(Xinput)
   local hreg = mc.mcRegGetHandle(mInst , string.format("mcX360_LUA/%s") , Xinput)
   return mc.mcRegGetHandle(hreg) -- This line is wrong
end

code should be
Code: [Select]
function GetXin(xinput)
local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
return mc.mcRegGetValue(hreg) -- or return mc.mcRegGetValueLong(hreg)
end

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: mhd on September 25, 2018, 04:22:55 PM
Your GetXin function is incorrect

your code
Code: [Select]
function GetXin(Xinput)
   local hreg = mc.mcRegGetHandle(mInst , string.format("mcX360_LUA/%s") , Xinput)
   return mc.mcRegGetHandle(hreg) -- This line is wrong
end

code should be
Code: [Select]
function GetXin(xinput)
local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
return mc.mcRegGetValue(hreg) -- or return mc.mcRegGetValueLong(hreg)
end

DazTheGas



Dear Sir


I change it and still, I can not get the feed in the stateusList



------- Declaer local---------------
local mInst = mc.mcGetInstance()
local Xenable= 0

-------------- Ps3 Init Panel------------

Ps3_Penal = wx.wxPanel( wx.NULL , wx.wxID_ANY , wx.wxDefaultPosition , wx.wxDefaultSize)
Ps3_Timer = wx.wxTimer (Ps3_Penal)
Ps3_Timer : Start(100)

----------- Fauntion----------------

function GetXin(xinput)
        local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
        return mc.mcRegGetValueLong(hreg)
    end

------------Event Timers-------------
Ps3_Penal : Connect (wx.wxEVT_TIMER , function(event)
      
      if GetXin("LTR_Val") > 150 and Xenable == 0 then
         Xenable = 1
         mc.mcCntlSetLastError(mInst , "Ps3 Running")
         
      elseif GetXin ("LTR_Val") < 50 and Xenable == 1 then
         Xenable =0
         mc.mcCntlSetLastError(mInst , "Ps3 Stopped")
         
      end   
      
      
   end   )
Title: Re: mcX360 Plugin for Lua
Post by: mhd on September 26, 2018, 10:07:43 AM
Your GetXin function is incorrect

your code
Code: [Select]
function GetXin(Xinput)
   local hreg = mc.mcRegGetHandle(mInst , string.format("mcX360_LUA/%s") , Xinput)
   return mc.mcRegGetHandle(hreg) -- This line is wrong
end

code should be
Code: [Select]
function GetXin(xinput)
local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
return mc.mcRegGetValue(hreg) -- or return mc.mcRegGetValueLong(hreg)
end

DazTheGas



Dear Sir


I change it and still, I can not get the feed in the stateusList



------- Declaer local---------------
local mInst = mc.mcGetInstance()
local Xenable= 0

-------------- Ps3 Init Panel------------

Ps3_Penal = wx.wxPanel( wx.NULL , wx.wxID_ANY , wx.wxDefaultPosition , wx.wxDefaultSize)
Ps3_Timer = wx.wxTimer (Ps3_Penal)
Ps3_Timer : Start(100)

----------- Fauntion----------------

function GetXin(xinput)
        local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
        return mc.mcRegGetValueLong(hreg)
    end

------------Event Timers-------------
Ps3_Penal : Connect (wx.wxEVT_TIMER , function(event)
      
      if GetXin("LTR_Val") > 150 and Xenable == 0 then
         Xenable = 1
         mc.mcCntlSetLastError(mInst , "Ps3 Running")
         
      elseif GetXin ("LTR_Val") < 50 and Xenable == 1 then
         Xenable =0
         mc.mcCntlSetLastError(mInst , "Ps3 Stopped")
         
      end   
      
      
   end   )




Sir, I copy this code for your previous and I insert what you explained from the video on it. It works know but I don't understand why?



local inst = mc.mcGetInstance()
local Xenable=0

local X360_PNL = mcLuaPanelParent;


    function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
    end


   X360_timer = wx.wxTimer(X360_PNL)
    X360_timer:Start(100)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)
   
    if GetXin("LTR_Val") > "150" and Xenable==0 then
      Xenable=1
    mc.mcCntlSetLastError(inst,"PS3 Running")
    end
   
    if GetXin("LTR_Val") < "1"  and Xenable==1 then
      Xenable=0
    mc.mcCntlSetLastError(inst,"PS3 Stopped")
    end
 
    end)
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on September 26, 2018, 12:30:13 PM
Glad you have it working, the problem I have is I can only test with an xbox controller as I dont have PS3 controller and I know it works. So as long as theres no typo`s then all should be good.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: mhd on September 26, 2018, 01:53:36 PM
Dear Sir ,

I think the problem was in the (( if GetXin("LTR_Val") > "150" )) the number should be in " ". I'm not that expert in coding but the fixed my problem.

Can I ask you for a favour. if you can do video in PMC it will be helpful for those who have a basic in code.

Best regard
shrooqi
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on September 27, 2018, 04:55:03 PM
I know absolutely nothing about the PMC/Ladder logic, ive always preferred to code what i need. Take a look at this example from an earlier post.

http://www.machsupport.com/forum/index.php/topic,33121.msg236608.html#msg236608

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: lenne0815 on December 23, 2018, 05:59:25 PM
Hey Daz, thank you for your work ! Just a quick question, does the controller script work for slaved axes aswell ?
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on December 24, 2018, 06:40:10 PM
Hey Daz, thank you for your work ! Just a quick question, does the controller script work for slaved axes aswell ?

Yes it does, you are moving an axis not individual motors so if you have a Y axis that consists of say motors 1 and 4 then both of these will move if you command the Y axis to move.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: cjlocey on April 24, 2020, 08:50:36 PM
Daz and others, you are amazing and my head is spinning.  I have enough on my plate and am not the biggest fan of reinventing the wheel so I would like to know what it would take to get or buy a copy of the script to allow me to use a xbox 360 controller with Mach4?
I would much rather spend my time using my CNC rather than coding in MACH4
Curt
Title: Re: mcX360 Plugin for Lua
Post by: cjlocey on May 10, 2020, 09:56:28 PM
TomL21
Are you or anyone reading this thread willing to help me with getting Mach4 set up to use a X360 controller?
Curt
Has anyone tried this in the PMC? I played with it for an hour or two this morning. Found a couple little bugs that I forwarded off to the Artsoft gang, but all in all works pretty good. And easy to implement.

If you haven't used the pmc yet just drop these two files in the mach4>pmc folder. Then in the screen editor on the screen properties one of the options is pmc objects. Check the one you want to use and that is it.

I did this with the following functions:
Left joystick X and Y axis jog
DPad X and Y axis jog
Right joystick Z axis jog
Hold left button to enable continuous jogging (must be held down to jog any axis)
Hold right button to enable incremental jogging (must be held down to jog any axis)
Start cycles the jog increment
B is disable
Y is enable
A is cycle start
X is feed hold
Back is cycle stop

One of the bugs was I couldn't enter negative values. So I had to hack the .lua file to get a dead zone on the sticks. You'll see in the ladder of the pmc file the stick values are compared to 0, in the lua file i changed these zeroes to 30 and -30.
Title: Re: mcX360 Plugin for Lua
Post by: wreckermech on May 30, 2020, 04:54:37 PM
TomL21
Are you or anyone reading this thread willing to help me with getting Mach4 set up to use a X360 controller?
Curt

I recently downloaded Mach4 and wanted to use my Xbox controller with it.  A big thanks to DazTheGas on his videos and code snippets.  My wife was able to put together Daz's code with a couple of tweaks to get it working the way I wanted.

I've noticed a lot of people asking for the same thing I was looking for:  the LUA code already typed up and ready to dump into Mach4.  So my wife and I put together a zip file (attached) with some pre-configured code and quick guides.  The guides will give you an idea of how to put the code into Mach4, and will show you which buttons are configured for the Xbox controller depending on which of the scripts you decide to install. 

So keep watching Daz since he's really an expert on the whole Mach4/Lua code topic, but hopefully, someone else will find the pre-configured code helpful - especially if you aren't a coder.
Title: Re: mcX360 Plugin for Lua
Post by: cjlocey on May 30, 2020, 11:29:21 PM
Thanks.  I will review this tomorrow and see if I can get it to work.
Title: Re: mcX360 Plugin for Lua
Post by: MikeRD03 on August 03, 2020, 11:09:14 AM
As a new Mach4 user I manged to get the x360 up and running. But there one little odd thing. My x360 controller is rumbeling (the force feedback motor) every second for a blink of an eye. Over and over again. This starts when the Mach4 is starting and the plugins are loading. I happens even without any LUA script installed. What can be wrong here?

Mike
Title: Re: mcX360 Plugin for Lua
Post by: squirrel_41 on October 08, 2020, 12:58:59 PM
scr.SetProperty("sliderJogRate", "Value","00.1")

Hi On the mach4 jogging tab there is a slider to adjust the jog rate and a value box that you can enter the jog rate.
I can enter .1 in the Value box and it works however if i set this value with
scr.SetProperty("droJogRate", .1) its set the value box to 0.0 and jogging stops working as expected
I have change the properties on both the slider and value box but it still will not accept 0.1
any ideas how I can implement very slow jog rates.
Cheers!
 
 
Title: Re: mcX360 Plugin for Lua
Post by: Ustapeter on February 22, 2021, 10:37:47 PM
What changed in the plugin? Release notes? :P

Im just finishing up my script, now everything is done except probing (waiting for andy @ warp9)

Title: Re: mcX360 Plugin for Lua
Post by: Ustapeter on February 22, 2021, 10:39:36 PM
I have got my xbox running and only thing i would like to add is probing, at least z probing. do you know if there is a way to access
the functionality that is already there in the 'Touch' panel? It looks like you're the only one who was interested in implementing it?
Title: Re: mcX360 Plugin for Lua
Post by: lenne0815 on February 24, 2021, 08:39:54 AM
Theres a new "official" plugin now with the latest mach4 version, maybe you can select probing there ?
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on February 24, 2021, 09:55:32 AM
There is a couple of ways to do this, if using the lua version you can assign a standard gcode probe ie G91 G31 Z-5 F30 to a button. if using the same way as i used to then you can use something like

Code: [Select]

if GetXin("Btn_X") == 1 and X_Btn == false then
    mc.mcCntlGcodeExecuteWait(inst,'G91 G31 Z-5 F30')
    X_Btn = true
end

If you are using the new plugin that is bundled with mach 4 then you can assign a button to 1 of 6 GCodes.

Example: if you want to assign  button x to gcode1 and want this to probe z then from the xbox config screen assign x to gcode1, then open your profile/machine.ini and edit the section XBOX_0 and add Gcode1=G91 G31 Z-5 F30

Hope this helps

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Ustapeter on February 24, 2021, 11:41:51 AM
Thanks for the quick reply, i will try it tonight. If i want to run a larger script (thinking of implementing something like your example on ess probing with safeguards, but i will write it for z,y,x corner for setting work origin would it work the same way or maybe put it in a module?  I'm still trying to get the big picture of how mach4 works internally.
Title: Re: mcX360 Plugin for Lua
Post by: pmageau on May 20, 2021, 11:48:47 AM
Is there any documentation for the x360 plug-in for mach4? Specifically can I switch between incremental and continuous jog mode somehow ?
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on June 04, 2022, 10:02:35 AM
Heres a different kind of plugin for you using an X360 controller that populates registry values so you can write your own scripts for it, if you have the registry plugin loaded you can see the values.

I will try and get some video`s on usage as soon as possible but is straight forward, create a lua panel from the screen editor and make sure you select that its hidden and in the code add as an example

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

local X360_PNL = mcLuaPanelParent;


    function GetXin(xinput)
    local hreg = mc.mcRegGetHandle(inst, string.format("mcX360_LUA/%s", xinput))
    return mc.mcRegGetValueString(hreg)
    end


X360_timer = wx.wxTimer(X360_PNL)

    X360_PNL:Connect(wx.wxEVT_TIMER,   function (event)

    if GetXin("Btn_A") == "1" then
     -- do something after button A is pressed
    end
   
    if GetXin("Btn_B") == "1" then
     -- do something after button B is pressed
    end
 
    end)

    X360_timer:Start(100)

You should get the gist ;-)

DazTheGas

When you say registry values, do you mean the actual OS registry or an internal registry for Mach4?

thanks
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on June 05, 2022, 12:21:55 PM
Also did you pick the XBOX360 controller just because you had one or was it a technical decision e.g. easier to interface?

thanks
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on June 05, 2022, 12:30:59 PM
These are Mach4 specific registers

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on June 05, 2022, 12:52:07 PM
These are Mach4 specific registers

DazTheGas
Ah - thank you
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on June 07, 2022, 03:12:55 PM
So how are you gathering the input form the USB device? In C++, C# etc etc I have used DirectX for this, is there a USB library including in the Mach4 CoreAPI? as i cannot seem to find anything

thanks 
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on June 17, 2022, 10:02:39 AM
I have a problem with the XBOX 360 LUA plugin. Whenever I install it to Mach4, the controller randomly (but consistently) rumbles.

It doesn't stop, just jumbles all the time randomly.

Any ideas? It is a Genuine MS controller and is picked up by the plugin successfully.

thanks

PS this is before I add any LUA code for it.
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on July 28, 2022, 11:13:39 AM
Treid this on a few machines, basically the Plugin doesn't seem to work properly on Win7.

Could @dazthegaz have a look at this please?

thanks
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on July 28, 2022, 11:43:59 AM
This Pugin is no longer maintained as it is now included as a plugin with mach4 and I use a shuttle now.

Sorry I cannot help at this time, but you never know what the future holds. why not use the plugin and for any specific things you want to do use the GCode commands to trigger a lua written M code.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on July 28, 2022, 12:07:52 PM
Unfortunately the standard Plugin does not allow any change on the D-Pad or Analogue stick movements. The LUA version was perfect (how they should all be written) but like I say just buzzes/vibrates all the time in Win7.

I can't seem to get acess to the Plugin core files etc or I would have had look at it myself.

thanks anyway.
Title: Re: mcX360 Plugin for Lua
Post by: DazTheGas on July 28, 2022, 03:36:29 PM
Here is a quick fix.

1. Undo the 7 screws underneath the controller, one is hidden under the bottom of the label.
2. Disconect the two motors (They are just pluged in).
3. Put back together and enjoy.

I have just tested this and everything in the registry is still working.

DazTheGas
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on July 29, 2022, 11:51:15 AM
Smashing idea Daz - thank you!!!!  ;)
Title: Re: mcX360 Plugin for Lua
Post by: Rimmel on July 30, 2022, 06:16:58 AM
and I use a shuttle now.
The Shuttle Pro V2??? Did you write an LUA version for that as well or do you use the standard one supplied with the program?