Hello Guest it is November 09, 2024, 09:14:39 AM

Author Topic: mcX360 Plugin for Lua  (Read 69884 times)

0 Members and 1 Guest are viewing this topic.

Offline DazTheGas

*
  •  778 778
  • DazTheGas
mcX360 Plugin for Lua
« 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
New For 2022 - Instagram: dazthegas

Offline Bx3mE

*
  •  68 68
Re: mcX360 Plugin for Lua
« Reply #1 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)
« Last Edit: November 26, 2016, 07:11:57 PM by Bx3mE »

Offline Bx3mE

*
  •  68 68
Re: mcX360 Plugin for Lua
« Reply #2 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)
« Last Edit: November 27, 2016, 03:19:20 PM by Bx3mE »

Offline DazTheGas

*
  •  778 778
  • DazTheGas
Re: mcX360 Plugin for Lua
« Reply #3 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
New For 2022 - Instagram: dazthegas

Offline Bx3mE

*
  •  68 68
Re: mcX360 Plugin for Lua
« Reply #4 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); ????

Offline DazTheGas

*
  •  778 778
  • DazTheGas
Re: mcX360 Plugin for Lua
« Reply #5 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
New For 2022 - Instagram: dazthegas

Offline Bx3mE

*
  •  68 68
Re: mcX360 Plugin for Lua
« Reply #6 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
« Last Edit: November 27, 2016, 09:38:43 PM by Bx3mE »

Offline Bx3mE

*
  •  68 68
Re: mcX360 Plugin for Lua
« Reply #7 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)
« Last Edit: November 27, 2016, 09:45:55 PM by Bx3mE »

Offline Bx3mE

*
  •  68 68
Re: mcX360 Plugin for Lua
« Reply #8 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?

Offline Bx3mE

*
  •  68 68
Re: mcX360 Plugin for Lua
« Reply #9 on: November 28, 2016, 04:16:53 AM »
Ok... I added a sleep and it does the trick...