Reader small image

You're reading from  Learn Robotics Programming - Second Edition

Product typeBook
Published inFeb 2021
PublisherPackt
ISBN-139781839218804
Edition2nd Edition
Concepts
Right arrow
Author (1)
Danny Staple
Danny Staple
author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple

Right arrow

Chapter 9: Programming RGB Strips in Python

LED lights can be used with a robot to debug and give it feedback so that the code running on the robot can show its state. Colored RGB LEDs let you mix the red, green, and blue components of light to make many colors, adding brightness and color to a robot. We have not paid much attention to making it look fun, so this time we will focus on that.

Mixing different sequences of LEDs can be used to convey information in real time. You can use which are on/off, their brightness, or their color to represent information. This feedback is easier to read than a stream of text, which will help as sensors are added to the robot. This also means the code on the robot can show state without relying on the SSH terminal to do so.

In this chapter, we will learn the following:

  • What is an RGB strip?
  • Comparing light strip technologies
  • Attaching the light strip to the Raspberry Pi
  • Making a robot display a code object
  • Using the...

Technical requirements

To build this you will need the following:

  • A computer with internet access and Wi-Fi
  • The robot, a Raspberry Pi, and the code from the previous chapter
  • The Pimoroni LED SHIM

The code for this chapter is on GitHub at https://github.com/PacktPublishing/Learn-Robotics-Fundamentals-of-Robotics-Programming-Second-Edition/blob/master/chapter9.

Check out the video at the following link to see the Code in Action: https://bit.ly/39vglXm.

What is an RGB strip?

Using lights to display data can be a simple yet flexible way to get data to the user without connecting a full display. For example, a single light could be turned on or off to indicate whether a robot is powered on or the state of a simple sensor. A multicolor light can change color to show more detail, to indicate a few different states that the robot is in. RGB in this chapter stands for Red-Green-Blue, so by controlling the intensity levels of these color channels in a light, multiple colors can be shown. We'll investigate how this happens later in the RGB values section.

Adding multiple lights lets you show more data. These can be in a strip (a line of lights), as well as panels/matrixes, rings, and other interesting shapes.

Comparing light strip technologies

There are many competing technologies for lights and light strips. For light types, incandescent lights, such as old light bulbs, tend to use a lot of power and take up too much space to be useful in robots. Fluorescent lights, such as kitchen strips or curly compact types, need complex power systems that also take up too much space. Electroluminescent wire, also known as EL wire, is often used to decorate objects by outlining them; it looks interesting but is tricky to control. Light Emitting Diode (LED) technology is low power and tends to be small and easy to control, which makes it best suited for robots such as ours. LEDs are also cheap.

The most useful kind, in our case, which we will use in this chapter, are addressable RGB LEDs. Addressable means that each individual LED in the strip can be set to different colors and brightness, allowing a sequence of colors along the strip. To keep it simple, we will use a type with a built-in controller...

Attaching the light strip to the Raspberry Pi

Before we write code to display color sequences on the LED SHIM, we need to attach it to the Raspberry Pi on our robot. After we have finished this section, the robot block diagram will look as in Figure 9.3:

Figure 9.3 – The robot block diagram with the LED strip

The block diagram now shows the LED strip connected to the Raspberry Pi, with an arrow indicating information flow from the Raspberry Pi to the strip. The strip is highlighted as a new addition to the system. Let's see how this works.

Attaching the LED strip to the robot

The Pimoroni LED SHIM attaches quite readily to the Raspberry Pi. We put it on top of the motor controller, with its pass-through header, so that we can see the lights on top. Take a look at Figure 9.4 to see how:

Figure 9.4 – Fitting the LEDs

Use Figure 9.4 with the following steps to attach the strip:

  1. The strip is small...

Making a robot display the code object

Although we are building around the Pimoroni LED SHIM, we've already seen that there are other types of RGB LED systems. Since we might later swap the SHIM out for a different system, it would be a good idea to make an interface on top of the LEDs. Like the motor's interface, this decouples handling hardware and making behaviors.

Making an LED interface

So, what interface do we want for the LEDs? First, we want them to be available on the robot as robot.leds. We want to clear the LEDs (turn them all off), set each individual LED to a different color, and set a bunch/range of LEDs to a list of colors.

It's useful for the code to tell us how many LEDs we have, so if the number changes, the animations or displays still make some sense.

For the colors, we use three values – r, g, and b – to represent the red, green, and blue components. Python has a type called a tuple, perfect for making a group from a small...

Making a rainbow display with the LEDs

Now we get to use these for some fun. We will extend our avoiding behavior from the previous chapter to show rainbow bar graphs on a side corresponding to the distances read. We could also use this for sensors. Before we can link the movement to the animation, how is a rainbow created?

Colour systems

RGB is how the hardware expects colors. However, RGB is less convenient for expressing intermediate colors or creating gradients between them. Colors that appear close to the eye can be a little far apart when in RGB. Because of this, there are other color systems.

The other color system we use is Hue, Saturation, and Value (HSV). We use HSV in this chapter to make rainbow-type displays and when doing computer vision in a later chapter to assist our code in detecting objects.

Hue

Imagine taking the colors of the spectrum and placing them on a circle, blending through red to orange, orange to yellow, yellow to green, green to blue...

Using the light strip for debugging the avoid behavior

LEDs in rainbows are fun, and switching colors looks nice. However, LEDs can be used for practical purposes too. In Chapter 8, Programming Distance Sensors with Python, we added sensors to our robot to avoid obstacles. You can follow along in a PuTTY window, and see what the sensors are detecting by reading the numbers. But we can do better; with the light strip, we can put information on the robot to tell us what it is detecting.

In this section, we will tie the LED output together to values from a behavior, first by basic lighting, and then by making some rainbow colors, too.

Adding basic LEDs to the avoid behavior

Before we get fancy and reintroduce the rainbow, let's start with the basic version. The intent here will be to make two indicator bars to the left and right side of the LED bar. For each bar, more LEDs will light when the corresponding distance sensor detects a closer obstacle. We'll make it so...

Summary

In this chapter, you learned how to interact with and use RGB LEDs, as well as how to choose and buy RGB LED strips that work with the Raspberry Pi. You learned how to make code for the LEDs on the robot, using them with robot behaviors. You also saw how the HSV color system works, which can be used to generate rainbows.

You can take the techniques used here to add LED-based status displays to robots and write code to link them with behaviors.

In the next chapter, we will look at servo motors and build a pan and tilt mechanism for moving sensors.

Exercises

  1. Try mixing a different RGB color, or looking one up, and using set_one, set_all, or set_range to light LEDs in that color.
  2. Use the show left rainbow and show right rainbow functions to make the robot turn on rainbows corresponding to the side it's turning to in the behaviour_path code.
  3. By making a timer loop and advancing an index or changing a range, it would be possible to animate the rainbows or make them scan across the LED bar. Try this out.
  4. Could the other parts of the HSV color be used to make pulsing LED strips that change brightness?

Further reading

Please refer to the following for more information:

  • Make Electronics: Learning by Discovery, Charles Platt, Make Community, LLC: I've only started to cover some basic electronics with the switch and breadboard. To get a real feel for electronics, Make Electronics is a superb introduction.
  • For more advanced electronics, try Practical Electronics for Inventors, Fourth Edition, Paul Scherz, Simon Monk, McGraw-Hill Education TAB: This gives practical building blocks for electronics that can be used to interface a robot controller with almost anything or build new sensors.
  • The colorsys library, like most Python core libraries, has a great reference: https://docs.python.org/3/library/colorsys.html.
  • Pimoroni have some other demos with the LED SHIM at https://github.com/pimoroni/led-shim/tree/master/examples. These could be fun to adapt to our LED layer.
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Robotics Programming - Second Edition
Published in: Feb 2021Publisher: PacktISBN-13: 9781839218804
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.
undefined
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 €14.99/month. Cancel anytime

Author (1)

author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple