Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn Robotics Programming - Second Edition

You're reading from  Learn Robotics Programming - Second Edition

Product type Book
Published in Feb 2021
Publisher Packt
ISBN-13 9781839218804
Pages 602 pages
Edition 2nd Edition
Languages
Concepts
Author (1):
Danny Staple Danny Staple
Profile icon Danny Staple

Table of Contents (25) Chapters

Preface Section 1: The Basics – Preparing for Robotics
Chapter 1: Introduction to Robotics Chapter 2: Exploring Robot Building Blocks – Code and Electronics Chapter 3: Exploring the Raspberry Pi Chapter 4: Preparing a Headless Raspberry Pi for a Robot Chapter 5: Backing Up the Code with Git and SD Card Copies Section 2: Building an Autonomous Robot – Connecting Sensors and Motors to a Raspberry Pi
Chapter 6: Building Robot Basics – Wheels, Power, and Wiring Chapter 7: Drive and Turn – Moving Motors with Python Chapter 8: Programming Distance Sensors with Python Chapter 9: Programming RGB Strips in Python Chapter 10: Using Python to Control Servo Motors Chapter 11: Programming Encoders with Python Chapter 12: IMU Programming with Python Section 3: Hearing and Seeing – Giving a Robot Intelligent Sensors
Chapter 13: Robot Vision – Using a Pi Camera and OpenCV Chapter 14: Line-Following with a Camera in Python Chapter 15: Voice Communication with a Robot Using Mycroft Chapter 16: Diving Deeper with the IMU Chapter 17: Controlling the Robot with a Phone and Python Section 4: Taking Robotics Further
Chapter 18: Taking Your Robot Programming Skills Further Chapter 19: Planning Your Next Robot Project – Putting It All Together Other Books You May Enjoy

Chapter 8: Programming Distance Sensors with Python

In this chapter, we look at distance sensors and how to use them to avoid objects. Avoiding obstacles is a key feature in mobile robots, as bumping into stuff is generally not good. It is also a behavior that starts to make a robot appear smart, as if it is behaving intelligently.

In this chapter, we find out about the different types of sensors and choose a suitable type. We then build a layer in our robot object to access them and, in addition to this, we create a behavior to avoid walls and objects.

You will learn about the following topics in this chapter:

  • Choosing between optical and ultrasonic sensors
  • Attaching and reading an ultrasonic sensor
  • Avoiding walls – writing a script to avoid obstacles

Technical requirements

To complete the hands-on experiments in this chapter, you will require the following:

  • The Raspberry Pi robot and the code from the previous chapters.
  • Two HC-SR04P, RCWL-1601, or Adafruit 4007 ultrasonic sensors. They must have a 3.3 V output.
  • A breadboard.
  • 22 AWG single-core wire or a pre-cut breadboard jumper wire kit.
  • A breadboard-friendly single pole, double toggle (SPDT) slide switch.
  • Male-to-female jumpers, preferably of the joined-up jumper jerky type.
  • Two brackets for the sensor.
  • A crosshead screwdriver.
  • Miniature spanners or small pliers.

The code for this chapter is available on GitHub at https://github.com/PacktPublishing/Learn-Robotics-Programming-Second-Edition/tree/master/chapter8.

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

Choosing between optical and ultrasonic sensors

Before we start to use distance sensors, let's find out what these sensors actually are, how they work, and some of the different types available.

The most common ways in which to sense distance are to use ultrasound or light. The principle of both of these mechanisms is to fire off a pulse and then sense its reflected return, using either its timing or angle to measure a distance, as can be seen in the following diagram:

Figure 8.1 – Using pulse timing in a distance sensor

We focus on the sensors that measure the response time, otherwise known as the time of flight. Figure 8.1 shows how these sensors use reflection time.

With this basic understanding of how sensors work, we'll now take a closer look at optical sensors and ultrasonic sensors.

Optical sensors

Light-based sensors, like the one in Figure 8.2, use infrared laser light that we cannot see. These devices can be tiny; however...

Attaching and reading an ultrasonic sensor

First, we should wire in and secure these sensors to the robot. We then write some simple test code that we can use to base our behavior code on in the next section. After completing this section, the robot block diagram should look like Figure 8.7:

Figure 8.7 – Robot block diagram with ultrasonic sensors

This diagram builds on the block diagram in Figure 6.33 from Chapter 6, Building Robot Basics – Wheels, Power, and Wiring by adding left and right ultrasonic sensors. Both have bi-directional arrows to the Raspberry Pi, since, being an active sensor, the Raspberry Pi triggers a sensor measurement and then reads back the result. Let's attach the sensors to the robot chassis.

Securing the sensors to the robot

In the Technical requirements section, I added an HC-SR04 bracket. Although it is possible to make a custom bracket with CAD and other part making skills, it is more sensible to use one...

Avoiding walls – writing a script to avoid obstacles

Now that we have tested both sensors, we can integrate them with our robot class and make obstacle avoidance behavior for them. This behavior loop reads the sensors and then chooses behavior accordingly.

Adding the sensors to the robot class

So, before we can use the sensors in a behavior, we need to add them to the Robot class, assigning the correct pin numbers for each side. This way, if pin numbers change or even the interface to a sensor changes, behaviors will not need to change:

  1. To use the DistanceSensor object, we need to import it from gpiozero; the new code is in bold:
    from Raspi_MotorHAT import Raspi_MotorHAT
    from gpiozero import DistanceSensor
  2. We create an instance of one of these DistanceSensor objects for each side in the robot class. We need to set these up in the constructor for our robot. We use the same pin numbers and queue length as in our test:
    class Robot:
        def __init__...

Summary

In this chapter, we have added sensors to our robot. This is a major step as it makes the robot autonomous, behaving on its own and responding in some way to its environment. You've learned how to add distance sensing to our robots, along with the different kinds of sensors that are available. We've seen code to make it work and test these sensors. We then created behaviors to avoid walls and looked at how to make a simplified but flawed behavior, and how a more sophisticated and smoother behavior would make for a better system.

With this experience, you can consider how other sensors could be interfaced with your robot, and some simple code to interact with them. You can output data from sensors so you can debug their behavior and create a behavior to make a robot perform some simple navigation on its own.

In the next chapter, we look further into driving predetermined paths and straight lines using an encoder to make sure that the robot moves far more accurately...

Exercises

  1. Some robots get by with just a single sensor. Can you think of a way of avoiding obstacles reliably with a single sensor?
  2. We have a pan/tilt mechanism, which we use later for a camera. Consider putting a sensor on this, and how to incorporate this into a behavior.
  3. The robot behavior we created in this chapter can reverse into things. How could you remedy this? Perhaps make a plan and try to build it.

Further reading

Please refer to the following links for more information:

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learn Robotics Programming - Second Edition
Published in: Feb 2021 Publisher: Packt ISBN-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.
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}