Raspberry Pi Robotics Essentials - Sample Chapter
Raspberry Pi Robotics Essentials - Sample Chapter
Preface
There have been many recent technological advances that have really changed the
way we live, work, and play. The television, the computer, and the cell phone all
have dramatically affected our lives. Each of these generally started out with a few
early adopters, for the most part, individuals with lots of resources that were able to
afford the new technology. However, soon after, there was a movement to make the
technology more affordable for a wider range of people.
The latest technological movement is robotics. The number, kind, and use of robots
is growing dramatically. The first of these robots were developed in university labs
or in military research centers. However, just as with the adaption of the computer,
there is already a growing grassroots movement of do-it-yourself developers that has
sprung up to make robots a part of our everyday life.
This movement has been fueled by inexpensive hardware and free, open source
software. However, it has also been enabled by a community of developers who are
willing to help others get started or overcome challenges that they have experienced.
This book is offered in the spirit of this do-it-yourself movement. Inside the book,
you'll find details about how to take Raspberry Pi B 2, an inexpensive, small, but
versatile computer, and marry it with inexpensive hardware and open source
software to build a bipedal robot that can walk, sense barriers, and even see its
surroundings.
However, be carefulthis sort of information can be dangerous. Before long, you
may be creating the next generation of thinking, walking, sensing machines that will
be at the heart of the robotic revolution.
Preface
Avoiding Obstacles
Using Sensors
You've constructed your biped robot. Now, your robot can move around. But what
if you want the robot to sense the outside world, so you don't run into things? In this
chapter, you'll discover how to add some sensors to help avoid barriers.
Specifically, you'll learn:
[ 69 ]
The different angles give you an indication of the distance from the object.
The sensor turns these angle measurements into a voltage that you can sense to
determine the distance. Unfortunately, the relationship between the output of the
sensor and the distance is not linear, so you'll need to do some calibration in order to
predict the actual distance and its relationship to the output of the sensor.
IR sensors are quite accurate, certainly with a low percentage of
errors; however, they may not work well if the area is brightly
lit. The accuracy is also affected by the reflective nature of
the material being sensed. This can be a consideration when
deciding which sensors to use.
Before you get started, you'll need to get a sensor. One of the more popular ones is an
inexpensive IR sensor by Sharp. It is available at many online electronics stores, and
it comes in models that sense various distances. You'll be using the Sharp 2Y0A02
model, a unit that provides sensing to a distance of 150 cm. Here is a picture of
the sensor:
[ 70 ]
Chapter 4
You'll also want to make sure you also get the connector cable for the device;
it normally comes with the device. Here is a picture of the sensor with the
cable attached:
As noted in the tutorial, the voltage out of the sensor will be a voltage that will be an
indication of the distance. However, this is an analog signal, and the Raspberry Pi
doesn't have an analog-to-digital converter that can convert this analog voltage to a
number that you can read in your program. You'll need to add an analog to digital
converter to your project.
[ 71 ]
There are two choices. If you want an analog-to-digital converter that plugs
directly into the USB interface, there is one offered by www.phidgets.com. This
board is really quite amazing; it takes the analog signals, turns them into digital
numbers using an analog to digital converter, and then makes them available so
that they can be read from the USB port. The model number of this part is 1011_0 PhidgetInterfaceKit 2/2/2 and it is shown in the following:
[ 72 ]
Chapter 4
This device is easier to program, so this is what you'll use in this project. Now, let's
connect the sensor:
1. Solder header pins to the ADC Pi+ board to connect it to the ADC, like this:
[ 73 ]
3. Now, you'll connect the IR sensor to the ADC. To connect this unit, you'll
connect the three pins that are available at the bottom of the sensor. Here is
the connection list:
ADC-DAC Board
Sensor Pin
5V
Vcc
GND
Gnd
In1
Vo
Unfortunately, there are no labels on the unit, but here are the pins
you'll connect:
It's easiest to connect to the three-wire cable that normally comes with the
sensor. Once the pins are connected, you are ready to access the data from
the sensor via a Python program on the Raspberry Pi. The entire system
looks like this:
[ 74 ]
Chapter 4
Now, you are ready to add some code to read the IR sensor. You'll need to follow
these steps to talk to the ADC:
1. The first step in enabling the ADC is to enable the I2C interface. This is done
by running raspi-config and selecting 8 Advanced Options like this:
[ 75 ]
2. Once there, go to the A7 I2C selection to enable the I2C like this:
Perform all the selections to enable the I2C interface and load the library,
and then reboot the Raspberry Pi.
You'll also need to edit the /etc/modules file and add the following
two lines:
[ 76 ]
Chapter 4
Reboot the Raspberry Pi. You can see whether the I2C is enabled by typing
sudo i2cdetect -y 1, and you should see something like this:
from the home directory, and the Python libraries will be installed on your
Raspberry Pi.
4. Go to the ./ABElecttronics_Python_Libraries/ADCPi directory; here
are the programs for your specific hardware. Following the instructions in
the README.md file, type sudo apt-get update, and then type sudo aptget install python-smbus. This will install the smbus library, which are
required for the ADC to work. Also, type sudo adduser pi i2c to add pi
to the group that can access i2c.
[ 77 ]
5. You'll need to edit your .bashrc file in your home directory, adding the
following lines:
Adding this line will add this library to the path so that you can access the
functionality. Reboot the Raspberry Pi.
6. Now, you can run one of the demo programs. Type python demoreadvoltage.py, and you should see something like this:
[ 78 ]
Chapter 4
These raw readings are great, but now you'll want to build a program that
takes the data from the first ADC and translates it to the distance. To do this,
you'll need a graph of the voltage to distance readings for your sensor. Here
is the graph for the IR sensor in this example:
There are really two parts to the curve; the first is the distance up to about
15 centimeters, and the second is the distance from 15 centimeters to 150
centimeters. It is easiest to build a simple mathematical model that ignores
distances closer than 15 centimeters and models the distance from 15
centimeters. For more information on how to build this model, refer to
http://davstott.me.uk/index.php/2013/06/02/raspberry-pi-sharpinfrared/. Here is the Python program using this model:
[ 79 ]
Now, you can measure the distance to objects using your IR sensor!
[ 80 ]
Chapter 4
The device sends out a sound wave 10 times a second. If an object is in the path of
these waves, then the waves reflect off the object, sending waves that return to the
sensor, as shown in the following figure:
The sensor then measures any return. It uses the time difference between when
the sound wave was sent out and when it returned to measure the distance from
the object.
[ 81 ]
Sonar sensors are also quite accurate, normally with low percentage
errors, and are not affected by the lighting or color in the environment.
There are several choices if you want to use a sonar sensor to sense the distance. The
first is to use a sonar sensor that connects to the USB port. The following is an image
of a USB sonar sensor:
This is the USB-ProxSonar-EZ sensor, and can be purchased directly from MaxBotix
or on Amazon. There are several models, each with a different distance specification;
however, they all work in the same way.
You can also choose a sonar sensor that connects to the GPIO of the Raspberry Pi.
Here is a picture of this sort of inexpensive sonar sensor:
[ 82 ]
Chapter 4
This sensor is less expensive and easy to use; it takes a bit of processing power to
coordinate the efforts of timing the send and receive signals, but the Raspberry Pi B
2 has the processing power needed. Here are the steps to set up this sonar sensor to
sense the distance:
1. The first step is to understand the GPIO pins of the Raspberry Pi B 2. Here is
a diagram of the layout of the pins:
In this case, you'll need to connect to the 5 volt connection of the Raspberry
Pi B2, which is pin 2. You'll also need to connect to the GND, which is pin 6.
You'll use pin 16 as an output trigger pin and pin 18 (GPIO24) as an input to
time the echo from the sonar sensor.
[ 83 ]
2. Now that you know the pins you'll connect to, you can connect the sonar
sensor. There is a problem, as you can't connect the 5 volt return from the sonar
sensor directly to the Raspberry Pi GPIO pins; they want 3.3 volts. You'll need
to build a voltage divider that will reduce the 5 volts to 3.3 volts. This can be
done with two resistors, which are connected as shown in this diagram:
[ 84 ]
Chapter 4
3. Now that the device is connected, you'll need a bit of code to read in the
value, make sure it is settled (a stable measurement), and then convert it to
distance. Here is the Python code for this program:
[ 85 ]
Now, you should be able to run the program and see a result, like this:
Now that you have your sensors up and working, you can avoid or find
objects with your biped.
Summary
Congratulations! You can now detect and avoid walls and other barriers to your
robot. You can also use these sensors to detect objects that you might want to find.
In the next chapter, you'll learn how to perform path planning to move your robot
from point A to point B and even give your robot intelligence as to what to do if it
encounters a barrier in its path.
[ 86 ]
www.PacktPub.com
Stay Connected: