MagPi07.pdf

(12041 KB) Pobierz
I I S S S S U U E E 0 0 7 7 ­ ­ N N O O V V 2 2 0 0 1 1 2 2
Supportusevenmore
bybuyingtheprinted
copyofthismag!
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
A A r r d d u u i i n n o o A A n n d d
R R a a s s P P i i G G e e t t
C C o o n n n n e e c c t t e e d d ! !
T T h h i i s s I I s s s s u u e e . . . . . .
I I n n t t e e r r u u p p t t s s
S S o o l l a a r r P P i i
T T u u r r b b o o M M o o d d e e
P P i i E E v v o o l l u u t t i i o o n n
C C + + + +
A A c c h h a a n n c c e e t t o o
w w i i n n a a P P I I N N K K
R R a a s s P P i i C C a a s s e e ! !
P P l l u u s s . . . . . .
A A n n i i n n t t e e r r v v i i e e w w w w i i t t h h t t h h e e
R R a a s s p p b b i i a a n n d d e e v v e e l l o o p p e e r r s s
M M a a k k e e y y o o u u r r o o w w n n l l a a d d d d e e r r
g g a a m m e e u u s s i i n n g g P P C C B B s s
T T h h e e b b a a s s i i c c s s o o f f G G N N U U m m a a k k e e
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.
1150072751.051.png 1150072751.062.png 1150072751.073.png 1150072751.080.png 1150072751.001.png 1150072751.002.png 1150072751.003.png 1150072751.004.png 1150072751.005.png 1150072751.006.png 1150072751.007.png 1150072751.008.png 1150072751.009.png 1150072751.010.png 1150072751.011.png 1150072751.012.png 1150072751.013.png 1150072751.014.png 1150072751.015.png 1150072751.016.png 1150072751.017.png 1150072751.018.png 1150072751.019.png 1150072751.020.png 1150072751.021.png 1150072751.022.png 1150072751.023.png 1150072751.024.png 1150072751.025.png 1150072751.026.png 1150072751.027.png 1150072751.028.png 1150072751.029.png 1150072751.030.png 1150072751.031.png 1150072751.032.png 1150072751.033.png 1150072751.034.png 1150072751.035.png 1150072751.036.png 1150072751.037.png 1150072751.038.png 1150072751.039.png 1150072751.040.png 1150072751.041.png 1150072751.042.png 1150072751.043.png 1150072751.044.png 1150072751.045.png 1150072751.046.png 1150072751.047.png 1150072751.048.png 1150072751.049.png 1150072751.050.png 1150072751.052.png 1150072751.053.png 1150072751.054.png 1150072751.055.png 1150072751.056.png 1150072751.057.png
WelcometoIssue7,
TheRaspberryPiandArduinoareaperfectmatchforrealtimeapplications
whereabitmoreCPUpowerisneededforcontrol.Couplingthetwodevices
togetheropensupthepossibilitytouseawealthofArduinoshieldstoo.Welook
forwardtoseeingsomereallyinterestingprojectsinthefuture.
ThereisaninterviewfromtheleaddeveloperofRaspbian(Debianbuildforthe
RaspberryPi),competitionsandaselectionofprogrammingarticlestogetyour
teethinto.
IfyouwouldprefertoreceiveyourcopyofTheMagPiinprintedform,visit
http://www.modmypi.comandplaceyourordernow!
AshStone ,ChiefEditorofTheMagPi
Ash Stone
ChiefEditor/Administrator
Jason 'Jaseman' Davies
Writer/Website/PageDesigns
Tim 'Meltwater' Cox
Writer/Photographer/PageDesigns
Chris 'tzj' Stagg
Writer/Photographer/PageDesigns
Ian McAlpine
PageDesigns/Graphics/Writer
Joshua Marinacci
PageDesigns/Graphics
Matt '0the0judge0'
Administrator/Website
Matthew Timmons-Brown
Writer
Gordon Henderson
Writer
Colin Deady
Writer/PageDesigns
Stewart C. Russell
Writer
W.H.Bell
Writer
Colin Norris
Editor/Graphics
Antiloquax
Writer
Richard Ryniker
Writer
Alex Kerr
Writer
Lix
PageDesigns/Graphics
PaisleyBoy
PageDesigns/Graphics
Sam Marshall
PageDesigns/Graphics
Aaron Shaw
PageDesigns/Graphics
Nick
PageDesigns/Graphics
2
1150072751.058.png
Contents
04 PI AND ARDUINO IN ACTION
P rog ram th e Ard u i n o u si n g a Raspberry P i , by Stewart C. Ru ssel l
07 THIS MONTH'S COMPETITION
Wi n som e excel l en t ad d i ti on s to you r setu p, from P C Su ppl i es Ltd
08 SOLAR PI
Wh en on th e m ove th e su n can keep th e Pi g oi n g , by M el twater
10 GORDON'S LADDER BOARD
Sol d eri n g i ron s at th e read y, by G ord on H en d erson
12 GPIO AND INTERRUPTS
A revi ew of h ow to h an d l e th e G PI O from th e com m an d l i n e, by Ri ch ard Ryn i ker
16 RASPBIAN, THE STORY SO FAR
An i n tervi ew wi th M i ke Th om pson , th e l ead d evel oper of Raspbi an , by Col i n Dead y
18 TURBO SETTINGS FOR MAXIMUM PERFORMANCE
A revi ew of h ow to tu n e u p th e Pi , by M atth ew Ti m m on s-Brown
21 THIS MONTH'S EVENTS LIST
Raspberry J am s an d oth er com m u n i ty even ts
22 PI-EVOLUTION
A revi ew on th e Raspberry Pi ' s d evel opm en t, by J asem an
24 THE BASICS OF GNU MAKE
Speed i n g u p cod e d evel opm en t wi th G N U M ake, by W. H . Bel l
26 WELCOME TO THE C++ CACHE
G etti n g to g ri ps wi th C++, by Al ex Kerr
28 THE SCRATCH PATCH
H ave a g o at d efen si ve prog ram m i n g , by An ti l oq u ax.
30 THE PYTHON PIT
U si n g com m an d l i n e arg u m en ts, by Col i n Dead y
32 FEEDBACK & DISCLAIMER
3
1150072751.059.png 1150072751.060.png 1150072751.061.png 1150072751.063.png
RaspberryPi&Arduino
While there are many I/O boards under development for the
Raspberry Pi, the Arduino is well established. This article
shows you how talk to an Arduino through Python and the
Firmata protocol.
DIFFICULTY: INTERMEDIATE
This example combines output (setting the
brightness of an LED with a graphical slider)
with input (reading the temperature from an
LM58).
You'll also need to choose which serial port to
use from Tools/Serial Port — for an Uno that
would be /dev/ttyACM0, and older boards
tend to use /dev/ttyUSB0.
Required Materials
Raspberry Pi
Arduino
Internet Connection
Small solder-less breadboard
LM35 temperature sensor
(http://www.ti.com/product/lm35)
5 mm red LED
120F resistor
4× male-male jumper wires (here coloured
red, yellow, blue and black)
Short-ish breadboard jumper (17.8 mm or
0.7", here coloured black)
To install the Firmata sketch onto your
Arduino, select File / Examples / Firmata /
StandardFirmata and click the Upload button.
The IDE goes off and compiles your sketch,
and uploads it. If all you get is blinking lights
and a ‘Done uploading’ message, success! If
you get any kind of red error messages, then
there's likely something up with the connection
or power to the Arduino.
I'd strongly recommend connecting your
Arduino either through a powered hub or
applying external power, as the Raspberry Pi
is a bit limited in what it can power over USB.
Firmata ( http://firmata.org ) is a simple serial
protocol that allows you to read and write I/O
ports on the Arduino from a host computer. It's
most often used with the graphical
programming language "Processing"
( http://processing.org ) but there is support for
other languages.
If you get stuck try the website
http://www.ladyada.net/learn/arduino/
Installing pyFirmata
pyFirmata is the magic that allows your
Arduino running Firmata to talk to Python. It
takes a few more commands to install it:
Installing the Arduino IDE and Firmata
The Arduino IDE is already included in the
Raspbian repositories, so you can install it all
with:
$sudoapt­getinstallpython­serialme
rcurial
$hgclonehttps://bitbucket.org/tino/p
yfirmata
$cdpyfirmata
$sudopythonsetup.pyinstall
$sudoapt­getinstallarduino
If it's the first time you've run it, the IDE may
ask you to create a folder for its programs
(called “sketches”).
If this succeeds, you can remove the pyfirmata
folder:
Next, you'll have to choose what Arduino
board you're using from the Tools/Board menu
(I'm using an Uno, but I have also tested this
on an older Duemilanove board).
$cd..;sudorm­rpyfirmata
4
1150072751.064.png 1150072751.065.png 1150072751.066.png 1150072751.067.png 1150072751.068.png 1150072751.069.png 1150072751.070.png 1150072751.071.png
Building the circuit
The LED doesn't seem to get much brighter
above 33%, you'll notice; there isn't a straight-
line link between brightness and PWM input
voltage.
Place the parts as shown in the diagram using
the following tables to assist:
If you touch the LM35, you'll notice the
temperature reading changing. LM35 sensors
are a bit noisy, so you might notice the
numbers jumping about.
ARDUINO
LM35
+5V
+Vs
Once you're done, hit the window close
widget, and everything should exit cleanly.
A0
Vout
The Python code does three main tasks:
GND
GND
1. Set up serial communications and configure
the Arduino's I/O pins.
NOTE: If you accidentally connect the LM35
the wrong way round, it goes from being a
temperature sensor to being a remarkable
temperature generator — the D-shaped burn
scar on my left index finger reminds me of this
every day…
Two important details we have to do here is
set up an iterator thread to stop the Raspberry
Pi's serial buffer overflowing while it receives
data from the Arduino, and also to wait for the
Arduino to start sending real data before we
move on to the main program.
ARDUINO
LED
2. Define three functions to be called by the
GUI:
D3
Anode (Long Wire)
get_temp() — this reads the value from
Arduino pin A0, converts it to °C, and updates
the label at the bottom of the window.
Cathode (Short Wire)
(Going Through the
120F resistor)
GND
If you're familiar with Arduino, pyFirmata
returns analogue values between 0.0 and 1.0
instead of the integer range 0-1023.
Running the code
Enter the progam on the next page and save it
as arduino_firmata_ty.py . With your Arduino
plugged in, and the circuit wired up, you run
the program like this:
This routine ends by asking the main Tk
process to reschedule itself after half a second
(500 ms), and that way we keep updating the
temperature display separate from the main
loop.
$pythonarduino_firmata_tk.py
It will take a few seconds for the window to
come up and, once it does, you can control the
LED's brightness with the slider on screen.
Continuedoverpage...
5
1150072751.072.png 1150072751.074.png 1150072751.075.png 1150072751.076.png 1150072751.077.png 1150072751.078.png 1150072751.079.png
Zgłoś jeśli naruszono regulamin