There is now (in version 2.5) a dedicated HAL component for this task. see the "timer" component in the docs or man timer
Prompted by a query on IRC, this page describes how to add a simple on-screen cycle timer to show how long a program has taken to run.
It works by using a 1Hz PWM signal as a clock, which is counted by an encoder in "Counter Mode". The PWM is enabled and disabled by a halui pin halui.program.is-running. An edge detector module resets the encoder when this pin toggles from false to true. It is possible that a siggen block would be more accurate, but the output of that is a float and would need to be converted to boolean.
loadrt pwmgen output_type=0 # This creates a 1Hz signal loadrt encoder num_chan=1 # This will count the 1Hz signals loadrt edge count=1 # and this looks for a change in program.is-running to reset the timer addf pwmgen.make-pulses base-thread addf encoder.update-counters base-thread # If you don't have a base-thread then put it in the servo-thread addf pwmgen.update servo-thread addf encoder.capture-position servo-thread addf edge.0 servo-thread setp pwmgen.0.pwm-freq 1 setp pwmgen.0.scale 1 setp pwmgen.0.offset 0 setp pwmgen.0.value 0.5 setp encoder.0.counter-mode 1 setp edge.0.in-edge false net run-timer halui.program.is-running => pwmgen.0.enable edge.0.in net OneHzPulse pwmgen.0.pwm => encoder.0.phase-A net timer-reset edge.0.out => encoder.0.reset
<pyvcp> <number> <halpin>"timer"</halpin> <format>"4.0f"</format> </number> </pyvcp>
net timer-display encoder.0.position => pyvcp.timer
[DISPLAY] PYVCP = pyvcp.xml [HAL] POSTGUI_HALFILE = custom_postgui.hal HALUI = halui
This setup assumes no existing encoder or PWM blocks in the HAL. If there are, then you will need to add one of each. In the case of encoders, if for example the current line says:
loadrt encoder num_chans=2then you would need to edit that line to say
loadrt encoder num_chans=3and all the references to encoder.0.**** above would need to read encoder.2.****(as three encoders means encoders 0, 1 and 2)
In the case of pwmgens, you might have
loadrt pwmgen output_type=1,0Which would need to have an extra Type-0 added to the list, eg
loadrt pwmgen output_type=1,0,0And again the references to pwmgen.0 above would need to be edited to suit.