Realtime kernel
Selecting a real-time kernel
After starting the code analysis in a ‘classical’ way, I had a look on RTOS kernels (Real Time Operating System) that can run multiple tasks in independent ways. The idea is to split the code into large functional groups that are executed in parallel. We can imagine for example creating the following tasks:
- LCD display management
- Managing serial links with the ultrasonic unit
- Distance measuring management
- Global robot management
Each task is programmed as an infinite loop having the following structure:
while (1) { wait_for_event(); actions(); }
There are two main families of real time kernels:
- The preemptive RTOS
- The Cooperative RTOS
In preemptive mode, the kernel run all tasks by distributing the processing time, based on priorities that can be assigned to each task. A task with a long running is interrupted during treatment to allow another task to be launched. The interrupted task will continue next time the scheduler will reactivate it at next time slice.
In the cooperative mode, the scheduler is not intrusive. The tasks are not interrupted and must avoid remaining active for long time. It would block other system tasks.
This RTOS type may seem more complex to program because it is necessary to ensure that a function does not take too long time to run.
In practice, it is quite simple to implement. Most of the time, the tasks are waiting for an event (a measurement result, a message to display, a detected obstacle, a timeout, …), which allows the kernel supervisor to activate other tasks. If we know that a loop will run for a long time, simply insert supervisor calls to activate other tasks.
This type of RTOS more ‘simplistic’ usually has a smaller code size for the kernel and lower memory consumption for its internal needs.
I studied (and try) the following different RTOS for PIC 18F:
- Salvo Pumpkin RTOS (http://www.pumpkininc.com/). This is a preemptive RTOS with many messages functions and inter-task synchronization. This is a sales RTOS and the free version has a limited number of tasks that can be declared.
- PicOS18 (http://www.picos18.com/) is no longer available. It was a preemptive RTOS dedicated to PIC18 micro-controllers.
- OSA (http://pic24.ru/doku.php/en/osa/ref/intro).It is a cooperative RTOS which only uses little memory and only adds little code for its kernel. It has messages functions and synchronization between tasks. However, be sure to read the documentation to know in what context the functions can be called. The OSA project is very active. During the development of my code, I contacted the developers and we had very constructive discussions.