Wednesday, January 13, 2016

First thoughts about RTOS

First of all, what is RTOS?
RTOS, as the name stands for, is a Real Time Operating System, on the same words on a different order, is a operating system with real time capabilities. As Jack the Ripper, let's think in parts (in Portuguese that's a good pun).



OS?
Imagine you're sitting on your chair, enjoying a nice IPA beer, reading a Ken Follett's book while listening to Pink Floyd in front of the warmth of the fire place. You're doing this 4 things at the same time, while some other automatic things are happening (you're breathing, your heart is pumping, your stomach is asking for help to digest the second pint of that strong beer, your liver is filtering your blood trying to get the alcohol away, etc). In order to do that, your brain has to manage everything at the same time, and it does, because it has a lot of parallel processing for the automatic things, but not so much for the non-automatic (drinking beer, read, etc). That's why we can't drive and talk to the phone without almost crashing or saying weird stuff. We can only concentrate well enough to do one thing at a time (some people say women can, but they can't, for me, that's rubbish).
The Operating System is like our brain, but a little better: it handles tasks by letting each be processed by a small amount of time and so it seems that everything is being processed at the same time. Think about your computer: all at the same time, you can move your mouse while typing and listening to music (ok, you can do much more than that). Today our processors have several cores (parallel processors), but a few year ago, there was only one processor to do all those things. So time was divided into small pieces, there was a task to read the keyboard, a task to read the mouse movements, a task to play music, a task to show something on the screen, etc, and each one used the processor for a piece and handed over to the next task, and so on. The Operating System decides which task will process at each piece of time, it will adjust the memory for that task and when the piece finishes, it will do it all over again, forever and ever.
There is actually a bit more to it, with all the drivers and stuff, but if you think about it, drivers are just tasks that talk to hardware more closely.


RT?
Imagine you're baking a nice sourdough bread [1], with that crispy outside and open crumb, your house is already smelling like a bakery, your neighbors are looking through the windows hoping you give them at least a piece of it, when you decide it's ready and you want to take it out of the oven. But you forgot the gloves, so you think "I can do it!". You quickly take the loaf of bread with your bare hands, throw it away on the kitchen floor and go straight to the sink to wash your hands while feeling very dumb and now the tip of your fingers are filled with bubbles. Of course you couldn't do it. The bread was too hot and by the time it started burning your fingers, you had to throw it away instantly. Now imagine your reaction didn't occur quickly enough and it took you 1.5 seconds more to throw the bread away. Your hands would be burned too, instead of just your fingers.
The real time factor means that the tasks will be processed and that they will be processed in a known period of time. For instance, you can configure your RTOS to run a task whenever a button is pressed and you know it won't take more than 30 ms to the task to be processed (the 30 ms is just an example and usually can be configured). On a common OS, this time can vary up to a few seconds and is usually not easily configurable.


Why should I use a RTOS?

If you just want to blink a LED or turn a Triac on, then you probably could go with a bare metal solution. Or even when the application is complex, but the memory restrains are severe, the RTOS overhead may be too much. But if you have to manage an HMI, with a custom keyboard and a screen, while writing some stuff to a SDCard and control some process, then the use of the OS can help you a lot. Not only can you abstract the time factor (you can pretend that everything is running at the same time), it's easier to test each task individually, since all the task needs should be constrained within its code. The code maintenance, scalability [2] and modularization are also facilitated, as you can develop to other platform and then just Ctrl+C Ctrl+V the code to your RTOS. The availability of code libraries also enhances the experience.

[1] http://yumarama.com/968/starter-from-scratch-intro/
[2] http://coverclock.blogspot.com.br/2012/04/all-interesting-problems-are.html

No comments:

Post a Comment