Fundamental responsibility of Robobo is to provide dynamic control over DC motors. That means Arduino needs to timely respond to input signal and provide concurrent control for multiple DCMotors. In terms of dynamic Arduino based controller is also to provide control in designated time slot. Three items:

  • Concurrency
  • Timing events
  • Determinism

Above functions are delivered to Robobo by using FreeRTOS. This article will provide introductory information on RTOS usage in embedded applications.

Operating System

Many of you when hearing the term Operating System envision system like Windows or Linux. You might envision it as program where you start your applications, games and whole ecosystem that allows to run them concurrently. OS is also responsible for basic networking services, logging, hardware monitoring and on top of that provides access to persistent data. For our Arduino Mega case study there is no way to get close to such functionality. We don’t have a room for all of these services. Arduino Mega provides us only about 256 kB for our program. Either way we still require essence of concurrency and timers. OS I selected is FreeRTOS. It satisfies Robobo requirements and it is RTOS after all.

FreeRTOS

Unlike Windows, Linux or Android, FreeRTOS is not a stand alone application. Basically FreeRTOS doesn’t provide you any support application other than just ecosystem. This ecosystem is a framework which you can use with your application. You link your application directly with FreeRTOS kernel. By doing this you get an embedded program as whole.

Thanks to gentlemen Phillip Stevens we got FreeRTOS customized for Arduino Mega. This FreeRTOS customization is ready to pickup even from Arduino IDE.

FreeRTOS is highly customizable. Basically you pick up a kernel core and then you might pick up other components such as file system support or IP stack. For our case at the moment we will use only core of FreeRTOS.

One more advantage of FreeRTOS is AWS compatibility which was recently built-in into FreeRTOS. That proves that FreeRTOS will be supported for long time because Amazon span umbrella over project.

Real Time Operating System Scheduling

Difference between RTOS and regular OS is determinism. This means when RTOS application is triggered by some external event we can predict when response for such event will occur. This is essence. Please note that in regular operating systems it is not true.

Lets go back to servo post. Spouse that encoder reading happens and motor is still running. Lets say we would like to stop motor shaft moving because current position is satisfying. We need to stop H-Bridge and we need to do it timely. Otherwise shaft will still move without intention. Suppose also there are other things that are executing on Arduino. For instance Arduino is also providing LCD indication of motor movement to user. Here comes biggest difference between RTOS and other OSes. In RTOS we can predict that upon event Arduino will take immediate care about DC motor and postpone LCD action. We cannot say same happen on non-RTOS. Usually non-RTOS are concerned about user experience or throughput rather than on determinism.

One of the critical thing in OSes is scheduling. Whenever OS determines that there are two concurrent tasks ready to be running it has to select one of them. Please note that single core CPU allows to run one at the time.

In other words scheduler is OS algorithm for task assignment to execution. Depending on what OS is, different scheduling principles are taken. In server based environment Linux tries to accomplish scheduler fairness. That means that Linux tries to divide CPU power equally among processes. This is mostly valuable scheduling principles in Server world as you don’t want to starve any of services. From control perspective scheduler fairness is not acceptable. Object controlling is usually far more important that any other activity that happens in background. If controlling requires much more CPU than other tasks scheduler will grant it.

Usually RTOS meets determinism by having priorities assigned for every task. Whenever task with higher priority wishes to run, scheduler is assigning CPU for high priority task and postpones lower priority task for later execution.

As a side note I will just mention that determinism and responsiveness is extra cost for CPU and truly there are limited scenarios where you actually need it.

FreeRTOS on Arduino

I would like to give some code snippets that will just highlight how FreeRTOS fits into real time scheduling. Whenever new task is created:

BaseType_t xTaskCreate(    TaskFunction_t pvTaskCode,
                            const char * const pcName,
                            unsigned short usStackDepth,
                            void *pvParameters,
                            UBaseType_t uxPriority,
                            TaskHandle_t *pxCreatedTask
                          );

Main task function is defined in pvTaskCode and upon task created programmer assign strict priority uxPriority which is then used for scheduling.

In future post I will describe object oriented approach for FreeRTOS instrumentation.

Summary

In this post I described what RTOS is and justified rationale why FreeRTOS was selected for Robobo.