Getting Started With Raspberry Pi Zero - Sample Chapter
Getting Started With Raspberry Pi Zero - Sample Chapter
$ 29.99 US
19.99 UK
P U B L I S H I N G
Richard Grimmett
ee
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Sa
m
Richard Grimmett
since his very first programming project that used Fortran on punch cards. He has
a bachelor's and master's degree in electrical engineering and a PhD in leadership
studies. He also has 26 years of experience in the radar and telecommunications
industries, and even has one of the original brick phones. He now teaches computer
science and electrical engineering at the Brigham Young University, Idaho, where his
office is filled with his numerous robotics projects.
Preface
For many years, robots and other advanced electronic wonders could only be
seen on the television, movies, or in university or military labs. In recent years,
however, the availability of new and inexpensive hardware and also free and
open source software, has provided the opportunity for almost anyone with a
little technical knowledge and imagination to build these technical wonders.
The first wave of projects were fueled by Arduino, an inexpensive and
simple-to-program microcontroller. The next wave was carried further by
the introduction of the Raspberry Pi, an even more capable processor powered
by the Linux operating system.
Now there is an even less expensive, powerful microprocessor: the Raspberry Pi
Zero. This little processor packs a processor powerful enough to run Linux into
a small and even less expensive package. This capability, coupled with some
additional power, inexpensive hardware, and free open source software provides
a platform for projects that range from simple wheeled robots to advanced
flying machines.
Preface
Chapter 3, Accessing the GPIO Pins on Raspberry Pi Zero, discusses the GPIO
capabilities of Raspberry Pi Zero by building and controlling some simple
LED circuits.
Chapter 4, Building and Controlling a Simple Wheeled Robot, discusses one of the
amazing things you can do with Raspberry Pi Zero, controlling a simple wheeled
robot. This chapter will show you how to add motor control, so you can build your
very own autonomous mobile robot.
Chapter 5, Building a Robot That Can Walk, tells us about another impressive
robotic project, an autonomous robot that can walk. This is done using servos
whose position can be controlled using Raspberry Pi and some additional
USB-controlled hardware.
Chapter 6, Adding Voice Recognition and Speech A Voice Activated Robot, tells us about
a voice-activated robot. One of the significant new features of today's computer
system is the ability to input commands and provide output without a screen or
keyboard. A few years ago, the concept of a computer that can talk and listen was
science fiction, but today it is becoming a standard part of new cell phones. This
chapter introduces how Raspberry Pi Zero can both listen to speech and also respond
in kind. This is not as easy as it sounds (pun intended) and you'll be exposed to some
basic functionality, while also understanding some of the key limitations. You'll take
a standard toy and turn it into a responsive robot.
Chapter 7, Adding Raspberry Pi Zero to an RC Vehicle, tells us about another astounding
capability of Raspberry Pi Zero, the ability to add "sight" to you projects. Raspberry
Pi Zero makes this very easy by supporting open source software and readily
available USB webcams. By adding this and a remote control, you can build a remote
control vehicle that can go around corners, into rooms, wherever you'd like to go.
Chapter 8, Playing Rock, Paper, or Scissors with Raspberry Pi Zero, tells us about how we
can use our toolkit to build and control a robotic hand that can see and respond to
the world around it. In this case, you'll program your hand to play rock, paper,
and scissors.
Chapter 9, Adding Raspberry Pi Zero to a Quadcopter, talks about the fact that building
a robot that can walk, talk, or play air hockey is cool, but one that can fly is
the ultimate goal.
[ 77 ]
Let's look at a couple of the more popular units that come fully assembled or can be
assembled with simple tools (a screwdriver or pliers). The simplest mobile platform
is one that has two DC motors, with each motor controlling a single wheel. On the
wheeled platform, there is a small wheel or ball in the front or at the back. Here is
one example of a wheeled platform, available at many online electronics retailers:
[ 78 ]
Chapter 4
This one also needs to be assembled but it is fairly straightforward. You could
also choose a tracked platform instead of a wheeled platform. A tracked platform
has more traction but is not as nimble, as it takes a longer distance to turn. Again,
manufacturers make pre-assembled units. The following image is an example
of a pre-assembled tracked platform made by Dagu. It's called the Dagu Rover
5 Tracked Chassis:
As part of the platform, you'll need a mobile power supply for the Raspberry Pi Zero
and your vehicle. I personally like the external 5V rechargeable cell phone batteries
which are available at almost any place that sells cell phones. These batteries can be
charged using a USB cable connected either through a DC power supply or directly
from a computer USB port, as shown in the following image:
You'll also need a USB cable to connect your battery to the Raspberry Pi Zero.
Now that you have the basic platform, you're ready to start controlling it with the
Raspberry Pi Zero.
[ 79 ]
There are two choices here and I'll walk you through both. Firstly, you can use a
chip called an H-bridge, plug it into your electronic breadboard, and control the DC
motors with connections to the GPIO of the Raspberry Pi Zero. The second choice
is to use a dedicated motor controller board designed to connect directly onto the
Raspberry Pi Zero's GPIO pins. Let's cover the H-bridge option first.
[ 80 ]
Chapter 4
You can, however, connect your Raspberry Pi Zero to the DC motors by using an
H-bridge DC motor controller. An H-bridge is a fairly simple device. It basically
consists of a set of electronic switches and provides the additional functionality of
allowing the direction of the current to be reversed so that the motor can be run in
either forward or reverse directions.
Let's start this example by building the H-bridge circuit and controlling just one
motor. To do this, you need to get an H-bridge. One of the most common options
is the L293 dual H-bridge chip. This chip allows you to control the direction of the
DC motors. These are available at most electronics stores and online. Once you have
your H-bridge, build the circuit as shown in the following image with the Raspberry
Pi Zero, the motor, the jumper wires, a 4AA battery holder, and a breadboard:
[ 81 ]
Also, before you start connecting wires, here is an image of the GPIO pins on the
Raspberry Pi Zero board:
You need to connect these pins on the Raspberry Pi Zero GPIO to the pins on the
H-bridge, as shown in the following table:
Raspberry Pi Zero GPIO pin
H-bridge pin
4 (5V)
1 (Enable pin)
13 (GPIO 27)
2 (Forward)
15 (GPIO 22)
7 (Backward)
4 (5V)
11 (Enable 2)
38 (GPIO 6)
10 (Forward)
40 (GPIO 13)
15 (Backward)
6 (GND)
4, 5, 12, 13 (GND)
2 (5 Volts)
16 (VCC)
8 (Vc)
Once you have the connections, you can test the system. To make this all work,
you need to add some code, which we will see in the next section.
[ 82 ]
Chapter 4
[ 83 ]
#!/usr/bin/python: This line lets you run the program without having to
type python before the filename. You'll learn how to do this at the end of
these instructions.
import RPi.GPIO as io: This lets you import the RPi library, which allows
def forward():: This defines the forward function. You have to turn on
GPIO 27 and GPIO 20 and turn off GPIO 22 and GPIO 21.
[ 84 ]
Chapter 4
def reverse():: This defines the reverse function. You'll turn on GPIO 22
and GPIO 21, and turn off GPIO 27 and GPIO 20.
def stop():: This defines the stop function. You set the level to 0 on the
while True:: This performs loops over and over. You can stop the program
pins off GPIO 22, GPIO 21, GPIO 27, and GPIO 20.
(this is GPIO 27).
(this is GPIO 22).
(this is GPIO 20).
(this is GPIO 21).
by pressing Ctrl + C.
next statement.
next statement.
next statement.
[ 85 ]
[ 86 ]
Chapter 4
The following is an explanation of the lines of code that you just added:
p1.start(0): This starts the pulses on p1, GPIO 27, with a pulse width of 0
percent, or off.
[ 87 ]
p2.start(0): This starts the pulses on p2, GPIO 22, with a pulse width of 0
p3.start(0): This starts the pulses on p3, GPIO 20, with a pulse width of 0
p4.start(0): This starts the pulses on p3, GPIO 21, with a pulse width of 0
def forward(50):: This function moves the unit forward by setting the
pulse width in a forward direction to 50 percent.
percent, or off.
percent, or off.
percent, or off.
percent off. This should result in the motor running forward at half speed.
def reverse(50):: This function moves the unit in reverse by setting the
pulse width in the reverse direction to 50 percent.
percent off. This should result in the motor running in reverse at half speed.
def stop():: This function sets all PWM signals to 0 percent, effectively
[ 88 ]
Chapter 4
The rest of the program is the same as the first dcmotor.py file. Running this
program should result in the unit running at half the speed of the first program. You
can easily change this speed by changing the value sent to the various start functions.
You can also control the DC motors by using a DC motor controller to connect to
the Raspberry Pi Zero directly. For example, Pololu, who can be found at https://
www.pololu.com/, make the DRV8835 Dual Motor Driver Kit for the Raspberry Pi.
Another option is the RasPiRobot Board V2 available at http://www.monkmakes.
com/. For this example, we will use the RasPiRobot Board V2.
[ 89 ]
The board provides the drive signals for the DC motors on the vehicle. You can
also turn the vehicle by driving each motor separately. You can change the vehicle's
direction and make very sharp turns by reversing the signals. The following steps
show how to connect the motor control board:
1. Connect the battery power connector to the power connector on the board.
Use a 6 to 7.4 volts battery; you can either use a 4 AA battery holder or a 2S
LiPo RC battery. Connect the ground and power wires to the motor control
board as shown:
[ 90 ]
Chapter 4
2. Next, connect one of the drive signals to the motor 1 connector on the board.
Connect motor 1 to the right motor and motor 2 to the left, as shown:
[ 91 ]
3. Then, connect the second drive connector to the motor 2 connector on the
board. The entire set of connections should look like this:
Now you are ready to drive your vehicle using the Raspberry Pi Zero.
[ 92 ]
Chapter 4
[ 93 ]
The second part of the code that drives the two different motors, based on whether
you want to go forwards, backwards, or turn right or left, is as follows:
The rr.set_motors() function allows you to specify the speed and direction of
each motor independently. This program takes in a single character and then sends a
command to the motors. f moves the vehicle forward, b moves it backward, l turns
it left, r turns it right, and s stops the vehicle.
[ 94 ]
Chapter 4
Now that you have the basic code to drive your tracked vehicle, you can modify it so
that each action is contained in a function. In that way, you can call these functions
from another Python program. You also need to add calibrated movement so that
your tracked vehicle is able to turn at a certain angle and move forwards a set
distance. The following example is what the code should look like:
If you have chosen to use the RasPiRobot Board V2 you can also use its special
connections and libraries to connect the HC-SR04 sonar sensor. Here is a picture
of the special connector on the board:
To use this connector, simply connect the VCC to the 5V, the Trig to the T
connection, the Echo to the E connector, and the GND to the GND connection. You
can then use the library for the motor controller board and simply call the function
rr.get_distance(). Similarly, there is also a special connector and libraries for the
I2C interface, if you want to add the compass to your mobile project.
[ 96 ]
Chapter 4
There are three key points on this grid that you need to understand. Here is an
explanation of them:
The lower left point is a fixed reference position. The directions x and y are
also fixed and all other positions are measured in relation to this position
and these directions. Each unit is measured with regards to how far the unit
travels in time in a single unit.
Another important point is the starting location of your robot. Your robot
will then keep track of its location using its x and y coordinates, the position
with respect to some fixed reference position in the x direction, or the
position with respect to some fixed reference position in the y direction
to the goal. It uses the compass to keep track of these directions.
The third important point is the position of the goal, also given in the x and
y coordinates with respect to the fixed reference position. If you know the
starting location and angle of your robot, you can plan an optimum (the
shortest distance) path to this goal. To do this, you can use the goal location,
the robot location and some fairly simple math to calculate the distance and
angle from the robot to the goal.
[ 97 ]
d=
(( Xgoal Xgoal )
+ (Ygoal Yrobot )
You use this equation to tell your robot how far to travel to reach the goal. A second
equation tells your robot the angle at which it needs to travel:
= arctan
(Ygoal Yrobot )
( Xgoal Xrobot )
Now that you have a goal angle and distance, you can program your robot to
move. To do this, you need to write a program to do the path planning and call the
movement functions that you created earlier in the chapter. You need to know the
distance that your robot will travel in a certain period of time so that you can tell
your robot in time units, rather than distance units, how far to travel.
You also need to be able to translate the distance that might be covered by your
robot in each time unit that you run the motor. If you know the angle and distance,
you can move your robot towards the goal by turning the robot and then running it
forward for a certain time.
[ 98 ]
Chapter 4
[ 99 ]
In this program, the user determines the goal location and the robot decides on the
shortest direction to the desired location by reading the angle. To make it simple, the
robot is positioned in the grid, heading in the direction of an angle of 0 degrees. If the
goal angle were less than 180 degrees, the robot would turn right. If it were greater
than 180 degrees, the robot would turn left. The robot turns until the desired angle
and the measured angle are within a few degrees of each other. Then, the robot takes
the number of steps to reach the goal. As an additional challenge, you could add a
loop to measure the actual angle and stop it when it reaches the target angle.
Summary
This chapter provided you with an opportunity to create a mobile platform for your
Raspberry Pi Zero. You can add the sonar sensor or the compass from Chapter 3,
Accessing the GPIO Pins on Raspberry Pi Zero to give it even more functionality. In the
next chapter, you'll learn how to build a Raspberry Pi Zero platform robot with legs,
an even more flexible mobile platform.
[ 100 ]
www.PacktPub.com
Stay Connected: