The_MagPi_issue_8.pdf

(15692 KB) Pobierz
ISSUE 08 - DEC 2012
Visit our Kickstarter
http://kck.st/TvkdvG
for printed MagPi!
A Magazine for Raspberry Pi Users
Catch Santa using
home automation
This Issue. . .
Win a 512MB
Raspberry Pi
• Skutter
• Nanpy
• Pi Gauge
• Pibow
• CESIL Pi
• C++
• Ada
• MySQL
• Python Pi t
Merry Christmas
From The MagPi
h t t p : / / www . t h ema g p i . c om
Raspberry Pi is a trademark of The Raspberry Pi Foundation.
This magazine was created using a Raspberry Pi computer.
1023902867.123.png 1023902867.134.png 1023902867.145.png 1023902867.156.png 1023902867.001.png 1023902867.012.png 1023902867.023.png 1023902867.033.png 1023902867.044.png 1023902867.055.png 1023902867.066.png 1023902867.077.png 1023902867.081.png 1023902867.082.png 1023902867.083.png 1023902867.084.png 1023902867.085.png 1023902867.086.png 1023902867.087.png 1023902867.088.png 1023902867.089.png 1023902867.090.png 1023902867.091.png 1023902867.092.png 1023902867.093.png 1023902867.094.png 1023902867.095.png 1023902867.096.png 1023902867.097.png 1023902867.098.png 1023902867.099.png 1023902867.100.png 1023902867.101.png 1023902867.102.png 1023902867.103.png 1023902867.104.png 1023902867.105.png 1023902867.106.png 1023902867.107.png 1023902867.108.png 1023902867.109.png 1023902867.110.png 1023902867.111.png 1023902867.112.png 1023902867.113.png 1023902867.114.png 1023902867.115.png 1023902867.116.png 1023902867.117.png 1023902867.118.png 1023902867.119.png 1023902867.120.png 1023902867.121.png 1023902867.122.png 1023902867.124.png 1023902867.125.png 1023902867.126.png 1023902867.127.png 1023902867.128.png 1023902867.129.png 1023902867.130.png 1023902867.131.png 1023902867.132.png 1023902867.133.png 1023902867.135.png 1023902867.136.png 1023902867.137.png 1023902867.138.png 1023902867.139.png 1023902867.140.png 1023902867.141.png 1023902867.142.png 1023902867.143.png 1023902867.144.png 1023902867.146.png 1023902867.147.png 1023902867.148.png 1023902867.149.png 1023902867.150.png 1023902867.151.png 1023902867.152.png 1023902867.153.png 1023902867.154.png 1023902867.155.png 1023902867.157.png 1023902867.158.png 1023902867.159.png 1023902867.160.png 1023902867.161.png 1023902867.162.png 1023902867.163.png 1023902867.164.png 1023902867.165.png 1023902867.166.png 1023902867.002.png 1023902867.003.png 1023902867.004.png 1023902867.005.png 1023902867.006.png 1023902867.007.png 1023902867.008.png 1023902867.009.png 1023902867.010.png 1023902867.011.png 1023902867.013.png 1023902867.014.png 1023902867.015.png 1023902867.016.png 1023902867.017.png 1023902867.018.png 1023902867.019.png 1023902867.020.png 1023902867.021.png 1023902867.022.png 1023902867.024.png 1023902867.025.png 1023902867.026.png 1023902867.027.png
 
Welcome to the eighth edition of the MagPi magazine,
It’s Christmas! In this issue we hope to entice you into some festive projects to try after gorging yourself to the brim with
Christmas pudding.
In this month’s edition, we introduce you to a simple home automation project allowing you to control lights and
appliances in your house using the power of the Pi! Just in time to catch Mr Claus! We get your Skutter project in motion
with Morphy’s article on adding wheels to your base. Gordon teaches us how to light up a Xmas tree, we have more on
using the Pi to control an Arduino and Ben describes how to control servos attached to the Pi using the Internet! If this
isn’t enough we have more of the old favourites plus an introduction to SQL.
As per always, we have some great gifts for you to win in our monthly magazine. The MagPi would like to say a big
thank you yet again to PC Supplies who this month has outdone themselves by offering up a 512MB Raspberry Pi for
grabs!
In addition to this we have some exciting news for you this month. As of December 1st, we at the MagPi, are so excited
to be able to offer our readers the possibility of a printed version of all eight issues of the magazine! This is something
which gets constantly requested of us from our readers. All eight issues will be beautifully wrapped up in limited edition
MagPi binder making it a great gift to yourself or any of your loved ones of any age. For more information on this please
visit www.kickstarter.com/projects/themagpi/the-magpi-magazine-from-virtual-to-reality
On behalf of the whole team, thank you again for all your support. We hope you have a fantastic Christmas and we will
see you in the New Year (1st of February). While we have not managed to squeeze it into this issue, you might be
interested in www.xmas4all.co.uk from which you will be able to control their Raspberry Pi powered Christmas lights!
Ash Stone
Chief Editor of The MagPi
A
2
1023902867.028.png 1023902867.029.png 1023902867.030.png 1023902867.031.png 1023902867.032.png 1023902867.034.png 1023902867.035.png 1023902867.036.png 1023902867.037.png 1023902867.038.png 1023902867.039.png 1023902867.040.png 1023902867.041.png 1023902867.042.png 1023902867.043.png 1023902867.045.png 1023902867.046.png 1023902867.047.png 1023902867.048.png 1023902867.049.png 1023902867.050.png 1023902867.051.png 1023902867.052.png 1023902867.053.png
Contents
Dig out the toolbox for the next thrilling installment, by Bodge N Hackitt
Control your home with a Raspberry Pi and catch Santa in the act! by Geoff Johnson
Win a 512Mb Raspberry Pi Model B, from PC Supplies UK
The power of Raspberry and the simplicity of Arduino using Nanpy, by Andrea Stagi
Control servos over the internet, by Ben Schaefer
Simon Monk's new book covering basic to full GPIO python examples.
An interview with the designers of the PiBow case, by Chris Stagg
Christmas from the 70's using the CESIL programming language, by Gordon Henderson
Using basic variables and STL strings, by Alex Kerr
The second installment in our Ada programming tutorial, by Luke A. Guest
Get your teeth into some Structured Query Language (SQL), by Richard Wenner
Raspberry Jams and other community events
Creating multiple desktop widgets, by Colin Deady
3
1023902867.054.png 1023902867.056.png 1023902867.057.png 1023902867.058.png 1023902867.059.png 1023902867.060.png 1023902867.061.png 1023902867.062.png 1023902867.063.png 1023902867.064.png 1023902867.065.png 1023902867.067.png 1023902867.068.png 1023902867.069.png 1023902867.070.png 1023902867.071.png
Adding a motorised base
Part 2
This diagram shows a simplified version of
such a circuit. Closing switches 1 and 2
effectively connects the positive rail of the
power supply to the + terminal on the motor
and ground to the – terminal and the motor
runs forwards. Alternatively, closing switches
3 and 4 connects the ground to the + terminal
and the positive rail to the – terminal and the
motor runs in reverse.
In the last article we looked at some physical
means of adding motors to a robot and
investigated adapting some motorised
electronic toys as a potential source for robot
bases.
In this article I will begin to explain how you
can build your very own DC electronic motor
driver module and write a basic control
program for it.
There is a potentially dangerous situation if
switches 1 and 4 or 3 and 2 are closed. This
would create a short circuit between Power
Supply + and Ground which can be very
problematic to say the least. Care must be
taken when controlling this circuit to ensure
that this situation can never happen.
We will start by re-examining the standard DC
motor that was covered in the previous article.
To make the motor run forwards we apply a
power source between the + and – terminals
on the motor and to make it run in reverse,
simply swap the power source terminals
around.
In reality we can’t have four physical on / off
switches like this as we need to control the
circuit using the GPIO on the Raspberry Pi.
There are electronic solutions to this. One
possibility is the use of electromagnetic relays
to close these “switches”, however the
Raspberry Pi is not able to deliver enough
power from the GPIO to directly activate such
a relay without having something in-between
such as a transistor. Thid leads us to the
second possible solution which is to simply
use some transistors as switches.
The motor driver module we are going to
create will need to be a circuit which is able to
do this swapping around of the power supply
terminals
electronically.
This
can
be
accomplished using a “H bridge circuit”.
The transistor is arguably the most important
electronic invention ever created. Its
development is responsible for everything
from portable music players to the processor
used in the Raspberry Pi.
4
1023902867.072.png 1023902867.073.png 1023902867.074.png 1023902867.075.png 1023902867.076.png 1023902867.078.png
 
We will be looking at NPN type transistors.
This device has three terminals called base,
collector and emitter.
draw when our robotic base is trundling along
the floor. One way to accurately obtain this
measurement is to make the base move on
the ground and measure the current that is
being drawn. Here is the method I used with
my Big Trak:
Connecting a power supply across the
collector and emitter allows the transistor to be
used as a switch. Without a connection to the
base, the “internal resistance” of the transistor
is extremely high and the “switch” is off.
Connect the multimeter in series between the
battery/power supply and one of the motors in
the Big Trak.
If we apply a current to the transistor base
then the internal resistance will drop by a
corresponding amount and more current will
flow from the collector to the emitter.
The transistor is able to vary its internal
resistance very quickly, tens of thousands of
times per second. (It’s this feature that allows
transistors to be used as amplifiers).
The amount that the current affects the
internal resistance of the transistor is defined
by a ratio known as the “DC current gain” and
is referred to as “h FE ”.
In our case we want to supply a current to the
base that will make the internal resistance be
zero – just like a closed switch. This is called
“Transistor Saturation” and there is an
equation which tells us the current we need to
apply to the base to make this happen,
I B
= I C
/ h FE
where I C is the collector current and I B is the
base current. In order to find out what this
current is it’s necessary to measure the
current that’s drawn by the motor. This means
an experiment is needed!
The second motor must also be connected to
the power supply and active otherwise only
one motor will try to drive the whole Big Trak
which will result in an inaccurate
measurement. However we only need to
measure the current drawn by one of these
two identical motors.
For this you will need your motorised base (in
my case it’s the modified Big Trak), a power
supply (some batteries) and a multimeter.
Add some weight to the Big Trak which
approximates the expected overall weight of
the finished robot. In the case of the Skutter
this includes adding the robot arm.
If you don’t own a multimeter yet, they are an
essential tool for anyone who is involved in
electronics and allow you take a wide range of
measurements including voltage, current,
resistance, capacitance and h FE . Maplins sell
one for £7.99 (CODE: N20AX).
Complete the circuit between the batteries and
motor, including the multimeter in series as
shown. As the Big Trak rolls along the floor,
take a measurement of the current which is
being drawn. Under the expected load for the
Skutter using this method, one of the two big
trak motors will draw a current of 2.5 Amps.
It is possible to obtain a good multimeter for
under ten pounds from a variety of retailers.
DC motors draw different currents under
different conditions. If a motor is “free-
wheeling” then the motor will draw a
comparatively small current.
CAUTION: When motor stalling was tested
the current drawn was approximately 20
Amps.
Alternatively a “stalled” motor (a motor that is
prevented from turning) will draw an extremely
high current. The harder we make a motor
work, the more current it will draw. In our case
we want to measure the current the motors
Continued over page...
5
1023902867.079.png 1023902867.080.png
Zgłoś jeśli naruszono regulamin