Hello Guest it is April 25, 2024, 05:41:16 AM

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

0 Members and 2 Guests are viewing this topic.

Offline Bx3mE

*
  •  68 68
    • View Profile
Re: mcX360 Plugin for Lua
« Reply #20 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
Re: mcX360 Plugin for Lua
« Reply #21 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;

Offline DazTheGas

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

Offline DazTheGas

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

Offline DazTheGas

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

Offline Bx3mE

*
  •  68 68
    • View Profile
Re: mcX360 Plugin for Lua
« Reply #26 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)


Re: mcX360 Plugin for Lua
« Reply #27 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

Offline DazTheGas

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