Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
BeagleBone Black Cookbook

You're reading from  BeagleBone Black Cookbook

Product type Book
Published in Nov 2015
Publisher Packt
ISBN-13 9781783982929
Pages 346 pages
Edition 1st Edition
Languages
Concepts

Table of Contents (16) Chapters

BeagleBone Black Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Setting Up for the First Time Basic Programming Recipes Physical Computing Recipes Using JavaScript, the BoneScript Library, and Python Exploring GNU/Linux Recipes Using Bash, Autotools, Debugger, and systemd Basic Programming Recipes with the Linux Kernel Run Faster, Run Real Time Applied Recipes – Sound, Picture, and Video The Internet of Things The Black in Outer Space Index

Chapter 6. Run Faster, Run Real Time

In this chapter, we will cover the following recipes:

  • Installing a test tool

  • Modifying the kernel using RT-PREEMPT

  • Modifying the kernel using Xenomai

  • Programmable real-time units – an introduction

  • A simple PRU test with an assembler

  • Running an ultrasonic range sensor with the PRUs

  • Using Python with the PRUs

Introduction


Things are getting interesting: no more blinky LEDs from sysfs. It's real time...time to learn about how to make your BeagleBone Black perform real-time tricks.

This is not to say that the BeagleBone Black is pokey. The challenge is that, nowadays, innovators, developers, product designers, and engineers always demand faster, more predictable results, especially in scenarios where small latencies can make the difference between success or crash. Crash in this case meaning bang, crash goes the device, not just a software crash.

So, in this chapter, we will examine recipes that help boost the responsiveness of your BBB under certain conditions. To perform this, we will explore the following topics:

  • Kernel modifications for the purpose of creating real-time environments using:

    • RT_PREEMPT

    • Xenomai

  • Programmable real-time units

And now for something completely contradictory: Linux is not real time. It is fast and efficient, but it is not a real-time system.

Harummph! But isn't Linux supposed...

Installing a test tool


Before we can convert our BeagleBone Black to a blazing real-time wonder, we need a way to measure latency in the Linux kernel. A popular tool for this purpose is cyclictest. Cylictest measures the amount of time that passes between when a timer expires and when the thread that set the timer actually runs. It uses time snapshots, one just before a specific time interval (t1), then another one just after the timer finishes (t2). We can then compare these two snapshot values to pinpoint excessive latency sources within the kernel.

Getting ready

The usual minimal setup is all you need: a BBB powered over mini USB with Internet connectivity enabled.

How to do it...

Cyclictest isn't available as a prebuilt binary. So, we will have to compile it from source:

  1. Log in as root:

    # sudo -i
    
  2. Go get the source files for cyclictest:

    # git clone git://git.kernel.org/pub/scm/linux/kernel/git/clrkwllms/rt-tests.git
    
  3. Navigate to the new directory that git creates:

    # cd rt-tests
    
  4. Compile that...

Modifying the kernel using RT-PREEMPT


To dive into the real time realm, we'll begin by exploring a piece of code that you apply to the Linux kernel directly, one which is often considered the de facto standard for real-time applications, a patch called PREEMPT_RT.

First, nomenclature. In the literature, our patch can be found referred to as CONFIG_PREEMPT_RT, PREEMPT_RT, RT-Preempt, or simply Linux RT patch. Whew! Can't anyone decide? We'll default to calling it RT-PREEMPT since that is the most common usage.

The RT-PREEMPT patch forces onto our system what is known as native real time pre-emption. This means that the patch is applied directly—or natively—onto the kernel, and allows you to preempt the entire kernel's events and processes in favor of targeted events or tasks. It makes sections of the Linux kernel pre-emptible that are ordinarily blocking.

For example, let's say we have two processes. We assign a higher priority to the second process than the first one. The patch enables a time...

Modifying the kernel using Xenomai


Because it's a magic spell handed down from Gandalf the Grey, this recipe will only work by saying it aloud three times fast: Xenomai, Xenomai, Xenomai! All right. It sounds like an incantation. But, unfortunately, it's only a software layer on top of a kernel.

The good news, however, is that adding Xenomai to the kernel mix can magically bring real-time(ish) oomph to our BeagleBone Black. So, what is it exactly? It is a set of patches bringing several useful ingredients:

  • Real-time support from the hardware interrupt level to user space applications.

  • A dual-kernel mix composed of the primary Linux kernel and the secondary Xenomai kernel.

  • A subsystem underneath Linux that does not rely on the kernel to pre-empt designated events (like our previous method).

But why should we use it instead of PREEMPT_RT? With its focus on embedded systems, Xenomai is useful for several reasons:

  • Lowers system overheads.

  • Legacy porting: Easier to port legacy real-time systems. As...

Programmable real-time units – an introduction


Now it's time for hardware magic, for serious real time, and a shift away from mere software mods on the kernel.

It's time for Programmable Real-Time Units And Industrial Controller Subsystem, also known as PRU-ICSS, or PRUs. How's that for a mouthful? More techno-acronyms are always so helpful and delicious. OK. Let's deconstruct it.

PRUs are specialized chips on the BeagleBone Black that give you real time task control over your system. These two 32-bit microcontrollers are actually separate CPUs from the main CPU and the operating system. That's right: PRUs are separate hardware. This means they are not reliant on the core kernel and are more efficient at accessing I/O pins and driving real time events.

Having onboard PRUs is one of the BBB's big differentiators in the microcomputer market. Real-time hardware control typically requires adding on daughterboards such as FPGAs (another big scary acronym) that boost your system's mojo. Using PRUs...

A simple PRU test with an assembler


Although there are many complex and far more sophisticated tests for the PRUs, our intention is to create a very minimal test recipe that you can observe from user space.

To do this, we will use a C program that runs directly on our BBB's ARM processor. From Linux user space, it will give a big hug to the PRU, upload an assembler code binary, run it, then pass and fetch information between the ARM and the PRU.

The basic steps are:

  1. Create a program in assembly language for the dedicated hardware (the PRU)

    Note

    The PRU only speaks assembly!

  2. Create a program in C for the main CPU (BBB's ARM)

    Note

    This program reaches over and handshakes the assembler code on the other PRU.

  3. Run a PRU test with the combined (compiled) magic of the first two steps.

Getting ready

The usual minimal setup is all you need: a BBB powered over mini USB with Internet connectivity. You will also need to have executed the preceding recipe on prepping and enabling the PRUs.

Note

Be sure that you are...

Running an ultrasonic range sensor with the PRUs


The fast twitch eye muscle on a robot is only as good as its processor. And with many robotic projects where mobility and motion is part of the design, the speed and reliability of the twitch can be the difference between a successful foray or a smash and crash.

Which is where nanosecond response time and reliability—a PRU specialty—is crucial. To that end, let us look at a more ambitious recipe that cooks up a way to use the PRUs with one of those cheap and ubiquitous ultrasonic range sensors, the kind that are commonly found on basic robotic devices. The sensor uses sound to send a ping, similar to what a submarine does to measure the time between sending and receiving a signal for determining distance from an object.

Getting ready

The requirements are as follows:

  • BBB powered over 5V power supply. If you only power the board via the USB tether, you will deliver very unreliable power to the sensor, so be sure you maintain a 5V supply.

  • Internet...

Using Python with the PRUs


Contrary to popular geek belief, ancient Egyptians did not write on PyPRUSS. But if they'd known Python, they might have.

Actually, the oldest thing about PyPRUSS— a Python library for the PRUs—is that it was primarily focused on the earlier Angstrom firmware for the BBB, a distribution now looking increasingly mummified. Which is not to say that PyPRUSS does not still remain a great userland touchstone for Pythonistas and PRU-ophiles.

In fact, even though there is no pre-compiled binary in the Debian repositories for PyPRUSS, it takes all of 90 seconds to compile and install from source, and begin running Python on the PRUs quickly.

Getting ready

The usual minimal setup is all you need: a BBB powered over mini USB with Internet connectivity. You will also need to have done the first recipe in this section on prepping and enabling the PRUs.

How to do it…

  1. Log in as root:

    $ sudo -i
    
  2. Grab the source files:

    # git clone https://github.com/HudsonWerks/pypruss.git
    
  3. Navigate to...

lock icon The rest of the chapter is locked
You have been reading a chapter from
BeagleBone Black Cookbook
Published in: Nov 2015 Publisher: Packt ISBN-13: 9781783982929
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}