Hello Guest it is January 26, 2020, 09:10:12 PM

Author Topic: Lua success with external buttons to Mach4  (Read 4500 times)

0 Members and 1 Guest are viewing this topic.

Offline thosj

*
  •  376 376
    • View Profile
Re: Lua success with external buttons to Mach4
« Reply #20 on: March 25, 2017, 11:43:25 AM »
Got it, sheesh, finally!  ;D

Thanks to Wallerwang (?), and I don't know why Craig's code didn't work, it worked mathematically!!

Here's the code that actually works for me, for the cutters/pasters among us! You'd only need to change the PoKeys Device and PoKeys Analog Input to yours.

This is my Speed Override, 50% to 150% to match Mach slider

Code: [Select]
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set SRO value
function SetSRO(analog)
    local percent = analog/1*1.0+0.5 --calculate percentage
    local inst = mc.mcGetInstance()
    mc.mcSpindleSetOverride(inst, percent)
end

--Main body -
local device = "PoKeys57" --Change this to the name of your PoKeys device
local analogPin = "44" --Analog input pin number on PoKeys

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetSRO(analogVal) -- Set SRO value in %

This is my Feed Override, 0% to 250% to also match the Mach slider.

Code: [Select]
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set FRO value
function SetFRO(analog)
    local percent = analog/1*250 --calculate percentage from 0% to 250%
    local inst = mc.mcGetInstance()
    mc.mcCntlSetFRO(inst, percent)
end

--Main
local device = "PoKeys57" --Change this to the name of your PoKeys device
local analogPin = "45" --Analog input pin number

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetFRO(analogVal) -- Set FRO value in %

Both of these are LUA files, stored, in my case, in the Mach4 Industrial directory and run with code in the PLC script, which runs the files every 50ms or whatever that interval is.

Here's the PLC script lines, way near the end.

Code: [Select]
-------------------------------------------------
--My Stuff!!
-------------------------------------------------
--Runs the PoKeys FRO and SRO lua files as shown!

dofile("C:\\Mach4Industrial\\PoKeys_analog_FRO.lua")
dofile("C:\\Mach4Industrial\\PoKeys_analog_SRO.lua")

--------------------------------------------------

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

Now, being old fashioned, I'd like the Rapid Override, 0% to 100%, to follow the Feed Override which is 0% to 250% with the one PoKeys Analog Input pot. Wonder if I can have two LUA files running, reading the same PoKeys Analog Input and moving two sliders? Or might it be better to have one file reading the input and updating two things? I had Mach3 doing this but don't remember how. Must have written Brains, eh?

It's always somethin', ain't it?  :o

Tom



« Last Edit: March 25, 2017, 11:46:55 AM by thosj »