The-MagPi-issue-8-en.pdf

(17779 KB) Pobierz
I I S S S S U U E E 0 0 8 8 ­ ­ D D E E C C 2 2 0 0 1 1 2 2
VisitourKickstarter
http://kck.st/TvkdvG
forprintedMagPi!
A A M M a a g g a a z z i i n n e e f f o o r r R R a a s s p p b b e e r r r r y y P P i i U U s s e e r r s s
C C a a t t c c h h S S a a n n t t a a u u s s i i n n g g
h h o o m m e e a a u u t t o o m m a a t t i i o o n n
T T h h i i s s I I s s s s u u e e . . . . . .
Wina512MB
RaspberryPi
S S k k u u t t t t e e r r
N N a a n n p p y y
P P i i G G a a u u g g e e
P P i i b b o o w w
C C E E S S I I L L P P i i
C C + + + +
A A d d a a
M M y y S S Q Q L L
P P y y t t h h o o n n P P i i t t
M M e e r r r r y y C C h h r r i i s s t t m m a a s s
F F r r o o m m T T h h e e M M a a g g P P i i
h h t t t t p p : : / / / / w w w w w w . . t t h h e e m m a a g g p p i i . . c c o o m m
RaspberryPiisatrademarkofTheRaspberryPiFoundation.
ThismagazinewascreatedusingaRaspberryPicomputer.
993337133.085.png 993337133.096.png 993337133.107.png 993337133.118.png 993337133.001.png 993337133.012.png 993337133.023.png 993337133.033.png 993337133.039.png 993337133.040.png 993337133.041.png 993337133.042.png 993337133.043.png 993337133.044.png 993337133.045.png 993337133.046.png 993337133.047.png 993337133.048.png 993337133.049.png 993337133.050.png 993337133.051.png 993337133.052.png 993337133.053.png 993337133.054.png 993337133.055.png 993337133.056.png 993337133.057.png 993337133.058.png 993337133.059.png 993337133.060.png 993337133.061.png 993337133.062.png 993337133.063.png 993337133.064.png 993337133.065.png 993337133.066.png 993337133.067.png 993337133.068.png 993337133.069.png 993337133.070.png 993337133.071.png 993337133.072.png 993337133.073.png 993337133.074.png 993337133.075.png 993337133.076.png 993337133.077.png 993337133.078.png 993337133.079.png 993337133.080.png 993337133.081.png 993337133.082.png 993337133.083.png 993337133.084.png 993337133.086.png 993337133.087.png 993337133.088.png 993337133.089.png 993337133.090.png 993337133.091.png 993337133.092.png 993337133.093.png 993337133.094.png 993337133.095.png 993337133.097.png 993337133.098.png 993337133.099.png 993337133.100.png 993337133.101.png 993337133.102.png 993337133.103.png 993337133.104.png 993337133.105.png 993337133.106.png 993337133.108.png 993337133.109.png 993337133.110.png 993337133.111.png 993337133.112.png 993337133.113.png 993337133.114.png 993337133.115.png 993337133.116.png 993337133.117.png 993337133.119.png 993337133.120.png 993337133.121.png 993337133.122.png 993337133.123.png 993337133.124.png 993337133.125.png 993337133.126.png 993337133.127.png 993337133.128.png 993337133.002.png 993337133.003.png 993337133.004.png 993337133.005.png 993337133.006.png 993337133.007.png 993337133.008.png 993337133.009.png 993337133.010.png 993337133.011.png 993337133.013.png 993337133.014.png 993337133.015.png 993337133.016.png 993337133.017.png 993337133.018.png 993337133.019.png 993337133.020.png 993337133.021.png 993337133.022.png 993337133.024.png 993337133.025.png 993337133.026.png 993337133.027.png
 
WelcometotheeightheditionoftheMagPimagazine,
It’sChristmas!Inthisissuewehopetoenticeyouintosomefestiveprojectstotryaftergorgingyourselftothebrimwith
Christmaspudding.
Inthismonth’sedition,weintroduceyoutoasimplehomeautomationprojectallowingyoutocontrollightsand
appliancesinyourhouseusingthepowerofthePi!JustintimetocatchMrClaus!WegetyourSkutterprojectinmotion
withMorphy’sarticleonaddingwheelstoyourbase.GordonteachesushowtolightupaXmastree,wehavemoreon
usingthePitocontrolanArduinoandBendescribeshowtocontrolservosattachedtothePiusingtheInternet!Ifthis
isn’tenoughwehavemoreoftheoldfavouritesplusanintroductiontoSQL.
Asperalways,wehavesomegreatgiftsforyoutowininourmonthlymagazine.TheMagPiwouldliketosayabig
thankyouyetagaintoPCSupplieswhothismonthhasoutdonethemselvesbyofferingupa512MBRaspberryPifor
grabs!
Inadditiontothiswehavesomeexcitingnewsforyouthismonth.AsofDecember1st,weattheMagPi,aresoexcited
tobeabletoofferourreadersthepossibilityofaprintedversionofalleightissuesofthemagazine!Thisissomething
whichgetsconstantlyrequestedofusfromourreaders.Alleightissueswillbebeautifullywrappedupinlimitededition
MagPibindermakingitagreatgifttoyourselforanyofyourlovedonesofanyage.Formoreinformationonthisplease
visitwww.kickstarter.com/projects/themagpi/the­magpi­magazine­from­virtual­to­reality
Onbehalfofthewholeteam,thankyouagainforallyoursupport.WehopeyouhaveafantasticChristmasandwewill
seeyouintheNewYear(1stofFebruary).Whilewehavenotmanagedtosqueezeitintothisissue,youmightbe
interestedinwww.xmas4all.co.ukfromwhichyouwillbeabletocontroltheirRaspberryPipoweredChristmaslights!
AshStone
ChiefEditorofTheMagPi
MagPi team
Ash Stone-ChiefEditor/Administrator
Chris 'tzj' Stagg-Writer/Photographer/PageDesigns
Colin Deady-Writer/PageDesigns
Jason 'Jaseman' Davies-Website/PageDesigns
Matt '0the0judge0'-Website/Administrator
Meltwater-Photographer/PageDesigns/Admin.
Aaron Shaw-PageDesigns/Graphics
Ian McAlpine-PageDesigns/Graphics
Lix-PageDesigns/Graphics
Sam Marshall-PageDesigns/Graphics
W. H. Bell-PageDesigns
Guest writers
Bodge N Hackitt-Writer
Geoff Johnson-Writer
Andrea Stagi-Writer
Ben Schaefer-Writer
Gordon Henderson-Writer
A lex Kerr-Writer
Luke Guest-Writer
Richard Wenner-Writer
2
993337133.028.png 993337133.029.png
Contents
04 SKUTTER RETURNS
Digoutthetoolboxforthenextthrillinginstallment,byBodgeNHackitt
08 HOME AUTOMATION - SANTA TRAP
ControlyourhomewithaRaspberryPiandcatchSantaintheact!byGeoffJohnson
11 THIS MONTH'S COMPETITION
Wina512MbRaspberryPiModelB,fromPCSuppliesUK
12 CONTROL YOUR ARDUINO WITH PYTHON & RASPBERRY PI
ThepowerofRaspberryandthesimplicityofArduinousingNanpy,byAndreaStagi
14 PI GAUGE
Controlservosovertheinternet,byBenSchaefer
17 BOOK PROMOTION - GETTING STARTED WITH PYTHON
SimonMonk'snewbookcoveringbasictofullGPIOpythonexamples.
18 PIBOW INTERVIEW
AninterviewwiththedesignersofthePiBowcase,byChrisStagg
20 CESIL POWERED CHRISTMAS TREE
Christmasfromthe70'susingtheCESILprogramminglanguage,byGordonHenderson
22 WELCOME TO THE C++ CACHE
UsingbasicvariablesandSTLstrings,byAlexKerr
24 BEGINING ADA
ThesecondinstallmentinourAdaprogrammingtutorial,byLukeA.Guest
26 DATABASE BOOTCAMP
GetyourteethintosomeStructuredQueryLanguage(SQL),byRichardWenner
29 THIS MONTH'S EVENTS LIST
RaspberryJamsandothercommunityevents
30 THE PYTHON PIT
Creatingmultipledesktopwidgets,byColinDeady
32 THE YEAR OF THE MAGPI
3
993337133.030.png 993337133.031.png
Addingamotorisedbase
DIFFICULTY: ADVANCED Part2
A simple switching “H bridge”
Thi s di agram shows a si mpl i fi ed versi on of
such a ci rcui t. Cl osi ng swi tches 1 and 2
effecti vel y connects the posi ti ve rai l of the
power suppl y to the + termi nal on the motor
and ground to the – termi nal and the motor
runs forwards. Al ternati vel y, cl osi ng swi tches
3 and 4 connects the ground to the + termi nal
and the posi ti ve rai l to the – termi nal and the
motor runs i n reverse.
I n the l ast arti cl e we l ooked at some physi cal
means of addi ng motors to a robot and
i nvesti gated adapti ng some motori sed
el ectroni c toys as a potenti al source for robot
bases.
I n
thi s
arti cl e
I
wi l l
begi n
to
expl ai n
how you
can
bui l d
your very own
DC el ectroni c motor
There
i s
a
potenti al l y
dangerous
si tuati on
i f
dri ver
modul e
and
wri te
a
basi c
control
swi tches 1
and 4 or 3 and 2 are cl osed. Thi s
program for i t.
woul d
create
a
short
ci rcui t
between
Power
Suppl y
+
and
Ground
whi ch
can
be
very
We wi l l start by re-exami ni ng the standard DC
motor that was covered i n the previ ous arti cl e.
probl emati c
to
say
the
l east.
Care
must
be
taken
when
control l i ng
thi s
ci rcui t
to
ensure
that thi s si tuati on can never happen.
To make the motor run
forwards we appl y a
power source between
the + and
– termi nal s
I n real i ty we can’ t have four physi cal on / off
swi tches
on
the
motor
and
to
make
i t
run
i n
reverse,
l i ke
thi s
as
we
need
to
control
the
si mpl y
swap
the
power
source
termi nal s
ci rcui t usi ng the GPI O on the Raspberry Pi .
around.
There are el ectroni c sol uti ons to thi s. One
possi bi l i ty i s the use of el ectromagneti c rel ays
to cl ose these “swi tches” , however the
Raspberry Pi i s not abl e to del i ver enough
power from the GPI O to di rectl y acti vate such
a rel ay wi thout havi ng
The motor dri ver modul e we are goi ng to
create wi l l need to be a ci rcui t whi ch i s abl e to
do thi s swappi ng around of the power suppl y
termi nal s
el ectroni cal l y.
Thi s
can
be
accompl i shed usi ng a “H bri dge ci rcui t” .
somethi ng
i n-between
such
as
a
transi stor.
Thi s
l eads
us
to
the
second
possi bl e
sol uti on
whi ch
i s
to
si mpl y
use some transi stors as swi tches.
Transistors as switches
The transi stor i s arguabl y the most i mportant
el ectroni c i nventi on ever created. I ts
devel opment i s responsi bl e for everythi ng
from portabl e musi c pl ayers to the processor
used i n the Raspberry Pi .
4
993337133.032.png 993337133.034.png 993337133.035.png 993337133.036.png
 
We
wi l l
be
l ooki ng
at
N PN
type
transi stors.
draw when our roboti c base i s trundl i ng al ong
the
Thi s
devi ce
has
three
termi nal s
cal l ed
base,
fl oor.
One
way
to
accuratel y
obtai n
thi s
col l ector and emi tter.
measurement
i s
to
make
the
base
move
on
the
ground
and
measure
the
current
that
i s
Connecti ng a power suppl y across the
col l ector and emi tter al l ows the transi stor to be
used as a swi tch. Wi thout a connecti on to the
base, the “i nternal resi stance” of the transi stor
i s extremel y hi gh and the “swi tch” i s off.
bei ng drawn. H ere i s the method I
used wi th
my Bi g Trak:
Connect the mul ti meter i n seri es between the
battery/power suppl y and one of the motors i n
the Bi g Trak.
I f
we
appl y
a
current
to
the
transi stor
base
then
the
i nternal
resi stance
wi l l
drop
by
a
correspondi ng
amount
and
more
current
wi l l
fl ow from the col l ector to the emi tter.
The transi stor i s abl e to vary i ts i nternal
resi stance very qui ckl y, tens of thousands of
ti mes per second. (I t’ s thi s feature that al l ows
transi stors to be used as ampl i fi ers) .
The amount that the current affects the
i nternal resi stance of the transi stor i s defi ned
by a rati o known as the “DC current gai n” and
i s referred to as “h FE ” .
I n our case we want to suppl y a current to the
base that wi l l make the i nternal resi stance be
near zero – j ust l i ke a cl osed swi tch. Thi s i s
cal l ed “Transi stor Saturati on” and there i s an
equati on whi ch tel l s us the current we need to
appl y to the base to make thi s happen,
I B =I C /h FE
where I C i s the col l ector current and I B i s the
base current. I n order to fi nd out what thi s
current i s i t’ s necessary to measure the
current that’ s drawn by the motor. Thi s means
an experi ment i s needed!
The second motor must al so be connected to
the power suppl y and acti ve otherwi se onl y
one motor wi l l try to dri ve the whol e Bi g Trak
whi ch
wi l l
resul t
i n
an
i naccurate
measurement.
H owever
we
onl y
need
to
measure
the
current
drawn
by
one
of
these
For thi s you wi l l need your motori sed base (i n
my case i t’ s the modi fi ed Bi g Trak) , a power
suppl y (some batteri es) and a mul ti meter.
two i denti cal motors.
Add some wei ght to the Bi g Trak whi ch
approxi mates the expected overal l wei ght of
the fi ni shed robot. I n the case of the Skutter
thi s i ncl udes addi ng the robot arm.
I f you don’ t own a mul ti meter yet, they are an
essenti al tool for anyone who i s i nvol ved i n
el ectroni cs and al l ow you take a wi de range of
measurements
i ncl udi ng
vol tage,
current,
Compl ete the ci rcui t between the batteri es and
motor, i ncl udi ng the mul ti meter i n seri es as
shown. As the Bi g Trak rol l s al ong the fl oor,
take a measurement of the current whi ch i s
bei ng drawn. U nder the expected l oad for the
Skutter usi ng thi s method, one of the two bi g
trak motors wi l l draw a current of 2. 5 Amps.
resi stance,
capaci tance and
h FE .
M apl i ns sel l
one for £7. 99 (CODE: N 20AX) .
I t i s possi bl e to obtai n a good mul ti meter for
under ten pounds from a vari ety of retai l ers.
DC
motors
draw
di fferent
currents
under
di fferent
condi ti ons.
I f
a
motor
i s
“free-
CAU TI ON :
When
motor
stal l i ng
was
tested
wheel i ng”
then
the
motor
wi l l
draw
a
the
current
drawn
was
approxi matel y
20
comparati vel y smal l current.
Amps.
Al ternati vel y a “stal l ed” motor (a motor that i s
prevented from turni ng) wi l l draw an extremel y
hi gh current. The harder we make a motor
work, the more current i t wi l l draw. I n our case
we
Continuedoverpage...
want
to
measure
the
current
the
motors
5
993337133.037.png 993337133.038.png
Zgłoś jeśli naruszono regulamin