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 7: Drive and Turn – Moving Motors with Python

In this chapter, we will take the robot we started building in the last chapter, connect the motors to the Raspberry Pi, and build the Python code to make them move. We will cover programming techniques to create a layer between the physical robot and its behavior code, to reduce the impact of hardware changes. Our code and build will get the robot moving! We finish by programming the robot to drive a small set path. The robot code layer will serve as a foundation for all our robot behaviors, and the set path will demonstrate how to use it.

We cover the following topics in this chapter:

  • Writing code to test your motors
  • Steering a robot
  • Making a Robot object—code for our experiments to talk to the robot
  • Writing a script to follow a predetermined path

Technical requirements

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

  • A computer with access to the internet
  • The chassis built in the Chapter 6, Building Robot Basics – Wheels, Power, and Wiring
  • The motor controller bought in Chapter 6, Building Robot Basics – Wheels, Power, and Wiring
  • A 2-meter by 2-meter flat space for the robot to drive on

    Important note

    Be prepared to stop your robot from driving over the edges if you use a table! It's best to use the floor.

Check out the following video to see the code in action: https://bit.ly/39sHxWL

Writing code to test your motors

Before we get stuck in and do fancy things with the motors, we need to get them set up and test them. This way, we can make sure they work and iron out any problems.

We need to download the library to work with the motor board we have chosen. Many robot parts, apart from the simplest ones, have an interface library to control the motors and other devices on the board. It's time to log in to your Pi using PuTTY again.

Preparing libraries

We download this code from a project on GitHub using Git on the Raspberry Pi. So, we need to install Git on the Pi; we also need I2C (i2c-tools and python3-smbus) and pip to install things into Python. Type the following command:

pi@myrobot:~ $ sudo apt-get install -y git python3-pip python3-smbus i2c-tools

To get the library for the motor board, Raspi_MotorHAT, we use Git and download it from GitHub, installing it for use in any of your scripts with the following command:

pi@myrobot:~ $ pip3 install...

Steering a robot

Now, we've made a robot drive forward. But how do we steer it? How does it turn left or right? In order to understand this, we need to first learn about a few significant forms of steering that exist. Let's take a look at some, settle on the one our robot uses, and write some test code to demonstrate it.

Types of steering

The most common techniques for steering a wheeled vehicle (including a robot) fall into two major categories—steerable wheels and fixed wheels, as discussed in the following subsections. Each of them comes with a couple of slightly unusual variants.

Steerable wheels

In movable wheel designs, one or more wheels in a robot face in a different direction from the others. When the robot drives, the differently positioned wheel makes the robot turn. There are two common styles of movable wheel steering on a robot, as shown here in Figure 7.2:

Figure 7.2 – Steerable wheel types

The green arrows...

Making a Robot object – code for our experiments to talk to the robot

Now we have seen how to move and turn our robot, we come on to a layer of software to group up some of the hardware functions and isolate them from behaviors. By behaviors, I mean code to make a robot behave a certain way, for example following a line or avoiding walls. Why would we want that isolation?

When we chose our motor controller, we made many trade-offs to find what works for our project. Motor controllers can change when the considerations change or when we simply want to build our next robot. Although controlling the speed and direction of two motors is the same kind of operation, each controller does it slightly differently. Creating a layer in front of a controller lets us use the same commands for it, even if it changes. This layer acts as a façade or interface to robot functionality.

Each controller has quirks. With this one, we set a run mode and speed. Many controllers use 0 to...

Writing a script to follow a predetermined path

So, we now get to the first behavior that feels like a robot. Let's make a quick sketch of a path for us to get our robot to follow. For an example, see Figure 7.7 here:

Figure 7.7 – Path for our robot

In Figure 7.7, I've drawn a path. The straight lines are for driving forward; the 1s mean 1 second. We don't yet have a way to consider distance traveled, only time. We may be able to guess at times relative to distances, but this isn't very precise or repeatable. The gentle curves are a turn where we slow one motor down more than the other.

The final spiral means a victory spin on the spot when the path is complete—we can do this by putting one motor in reverse while the other drives forward.

Let's write this code. First, we want the imports: sleep and robot. But before we do anything, let's make some helper functions for this behavior. I called my file behavior_path...

Summary

In this chapter, we've learned how to install the libraries for the motor board and demonstrate that our motors work. We then started building the first layer of code for our behaviors to use, while noting how we could make a layer like that for other robots. We saw our robot move in a path and tuned it, while finding out some of the shortcomings of using motors without any sensors.

You can now use this when starting any hardware project: get the motors/output devices tested first, then create a layer for a behavior to use them, such that if their hardware later changes, you only need to change the motor code.

In the following chapters, we start adding sensors and building behaviors using these sensors.

Exercises

Try these further ideas to enhance your learning from this chapter:

  1. Sketch out another simple path and write code for the robot to follow it. For example, try to follow a figure-of-8 shape using your experience.
  2. Which methods would you add to the Robot object if you had an additional output to control, perhaps a single light-emitting diode (LED)?
  3. Consider how you would lay out a Robot object for a robot with kart-style steering. Which methods would it have? You don't need to write the code yet, but having an interface in mind is a good start. Hint—it probably has one motor speed for the drive and a motor position for the steering.

Further reading

Please refer to the following for more information:

  • For more information on the style used for the Robot object, along with the use of similar interfaces and classes, I recommend Learning Object-Oriented Programming, Gastón C. Hillar, Packt Publishing. This book not only works through these concepts in Python but takes them more generally and shows how object-oriented (OO) concepts also apply to the C# and JavaScript languages.
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