Mach 4. I need to write a couple of simple macros to turn outputs on and off, a bit of a sleep time, with an input to boot. I have seen two types of code, one of them I know works fine I have put the script on a button after adding in some additional lines. But this one I came across and would be keen to know what its for, what it means as it has a hParam section which I do not understand.
So this is the one I am asking about:
function m162_setOutput(number)
local Osig = mc.OSIG_OUTPUT0
Osig = (Osig + number)
local hReg = mc.mcSignalGetHandle(mc.mcGetInstance(), Osig)
if( hReg ~= nil ) then
mc.mcSignalSetState(hReg, true) --Turn the output on
end
end
function m162(hParam) --The param is a P value called from Gcode. M162P3 for example.
if (hParam ~= nil) then
local inst = mc.mcGetInstance()
local varP = 0
local flagP, rc = mc.mcCntlGetLocalVarFlag(inst, hParam, mc.SV_P)
if (flagP == 1) then --Check that the flag has been set so we do not get an unexpected value for mc.SV_P
varP = mc.mcCntlGetLocalVar(inst, hParam, mc.SV_P)
m162_setOutput(varP)
end
end
end
if (mc.mcInEditor() == 1) then
m162_setOutput(1)
end
And this is the one I would normally use:
----------Variables----------
local inst = mc.mcGetInstance() --Get the instance of Mach4
local brake = mc.OSIG_OUTPUT1 --Save the name of the Signal (as stored inside of Mach4)
local ActivateSignalTime = 5000 --Time in milliseconds that we want the signal to be active.
local hReg = 0 --The handle to the signal
local rc = 0
hSig, rc = mc.mcSignalGetHandle(inst, brake)
if (rc ~= 0) then --There was error
mc.mcCntlSetLastError(inst, "There was an error")
else
rc = mc.mcSignalSetState(hSig, 1) --Turn the output on
if (rc ~= 0) then --There was error
mc.mcCntlSetLastError(inst, "There was an error")
else
wx.wxMilliSleep(ActivateSignalTime) --Sleep for the commanded time (so that the output stays on as long as we want).
rc = mc.mcSignalSetState(hSig, 0) --Turn the output off
if (rc ~= 0) then --There was error
mc.mcCntlSetLastError(inst, "There was an error")
end
end
end
I need to write a brake on/off M10/M11 macro set which is the reason for this. Like I said I have managed to get the multiple outputs into a button script. If its a Macro do I need to do anything much differently except for name it and put it into the Macros folder? Btw I use ChatGpt and managed to write (with a lot of back and forth) a turret change for my lathe in Mach 3