Hello Guest it is March 29, 2024, 01:52:51 AM

Author Topic: loop in macro till register changes  (Read 1211 times)

0 Members and 1 Guest are viewing this topic.

Offline Bill_O

*
  •  563 563
    • View Profile
loop in macro till register changes
« 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
Re: loop in macro till register changes
« Reply #1 on: April 18, 2019, 07:20:47 PM »
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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline Bill_O

*
  •  563 563
    • View Profile
Re: loop in macro till register changes
« Reply #2 on: April 29, 2019, 12:00:10 PM »
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
Re: loop in macro till register changes
« Reply #3 on: April 29, 2019, 02:31:15 PM »
Hi,
try this:

Code: [Select]
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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: loop in macro till register changes
« Reply #4 on: April 29, 2019, 02:35:57 PM »
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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: loop in macro till register changes
« Reply #5 on: April 29, 2019, 02:58:59 PM »
Hi,
just spotted a glaring mistake in the code, it should be:

Code: [Select]
local inst=mc.mcGetInstance()
local regHandle=mc.mcRegGetHandle(inst,'myRegisterPath')
while (mc.mcRegGetValue(regHandle)==0) do
wx.wxMilliSleep(100)
end

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline Bill_O

*
  •  563 563
    • View Profile
Re: loop in macro till register changes
« Reply #6 on: April 29, 2019, 03:04:42 PM »
Craig,

Thanks again.

Bill

Offline Bill_O

*
  •  563 563
    • View Profile
Re: loop in macro till register changes
« Reply #7 on: May 02, 2019, 04:55:14 PM »
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.