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

Measuring Movement with Encoders on Raspberry Pi Pico

Robots don’t just run code blindly; they need sensors. What sensors do we add first? Our robot already has some sensors on board, and in this chapter, we’ll see how to use them.

We finished the previous chapter noticing that timing isn’t the most accurate way to determine robot movement. So, let’s see how our first sensor, encoders, can improve this. Encoders are the first step in getting accurate movement and location estimation in robots. We will also learn one of Raspberry Pi Pico’s excellent features – Programmable IO (PIO).

We’ll dig into movement fundamentals, odometry and encoding, look at Raspberry Pi Pico PIO in CircuitPython, and use this to get data from the encoders on our robot.

In this chapter, we’re going to cover the following main topics:

  • About encoders and odometry
  • Wiring in encoders on a Raspberry Pi Pico robot
  • Programming Raspberry...

Technical requirements

For this chapter, you will need the following:

  • First is the built robot, as made in the previous chapters
  • 6 x fresh AA batteries
  • A PC or laptop with a USB micro cable
  • Mu software to write our code and upload it
  • Clear floor space with a meter or so in each direction to test the robot

All code examples are on GitHub at https://github.com/PacktPublishing/Robotics-at-Home-with-Raspberry-Pi-Pico/tree/main/ch-06.

About encoders and odometry

Odometry is measuring how a position has changed over time. We can combine measuring and estimation to determine where you are on a route. An encoder is a sensor designed to measure distance traveled via wheel turns. They are like tachometers, but encoders measure position whereas tachometers measure only speed. Combined with time, they can make a speed measurement too.

Absolute and relative sensing

Sensors for a robot’s location come in two primary forms. They are as follows:

  • Absolute sensors encode a position to a repeatable position. They have a limited range or resolution, such as encoding a position along a known line. For example, GPS sensors have exact positioning with low resolution, suitable for meters but not millimeters.
  • Relative sensors tend to be cheaper. However, they produce a relative change in position, which needs to be combined with the previous state to get an absolute estimate – this means that errors...

Wiring in encoders on a Raspberry Pi Pico robot

Our robot has already got encoders on board, and we have already wired them in. We can take a closer look at the motors and how they are wired into Raspberry Pi Pico GPIO pins to understand the robot better.

Examining the motors

We use N20 geared motors with encoders. The following diagram labels the motor parts:

Figure 6.3 – The N20 motor parts

Figure 6.3 shows a drawing of the motors we have used. Marked on it are essential features that affect how we use the encoders. On the left is a magnetic disk with markers in it. This disk is attached to the motor’s driveshaft and sensed by the encoder sensor board. On the right are the gearbox and the motor output shaft.

The driveshaft goes through the gearbox, so the output shaft will not make the same number of rotations as the disk – the gear ratio will determine this relationship. So one revolution of the output wheel could count many...

Programming Raspberry Pi Pico PIO

We saw the PIO system back in Chapter 1. We could read encoders in Python on the Pico central cores; however, we can make monitoring the encoders the responsibility of PIO, letting those central cores do other things. The PIO system can read and decode the Gray code emitted by the encoders. This section will show how to program PIO in assembler and load the programs with CircuitPython.

Introduction to PIO programming

As we saw in Chapter 1, Pico has two PIO devices, each with four state machines. We program PIO in assembly language. Instructions command PIO to perform operations such as manipulating IO pins, registers, and first in first out (FIFO) queues. The following diagram is a simplified representation of a state machine:

Figure 6.5 – The Raspberry Pi Pico PIO state machines

The preceding diagram shows Raspberry Pi Pico PIO state machines, highlighting registers and other features. It shows the two PIO devices...

Measuring encoder count for movement

We know what sequences to expect for our encoder, and we have a working knowledge of PIO assembler. So, we can bring these together to create the counter. We’ll start simple though; let’s see how to detect when a system has changed.

Making a simple PIO change detection loop

As we saw in the read two pins example, when we output the system’s state in a tight loop, it floods off anything interesting. We are interested in state changes, a step toward the full decoder. In the pio_one_encoder_when_changed.py file, we go straight from imports into the assembler:

import board
import rp2pio
import adafruit_pioasm
import array

We start by clearing y –; we are going to use y to store a pin value for comparison:

program = """
    set y, 0

The following code creates a read label; we can loop to this point to get new pin readings. It stores the old y in x so that we can get a new...

Summary

In this chapter, you have learned about measuring distance traveled using encoders, including the different types of encoders.

You saw the output that quadrature encoders create and how to interpret this as a turning direction.

You were introduced to the powerful PIO state machines present within Pico and saw how you can give tasks such as handling encoders to them.

You brought this together to create a reusable handler for the encoders, and we had a demonstration to see them working.

In the next chapter, we will plan and buy more devices for our robot, leading to more sensing, and remotely drive it.

Exercises

These exercises can improve your robot and let you practice your skills:

  • You have been able to get readings and a count for each wheel when driving for a fixed time. How could you make code that stops the motors after a fixed number of counts? You may need to check the encoder readings in a loop regularly.
  • You may have noticed imbalances in the counts – this is normal and due to motor and wheel differences. One way you could improve this would be to design and make a holder for the breadboard with the styrene rod so that it doesn’t slide around on the platform.
  • Could you write code to slow a motor if it’s overtaken another one in its count?

Further reading

These further reading items will help you continue your studies:

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