[MAT logo][Prev][Up][Next]

Custom modules - timer reference

The timer and timer_ext sections aren't part of the MatPLC library proper; if you want them, you have to explicitly link to them. They can be found in the mat/lib/logic directory.

timer

Timers are basically floats, with the additional feature that they can be enabled to increase at a rate of one per second. They are also fixed-precision, storing time as an exact number of microseconds.

Note that most of the functions listed below are actually macros; they modify the value of timer directly.

plc_timer_t
The timer type.

plc_timer_clear(timer)
Set to zero and disable. The timer is now zeroed and stopped.

plc_timer_start(timer)
Set to zero and enable. The timer is now zeroed and running.

plc_timer_enable(timer)
Enable the timer. This will make the value increase at a rate of one per second.

plc_timer_disable(timer)
Disable the timer. This will make it keep whatever value it has at the moment without change.

plc_timer_add(timer, value)
Add a number to the timer, effectively winding it forward. This works whether it's enabled or disabled. Use negative values to decrease the timer (wind it back).

plc_timer_set(timer, value)
Set to a particular value. This works whether it's enabled or disabled. However, to avoid loss of precision, prefer to use plc_timer_add() whenever possible.

double plc_timer_get(timer)
Get the current value of the timer. This works whether it's enabled or disabled.

int plc_timer_done(timer, preset)
Check whether timer is at least preset seconds. This works whether it's enabled or disabled.

timer_ext

This is very similar to the above, with two differences: all the functions/macros end with _ext, and they all take an extra argument which they use as the current time instead of obtaining it from the operating system. The extra argument should be of type struct timeval, as used by the gettimeofday(2) system call.

One might use timer_ext if one runs in a conventional scan and doesn't want timers to expire mid-loop. To achieve this, call gettimeofday(2) once at the start of the loop and then use that time as the last argument to all the timer_ext calls.

Real-time considerations

FIXME

[Prev][Up][Next]

$Date: 2005/12/08 09:24:46 $