Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Robotics at Home with Raspberry Pi Pico

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

Product type Book
Published in Mar 2023
Publisher Packt
ISBN-13 9781803246079
Pages 400 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Danny Staple Danny Staple
Profile icon Danny Staple

Table of Contents (20) Chapters

Preface Part 1: The Basics – Preparing for Robotics with Raspberry Pi Pico
Chapter 1: Planning a Robot with Raspberry Pi Pico Chapter 2: Preparing Raspberry Pi Pico Chapter 3: Designing a Robot Chassis in FreeCAD Chapter 4: Building a Robot around Pico Chapter 5: Driving Motors with Raspberry Pi Pico Part 2: Interfacing Raspberry Pi Pico with Simple Sensors and Outputs
Chapter 6: Measuring Movement with Encoders on Raspberry Pi Pico Chapter 7: Planning and Shopping for More Devices Chapter 8: Sensing Distances to Detect Objects with Pico Chapter 9: Teleoperating a Raspberry Pi Pico Robot with Bluetooth LE Part 3: Adding More Robotic Behaviors to Raspberry Pi Pico
Chapter 10: Using the PID Algorithm to Follow Walls Chapter 11: Controlling Motion with Encoders on Raspberry Pi Pico Chapter 12: Detecting Orientation with an IMU on Raspberry Pi Pico Chapter 13: Determining Position Using Monte Carlo Localization Chapter 14: Continuing Your Journey – Your Next Robot Index Other Books You May Enjoy

Teleoperating a Raspberry Pi Pico Robot with Bluetooth LE

We intend for the robot we are building to be mobile. We already have the robot driving on the floor and able to sense and respond to its surroundings. However, we either rely on it blindly or are tethered to it with a laptop. Neither is quite what we want. What if we could get feedback while it’s untethered and roaming the floor?

In this chapter, we’ll see how Bluetooth Low Energy (LE) is well suited to this task, allowing us to get data from the robot, use an app to graph data, and even remotely control our robot from our smartphone!

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

  • Wireless robot connection options
  • Connecting Bluetooth LE to Raspberry Pi Pico
  • Making a Bluetooth LE sensor feed on Raspberry Pi Pico
  • Teleoperating the robot with Bluetooth LE

Technical requirements

This chapter requires the following:

  • The robot from Chapter 8, Sensing Distances to Detect Objects with Pico
  • An Adafruit Bluefruit LE UART Friend ADA2479
  • 1 x eight-way single-row 2.54-mm header (included with the module)
  • 5 x male-to-female jump wires with a 2.54-mm DuPont connector
  • Access to an Android or iOS smartphone
  • Velcro hook and loop dots
  • The code from previous chapters
  • A Raspberry Pi Pico code editor such as Mu or Thonny
  • A USB micro cable

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

Wireless robot connection options

So far, we’ve been working with the robot tethered to our computer. We send code to it and use the REPL tools to see what it is doing or printing out. While the REPL tools can be convenient, having a wire between the computer and the robot is not so convenient and limits how far the robot can drive or has you running behind it with the laptop. The following diagram shows how we could do things:

Figure 9.1 – Robot connections

The top part of the diagram shows things tethered with a wire. But the bottom part shows that the computer and the robot are not physically wired together. Instead, they are using wireless to send data to each other.

Once we are wireless, we can also consider a smartphone coming in as an alternative item. We can use a wireless medium to send data from the robot’s sensors or code to see what is going on and monitor it. We can also send control signals to take control and drive our...

Connecting Bluetooth LE to Raspberry Pi Pico

The Bluefruit LE UART Friend is relatively simple to wire in. First, the module will need headers soldered onto it, and then we can look at how to attach it to the robot physically and how to wire it. We will then connect to it from our robot and a smartphone.

Adding Bluetooth LE will result in our robot having a block diagram as follows:

Figure 9.2 – Robot block diagram with Bluetooth

The preceding diagram shows the robot blocks with the additional Adafruit Bluefruit LE UART Friend (marked as a Bluefruit module) connected via UART to Raspberry Pi Pico.

Solder a set of male headers onto the board using the same techniques used for the modules in Chapter 8, Sensing Distances to Detect Objects with Pico.

Attaching the Bluetooth module to the robot

We want to place the module above devices that are most likely to restrict and interfere with the Bluetooth. We created a breakout shelf in Chapter 7,...

Getting sensor data over Bluetooth LE on Raspberry Pi Pico

So far, you’ve tested the sensor-based examples, seeing their output in the console by connecting your laptop to it. However, building on our hello world example and the distance sensing in Chapter 8, Sensing Distances to Detect Objects with Pico, we can not only see the sensor output over UART as text but also plot in in a graph. So, let’s get into it.

We’ll put this code in a folder named bluetooth-distance-sensors. Copy in the robot.py and pio_encoder.py files. We will add code.py. Let’s start with the imports, combining the sensors and bus setup:

import board
import time
import busio
import robot
uart = busio.UART(board.GP12, board.GP13, baudrate=9600)

With the UART now prepared, we can prepare the sensors:

robot.left_distance.distance_mode = 1
robot.left_distance.start_ranging()
robot.right_distance.distance_mode = 1
robot.right_distance.start_ranging()

We’ve set both sensors...

Controlling the robot with Bluetooth LE

Bluetooth LE is a two-way medium. In this section, we’ll see how to receive data from the UART and, better yet, how to decode that data into control signals. By the end of this section, you’ll be able to drive your robot with a smartphone!

Printing what we got

Before we try to decode control packets, let’s just make a simple app to echo whatever shows on the Bluefruit UART out onto the Raspberry Pi Pico console.

Put the following code in bluetooth-print-incoming/code.py. We start by importing and setting up the UART port:

import board
import busio
uart = busio.UART(board.GP12,board.GP13,baudrate=9600, timeout=0.01)
print("Waiting for bytes on UART...")

The one difference here is that I’ve added a short timeout. Without the short timeout, the port will wait a full second for the number of bytes read. You might have noticed with the Hello world example that it took a second before you got the...

Summary

In this chapter, we have seen how to hook a Bluefruit LE transceiver to our robot and then use it to send and receive data. We’ve seen the robot data go to a smartphone and data go from a smartphone back to Pico on the robot.

We then took this up a level and sent formatted data to plot sensor information on the phone, allowing us to remotely visualize the robot’s state.

Finally, we used the smartphone app to control and drive the robot. In the next chapter, we will look at the PID algorithm, a neat way to tie sensor data and outputs together in a feedback loop, and we’ll use our new remote data plotting ability to tune it!

Exercises

These exercises let you extend the functionality of your robot code and deepen your understanding of the topics:

  • In the Bluetooth control app, there are four numeric buttons. Could you extend the control program to use these to control the robot’s speed?
  • The Bluetooth control pad app also has a little window to show messages. Try sending messages back from the robot code to the app to show in this window.
  • Could you use the plotting code with the encoder counts and plot these? Perhaps divide their total counts by elapsed time in the code, or reset the encoder counts and reread them to plot a rate per second.

Further reading

Adafruit also has a #help-with-radio channel on their Discord with a community that specifically helps with problems, questions, and ideas about their transceiver modules.

  • For further information on Bluetooth LE, check out Building Bluetooth Low Energy Systems by Muhammad Usama bin Aftab. This book has a detailed dive into wireless network communication systems suitable for use in Internet of Things (IoT). IoT concepts translate well into robotics.
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 2023 Publisher: Packt ISBN-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.
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}