To handle the Thumbsticks you can check out my code. See the function HandleThumbInput...
Here is my current script:
--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)