Page 1 of 1

Setting thread execution rate - update

Posted: Fri Jul 12, 2013 5:58 pm
by ksweeney
This post is an update to one originally posted to the SPC56x Support forum. viewtopic.php?f=19&t=1202

In that post, I asked how to cause a thread to execute at an exact frequency. Giovanni was kind enough to not call me an idiot for wanting to do this and he provided a code snippet to get me started.

Here is what I ended up with;

Code: Select all

uint32_t TickCount = chTimeNow();            \
chSysLockFromIsr();                      \
   if (!(TickCount % 10))                \
      chSemResetI(&sem100Hz, 0);               \
   if (!(TickCount % 100))                \
      chSemResetI(&sem10Hz, 0);          \
chSysUnlockFromIsr();       

Works well.

I would like to explain why I need to do this and to see if anyone has a better way of doing it.


My application is in real time control of a complex physical system. Engine control, to be specific. Control of this system requires several PID controllers and two complex state machine controllers. Implementation of PID controllers in digital circuitry requires an exact time interval between calculations and the same interval between outputs to the physical system.
I accomplish this by placing the computations and updates in a single thread that executes at an exact rate. This way there is no accumulated error.

Surely, this problem has come up before. Most control systems require things that happen at a specific, exact time. My solution works well in practice, but it does seem somewhat inelegant.

Ant ideas?

Re: Setting thread execution rate - update

Posted: Fri Jul 12, 2013 6:12 pm
by Giovanni
Hi,

You could use two virtual timers in place of those counters, VTs use the same systick routine and thus have an equivalent jitter "spectrum". Since the system uses VTs in other places already you would offload the system tick handler of that code.
Virtual timers are one-shot but can be restarted from within their callback.

Giovanni