Reader small image

You're reading from  Robotics at Home with Raspberry Pi Pico

Product typeBook
Published inMar 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781803246079
Edition1st Edition
Languages
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

Detecting Orientation with an IMU on Raspberry Pi Pico

Our robot can track how far it’s moved, but what about tracking which direction the robot is facing? Or how far it has turned? In this chapter, we will learn about the Inertial Measurement Unit (IMU), a device that can track the motion of the robot measured against gravity and the Earth’s magnetic field.

We’ll look at how to select one of these devices, get it connected and then write code for it on our robot using the PID controller to steer the robot based on the IMU data.

In this chapter, we will cover the following main topics:

  • What is an IMU and how to choose one
  • Connecting the IMU to the robot
  • Calibrating and getting readings
  • Always face North behavior
  • Making a known turn behavior

Technical requirements

For this chapter, you will require the following:

  • The robot from Chapter 11, Controlling Motion with Encoders on Raspberry Pi Pico
  • The robot, encoder, and PID code from Chapter 11, Controlling Motion with Encoders on Raspberry Pi Pico
  • A screwdriver, bolts, and stand-offs
  • Dupont jumper cables
  • A space where strong magnets can be avoided
  • A PC or laptop
  • An Android/iOS smartphone with Bluetooth LE and the Bluefruit LE Connect app

You can find the code for this chapter at https://github.com/PacktPublishing/Robotics-at-Home-with-Raspberry-Pi-Pico/tree/main/ch-12.

What is an IMU and how to choose one

In this section, we’ll look at the components of an IMU and what criteria we used to choose the one used in this robot.

Components of an IMU

An IMU is a module that can measure movement. It uses multiple sensors to achieve this. In this section, we’ll briefly look at each sensor and how they contribute to the whole measurement.

These sensors are made using the Micro-Electro-Mechanical-Systems (MEMS) process. They have tiny moving parts embedded into the chips. We can model them mechanically to understand them. These parts sense the movement of parts through their magnetic fields and amplify tiny signals. Let’s look at the components.

The thermometer

The mechanical components of an IMU will change size, depending on their temperature. These tiny changes may be enough to change the signals so that the IMU controller can use a temperature measurement to compensate for this.

The accelerometer

An accelerometer...

Connecting the IMU to the robot

Installing the BNO055 requires performing a few steps. In this section, we’ll prepare the module, attach it to the robot rigidly, wire the part into the circuit, and then use some simple code to test that it is responding.

Preparing the BNO055

The BNO055 from Adafruit comes without the headers attached. You’ll need to solder the headers in, as we have done previously. Adafruit has a guide for this at https://learn.adafruit.com/adafruit-bno055-absolute-orientation-sensor/assembly.

For this robot, you should solder this part with the headers facing up from the component side.

Attaching the BNO055

To attach the part to the robot, see the following diagram:

Figure 12.6 – Drawing of the shelf with additional holes for the BNO055 module

You will need to attach the IMU rigidly to the robot, so the velcro pad will not be sufficient. Stand-offs will make a suitable attachment here. The preceding figure...

Calibrating and getting readings

When you start up code using an IMU module with a controller, the sensors will not get correct readings. So, the IMU module will need to determine the sensitivity and correct states of the sensor, a process known as calibration. First, we need some code; then, we’ll need to take the robot through some motions to perform this.

Calibration code

Let’s start with the code. In a file called imu_calibration/code.py, add the following:

import adafruit_bno055
import board
import busio
import time
i2c = busio.I2C(sda=board.GP0, scl=board.GP1)
sensor = adafruit_bno055.BNO055_I2C(i2c)

This code handles importing the module and setting it up. We also import time so that we can use it in loops later.

Next, we must check the calibration state of the module:

def check_status():
  sys_status, gyro, accel, mag = imu.calibration_status
  print(f"Sys: {sys_status}, Gyro: {gyro}, Accel: {accel}, Mag: {mag}"...

Always face North behavior

We’ll build a behavior with a heading as a set point for a PID and the IMU Euler heading as feedback. The error value between these will be how far, in degrees, the robot is facing away from the North heading. For example, a heading of 0 should be North – note that you could pick another heading as needed. We will use the PID output to control the motor movements, with the output adding to the speed of one motor and subtracting from the other, producing a turn.

Let’s see how this looks as a block diagram:

Figure 12.10 – Face North behavior block diagram

The preceding diagram shows the flow of data. The expected heading (or target) with the actual heading from the IMU are used to calculate the error. This error and dt (delta time) are the inputs to the PID. The output from the PID, the control signal, is added for one motor and subtracted for the other. The motors then result in robot movement, which...

Making a known turn behavior

The known turn behavior is a variation of the always face North behavior. The idea is to measure the angle at the start of the turn and then make the set point the new intended angle.

We’ll make it so that the whole app will accept a difference in the intended angle, offsetting the last intended angle, with the whole app starting based on the robot’s current heading. The user can send +30 to turn 30 degrees and -90 to rotate 90 degrees back.

The block diagram is exactly as before, as we only need to manipulate the expected heading. Make a copy of face_north in a folder called known_turn. Let’s rename the controller IMUTurnController:

class IMUTurnController:
    def __init__(self):
        self.pid = pid_controller.PIDController(0.01, 0.008, 0)
        self.target = 0

The update method doesn’t change, as shown here...

Summary

In this chapter, we investigated the IMU and how we can use it to control the heading of our robot. We learned how to connect the device and calibrate it.

Then, we used data from it to face North by combining the sensor data with a PID controller. Finally, we built on this example so that it can turn a specified number from the current heading.

In the next chapter, we will build a small arena for the robot and look at how we can combine the encoders and distance sensors to estimate the robot’s position within this arena, improving its estimation as it moves.

Exercises

These exercises will deepen your understanding of the topics that were covered in this chapter and make the robot code better:

  • Combining the preceding behaviors with the menu system for the UART we’ve seen in previous chapters would allow you to tune the PID with the robot running.
  • Could you use the known turn behavior and straight-line behavior to write a better version of the planned path program from Chapter 5, Driving Motors with Raspberry Pi Pico?
  • Experiment with the Euler heading reading – after calibrating, see how the readings change when you bring the robot near objects such as a laptop or kitchen appliances. This experiment will demonstrate a weakness with this kind of sensor.
  • An advanced experiment would be to extract the quaternion (instead of Euler data) and write this to the UART.

Further reading

These further study aids will help you learn more and dive deeper into the PID algorithm and its quirks:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Robotics at Home with Raspberry Pi Pico
Published in: Mar 2023Publisher: PacktISBN-13: 9781803246079
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