Beginner: How to handle this timing requirement

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

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Flo89
Posts: 8
Joined: Fri Oct 25, 2013 9:39 pm

Beginner: How to handle this timing requirement

Post by Flo89 »

Hello Chibios-friends,

I'm a beginner of RTOS programming and this is not a chibios specific question, but I hope someone can give me a short answer.
One aim of the application should be to do this every 5 ms:
Read out 2 sensors over SPI/I2C by DMA --> Calculate current state by a filter and new motor control values by pid-controller --> Send to motor driver over I2C/CAN

Now I don't really know what is the best way to handle this.
Without RTOS I would use hardware timers to get exact timing.
When now systick is 1000 Hz does it make sense to do this with something like this in the "DMA thread" and then give a semaphore to another thread when values have arrived: (from http://www.chibios.org/dokuwiki/doku.php?id=chibios:kb:timing)

Code: Select all

while (TRUE) {
    time += MS2ST(1000);            // Next deadline
    do_something();
    chThdSleepUntil(time);
  }


Or does it then has an accurateness of +- 1ms (systick), which is quite a large percentage in this case.
Is a better solution to use virtual timers or what would you suggest in this case?

Thanks for any replies!!
Best regards,
Florian
Anubirux
Posts: 39
Joined: Sat Apr 21, 2012 2:28 pm

Re: Beginner: How to handle this timing requirement

Post by Anubirux »

Hello !

Thread sleep and virtual timers both use the same mechanism with same precision. Generally it's accuracy depends how much you have IRQs, other threads and their priorities and much more but not +/- 1ms . However u can increase chibios frequency, set higher priority on this "DMA thread" and should be fine. What MCU are you using ?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Beginner: How to handle this timing requirement

Post by Giovanni »

Hi,

You could raise CH_FREQUENCY to 10000 and have 100uS resolution, I wouldn't recommend going above this value.

ChibiOS 3.0 will be able to handle much higher resolutions thanks to the tickless kernel.

Giovanni
Flo89
Posts: 8
Joined: Fri Oct 25, 2013 9:39 pm

Re: Beginner: How to handle this timing requirement

Post by Flo89 »

Thanks for the answers!
I now see that a systick of 1ms don't mean that the accuracy is only +-1ms. Is there any "rule" to get a good value for systick frequency depending on the fastest regular timed thread?

I have here an STM32F4Discovery and an Stm32F3Discovery board.
Is it arguable now to go with Chibi os 3.0 even if it's in development or would you recommend me to take the stable version as a newbie?

Thanks!
Florian
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Beginner: How to handle this timing requirement

Post by Giovanni »

Hi,

3.0 is stable enough but it is not finalized and may change often in next months. 2.6.x would give you more time to learn.

Giovanni
Post Reply