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 12: IMU Programming with Python

Modern robots need to know their position relative to the world. In Chapter 11, Programming Encoders with Python, we looked at how encoders can get an idea of how much the robot has moved or turned. However, this turning was relative to where the robot was and had no absolute reference. Wheel slipping could lead to false readings. In this chapter, you will see more ways the robot can read changes in its position and measure its movements.

In principle, an inertial measurement unit (IMU) can give absolute position measurements and not slip. In practice, they are complicated. This chapter is a small practical tour of adding an IMU to your robot. I will introduce the components of an IMU in this chapter. You will also learn how to solder in order to add headers to a breakout, a skill that opens up a world of additional robot parts.

We'll write some code to try the various functions and see the kind of output the sensors provides. We will...

Technical requirements

For this chapter, you will need the following items:

  • The robot from Chapter 7, Drive and Turn – Moving Motors with Python
  • The robot code from Chapter 11, Programming Encoders with Python, which can be found at https://github.com/PacktPublishing/Learn-Robotics-Programming-Second-Edition/tree/master/chapter11
  • An ICM20948 breakout board with headers, such as the Pimoroni PIM448 module
  • A soldering iron and stand
  • A soldering iron tip-cleaning wire
  • Solder – should be flux-cored solder for electronics
  • A solder sucker
  • A well-lit bench for soldering
  • A ventilated space or extractor
  • Safety goggles
  • A breadboard
  • A 2.5 mm standoff kit
  • Female-to-female Dupont jumper wires

For the complete code for this chapter, please go to https://github.com/PacktPublishing/Learn-Robotics-Programming-Second-Edition/tree/master/chapter12.

Check out the following video to see the Code in Action: https://bit.ly/38FJgsr...

Learning more about IMUs

An IMU is a combination of sensors designed to sense a robot's movement in a 3D space. These devices are found in drones, useful in floor-based robots, and critical for balancing robots. The IMU is not a single sensor, but a collection designed to be used together and have their readings combined.

These devices are tiny but have their roots in flight hardware with large spinning gyroscopes. IMUs use the micro-electro-mechanical systems (MEMS) technology to put mechanical devices on micro-scale chips. They do have tiny moving parts and use electronic sensors to measure their movements.

Since some measurements are analog (see Chapter 2, Exploring Controllers and I/O, IMU modules often include an analog to digital converter (ADC) and communicate over I2C.

There are different combinations of sensors on an IMU. These sensors are as follows:

  • A temperature sensor, to account for temperature effects on other sensors
  • A gyroscope, which measures...

Soldering – attaching headers to the IMU

Most IMU breakouts, including the suggested PM448, are likely to come with headers in a bag, which you will need to solder onto the board. You are going to need a small bit of tuition if you want to solder on these headers:

Figure 12.2 – Bare PIM448 with headers

The preceding image shows the PIM448 as it comes out of the bag. On the left is the ICM20948 board with only holes and no headers. In the middle are the male headers, while the female headers are on the right. We will use the male headers since these are easier to hold in place when soldering.

As we mentioned in the Technical requirements section, you need a soldering iron and solder, a soldering iron stand, safety goggles, an extractor or well-ventilated space, an additional breadboard, and a well-lit workspace. Soldering creates fumes that you do not want to breathe in.

Wear your safety goggles at this point. Heat the soldering iron...

Attaching the IMU to the robot

Before we can use the IMU and write code for it, we must securely mount it on the robot and wire it so that the Raspberry Pi can talk to it.

Physical placement

The IMU magnetometer is sensitive to magnetic fields and needs to be away from the motors. For this reason, it should be on a stalk above the robot.

The orientation of the IMU is essential for other experiments to make sense:

Figure 12.4 – Lining up the IMU with the robot

There is a diagram on top of the IMU. The preceding diagram shows how this diagram should line up with the robot. The X-axis should face forward, while the Z-axis should face up, with the little square on the IMU pointing upward. Finally, the Y-axis should point to the left.

The sensor uses I2C. I2C is sensitive to wire distances, so we should mount it above the Raspberry Pi and motor control board where the wire distances are low. The following image shows the parts you will need...

Reading the temperature

  1. With the device wired and attached, you'll want to try some code on it to confirm we can talk to this device and get data out of it. Let's get some tools installed and make it work.

    Installing the software

    Before we can start interacting with this device, as with most devices, we will install a helper library to communicate with it. Pimoroni, the suppliers of the ICM20948 module I've suggested, have made a handy library for Python to talk to it. I recommend taking their latest version from GitHub.

    Perform the following steps to install it:

    Boot up the Raspberry Pi on the robot. This Pi should have been used previously for the motor board and LED shim and have I2C enabled. If not, go back to Chapter 7, Drive and Turn – Moving Motors with Python, and follow the steps for preparing the I2C.

  2. Type in i2cdetect -y 1 to check that you've installed the device correctly. The output should look like this:
    pi@myrobot:~ $ i2cdetect -y 1
      ...

Reading the gyroscope in Python

In this section, we are going to use the gyroscope in the IMU. We will use it to approximate where the robot is facing in three dimensions.

But before we do that, let's understand it.

Understanding the gyroscope

A gyroscope measures rotation as a rate of change in angle, perhaps in degrees per second. At each measurement, it can determine the speed of rotation around each axis:

Figure 12.13 – Illustration of a gyroscope

A gyroscope is traditionally a mechanical system, as shown in the preceding image. It has a gimbal – a set of concentric rings – connected by pivots so that they can pivot around the X-axis, Y-axis, and Z-axis. The middle has a spinning mass, known as a rotor. When the rotor is spinning, moving the handle (shown as a stand at the bottom of the image) does not affect the spinning mass, which keeps its orientation, with the gimbals allowing it to turn freely.

In the case...

Reading an accelerometer in Python

In this section, we will learn how to use an accelerometer to measure forces acting on the robot, and most often, which way is down. Let's find out more about it, then write some code to see how it works.

Understanding the accelerometer

An accelerometer measures acceleration or changes in speed, both in terms of size and direction. It does so by providing three values – one for each of the X, Y, and Z axes:

Figure 12.17 – Accelerometer concept – mass with springs

The preceding diagram shows a conceptual view of an accelerometer. Let's take a look at it in more detail:

  1. This shows a ball (a mass) suspended by six springs in a box. When there are no forces on the box, the ball stays in the middle.
  2. This shows how this system behaves when the large arrow pushes it. The mass retains inertia by moving to the right, compressing the right spring and extending the left spring.
  3. ...

Working with the magnetometer

A magnetometer reads magnetic field strengths in 3D to produce a vector. Code you write can use this to find the magnetic north, in the same way as a compass. In this section, we'll look closer at the device, learn how to get a reading from it, and see what vectors it produces.

It may be useful to have a space with very few magnets present. Let's understand the magnetometer more.

Understanding the magnetometer

A compass measures a heading from the Earth's magnetic field by using a magnetized needle or disk. The following image is of a compass:

Figure 12.19 – A traditional compass

The compass shown in the preceding image has a rotating magnetized disk balanced on a center pin. This variety is a small button compass, which is about 25 mm in diameter.

Our chosen IMU contains a device known as a magnetometer. This electronically senses a magnetic field and can be used as a compass.

Most magnetometers...

Summary

In this chapter, you learned how to read four sensors on an inertial measurement unit, as well as how to display or graph data. You then had your first experience with soldering – a vital skill when it comes to making robots. You also learned about robot coordinate systems.

Later in this book, we will dive deeper into knitting the IMU sensors together to get an approximation of the robot's orientation.

In the next chapter, we will look at computer vision; that is, how to extract information from a camera and make the robot respond to what it can see.

Exercises

  • In the temperature graph, you will notice a lot of noise in the graph and the output. The Python round function takes a number and the number of decimal places to keep, defaulting to 0. Use this to round off the temperature to a more reasonable value.
  • Try putting the accelerometer values into an X, Y, and Z graph, as we did for the gyroscope. Observe the changes in the chart when you move the robot. Is it smooth, or is there noise?
  • Could the gyroscope values be shown as a vector?
  • Are there other sensors that can be soldered that you might find interesting for your robot to use?

Further reading

Please refer to the following links for more information regarding what was covered in this chapter:

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 $15.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