A Tutorial on Using Linux for Real-Time Tasks

Real Time Linux Tasks

[Andreas] has created this tutorial on real-time (RT) tasks in Linux. At first blush that sounds like a rather dry topic, but [Andreas] makes things interesting by giving us some real-world demos using a Raspberry Pi and a stepper motor. Driving a stepper motor requires relatively accurate timing. Attempting to use a desktop operating system for a task like this is generally ill-advised. Accurate timing is best left to a separate microcontroller. This is why we often see the Raspi paired with an Arduino here on Hackaday. The rationale behind this is not often explained.

[Andreas] connects a common low-cost 28BYJ-48 geared stepper motor with a ULN2003 driver board to a Raspberry Pi’s GPIO pins. These motors originally saw use moving the louvers of air conditioners. In general, they get the job done, but aren’t exactly high quality. [Andreas] uses a simple program to pulse the pins in the correct order to spin the motor. Using an oscilloscope, a split screen display, and a camera on the stepper motor, [Andreas] walks us through several common timing hazards, and how to avoid them.

The most telling hazard is shown last. While running his stepper program, [Andreas] runs a second program which allocates lots of memory. Eventually, Linux swaps out the stepper program’s memory, causing the stepper motor to stop spinning for a couple of seconds. All is not lost though, as the swapping can be prevented with an mlockall() call.

The take away from this is that Linux is not a hard real-time operating system. With a few tricks and extensions, it can do some soft real-time tasks. The best solution is to either use an operating system designed for real-time operation, or offload real-time operations to a separate controller.

 

Comments

  1. cdsteinkuehler says:

    Other options include mixing a real-time OS micro-kernel with Linux (like Xenomai or RTAI), or using the PREEMPT_RT patches for the Linux kernel. Xenomai is readily available for the Pi and several other ARM boards like the BeagleBone.

  2. ka1axy says:

    When we design products that require a real time controller, we do something similar: ARM running a higher level OS for the UI, netwroking, file system and a PIC, running uCOS or something similar for the A/D, D/A, stepper motors, sensors, etc. They communicate with a simple serial protocol over a regular async link. The ARM makes data requests, issues commands and the PIC responds with data or a status message indicating, for example, “stepper #3, move complete, success”.

    The other advantage of this dual processor approach, is that the code bases are completely separate, and one developer can do the GUI while another can work on the real-time code.

    If you are lucky enough to have something like a TI OMAP, with two independent processors, you can probably combine them, but we find that two separate systems is easier for everyone. Sometimes they’re on the same PCB, sometimes they’re on different PCBs, it just depends on how dense we need to pack the hardware.

    Source: EE doing industrial, consumer and medical embedded product design.

  3. also of note, the pi has 3 general purpose cpu cores in it

    the ARM 700mhz is the main one running linux
    but it also has a dual core VPU that is already running a proper realtime operating system (thats the blob providing all the closed source functions)

    there is a mailbox API to run code on the VPU, so in theory you could insert a custom interrupt handler on it and do proper realtime on the VPU, totally ignoring the arm and whatever linux does

    but without the source of the blob, you dont know which interrupt is free, and the VPU has no MMU, so its all in physical memory addressing

  4. peter says:

    wow. are there C-Compilers for the PRU ? how can the limited PRU-Code/RAM size be overcome? from what i read the PRU can only access few KB of Code and RAM. is it possible to run eg. FreeRTOS on the PRUs?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 96,725 other followers