Here is the updated script...
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)