LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Timed delay in hardware-timed loop

My application uses EtherCAT, so it's built around a loop that is timed by hardware, iterating once per millisecond. My program is then implemented as a state machine, changing state within that loop when certain signals indicate. However, sometimes I need to remain in a state for a set amount of time, rather than waiting for a particular signal.

 

What is the most elegant way of doing this? I could just keep a counter variable, another shift register alongside of my state, but I wonder if there is a nicer way that doesn't need a shift register running all the way across my main loop.little localized operators to say "load timer for 20 ms" and "is timer finished?"

 

Any ideas?

0 Kudos
Message 1 of 2
(2,009 Views)

Did I understand correctly that your timed loop runs once per millisecond, and then does whatever State is indicated at that time?  Then as long as your program runs for less than 7 weeks (I think that's the right number), the following should work: 

  • Have a Shift Register, labeled "Timer", initialized to -1.
  • Each time through the loop, decrement the Timer.  Note that it will take >49 days to count down to 0.
  • Any State that needs to set a Timer just resets the Timer to the (positive) number of milliseconds to delay, then goes about its business.
  • Just before the loop exits (outside the Case Statement of the State Machine), check the Timer.  If it is 0, replace the "Next State" Shift Register with whatever State you want to do in case of a Timeout.

Note that you should possibly "cache" the "Replaced-by-Timeout State" so that when finishing the Timeout State, you can (if it makes sense to do so) next do the Cached State.  You should also think whether you want to do two "States" in a single Tick, the "Timeout" code + whatever would have next run if the Timeout had not occurred.

 

Bob Schor

Message 2 of 2
(1,957 Views)