Page 1 of 1

Beginner: How to handle this timing requirement

Posted: Mon Nov 04, 2013 12:42 am
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

Re: Beginner: How to handle this timing requirement

Posted: Mon Nov 04, 2013 6:51 am
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 ?

Re: Beginner: How to handle this timing requirement

Posted: Mon Nov 04, 2013 9:08 am
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

Re: Beginner: How to handle this timing requirement

Posted: Mon Nov 04, 2013 10:09 pm
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

Re: Beginner: How to handle this timing requirement

Posted: Mon Nov 04, 2013 10:19 pm
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