Setting thread execution rate - update

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
ksweeney
Posts: 6
Joined: Sat Jun 01, 2013 3:41 pm

Setting thread execution rate - update

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Setting thread execution rate - update

Post 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
Post Reply