Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: Bill_O on April 18, 2019, 11:44:44 AM
-
I need to have a loop in a macro that waits till a register gets changed before continuing through the macro.
I really have no idea how to do this.
Bill
-
Hi,
if I understand correctly you want the macro to proceed to a certain point and then pause until a register changes?
Can you clarify a number of points.
1) This is a macro, ie an m*********() called from a Gcode job?
2) What happens if the register does not change value as expected? Is the macro supposed to wait forever?
3) What does the register change from and what does it change to?
4) How long is the pause expected to be? Would it be adequate for instance to wait 1 second , test the register, if
negative wait for another second? That would mean that if the register changed state it could be up to a 1 second delay
before it was tested and the remainder of the macro run. It that acceptable?
Craig
-
Craig,
Sorry for the late reply I was out of town at a trade show.
1. The macro is an m code. Happens to be m60 called in a g code file.
2. If the register does not change it just sits in the loop until changed or the software is turned off.
3. The register just changes between 0 and 1
4. A second would probably be fine but less would be better.
Thanks,
Bill
-
Hi,
try this:
local inst=mc.mcGetInstance()
local regHandle=mc.mcRegGetHandle(inst,'myRegisterPath')
while (mc.mcRegGetValue==0) do
wx.wxMilliSleep(100)
end
Naturally you have to substitute the path to the register that you are using.
Craig
-
Hi,
note that the code I posted has no time-out mechanism. If the register never changes then Mach is
in an infinite loop, you'll have to crash it to stop.
If that's OK...all well and good otherwise you'll need to add some extra code.
Craig
-
Hi,
just spotted a glaring mistake in the code, it should be:
local inst=mc.mcGetInstance()
local regHandle=mc.mcRegGetHandle(inst,'myRegisterPath')
while (mc.mcRegGetValue(regHandle)==0) do
wx.wxMilliSleep(100)
end
Craig
-
Craig,
Thanks again.
Bill
-
It worked great once i fixed my mistake.
I changed what Craig wrote a little just because it is easier to understand for me.
Unfortunately when I do that it usually results in an oops.
Here is the completed code.
--check for manual cut and wait if manual cut on
local hregC = mc.mcRegGetHandle(inst, 'iRegs0/ManCut')
local ManualCut = mc.mcRegGetValue(hregC)
while (ManualCut == 1) do
wx.wxMilliSleep(100)
ManualCut = mc.mcRegGetValue(hregC)
ManCutWasOn = 1
end
if (ManCutWasOn == 1) then
mc.mcRegSetValue(hregC, 1)
else
mc.mcRegSetValue(hregC, 0)
end
My mistake was that instead of getting the Register value in the While loop like Craig showed me I did it with a variable.
Well at first I did not have it getting the register value inside the loop so of course the variable never changed in the While loop.
Worked great after I added the "ManualCut = mc.mcRegGetValue(hregC)" inside the loop.