Totally new to me anything in a macro thats more than just a few commands. Ive managed to get this one together but it will be full of mistakes. I have been on this for a while now and its time to ask. Aim is to have a M10 and M11 for a hydraulic brake on a 4th Axis I have built. All it needs to do is:
M10
Switch on hyd pump
Make sure the "brake off" solenoid is closed or close it if its open
Open the "brake on" solenoid
Wait for an input (NO signal) closed on brake extension.
Close "brake on" solenoid
Turn off hyd pump.
M11 will be the same but with the IOs swapped around. I managed to get a macro without the inputs working. But only if the "Brake Off" button on my screen is toggled. I guess someone familia with LUA will say - "thats where you went wrong" Hey anyways would someone please give me some advice on how to change this macro to function as it should?
If I can get this working, I would also like to add in a section that checks pump pressure prior to proceeding. And if there is no pressure, via another input, an error comes up. I think I know what I need to code for this, I just want to get the basic version working. Would appreciate some help please. I can build things, coding is not my strength. To be brutally honest I have no idea what half of my code does. Well, the hSig thing for a start.
function m10()
local inst = mc.mcGetInstance() -- Get Mach 4 Instance
local hSig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4) -- Set the handle for output4 pump on output
local state, rc = mc.mcSignalGetState(hSig) -- Set the state of the handle
if state == 0 then -- Not sure why but this seems to work to make sure the pump is on.
mc.mcSignalSetState(hSig, 1) -- Set the pump on
else
mc.mcSignalSetState(hSig, 1) -- Regardless of state make sure the pump is on.
end
local hSig6, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6) -- Set the handle for Output 6 which is the brake release solenoid
local state6, rc = mc.mcSignalGetState(hSig6) -- State
if state6 == 1 then
mc.mcSignalSetState(hSig6, 0) -- Ensure the solenoid is closed or the brake wont work
else
mc.mcSignalSetState(hSig6, 0) -- Again
end
local hSig2, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5) -- Set the handle for output 5 which is the brake on solenoid. On = open
local state2, rc = mc.mcSignalGetState(hSig2) -- State
if state2 == 0 then
mc.mcSignalSetState(hSig2, 1) --If its off set it on.
end
local hSig3, rc = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT4) -- Set the handle for input 4 which is the brake on micro switch. Made = brake on.
local state3, rc = mc.mcSignalGetState(hSig3)
if state3 == 1 then
mc.mcSignalSetState(hSig2, 0) -- Close the brake on solenoid which will lock the brake.
mc.mcSignalSetState(hSig, 0) -- Turn the pump off
Ok well I sorted it anyway. Not sure why it wouldn't work, and now it does. Code below for anyone although I am pretty new to this so whilst it might work for me, it might not for someone else. Also to note the M10 needs a couple of the outputs changed and then its a M11.
My next step now the basic version is working is to add in some safety locks and parameters like HYD Pressure Alarm etc.
function m10()
local inst = mc.mcGetInstance() -- Get Mach 4 Instance
local hSig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4) -- Set the handle for output4 pump on output
local hSig1, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5) -- Set the handle for output5 brake on solenoid
local hSig2, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6) -- Set the handle for output6 brake release solenoid
-- Check if signal handles were obtained successfully
if not hSig or not hSig1 or not hSig2 then
mc.mcCntlSetLastError(inst, "There was a handle error")
return
end
-- Set initial states
mc.mcSignalSetState(hSig, 1)
mc.mcSignalSetState(hSig2, 0)
mc.mcSignalSetState(hSig1, 1)
rc = mc.mcSignalWait(inst, mc.ISIG_INPUT4, 1, 5) --Wait 5 seconds for input 21 to become active
if (rc~= 0) then --Check our return call
mc.mcCntlSetLastError(inst, "There was a brake error")
else
mc.mcSignalSetState(hSig, 0)
mc.mcSignalSetState(hSig1, 0)
end
Hi,
yes that last lot of code looks very much better. In particular you have to be very careful with Lua that a variable remains 'in scope'.
For instance your local variables hSig, hSig1 and hSig2 must remain in scope throughout the function so that they can be used near the end of the function.
I still get confused about the rules of when a local variable is in scope and so I make it a rule to EVERY time I need a memory handle I explicitly get it just prior to use,
it results in fewer issues.
You are also right to test the intermediate results and offer some sort of error signaling.