SimpleCycleTimer - rmu75/linuxcnc-wiki GitHub Wiki
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.
*Add these lines to your HAL config.
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
*Create a Display in pyVCP by putting this in a file called pyvcp.xml in the config directory
<pyvcp> <number> <halpin>"timer"</halpin> <format>"4.0f"</format> </number> </pyvcp>
*Connect the timer output to the pyVCP display using this line in custom-postgui.hal
net timer-display encoder.0.position => pyvcp.timer
*You now need to point the INI file to the new components, add or edit these lines in the .INI file
[DISPLAY] PYVCP = pyvcp.xml [HAL] POSTGUI_HALFILE = custom_postgui.hal HALUI = halui
And that should do the trick.
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=2
then you would need to edit that line to say
loadrt encoder num_chans=3
and 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,0
Which would need to have an extra Type-0 added to the list, eg
loadrt pwmgen output_type=1,0,0
And again the references to pwmgen.0 above would need to be edited to suit.