the raspberry pi magazine.the magpi. issue 19

Upload: iscrbdi

Post on 04-Jun-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    1/44

    IISSSSUUEE1199--DDEECC22001133

    AAMM

    aaggaazziinneeffoorrRRaassppbbeerrrryyPPiiUUsseerrss

    hhttttpp::////wwwwww..tthheemmaaggppii..ccoommRRaassppbbeerrrryyPPiiiissaattrraaddeemmaarrkkooffTThheeRRaassppbbeerrrryyPPiiFFoouunnddaattiioonn..TThhiissmmaaggaazziinneewwaassccrreeaatteedduussiinnggaaRRaassppbbeerrrryyPPiiccoommppuutteerr..

    GGeettpprriinntteeddccooppiieess

    aatttthheemmaaggppii..ccoomm

    EEnnvviirroonnmmeennttaallMMoonniittoorr

    XXLLooBBoorrggPPaaiinnttBBrruusshh

    GGPPIIOOLLCCDDSSccrreeeenn

    RReemmootteeSSeennssiinngg

    PPiiSSttoorreeRReevviieewwCCaattcchhUUppTTVV

    SSoonniiccPPii

    QQuuaaddccoopptteerr

    LLiiffttOOffff

    W i n a

    R a s p b e r r y P i

    1 6 G B S D c a r d

    b r e a d b o a r d &

    m o r e

  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    2/44

    A s h S t o n e - C h i e f E d i t o r / A d m i n i s t r a t i o n / L a y o u t

    W . H . B e l l - I s s u e E d i t o r / L a y o u t / A d m i n i s t r a t i o n

    B r y a n B u t l e r - P a g e D e s i g n / G r a p h i c s

    I a n M c A l p i n e - L a y o u t / P r o o f R e a d i n g

    M a t t J u d g e - W e b s i t e / A d m i n i s t r a t i o n

    A a r o n S h a w - L a y o u t / P r o o f R e a d i n g

    C o l i n D e a d y - L a y o u t / P r o o f R e a d i n g

    T h e M a g P i T e a m

    C l a i r e P r i c e - L a y o u t / P r o o f R e a d i n g

    M a t t W e a v e r - L a y o u t

    A m y - C l a r e M a r t i n - L a y o u t

    G e r r y F i l l e r y - P r o o f R e a d i n g / T e s t i n g

    P a u l C a r p e n t e r - T e s t e r

    N e i l M a t t h e w s - T e s t e r / P r o o f R e a d i n g

    N i g e l C u r t i s - P r o o f R e a d i n g

    19

    W e l c o m e t o I s s u e 1 9 o f T h e M a g P i m a g a z i n e .

    T h i s y e a r h a s f l o w n b y a n d w e a r e b a c k w i t h o u r C h r i s t m a s i s s u e o n c e a g a i n ! I s S a n t a b r i n g i n g y o u a n y

    R a s p b e r r y P i g o o d i e s t h i s C h r i s t m a s ? I f s o w e h a v e p l e n t y o f g r e a t a r t i c l e s t o h e l p y o u p u t t h i s c l e v e r

    c o m p u t e r s t r a i g h t t o g o o d u s e .

    A r e y o u b o r e d o f h a v i n g y o u r p r e s e n t s d e l i v e r e d b y r e i n d e e r ? I f y o u f a n c y a c h a n g e , w h y n o t h a v e y o u r

    o w n P i - p o w e r e d q u a d c o p t e r a i r d r o p t h e m i n ? A n d y B a k e r b e g i n s h i s s e r i e s o n b u i l d i n g t h i s f l y i n g

    m a c h i n e c o v e r i n g i n t h i s i s s u e t h e p a r t s r e q u i r e d , t h e i r f u n c t i o n a n d d i s c u s s e s s o m e o f t h e c o d i n g u s e d

    f o r l i f t o f f . . . . n o , i t s n o t p o w e r e d b y C h r i s t m a s s p i r i t !

    I f y o u a r e t o o b u s y a t t e n d i n g C h r i s t m a s p a r t i e s a n d a r e m i s s i n g y o u r f a v o u r i t e s o a p s , n e v e r f e a r , w e

    h a v e y o u c o v e r e d . G e o f f h a s p r o d u c e d a g r e a t a r t i c l e o n O p e n E L E C , b r i n g i n g y o u c a t c h u p T V o n y o u r

    R a s p b e r r y P i s o y o u n e v e r h a v e t o m i s s a n e p i s o d e a g a i n ! C l a i r e P r i c e c o n t i n u e s t h e f e s t i v e s p i r i t w i t h

    a f a n t a s t i c a r t i c l e o n S o n i c P i w h i c h w i l l h a v e y o u r R a s p b e r r y P i s i n g i n g a l o n g w i t h a r e n d i t i o n o f G o o d

    K i n g W e n c e s l a s .

    T h e n e w s i n t h e U K i s c u r r e n t l y f i l l e d w i t h s t o r i e s o f e l e c t r i c i t y f i r m s p u t t i n g u p t h e i r p r i c e s t h i s w i n t e r . I f

    y o u w a n t t o b e s a v v y w i t h y o u r h e a t i n g a n d e l e c t r i c i t y b i l l s , w i t h o u t t u r n i n g o f f t h e t r e e l i g h t s a n d t u r n i n g

    t h e t h e r m o s t a t d o w n , w h y n o t c a s t y o u r e y e o v e r P i e r r e s a r t i c l e o n e n v i r o n m e n t a l m o n i t o r i n g .

    A l t e r n a t i v e l y , t o w a r m y o u u p w e r e t u r n t o P r o j e c t C u r a c a o l o o k i n g a t t h e e n v i r o n m e n t a l s u b s y s t e m

    u s e d i n t h i s r e m o t e s e n s i n g p r o j e c t .

    I f t h a t s n o t e n o u g h t o k e e p y o u b u s y o v e r t h e h o l i d a y s , w h y n o t p a i n t a n e l e c t r o n i c m a s t e r p i e c e w i t h

    X L o B o r g ? A n d y W i l s o n l o o k s a t s c r o l l i n g a n R S S f e e d o n a n L C D v i a G P I O p l u s w e p a y a v i s i t t o t h e P i

    S t o r e . F i n a l l y , f i n d o u t a b o u t e v e n t s i n y o u r a r e a a n d P C S u p p l i e s a r e y e t a g a i n g e n e r o u s l y o f f e r i n g

    R a s p b e r r y P i g o o d i e s i n t h e i r m o n t h l y c o m p e t i t i o n .

    W e a r e a l s o p l e a s e d t o b e a b l e t o a n n o u n c e t h a t

    p r i n t e d c o p i e s o f t h e m a g a z i n e a r e n o w a v a i l a b l e

    f r o m v a r i o u s r e t a i l e r s l i s t e d a t w w w . t h e m a g p i . c o m .

    T h e M a g P i w i l l b e t a k i n g a s h o r t b r e a k o v e r

    C h r i s t m a s a n d t h e f i r s t i s s u e o f 2 0 1 4 w i l l b e

    p u b l i s h e d a t t h e s t a r t o f F e b r u a r y .

    M e r r y C h r i s t m a s a n d b e s t w i s h e s f o r 2 0 1 4 .

    C h i e f E d i t o r o f T h e M a g P i

    http://www.themagpi.com/
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    3/443

    4

    Q U A D C O P T E R

    P a r t 1 : A n i n t r o d u c t i o n t o b u i l d i n g a n d c o n t r o l l i n g a q u a d c o p t e r w i t h t h e R a s p b e r r y P i

    1 0

    D a t a l o g g i n g w i t h t h e R a s p b e r r y P i

    S c r o l l i n g a n R S S f e e d o n a n A n d y P i L C D v i a G P I O

    1 8

    B u t t o n s a n d s w i t c h e s w i t h t h e R a s p b e r r y P i P a r t 3

    2 2

    P a i n t i n g w i t h t h e X L o B o r g a c c e l e r o m e t e r a n d m a g n e t o m e t e r f r o m P i B o r g

    2 8

    C A T C H - U P T V

    A v o i d m i s s i n g y o u r f a v o u r i t e p r o g r a m m e b y u s i n g O p e n E L E C t o w a t c h T V

    3 4

    T H E P I S T O R E

    A l o o k a t t h e d i v e r s e r a n g e o f a p p l i c a t i o n s a n d g a m e s

    E N V I R O N M E N T A L M O N I T O R

    1 4

    P R O J E C T C U R A C A O : R E M O T E S E N S O R M O N I T O R I N G I N T H E C A R I B B E A N

    A N D Y P I

    P a r t 2 : T h e e n v i r o n m e n t a l s u b s y s t e m

    1 6

    P H Y S I C A L C O M P U T I N G

    P I B R U S H

    3 8

    W i n a R a s p b e r r y P i M o d e l B , b r e a d b o a r d , 1 6 G B N O O B S S D c a r d , G P I O C o b b l e r k i t a n d a c c e s s o r i e s

    C O M P E T I T I O N

    3 9

    S t e v e n a g e U K , W i n c h e s t e r U K , N o t t i n g h a m U K , P a i g n t o n U K , H e l s i n g b o r g S w e d e n

    T H I S M O N T H ' S E V E N T S G U I D E

    4 4

    H a v e y o u r s a y a b o u t T h e M a g P i

    F E E D B A C K

    L e a r n i n g t o p r o g r a m w i t h S o n i c P i

    S O N I C P I A T C H R I S T M A S

    4 0

    h t t p : / / w w w . t h e m a g p i . c o m

    C o n t e n t s C o n t e n t s C o n t e n t s

    http://www.themagpi.com/
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    4/44

    S K I L L L E V E L : A D V A N C E D

    A n d y B a k e r

    G u e s t W r i t e r

    B u i l d i n g a n a i r b o r n e a u t o b o t

    A q u a d c o p t e r i s a f l y i n g m a c h i n e w i t h f o u r p r o p e l l e r s

    c o n t r o l l e d e i t h e r a u t o n o m o u s l y ( p r o g r a m m e d w i t h a

    f i x e d f l i g h t r o u t i n e ) o r v i a a r e m o t e c o n t r o l .

    T h i s f i r s t a r t i c l e ( h o p e f u l l y o f a s e r i e s ) c o v e r s a b r i e f

    o v e r v i e w o f h o w t h e y w o r k , h o w t o b u i l d o n e

    c o n t r o l l e d b y a R a s p b e r r y P i , i n f o r m a t i o n a b o u t

    w h e r e t o g e t a l l t h e b i t s y o u n e e d a n d h o w t o b o l t

    t h e m a l l t o g e t h e r p h y s i c a l l y , e l e c t r o n i c a l l y a n d i n

    s o f t w a r e . T h e r e s u l t s h o u l d b e a q u a d c o p t e r w h i c h

    c a n t a k e - o f f , h o v e r a n d l a n d a u t o n o m o u s l y ( a n d w i t h

    c a r e ! )

    F u t u r e a r t i c l e s w i l l c o v e r m o r e d e t a i l s o n t e s t i n g a n d

    t u n i n g t h i s b a s i c q u a d i n c l u d i n g c o d e e n h a n c e m e n t s

    t o a l l o w l a t e r a l m o v e m e n t , a R a s p b e r r y P i r e m o t e

    c o n t r o l , a n d p e r h a p s f u t u r e d e v e l o p m e n t s c o v e r i n g

    G P S t r a c k i n g .

    P a r t s o f a q u a d c o p t e r

    F i r s t a q u i c k b r e a k d o w n o f a l l t h e p a r t s t h a t m a k e u p

    a q u a d c o p t e r .

    T h e r e a r e f o u r p r o p e l l e r b l a d e s . T w o o f t h e f o u r a r e

    d e s i g n e d t o r o t a t e c l o c k - w i s e ; t h e o t h e r t w o a n t i -

    c l o c k w i s e . B l a d e s w h i c h a r e d e s i g n e d t o m o v e t h e

    s a m e w a y a r e p l a c e d d i a g o n a l l y o p p o s i t e o n t h e

    f r a m e . O r g a n i s i n g t h e b l a d e s l i k e t h i s h e l p s s t o p t h e

    q u a d c o p t e r s p i n n i n g i n t h e a i r . B y a p p l y i n g d i f f e r e n t

    p o w e r t o e a c h p r o p e l l e r , a n d h e n c e d i f f e r e n t a m o u n t s

    o f l i f t t o c o r n e r s o f t h e q u a d c o p t e r , i t i s p o s s i b l e t o n o t

    o n l y g e t a q u a d c o p t e r t o t a k e - o f f , h o v e r a n d l a n d b u t

    a l s o b y t i l t i n g i t , m o v e h o r i z o n t a l l y a n d t u r n c o r n e r s .

    E a c h p r o p e l l e r h a s i t s o w n D C b r u s h l e s s m o t o r .

    T h e s e m o t o r s c a n b e w i r e d t o r o t a t e c l o c k w i s e o r

    a n t i - c l o c k w i s e t o m a t c h t h e p r o p e l l e r c o n n e c t e d t o

    t h e m . T h e m o t o r h a s c o i l s i n t h r e e g r o u p s a r o u n d t h e

    b o d y ( c a l l e d t h e s t a t o r ) a n d g r o u p s o f m a g n e t s

    a t t a c h e d t o t h e p r o p e l l o r s h a f t ( c a l l e d t h e r o t o r ) . T o

    m o v e t h e b l a d e s , p o w e r i s a p p l i e d t o o n e g r o u p o f t h e

    c o i l s a n d t h e r o t o r m a g n e t s a r e a t t r a c t e d t o t h a t c o i l ,

    m o v i n g r o u n d . I f t h a t c o i l i s t h e n t u r n e d o f f a n d t h e

    n e x t o n e p o w e r e d u p , t h e r o t o r m o v e s a r o u n d t o t h e

    n e x t c o i l . R e p e a t i n g t h i s a r o u n d t h e t h r e e c o i l s i n

    s e q u e n c e r e s u l t s i n t h e m o t o r r o t o t a t i n g ; t h e f a s t e r

    y o u s w a p b e t w e e n t h e t h r e e p o w e r e d c o i l s t h e f a s t e r

    t h e m o t o r r o t a t e s . T h i s m a k e s t h e m o t o r s u i t a b l e f o r

    d i g i t a l c o n t r o l t h e d i r e c t i o n a n d s p e e d o f

    m o v e m e n t o f t h e p r o p e l l e r b l a d e e x a c t l y m a t c h e s t h e

    s e q u e n c e a n d r a t e p o w e r p u l s e s a r e a p p l i e d t o t h e

    c o i l s . T h e s e m o t o r s t a k e a l o t o f p o w e r t o s p i n t h e

    p r o p e l l e r b l a d e s f a s t e n o u g h t o f o r c e e n o u g h a i r

    d o w n t o m a k e t h e q u a d c o p t e r t a k e - o f f f a r m o r e

    p o w e r t h a n a R a s p b e r r y P i c a n p r o v i d e s o a n

    E l e c t r o n i c S p e e d C o n t r o l l e r ( E S C ) b r i d g e s t h a t g a p .

    I t t r a n s l a t e s b e t w e e n a P u l s e W i d t h M o d u l a t i o n

    ( P W M ) c o n t r o l s i g n a l f r o m t h e R a s p b e r r y P i a n d

    c o n v e r t s i t t o t h r e e h i g h - c u r r e n t s i g n a l s , o n e f o r e a c h

  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    5/445

    c o i l o f t h e m o t o r s . T h e y a r e t h e s m a l l w h i t e o b j e c t s

    v e l c r o d u n d e r t h e a r m s o f t h e q u a d c o p t e r .

    N e x t t h e r e a r e s e n s o r s a t t a c h e d t o t h e b r e a d b o a r d o n

    t h e s h e l f b e l o w t h e R a s p b e r r y P i ; t h e s e p r o v i d e

    i n f o r m a t i o n t o t h e R a s p b e r r y P i a b o u t r o c k i n g a n d

    r o l l i n g i n t h r e e d i m e n s i o n s f r o m a g y r o s c o p e , p l u s

    i n f o r m a t i o n a b o u t a c c e l e r a t i o n f o r w a r d , b a c k w a r d s ,

    l e f t , r i g h t , u p a n d d o w n . T h e s e n s o r s c o n n e c t t o t h e

    R a s p b e r r y P i G P I O I

    2

    C p i n s .

    I n t h e c i r c u i t d i a g r a m y o u c a n s e e I a m c o n s i d e r i n g

    a d d i n g a b e e p e r , s o I c a n h e a r w h a t t h e q u a d c o p t e r

    t h i n k s i t s d o i n g .

    T h e p o w e r f o r e v e r y t h i n g c o m e s f r o m a s i n g l e l i t h i u m

    p o l y m e r ( L i P o ) b a t t e r y w h i c h p r o v i d e s 1 1 . 1 V u p t o a

    p e a k c u r r e n t o f 1 0 0 A , w i t h t h e f u l l - c h a r g e o f 3 3 0 0

    m A h t h u s s u p p l y i n g 3 . 3 A f o r a n h o u r o r 1 0 0 A f o r t w o

    m i n u t e s o r a n y w h e r e i n b e t w e e n . T h i s i s a p o w e r f u l

    a n d d a n g e r o u s b e a s t , y e t i t o n l y w e i g h s 2 5 0 g r a m s .

    I t r e q u i r e s a s p e c i a l c h a r g e r i f n o t u s e d , a L i P o

    b a t t e r y c a n e a s i l y b e c o m e a L i P o b o m b b e w a r e .

    T h e r e i s a r e g u l a t o r o n t h e b r e a d b o a r d t o t a k e p o w e r

    f r o m t h e b a t t e r y a n d p r o d u c e t h e 5 V f o r t h e

    R a s p b e r r y P i a n d a l s o p r o v i d e a d e g r e e o f p r o t e c t i o n

    f r o m t h e v a s t p o w e r s u r g e s t h e m o t o r s d r a w f r o m t h e

    b a t t e r y .

    T h a t j u s t l e a v e s t h e b e a t i n g h e a r t o f t h e q u a d c o p t e r

    i t s e l f ; t h e R a s p b e r r y P i . U s i n g P y t h o n c o d e i t r e a d s

    t h e s e n s o r s , c o m p a r e s t h e m t o a d e s i r e d a c t i o n ( f o r

    e x a m p l e t a k e - o f f , h o v e r , l a n d ) s e t e i t h e r i n c o d e o r

    f r o m a r e m o t e c o n t r o l , c o n v e r t s t h e d i f f e r e n c e

    b e t w e e n w h a t t h e q u a d i s d o i n g ( f r o m t h e s e n s o r s )

    a n d w h a t i t s h o u l d b e d o i n g ( f r o m t h e c o d e o r r e m o t e

    c o n t r o l ) a n d c h a n g e s t h e p o w e r t o e a c h o f t h e

    m o t o r s i n d i v i d u a l l y s o t h a t t h e d e s i r e d a c t i o n a n d

    s e n s o r o u t p u t s m a t c h .

    C r e a t i n g y o u r q u a d c o p t e r

    F i r s t a n d f o r e m o s t , f l y i n g m a c h i n e s a n d b o a t s a r e

    f e m a l e a n d t h e y h a v e n a m e s ; m i n e i s c a l l e d P h o e b e

    ( F e e - B e e ) . C h o o s e a n a m e f o r y o u r s a n d t r e a t h e r

    w e l l , a n d t h e c h a n c e s a r e s h e l l r e c i p r o c a t e !

    P h o e b e s b o d y , a r m s , l e g s , b l a d e s , m o t o r s , E S C s

    a n d b a t t e r i e s a r e f r o m k i t s . T o t a l c o s t i s a b o u t 2 5 0

    t o g e t h e r w i t h a R a s p b e r r y P i , a n d o t h e r

    a c c o u t r e m e n t s , t h e t o t a l c o s t i s p e r h a p s 3 0 0

    3 5 0 . N o t c h e a p f o r s o m e t h i n g w h i c h c e r t a i n l y a t t h e

    s t a r t h a s a p r e f e r e n c e t o c r a s h r a t h e r t h a n f l y !

    A c o m p l e t e b i l l o f m a t e r i a l s ( B O M ) i s a v a i l a b l e a t

    h t t p : / / b l o g . p i s t u f f i n g . c o . u k / ? p = 1 1 4 3

    I v e a c t u a l l y u p g r a d e d m y m o t o r s t o h i g h e r p o w e r ,

    l i g h t e r w e i g h t v a r i e t i e s b u t t h i s i s a b s o l u t e l y n o t

    n e c e s s a r y t h e e q u i p m e n t p r o v i d e d b y t h e k i t s i s

    e x c e l l e n t . U p g r a d i n g c o m p o n e n t s f o r w e i g h t

    r e d u c t i o n / p o w e r e f f i c i e n c y a n d s t r e n g t h i s d e f i n i t e l y

    a n a f t e r t h o u g h t o n c e t h e b a s i c s a r e w o r k i n g .

    T h e R a s p b e r r y P i i s a m o d e l A , c h o s e n f o r l o w e r

    w e i g h t a n d l o w e r p o w e r c o n s u m p t i o n ; t h e s e a r e

    f a c t o r s r e f l e c t e d t h r o u g h o t h e r p i e c e s o f t h e d e s i g n . I

    h a v e r e m o v e d t h e a u d i o a n d v i d e o o u t p u t s a n d u s e a

    m i c r o - S D c a r d a d a p t e r f r o m t h e g u y s a t P i m o r o n i a l l

    i n t h e n a m e o f w e i g h t s a v i n g .

    T h e R a s p b e r r y P i c a s e i s a v a r i a n t o f t h e P i m o r o n i

    P i B o w m o d e l B c a s e w i t h a c o u p l e o f l e v e l s r e m o v e d

    a n d s o m e h o l e s s e a l e d f o r r e d u c e d w e i g h t a n d

    i n c r e a s e d s t r e n g t h ( p r o t e c t i o n f r o m c r a s h e s ! ) . I v e

    p o s t e d t h e d e s i g n f o r t h e s e a t

    h t t p : / / b l o g . p i s t u f f i n g . c o . u k / w p - c o n t e n t / u p l o a d s / 2 0 1 3 /

    1 0 / P i b o w 0 0 2 - A B 5 . p d f . P h e n o p t i x d o a g r e a t j o b o f

    l a s e r c u t t i n g 3 m m a c r y l i c a t a v e r y r e a s o n a b l e p r i c e .

    http://blog.pistuffing.co.uk/wp-content/uploads/2013/10/Pibow002-AB5.pdfhttp://blog.pistuffing.co.uk/?p=1143
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    6/44

    T a l k i n g t o P h o e b e

    W h e t h e r P h o e b e i s a u t o n o m o u s o r r e m o t e c o n t r o l l e d

    s o m e o n e n e e d s t o t a l k t o h e r t o t e l l h e r w h a t t o d o .

    T o t h a t e n d , P h o e b e r u n s a w i r e l e s s a c c e s s p o i n t

    ( W A P ) s o a n o t h e r c o m p u t e r c a n j o i n h e r p r i v a t e

    n e t w o r k a n d e i t h e r S S H i n o r p r o v i d e r e m o t e c o n t r o l

    c o m m a n d s . Y o u c a n s e e h o w I d i d t h i s a t

    h t t p : / / b l o g . p i s t u f f i n g . c o . u k / ? p = 5 9 4 .

    F o r i n i t i a l t e s t i n g t h e W A P f u n c t i o n i s n t n e c e s s a r y ,

    a n y h o u s e h o l d w i r e l e s s n e t w o r k w i l l d o , b u t a s y o u r

    q u a d c o p t e r c o m e s t o l i f e y o u w i l l w a n t t o b e d o i n g

    y o u r t e s t i n g a w a y f r o m a n i m a l s , c h i l d r e n a n d o t h e r

    v a l u a b l e s y o u d o n t w a n t d a m a g e d ( l i k e y o u r s e l f ) .

    H a v i n g a W A P m e a n s y o u c a n t a k e t h e t e s t i n g o u t

    i n t o t h e g a r d e n o r l o c a l p a r k o r f i e l d .

    P r e s e n t i n g P h o e b e s P y t h o n c o d e

    T h e f i n a l s t e p i s o b v i o u s l y t h e m o s t i m p o r t a n t ; o n c e

    y o u h a v e a p h y s i c a l q u a d c o p t e r w i t h a s s o c i a t e d

    b l a d e s , m o t o r s , E S C s , p o w e r a n d c i r c u i t r y , w e u s e

    P y t h o n c o d e t o g l u e a l l t h e p i e c e s t o g e t h e r . I m n o t

    g o i n g t o g o i n t o t h i s b l o w b y b l o w h e r e a s t h e c o d e i s

    a v a i l a b l e a t h t t p s : / / g i t h u b . c o m / P i S t u f f i n g / Q u a d c o p t e r

    a n d i t s h o u l d b e s e l f - d o c u m e n t i n g . T h e r e a r e m o r e

    l i n e s o f e x p l a n a t o r y c o m m e n t s t h a n t h e r e a r e l i n e s o f

    c o d e a c t u a l l y d o i n g s o m e t h i n g c o n s t r u c t i v e !

    T h e I

    2

    C c l a s s p r o v i d e s a p r o g r a m m i n g i n t e r f a c e t o

    r e a d a n d w r i t e d a t a f r o m t h e s e n s o r s . B u i l t o n t h a t ,

    t h e M P U 6 0 5 0 c l a s s c o n f i g u r e s t h e s e n s o r s a n d t h e n

    p r o v i d e s A P I a c c e s s t o r e a d i n g t h e d a t a a n d

    c o n v e r t i n g t h e v a l u e s f r o m t h e s e n s o r i n t o m e a n i n g f u l

    v a l u e s h u m a n s w o u l d u n d e r s t a n d ( l i k e d e g r e e s / s e c

    f o r r o t a t i o n o r m e t r e s / s e c

    2

    f o r a c c e l e r a t i o n ) .

    T h e Q U A D B L A D E c l a s s h a n d l e s t h e P W M f o r e a c h

    b l a d e h a n d l i n g i n i t i a l i z a t i o n a n d s e t t i n g t h e P W M d a t a

    t o c o n t r o l t h e p r o p e l l e r b l a d e s p i n s p e e d s . T h e P I D

    c l a s s i s t h e j i g s a w g l u e a n d t h e c o r e o f t h e

    d e v e l o p m e n t a n d t e s t i n g . I t i s t h e u n d e r s t a n d i n g o f

    t h i s w h i c h m a k e s c o n f i g u r i n g a q u a d c o p t e r b o t h

    e x c i t i n g a n d s c a r y ! I t i s w o r t h a n a r t i c l e i n i t s o w n

    r i g h t f o r n o w t h e r e i s a b r i e f o v e r v i e w o f w h a t t h e y

    d o a n d h o w a t t h e e n d .

    T h e r e a r e u t i l i t y f u n c t i o n s f o r p r o c e s s i n g t h e s t a r t u p

    c o m m a n d l i n e p a r a m e t e r s , s i g n a l h a n d l i n g ( t h e p a n i c

    b u t t o n C t r l - C ) a n d s o m e s h u t d o w n c o d e .

    L a s t , b u t n o t l e a s t , t h e r e i s t h e b i g w h i l e

    k e e p _ l o o p i n g : l o o p w h i c h c h e c k s o n w h a t i t s h o u l d

    b e d o i n g ( t a k e - o f f , h o v e r , l a n d , e t c ) , r e a d s t h e

    s e n s o r s , r u n s t h e P I D s , u p d a t e s t h e P W M s a n d

    r e t u r n s t o t h e s t a r t o n e h u n d r e d t i m e s a s e c o n d !

    P I D

    T h e P I D ( P r o p o r t i o n a l , I n t e g r a l , D i f f e r e n t i a l ) c l a s s i s a

    r e l a t i v e l y s m a l l , s i m p l e p i e c e o f c o d e u s e d t o a c h i e v e

    q u i t e a c o m p l e x t a s k . I t i s f e d a t a r g e t v a l u e a n d a n

    i n p u t v a l u e . T h e d i f f e r e n c e b e t w e e n t h e s e i s t h e

    e r r o r . T h e P I D p r o c e s s e s t h i s e r r o r a n d p r o d u c e s

    a n o u t p u t w h i c h a i m s t o s h r i n k t h e d i f f e r e n c e

    b e t w e e n t h e t a r g e t a n d i n p u t t o z e r o . I t d o e s t h i s

    r e p e a t e d l y , c o n s t a n t l y u p d a t i n g t h e o u t p u t , y e t

    w i t h o u t a n y i d e a o f w h a t i n p u t , o u t p u t o r t a r g e t

    a c t u a l l y m e a n i n i t s r e a l w o r l d c o n t e x t a s t h e c o r e o f a

    q u a d c o p t e r : w e i g h t , g r a v i t y , w i n d s t r e n g t h , R C

    c o m m a n d s , l o c a t i o n , m o m e n t u m , s p e e d a n d a l l t h e

    o t h e r f a c t o r s w h i c h a r e c r i t i c a l t o q u a d c o p t e r s .

    I n t h e c o n t e x t o f a q u a d c o p t e r , t a r g e t i s a f l i g h t

    c o m m a n d ( h o v e r , t a k e - o f f , l a n d , m o v e f o r w a r d s ) ,

    i n p u t i s s e n s o r d a t a a n d o u t p u t i s t h e P W M p u l s e

    s i z e f o r t h e m o t o r s .

    P h o e b e h a s 4 P I D s r u n n i n g c u r r e n t l y p i t c h , r o l l , y a w

    a n d v e r t i c a l s p e e d t h e s e a r e t h e b a r e m i n i m u m

    n e e d e d f o r a n o r d e r l y t a k e o f f , h o v e r a n d l a n d i n g .

    T h e P I D ' s m a g i c i s t h a t i t d o e s n o t c o n t a i n a n y

    c o m p l e x c a l c u l a t i o n s c o n n e c t i n g p o w e r , w e i g h t ,

    b l a d e s p i n r a t e s , g r a v i t y , w i n d - s p e e d , i m b a l a n c e d

    f r a m e , p o o r c e n t e r o f g r a v i t y o r t h e m a n y o t h e r

    http://blog.pistuffing.co.uk/?p=594https://github.com/PiStuffing/Quadcopter
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    7/447

    f a c t o r s t h a t p e r t u r b t h e p e r f e c t f l i g h t m o d e l l e d b y a

    p u r e m a t h e m a t i c a l e q u a t i o n . I n s t e a d i t d o e s t h i s b y

    r e p e a t e d , r a p i d r e - e s t i m a t i o n o f w h a t t h e c u r r e n t b e s t

    g u e s s o u t p u t m u s t b e b a s e d o n l y o n t h e t a r g e t

    a n d t h e i n p u t .

    T h e P o f P I D s t a n d s f o r p r o p o r t i o n a l e a c h t i m e t h e

    P I D i s c a l l e d i t s o u t p u t i s j u s t s o m e f a c t o r t i m e s t h e

    e r r o r i n a q u a d c o p t e r c o n t e x t , t h i s c o r r e c t s

    i m m e d i a t e p r o b l e m s a n d i s t h e d i r e c t a p p r o a c h t o

    k e e p i n g t h e a b s o l u t e e r r o r t o z e r o .

    T h e I o f P I D s t a n d s f o r i n t e g r a l e a c h t i m e t h e P I D

    i s c a l l e d t h e e r r o r i s a d d e d t o a g r a n d t o t a l o f e r r o r s

    t o p r o d u c e a n o u t p u t w i t h t h e i n t e n t t h a t o v e r t i m e ,

    t h e t o t a l e r r o r r e m a i n s a t z e r o i n a q u a d c o p t e r

    c o n t e x t , t h i s a i m s t o p r o d u c e l o n g t e r m s t a b i l i t y b y

    d e a l i n g w i t h p r o b l e m s l i k e i m b a l a n c e i n t h e p h y s i c a l

    f r a m e , m o t o r a n d b l a d e p o w e r p l u s w i n d .

    T h e D o f P I D s t a n d s f o r d i f f e r e n t i a l e a c h t i m e t h e

    P I D i s c a l l e d t h e d i f f e r e n c e i n e r r o r s i n c e l a s t t i m e i s

    u s e d t o g e n e r a t e t h e o u t p u t i f t h e e r r o r i s w o r s e

    t h a n l a s t t i m e , t h e P I D D o u t p u t i s h i g h e r . T h i s a i m s

    t o p r o d u c e a p r e d i c t i v e a p p r o a c h t o e r r o r c o r r e c t i o n .

    T h e r e s u l t s o f a l l t h r e e a r e a d d e d t o g e t h e r t o g i v e a n

    o v e r a l l o u t p u t a n d t h e n , d e p e n d i n g o n t h e p u r p o s e o f

    t h e P I D , a p p l i e d t o e a c h o f t h e b l a d e s a p p r o p r i a t e l y .

    I t s o u n d s l i k e m a g i c . . . a n d t o s o m e e x t e n t i t i s ! E v e r y

    P I D h a s t h r e e c o n f i g u r a b l e g a i n f a c t o r s c o n f i g u r e d f o r

    i t , o n e e a c h f o r P , I a n d D . S o i n m y c a s e I h a v e

    t w e l v e d i f f e r e n t g a i n f a c t o r s . T h e s e a r e m a g i c

    n u m b e r s , w h i c h i f t o o s m a l l d o n o t h i n g , i f t o o l a r g e

    c a u s e c h a o s a n d i f a p p l i e d w r o n g l y c a u s e

    c a t a s t r o p h e . M y n e x t a r t i c l e w i l l c o v e r t h i s i n m u c h

    m o r e d e t a i l , b o t h h o w t h e y w o r k a n d h o w t o t u n e t h e

    g a i n s . I n t h e m e a n t i m e , u s e t h e b i l l o f m a t e r i a l s o n

    p a g e 5 a n d g e t o n w i t h b u i l d i n g y o u r o w n q u a d c o p t e r .

    T h e P I D g a i n s i n t h e c o d e I v e s u p p l i e d s h o u l d b e a

    r e a s o n a b l e s t a r t i n g p o i n t f o r y o u r s .

    F l y i n g P h o e b e

    A t t h e m o m e n t i t i s s i m p l e b u t d a n g e r o u s ! P u t

    P h o e b e o n t h e g r o u n d , p l a c e a f l a t s u r f a c e a c r o s s h e r

    p r o p e l l e r t i p s , p u t a s p i r i t l e v e l o n t h a t s u r f a c e a n d

    m a k e s u r e s h e s o n a b s o l u t e h o r i z o n t a l b y p u t t i n g

    p a d d i n g u n d e r h e r f e e t t h i s i s a b s o l u t e l y c r i t i c a l i f

    y o u d o n t w a n t h e r t o d r i f t i n f l i g h t w e l l f i x t h i s i n

    a n o t h e r a r t i c l e w i t h s o m e m o r e P I D s .

    C o n n e c t t h e L i P o b a t t e r y . T h e E S C s w i l l s t a r t

    b e e p i n g l o u d l y i g n o r e t h e m .

    W a i t u n t i l t h e W i f i d o n g l e s t a r t s t o f l a s h t h a t m e a n s

    P h o e b e s W A P i s w o r k i n g .

    C o n n e c t v i a S S H / r l o g i n f r o m a c l i e n t s u c h a s

    a n o t h e r R a s p b e r r y P i , i P a d e t c . w h i c h y o u h a v e

    j o i n e d t o P h o e b e s n e t w o r k .

    U s e t h e c d c o m m a n d t o c h a n g e t o t h e d i r e c t o r y

    w h e r e y o u p l a c e d P h o e b e s c o d e . T h e n e n t e r :

    s u d o p y t h o n . / p h o e b e . p y - c

    s u d o p y t h o n . / p h o e b e . p y - c - t 5 5 0 - v

    - c c a l i b r a t e s t h e s e n s o r s t o t h e f l a t s u r f a c e s h e s o n

    - t 5 5 0 s e t s u p t h e b l a d e s t o j u s t u n d e r t a k e - o f f

    s p e e d

    - v r u n s t h e v i d e o c a m e r a w h i l e s h e s i n f l i g h t .

    T h e r e a r e o t h e r o p t i o n s . Y o u c a n f i n d t h e m b y

    e n t e r i n g :

    s u d o p y t h o n . / p h o e b e . p y

    E n j o y , b u t b e c a r e f u l .

  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    8/44

    http://shop.pimoroni.com/
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    9/44

    http://www.pibot.org/
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    10/440

    S K I L L L E V E L : I N T E R M E D I A T E

    P i e r r e F r e y e r m u t h

    G u e s t W r i t e r

    E N V I R O N M E N T A L M O N I T O R

    U s i n g t h e B M P 0 8 5 t o l o g

    t e m p e r a t u r e a n d p r e s s u r e d a t a

    D a t a l o g g i n g s y s t e m s a r e o f t e n u s e d i n i n d u s t r y t o

    a l l o w a n a l y s i s a f t e r d a t a h a s b e e n r e a d a n d f o r

    m o n i t o r i n g . S t u d y i n g c o l l e c t e d d a t a , w h i c h c o u l d b e

    a l a r g e d a t a s a m p l e , l e a d s t o a b e t t e r u n d e r s t a n d i n g

    o f t h e m o n i t o r e d s y s t e m b e h a v i o u r . T h e R a s p b e r r y

    P i p r o v i d e s a n a f f o r d a b l e , a c c e s s i b l e a n d l o w p o w e r

    c o n s u m p t i o n s o l u t i o n t h a t c a n l o g , p r e s e n t a n d

    u p l o a d d a t a . F o r e x a m p l e , a R a s p b e r r y P i c o u l d b e

    u s e d t o m o n i t o r t h e e n e r g y c o n s u m p t i o n o f a h e a t i n g

    s y s t e m i n a h o u s e .

    I n t h i s a r t i c l e s e r i e s I w i l l p r e s e n t a n i n d e p e n d e n t d a t a

    l o g g i n g s y s t e m t h a t i s a s s e m b l e d f r o m s e v e r a l

    p i e c e s . T h e c o m p o n e n t s o f t h e s y s t e m c o u l d b e

    m o d i f i e d f o r o t h e r a p p l i c a t i o n s . T h i s s e r i e s u s e s t h e

    R a s p b i a n R a s p b e r r y P i o p e r a t i n g s y s t e m , t o g e t h e r

    w i t h t h e J a v a p r o g r a m m i n g l a n g u a g e .

    B a s e s t a t i o n r e q u i r e m e n t s

    T h e g o a l f o r t h e p r o j e c t i s t o r u n d a y a n d n i g h t w i t h o u t

    i n t e r r u p t i o n . T h e r e f o r e , p a r t i c u l a r a t t e n t i o n n e e d s t o

    b e t a k e n w h e n c h o o s i n g t h e p o w e r s u p p l y . I t s h o u l d

    b e e f f i c i e n t a n d r e l i a b l e . I f o n l y a f e w l o w p o w e r

    c o n s u m p t i o n s e n s o r s a r e c o n n e c t e d , a m o b i l e p h o n e

    c h a r g e r i s p r o b a b l y e n o u g h . I t i s u p t o y o u t o d e c i d e i f

    a s c r e e n , k e y b o a r d , m o u s e o r r e m o t e a c c e s s i s

    n e e d e d t o i n t e r a c t w i t h t h e R a s p b e r r y P i . I c h o s e t o

    r e c o r d a l l o f t h e d a t a o n t o t h e S D c a r d . H o w e v e r , t h e

    d a t a c o u l d b e w r i t t e n t o a U S B h a r d d r i v e c o n n e c t e d

    t h r o u g h a p o w e r e d U S B h u b , o r t o a n e t w o r k

    m o u n t e d f i l e s y s t e m , o r b u f f e r e d l o c a l l y a n d t h e n

    u p l o a d e d . W r i t i n g a l l o f t h e d a t a a l l o w s a c o m p l e t e

    a n a l y s i s a f t e r w a r d s a n d r e d u c e s t h e w r i t e a c c e s s t o

    t h e s t o r a g e m e d i a i n c o m p a r i s o n t o a r o t a t i n g b u f f e r .

    R e c o r d i n g s e n s o r d a t a f o r o n e y e a r c o u l d r e p r e s e n t

    h u n d r e d s o f m e g a b y t e s o f d a t a . I f a l o g o f d a t a

    n e e d s t o b e w r i t t e n t h e n e i t h e r a h a r d d i s k d r i v e o r

    n e t w o r k m o u n t e d f i l e s y s t e m m a y b e n e e d e d .

    R a s p b i a n i s a r e a l l y c o n v e n i e n t o p e r a t i n g s y s t e m .

    T h e d e f a u l t i m a g e n o w i n c l u d e s t h e O r a c l e J a v a

    v i r t u a l m a c h i n e , w h i c h i s v e r y e f f i c i e n t . T h e d e f a u l t

    R a s p b i a n i m a g e a l s o i n c l u d e s s y s t e m d r i v e r s f o r t h e

    b u s e s ( I

    2

    C , S P I , U A R T ) a v a i l a b l e v i a t h e G P I O p i n s .

    T o i n s t a l l R a s p b i a n o n a S D c a r d , f o l l o w t h e o f f i c i a l

    t u t o r i a l s a t h t t p : / / w w w . r a s p b e r r y p i . o r g / d o w n l o a d s .

    T h e b u s e s a v a i l a b l e o n t h e R a s p b e r r y P i G P I O c a n

    b e c o n t r o l l e d u s i n g t h e P i 4 J J a v a l i b r a r y . T h i s l i b r a r y

    i s d i s c u s s e d l a t e r i n t h i s a r t i c l e . F o r t h o s e u n f a m i l i a r

    w i t h J a v a , t h e C u p o f J a v a M a g P i s e r i e s p r o v i d e s a

    b a s i c i n t r o d u c t i o n i n I s s u e s 1 4 a n d 1 7 .

    C o n n e c t i n g t h e B M P 0 8 5

    T h e B M P 0 8 5 i s a v e r y p r e c i s e a n d a f f o r d a b l e s o l u t i o n

    t o m e a s u r e t e m p e r a t u r e a n d a i r p r e s s u r e . I t c a n b e

    o r d e r e d f r o m h t t p : / / w w w . a d a f r u i t . c o m o r f r o m o n e o f

    t h e i r d i s t r i b u t o r s . I t u s u a l l y c o m e s m o u n t e d o n a P C B

    b o a r d t h a t c a n b e c o n n e c t e d d i r e c t l y t o t h e

    http://www.adafruit.com/http://www.raspberrypi.org/downloads
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    11/441 1

    R a s p b e r r y P i I

    2

    C b u s .

    B e f o r e t h e B M P 0 8 5 c a n b e u s e d t h e i 2 c k e r n e l

    m o d u l e n e e d s t o b e e n a b l e d . F r o m t h e c o m m a n d

    l i n e , e n t e r :

    cd /etc

    sudo nano modprobe.d/raspi-blacklist.conf

    L o o k f o r t h e e n t r y blacklist i2c-bcm2708 a n d a d d

    a h a s h # a t t h e b e g i n n i n g o f t h e l i n e s o i t b e c o m e s

    #blacklist i2c-bcm2708. P r e s s < C t r l > + < X > t h e n

    p r e s s < Y > a n d < E n t e r > t o s a v e a n d e x i t .

    N e x t e d i t t h e m o d u l e s f i l e . F r o m t h e c o m m a n d l i n e ,

    e n t e r :

    sudo nano modules

    A d d i2c-dev o n a n e w l i n e . P r e s s < C t r l > + < X > t h e n

    p r e s s < Y > a n d < E n t e r > t o s a v e a n d e x i t .

    R e b o o t t h e R a s p b e r r y P i , o p e n a t e r m i n a l w i n d o w

    a n d e n t e r :

    sudo i2cdetect -y 1

    N O T E : U s e 0 i n s t e a d o f 1 f o r t h e b u s n u m b e r i n t h e

    a b o v e c o m m a n d i f y o u h a v e a r e v i s i o n 1 R a s p b e r r y

    P i . T h e r e v i s i o n 1 P i d o e s n o t h a v e a n y m o u n t i n g

    h o l e s o n t h e P C B ; n e w e r r e v i s i o n s h a v e 2 m o u n t i n g

    h o l e s .

    T h e r e s p o n s e s h o u l d s h o w 7 7 i n t h e a d d r e s s l i s t .

    M o r e i n f o r m a t i o n i s p r o v i d e d i n t h e A d a f r u i t t u t o r i a l a t

    h t t p : / / g o o . g l / P D r Z G L .

    J a v a d a t a l o g g i n g p r o g r a m

    O r a c l e h a s p r o v i d e d a J a v a v i r t u a l m a c h i n e t h a t h a s

    b e e n o p t i m i s e d f o r t h e R a s p b e r r y P i . C h e c k y o u r

    J a v a v e r s i o n b y t y p i n g :

    java -version

    T h e r e s p o n s e s h o u l d b e 1 . 7 . 0 _ 4 0 o r h i g h e r .

    T o g e t t h e s o u r c e c o d e n e e d e d f o r t h i s p r o j e c t f o l l o w

    t h e i n s t r u c t i o n s g i v e n a t h t t p : / / g o o . g l / K q P H 2 4 .

    t o c h e c k o u t . T h e s o u r c e c o d e m u s t b e c u s t o m i s e d

    a c c o r d i n g t h e s e n s o r s c o n n e c t e d a n d t h e o n l i n e

    s e r v e r u s e d . D e t a i l s o f t h e P i 4 J J a v a l i b r a r y a r e g i v e n

    a t h t t p : / / p i 4 j . c o m .

    T h e P i 4 J l i b r a r y p r o v i d e s d i r e c t a c c e s s t o t h e S P I

    b u s a n d a l o t o f G P I O f a c i l i t i e s .

    G o a l s o f t h e p r o g r a m

    A t s t a r t - u p , c o n f i g u r e t h e c o m m u n i c a t i o n s f o r t h e

    p r o b e s a n d t h e n t h e p r o b e s t h e m s e l v e s .

    A t s t a r t - u p , l o a d t h e p r e v i o u s r e c o r d e d d a t a f r o m

    l o g f i l e s .

    R e t r i e v e d a t a f r o m p r o b e s a n d r e c o r d t h e m t o a

    p l a i n t e x t f i l e o n t h e S D c a r d .

    P e r f o r m a v e r a g i n g w i t h d i f f e r e n t t i m e s c a l e s , i n

    o r d e r t o p r e s e n t h i s t o r i c a l d a t a i n a c o n v e n i e n t w a y .

    U p l o a d a v e r a g e d d a t a t o a w e b s e r v e r u s i n g F T P t o

    p r o v i d e a c c e s s v i a t h e i n t e r n e t .

    U s e a c h a r t a s a g r a p h i c a l u s e r i n t e r f a c e , t o m a k e

    l o c a l v i s u a l i s a t i o n e a s y .

    B y r e c o r d i n g a l l o f t h e d a t a , i t i s t h e n p o s s i b l e t o

    c h a n g e t h e a n a l y s i s m e t h o d o r a n a l y s e t h e f u l l d a t a .

    http://pi4j.com/http://goo.gl/KqPH24http://goo.gl/PDrZGL
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    12/442

    S t r u c t u r e

    P r o b e s c o n n e c t e d t o t h e R a s p b e r r y P i c a n p r o v i d e s e v e r a l v a l u e s t h a t m a y b e i n d e p e n d e n t . T h e c h o s e n s o l u t i o n

    c o n s i s t s o f a n a b s t r a c t c l a s s f o r p r o b e s w h i c h m u s t b e i m p l e m e n t e d f o r e a c h s e n s o r c o n n e c t e d t o t h e s y s t e m . A

    p r o b e i m p l e m e n t a t i o n o f t h i s c l a s s m u s t p r o v i d e o n e o r s e v e r a l D a t a C h a n n e l o b j e c t s , w h i c h r e p r e s e n t t h e d a t a

    m o d e l . I n t e r m s o f a m o d e l - v i e w - c o n t r o l l e r p a t t e r n , t h e P r o b e M a n a g e r c l a s s c a n b e s e e n a s t h e c o n t r o l l e r ,

    c u s t o m i s i n g t h e v i e w .

    T h e D a t a C h a n n e l c l a s s i n c l u d e s a c t i v i t i e s t h a t a r e c o m m o n f o r e a c h t y p e o f d a t a - i t l o a d s p r e v i o u s d a t a f r o m t h e

    l o g f i l e w h e n i t i s i n s t a n t i a t e d , l o g s n e w d a t a a n d p e r f o r m s a v e r a g i n g . T h e d i f f e r e n t i m p l e m e n t a t i o n s o f

    A b s t r a c t P r o b e i n c l u d e f u n c t i o n a l i t y s p e c i f i c t o a g i v e n t y p e o f s e n s o r . E a c h d e r i v e d c l a s s s h o u l d c o n f i g u r e t h e

    s e n s o r , p r o c e s s o r c o n v e r t d a t a a n d t h e n a d d t h e m t o t h e r i g h t D a t a C h a n n e l o b j e c t .

    I n t e r f a c i n g w i t h t h e B M P 0 8 5

    T h e c l a s s B M P 0 8 5 p r o b e e x t e n d s A b s t r a c t P r o b e a n d p r o v i d e s a c c e s s t o D a t a C h a n n e l o b j e c t s . T h e r e a r e t w o

    o b j e c t s : o n e f o r t e m p e r a t u r e a n d a s e c o n d o n e f o r a i r p r e s s u r e . T h e t w o a r g u m e n t s o f t h e D a t a C h a n n e l

    c o n s t r u c t o r a r e t h e n a m e f o r t h e d i s p l a y a n d t h e n a m e o f t h e d a t a f i l e .

    private DataChannel pressureChannel = new DataChannel("Atmospheric Pressure",

    "Atmospheric_Pressure");

    private DataChannel temperatureChannel = new DataChannel("Room Temperature", "Room_Temperature");

    T h e B M P 0 8 5 p r o b e c l a s s o v e r i d e s t h e a b s t r a c t m e t h o d t h a t p r o v i d e s a c c e s s t o t h e D a t a C h a n n e l o b j e c t s :

    @Override

    public DataChannel[] getChannels() {return new DataChannel[]{pressureChannel, temperatureChannel};

    }

    T h e I 2 C B u s o b j e c t f r o m t h e P i 4 J l i b r a r y i s p a s s e d t o t h e B M P 0 8 5 c o n s t r u c t o r , s i n c e i t c a n b e s e v e r a l p e r i p h e r a l s

    o n t h e s a m e I

    2

    C b u s . W i t h t h i s b u s , w e c a n c o n f i g u r e o u r I

    2

    C d e v i c e . T h e B M P 0 8 5 h a s t h e a d d r e s s 0 7 7 .

    public static final int BMP085_I2C_ADDR = 0x77;

    private I2CDevice bmp085device;

    public BMP085probe(I2CBus bus) throws IOException {

    bmp085device = bus.getDevice(BMP085_I2C_ADDR);

    readCalibrationData();DataReaderThread dataReaderThread = new DataReaderThread();

    dataReaderThread.start();

    }

    T h e d a t a R e a d e r T h r e a d o b j e c t i s u s e d t o s e n d a r e q u e s t f o r t e m p e r a t u r e a n d p r e s s u r e i n f o r m a t i o n . T h e n t h e

    t h r e a d r e a d s t w o b y t e s o f r a w t e m p e r a t u r e i n f o r m a t i o n a n d t h r e e b y t e s o f r a w p r e s s u r e i n f o r m a t i o n .

    bmp085device.write(CONTROL, READTEMPCMD); //Send read temperature command

    sleep(50); //wait the convertion time

    rawTemperature = readU16(DATA_REG); //retrieve the 2 bytes

    bmp085device.write(CONTROL, (byte) READPRESSURECMD); //Send read pressure commandsleep(50); //wait the convertion time

    msb = readU8(DATA_REG); //retrieve the 3 bytes

  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    13/441 3

    lsb = readU8(DATA_REG+1);

    xlsb = readU8(DATA_REG+2);

    rawPressure = ((msb (8-OSS); //make raw pressure integer

    T h i s r a w d a t a c a n b e c o n v e r t e d i n t o u n i t s o f P a s c a l s a n d D e g r e e s b y f o l l o w i n g t h e B M P 0 8 5 d a t a s h e e t a t

    h t t p : / / g o o . g l / C b o r F s a n d u s i n g t h e c a l i b r a t i o n v a l u e s p r e v i o u s l y r e a d . J a v a d o e s n o t s u p p o r t n a t i v e u n s i g n e d

    i n t e r g e r t y p e s . I t i s t h e r e f o r e m o r e c o n v e n i e n t t o r e p l a c e b i t s h i f t i n g w i t h d i v i s i o n a n d m u l t i p l i c a t i o n .

    double temperature = 0.0;

    double pressure = 0.0;

    double x1 = ((rawTemperature - cal_AC6) * cal_AC5) / 32768;

    double x2 = (cal_MC *2048) / (x1 + cal_MD);

    double b5 = x1 + x2;

    temperature = ((b5 + 8) / 16) / 10.0;

    double b6 = b5 - 4000;

    x1 = (cal_B2 * (b6 * b6 / 4096)) / 2048;

    x2 = cal_AC2 * b6 / 2048;

    double x3 = x1 + x2;

    double b3 = (((cal_AC1 * 4 + x3) * Math.pow(2, OSS) )+2) / 4;

    x1 = cal_AC3 * b6 / 8192;

    x2 = (cal_B1 * (b6 * b6 / 4096)) / 65536;

    x3 = ((x1 + x2) + 2) / 4;

    double b4 = cal_AC4 * (x3 + 32768) / 32768;

    double b7 = (rawPressure - b3) * (50000 / Math.pow(2, OSS));

    if (b7 < 0x80000000) pressure = (b7 * 2) / b4;

    else pressure = (b7 / b4) * 2;

    x1 = (pressure / 256) * (pressure / 256);

    x1 = (x1 * 3038) / 65536;

    x2 = (-7375 * pressure) / 65536;pressure = pressure + (x1 + x2 + 3791) / 16;

    T h i s s e n s o r c a n p r o v i d e a h i g h r a t e o f d a t a . T o e n h a n c e t h e p r e c i s i o n , a t h r e a d c a n t a k e 5 d a t a p o i n t s , a v e r a g e

    t h e m a n d a d d t h i s n e w m e a s u r e m e n t e v e r y s e c o n d t o t h e d a t a C h a n n e l o b j e c t .

    T o a d d t h e B M P 0 8 5 t o t h e f i n a l p r o g r a m , i n t h e m a i n c l a s s o f t h e p r o g r a m w e g e t a n i n s t a n c e o f t h e P i 4 J I 2 C B u s ,

    i n s t a n t i a t e t h e B M P 0 8 5 p r o b e a n d a d d i t t o t h e P r o b e M a n a g e r .

    private void initI2CandBMP085probe() {

    try {final I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1); //Change to BUS_0 for Rev 1 boards.

    bmp085Probe = new BMP085probe(bus);

    probeManager.addProbe(bmp085Probe);

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    N o w t h e t w o D a t a C h a n n e l t e m p e r a t u r e a n d a i r p r e s s u r e v a l u e s c a n b e a c q u i r e d , l o g g e d , a n d d i s p l a y e d a s a

    c h a r t .

    I n t h e n e x t a r t i c l e , a d d i t i o n a l i n s t r u c t i o n s w i l l b e g i v e n t o e x p o r t t h e d a t a a n d s e t u p a w e b p a g e t o v i e w t h e d a t a .

    U n t i l t h e n , t r y p u t t i n g t h e c o d e t o g e t h e r a n d t r y i t o u t w i t h t h e h a r d w a r e c o n f i g u r a t i o n d e s c r i b e d .

    http://goo.gl/CborFs
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    14/4414

    SKILL LEVEL : INTERMEDIATE

    Andrew Wilson

    A n d y P i

    PYTHON CONTROLLED LCDG e t y o u r s f r o m h t t p : / / a n d y p i . c o . u k /

    Scrolling an RSS feed on anAndyPi HD44780 LCD via GPIO

    F o r m a n y R a s p b e r r y P i p r o j e c t s , p r o v i d i n g v i s u a l

    o u t p u t i s i m p o r t a n t , b u t a s t a n d a r d H D M I s c r e e n

    i s e i t h e r t o o l a r g e o r u n n e c e s s a r y . F o r e x a m p l e ,

    y o u m a y h a v e a t e m p e r a t u r e s e n s o r a n d o n l y

    n e e d t o d i s p l a y t h e c u r r e n t v a l u e , o r y o u m a y

    w a n t t o d i s p l a y a n i n t e r n e t r a d i o s t a t i o n n a m e o f

    a " h e a d l e s s " R a s p b e r r y P i . A l t e r n a t i v e l y , y o u

    c o u l d h a v e a s t a n d a l o n e d i s p l a y f o r y o u r l a t e s t

    t w e e t s .

    T h i s t u t o r i a l e x p l a i n s h o w y o u c a n c o n n e c t a n

    i n e x p e n s i v e H D 4 4 7 8 0 t y p e 1 6 c h a r a c t e r 2 l i n e

    L C D ( L i q u i d C r y s t a l D i s p l a y ) t o y o u r R a s p b e r r y

    P i ' s G P I O p o r t a n d d i s p l a y t h e t i m e a n d t h e l a t e s t

    n e w s h e a d l i n e f r o m t h e B B C R S S f e e d .

    T h e A n d y P i w e b s i t e ( h t t p : / / a n d y p i . c o . u k / ) a l s o

    c o n t a i n s v i d e o a n d w r i t t e n t u t o r i a l s o n h o w y o u

    c a n u s e t h i s L C D t o d i s p l a y m e d i a i n f o r m a t i o n

    s u c h a s m p 3 t i t l e s u s i n g R a s p B M C o r R a s p y F i .

    Hardware set-up

    A b r i e f d e s c r i p t i o n o f p i n c o n n e c t i o n s i s

    d e s c r i b e d h e r e b u t a f u l l l i s t o f p a r t s a n d d e t a i l e d

    c o n s t r u c t i o n i n f o r m a t i o n i s a v a i l a b l e a t

    h t t p : / / a n d y p i . c o . u k / . Y o u c a n b u y t h e p a r t s

    i n d i v i d u a l l y a n d m a k e y o u r o w n , o r y o u c a n

    p u r c h a s e a c o m p l e t e , p r e - s o l d e r e d k i t o f p a r t s

    f r o m A n d y P i f o r 1 2 . 9 9 o r + P & P ( E U o n l y ) .

    O n t h e L C D , w i r e a 1 0 K p o t e n t i o m e t e r ( f o r

    c o n t r a s t c o n t r o l ) b e t w e e n V S S ( 1 ) a n d V O ( 3 ) ,

    c o n n e c t K ( 1 6 ) t o R W ( 5 ) , a n d c o n n e c t K ( 1 6 ) t o

    V S S ( 1 ) . L C D t o G P I O c o n n e c t i o n s a s f o l l o w s :

    Software set-up

    S t a r t i n g w i t h t h e l a t e s t v e r s i o n o f R a s p b i a n ,

    m a k e s u r e y o u r s y s t e m i s u p t o d a t e , a n d i n s t a l l

    s o m e p y t h o n r e l a t e d t o o l s :

    s u d o a p t - g e t u p d a t e

    s u d o a p t - g e t i n s t a l l - y p y t h o n - d e v \

    p y t h o n - s e t u p t o o l s p y t h o n - p i p

    W e n e e d t o i n s t a l l s o m e p y t h o n m o d u l e s ;

    w i r i n g p i f o r G P I O c o n t r o l ; f e e d p a r s e r t o r e a d t h e

    R S S f e e d , a n d t h e p r o c e s s i n g m o d u l e t o a l l o w u s

    t o u s e t h r e a d i n g ( e x p l a i n e d l a t e r ) :

    LCD pin LCD pin name GPIO (P1) pin

    1

    V S S G N D

    2V D D

    + 5 v

    1 5A

    1 8

    1 4D 7

    1 4

    1 3D 6

    2 3

    1 2 2 4

    0 8

    1 1D 4

    2 5

    6E

    D 5

    4

    R S

    0 7

    http://andypi.co.uk/http://andypi.co.uk/http://andypi.co.uk/http://andypi.co.uk/http://andypi.co.uk/http://andypi.co.uk/
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    15/44

    G e t f u l l d e t a i l s o f a l l f u n c t i o n s o f t h e A n d y P i _ L C D

    c l a s s a n d b u y t h e k i t f r o m h t t p : / / a n d y p i . c o . u k

    T h i s a r t i c l e i s s p o n s o r e d b y :

    15

    s u d o p i p i n s t a l l f e e d p a r s e r \

    p r o c e s s i n g w i r i n g p i

    F i n a l l y , d o w n l o a d t h e A n d y P i L C D p y t h o n c l a s s :

    s u d o w g e t h t t p : / / a n d y p i . c o . u k / d o w n l o a d s /

    A n d y P i _ L C D . p y

    Python script

    T h e A n d y P i L C D c l a s s h a s a n u m b e r o f f u n c t i o n s

    t o e n a b l e s i m p l e c o n t r o l o f t h e L C D . W e ' l l m a k e

    u s e o f a f e w o f t h e s e . C r e a t e a n e w s c r i p t ( i n t h e

    s a m e f o l d e r a s A n d y P i _ L C D . p y ) a s f o l l o w s :

    # ! / u s r / b i n / p y t h o n

    f r o m A n d y P i _ L C D i m p o r t A n d y P i _ L C D

    f r o m p r o c e s s i n g i m p o r t P r o c e s s

    i m p o r t t i m e

    i m p o r t f e e d p a r s e r

    l c d = A n d y P i _ L C D ( )

    l c d . l c d _ i n i t ( )

    l c d . l e d ( 5 1 2 )

    m s g = f e e d p a r s e r . p a r s e (

    ' h t t p : / / f e e d s . b b c i . c o . u k / n e w s / r s s . x m l ? e d i t

    i o n = u k ' ) . e n t r i e s [ 0 ] . t i t l e

    A f t e r i m p o r t i n g t h e r e q u i r e d m o d u l e s , w e s e t t h e

    v a r i a b l e " l c d " a s t h e A n d y P i _ L C D ( ) c l a s s ,

    i n i t i a l i s e t h e L C D , a n d s e t t h e b r i g h t n e s s o f t h e

    b a c k l i g h t ( 0 = o f f , 5 1 2 = f u l l b r i g h t n e s s , u s i n g

    P W M ) . T h e n w e c a n u s e t h e f e e d p a r s e r m o d u l e

    t o s e t t h e s t r i n g v a r i a b l e " m s g " t o t h e f i r s t t i t l e o f

    t h e B B C w o r l d n e w s f e e d .

    T o d i s p l a y t e x t , y o u c a n u s e t h e A n d y P i _ L C D

    f u n c t i o n s t a t i c _ t e x t ( ) . H e r e w e d i s p l a y t e x t o n l i n e

    1 a n d 2 , a n d c l e a r i t a f t e r 3 0 s e c o n d s :

    l c d . s t a t i c _ t e x t ( 1 , c , W o r l d N e w s : )

    l c d . s t a t i c _ t e x t ( 2 , l , m s g )

    t i m e . s l e e p ( 3 0 )

    l c d . c l s ( )

    T h i s s c r i p t i s u s e f u l f o r d i s p l a y i n g a s h o r t s t a t i c

    m e s s a g e , b u t i t ' s n o t m u c h u s e f o r a n R S S f e e d

    a s i t o n l y d i s p l a y s t h e f i r s t 1 6 c h a r a c t e r s t h a t f i t .

    I n s t e a d w e c a n u s e t h e f u n c t i o n c a l l e d

    s c r o l l _ c l o c k , w h i c h d i s p l a y s t h e t i m e o n o n e l i n e ,

    a n d s c r o l l s t h e f u l l t e x t a l o n g t h e t h e s e c o n d .

    H o w e v e r , i n o r d e r t o u p d a t e t h e L C D b y m o v i n g

    o n e c h a r a c t e r a l o n g a t a t i m e , t h e f u n c t i o n l o o p s

    i n f i n i t e l y - a n d t h e r e f o r e n o c o d e a f t e r t h i s i s

    e x e c u t e d . T o g e t a r o u n d t h i s , w e c a n r u n t h i s

    f u n c t i o n i n a t h r e a d ( a s i m u l t a n e o u s l y r u n n i n g

    p r o c e s s ) s o w e c a n t h e n c o n t i n u e t o g i v e f u r t h e r

    c o m m a n d s ( i n t h i s c a s e t o c h e c k f o r t h e l a t e s t

    n e w s u p d a t e s ) . H e r e w e s e t u p a t h r e a d p r o c e s s

    u s i n g s c r o l l _ c l o c k , s t a r t t h e t h r e a d , w a i t 6 0

    s e c o n d s , u p d a t e " m s g " t o t h e l a t e s t R S S f e e d ,

    a n d s t o p t h e t h r e a d . T h e w h i l e l o o p t h e n r e p e a t s

    t o c o n t i n u e s c r o l l i n g t h e t e x t . I n g e n e r a l , i t ' s b a d

    p r a c t i c e t o t e r m i n a t e a t h r e a d , b u t i n t h i s c a s e w e

    k n o w t h a t s c r o l l _ c l o c k i s a n i n f i n i t e l o o p t h a t w i l l

    n e v e r c o m p l e t e .

    w h i l e T r u e :

    p = P r o c e s s ( t a r g e t = l c d . s c r o l l _ c l o c k ,

    a r g s = ( 1 , " c " , 0 . 3 , m s g ) )

    p . s t a r t ( )

    t i m e . s l e e p ( 6 0 . 0 )

    m s g = f e e d p a r s e r . p a r s e ( ' h t t p : / / f e e d s .

    b b c i . c o . u k / n e w s / r s s . x m l ? e d i t i o n = u k ' ) . e n t r i

    e s [ 0 ] . t i t l e

    p . t e r m i n a t e ( )

    T h e s c r o l l _ c l o c k f u n c t i o n t a k e s f o u r a r g u m e n t s .

    F i r s t l y , c h o o s e e i t h e r 1 o r 2 t o d e t e r m i n e w h i c h

    l i n e t h e c l o c k i s p l a c e d o n . S e c o n d l y , c h o o s e l ,

    r o r c t o s e t t h e c l o c k a l i g n m e n t . T h i r d l y ,

    s p e c i f y t h e s c r o l l i n g s p e e d , a n d t h e f i n a l

    a r g u m e n t t a k e s a n y s t r i n g o f c h a r a c t e r s - h e r e

    t h e v a r i a b l e m s g ( w h i c h c o n t a i n s R S S f e e d t e x t ) .

    R S S f e e d s o f m a n y d i f f e r e n t t o p i c s a r e w i d e l y

    a v a i l a b l e o n t h e i n t e r n e t , b u t t h e r e a r e m a n y

    o t h e r t h i n g s y o u c o u l d u s e t h i s d i s p l a y f o r t o o -

    t h i s s c r i p t i s j u s t t h e s t a r t f o r y o u r o w n

    e x p e r i m e n t s !

    http://andypi.co.uk/
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    16/446

    S K I L L L E V E L : I N T E R M E D I A T E

    J o h n S h o v i c

    G u e s t W r i t e r

    P R O J E C T C U R A C A O

    R e m o t e s e n s o r m o n i t o r i n g i n t h e C a r i b b e a n

    P a r t 2 : T h e e n v i r o n m e n t a l

    s u b s y s t e m

    W h a t i s P r o j e c t C u r a c a o ?

    T h i s i s t h e s e c o n d i n a f o u r p a r t s e r i e s d i s c u s s i n g

    t h e d e s i g n a n d b u i l d i n g o f P r o j e c t C u r a c a o , a

    s e n s o r f i l l e d p r o j e c t t h a t w i l l h a n g o n a r a d i o t o w e r

    o n t h e i s l a n d n a t i o n o f C u r a c a o . C u r a c a o i s a

    d e s e r t i s l a n d 1 2 d e g r e e s n o r t h o f t h e e q u a t o r i n t h e

    C a r i b b e a n .

    P r o j e c t C u r a c a o i s d e s i g n e d t o m o n i t o r t h e l o c a l

    e n v i r o n m e n t u n a t t e n d e d f o r s i x m o n t h s . I t o p e r a t e s

    o n s o l a r p o w e r c e l l s a n d w i l l c o m m u n i c a t e w i t h t h e

    d e s i g n e r v i a a n i P a d A p p c a l l e d R a s P i C o n n e c t . A l l

    a s p e c t s o f t h i s p r o j e c t a r e d e s i g n e d t o b e

    m o n i t o r e d r e m o t e l y .

    S y s t e m d e s c r i p t i o n

    P r o j e c t C u r a c a o c o n s i s t s o f f o u r s u b s y s t e m s . A

    R a s p b e r r y P i M o d e l A i s t h e b r a i n s a n d t h e o v e r a l l

    c o n t r o l l e r . T h e P o w e r S u b s y s t e m w a s d e s c r i b e d i n

    p a r t 1 . I n P a r t 2 w e w i l l d e s c r i b e t h e E n v i r o n m e n t a l

    S e n s o r S u b s y s t e m .

    T h e E n v i r o n m e n t a l S u b s y s t e m

    T h i s s u b s y s t e m c o n s i s t s o f a n i n d o o r h u m i d i t y

    s e n s o r ( i n d o o r h e r e r e f e r s t o i n s i d e t h e b o x ) , a n

    i n d o o r t e m p e r a t u r e a n d b a r o m e t r i c p r e s s u r e

    s e n s o r , a n d a l u m i n o s i t y s e n s o r . A n o u t d o o r

    t e m p e r a t u r e a n d h u m i d i t y s e n s o r i s p l a c e d o n t h e

    o u t s i d e o f t h e b o t t o m o f t h e b o x . A l l o f t h e s e

    s e n s o r s a r e c o n n e c t e d t o t h e R a s p b e r r y P i M o d e l

    A , w i t h t h e e x c e p t i o n o f t h e A M 2 3 1 5 o u t d o o r

    t e m p e r a t u r e a n d h u m i d i t y s e n s o r , w h i c h i s

    c o n n e c t e d t o t h e A r d u i n o b a s e d b a t t e r y w a t c h d o g

    f o r r e a s o n s g i v e n o n t h e n e x t p a g e . A s m a l l

    c o m p u t e r f a n u n d e r R a s p b e r r y P i c o n t r o l i s a l s o

    c o n n e c t e d t o p r o v i d e a i r f l o w t h r o u g h t h e b o x w h e n

    i n b o x t e m p e r a t u r e s g e t t o o h i g h o r t h e i n d o o r

    h u m i d i t y g e t s t o o h i g h .

    W h a t h a r d w a r e t o u s e ?

    W e a r e u s i n g t h e f o l l o w i n g h a r d w a r e f o r t h e

    E v i r o n m e n t a l s u b s y s t e m :

    1 A d a f r u i t B M P 0 8 5 T e m p / B a r o S e n s o r ( I

    2

    C )

    1 A d a f r u i t D H T 2 2 T e m p / H u m i d i t y S e n s o r

    1 A M 2 3 1 5 - E n c a s e d T e m p / H u m i d i t y S e n s o r

    1 A d a f r u i t T S L 2 5 6 1 D i g i t a l L u m i n o s i t y S e n s o r

    1 A k u s t 5 V - 1 2 V 8 c m C o m p u t e r F a n

    1 E v i l M a d S c i e n t i s t S i m p l e R e l a y S h i e l d

  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    17/441 7

    W h a t t o m e a s u r e ?

    W e w a n t t o m e a s u r e t h e t e m p e r a t u r e , h u m i d i t y a n d

    l o c a l l i g h t l e v e l s b o t h i n s i d e a n d o u t s i d e o f t h e

    c o n t a i n i n g b o x t o s e e w h a t i s h a p p e n i n g i n t h e l o c a l

    e n v i r o n m e n t . T h i s i n f o r m a t i o n w i l l b e p l a c e d i n a

    M y S Q L d a t a b a s e f o r l a t e r a n a l y s i s .

    P u t t i n g i n t h e s e n s o r s

    T h e B M P 0 8 5 a n d T S L 2 5 6 1 a r e s o l d e r e d o n t o a

    p r o t o t y p e b o a r d w i t h I

    2

    C c o m i n g i n f r o m t h e

    R a s p b e r r y P i . T h e r e i s a p l u g t o f u r t h e r e x t e n d t h e

    I

    2

    C b u s t o a n e n c a s e d t e m p e r a t u r e a n d h u m i d t y

    s e n s o r o u t s i d e o f t h e b o x ( t h e A M 2 3 1 5 ) . T h e

    A M 2 3 1 5 p r o v e d t o b e p r o b l e m a t i c ( s e e b e l o w ) , b u t

    t h e o t h e r I

    2

    C s e n s o r s w o r k e d l i k e c h a m p i o n s . T h e

    D H T 2 2 i n e x p e n s i v e i n d o o r t e m p e r a t u r e a n d

    h u m i d i t y s e n s o r w o r k e d w e l l w i t h a d d i t i o n a l

    s o f t w a r e t o f i l t e r o u t b a d r e a d i n g s . T h i s c a u s e d

    p r o b l e m s w i t h t h e R a s P i C o n n e c t c o n t r o l s o f t w a r e

    b e c a u s e o f t h e u n r e l i a b l e d e l a y t o g o o d r e a d i n g s

    ( s e e b e l o w ) . W e c o n t r o l t h e f a n w i t h a r e l a y t h a t

    c o n n e c t s d i r e c t l y t o t h e o u t p u t o f t h e s o l a r c e l l s

    ( 6 . 5 V o n a g o o d d a y ) . W e f i g u r e d t h a t t h e f a n

    w o u l d b e u s e d o n t h e s u n n i e s t d a y s . T h e f a n a n d

    r e l a y c o i l e a c h t a k e 7 0 m A . W e a r e i n v e s t i g a t i n g

    r e p l a c i n g t h e r e l a y w i t h a l o w e r e n e r g i z e d c o i l

    c u r r e n t ( s e e S a i n s m a r t : h t t p : / / g o o . g l / a S T U 0 z ) a s

    1 4 0 m A r e a l l y h u r t s o u r p o w e r b u d g e t .

    M o n i t o r i n g t h e s e n s o r s a n d f a n

    r e m o t e l y

    P r o j e c t C u r a c a o i s m o n i t o r e d r e m o t e l y t h r o u g h t h e

    I n t e r n e t b y t h e u s e o f S S H a n d R a s P i C o n n e c t

    ( w w w . m i l o c r e e k . c o m ) . E a c h o f t h e m a i n

    s u b s y s t e m s h a s a d i f f e r e n t d i s p l a y . T h e

    e n v i r o n m e n t a l d i s p l a y h a s a g r a p h f o r

    t e m p e r a t u r e / h u m i d i t y a n d l u m i n o s i t y / b a r o m e t r i c

    p r e s s u r e / f a n s t a t e ( s h o w n b e l o w ) . T h e s o f t w a r e f o r

    g e n e r a t i n g t h i s d i s p l a y i s a l s o o n

    g i t h u b . c o m / p r o j e c t c u r a c a o .

    P r o b l e m s w i t h s e n s o r s

    T h e r e w e r e t w o m a j o r p r o b l e m s w i t h s e n s o r s i n

    t h i s d e s i g n . F i r s t o f a l l t h e A M 2 3 1 5 i s a n o d d d u c k .

    I t h a s a p o w e r d o w n s e q u e n c e t h a t r e q u i r e s i t t o b e

    a d d r e s s e d t w i c e ( o n c e t o w a k e u p a n d o n c e t o

    r e a d - f o r e x a m p l e i t t a k e s t w o i n v o c a t i o n s o f

    i 2 c d e t e c t t o s e e t h e s e n s o r ) a n d s e c o n d l y , i t j u s t

    i s n ' t r e l i a b l e u s i n g 3 . 3 V f o r I

    2

    C . A s o l u t i o n m i g h t b e

    t o p u t a l e v e l c o n v e r t e r i n f o r t h e d e v i c e , b u t s i n c e

    w e h a d a 5 V I

    2

    C o n t h e b a t t e r y w a t c h d o g A r d u i n o ,

    w e d e c i d e d t o a d d i t t o t h e w a t c h d o g . S e c o n d l y ,

    t h e D H T 2 2 h a s s o m e v e r y t r i c k y t i m i n g i n o r d e r t o

    r e a d i t f r o m t h e R a s p b e r r y P i . S i n c e t h e R a s p b e r r y

    P i i s a m u l t i t a s k i n g m a c h i n e , y o u c a n ' t d e p e n d o n

    t h e t i m i n g 1 0 0 % . T h i s m e a n s t h a t r e a d i n g s f r o m

    t h e D H T 2 2 f a i l o n a r e g u l a r b a s i s . T h e w a y t o f i x

    t h i s p r o b l e m i s t o k e e p r e a d i n g i t u n t i l t h e c o r r e c t

    f o r m a t i s r e c e i v e d . H o w e v e r , y o u c a n o n l y r e a d i t

    e v e r y 3 s e c o n d s . T h i s p l a y s h a v o c w i t h a n H T T P

    b a s e d m o n i t o r i n g s y s t e m s u c h a s R a s P i C o n n e c t

    w i t h 1 0 - 1 5 s e c o n d t i m e o u t s . T h i s p r o b l e m w a s

    f i x e d b y r e a d i n g t h e D H T 2 2 i n t h e m a i n s o f t w a r e

    a n d h a v i n g R a s P i C o n n e c t r e a d t h e l a s t r e a d i n g

    f r o m t h e M y S Q L d a t a b a s e .

    W h a t i s c o m i n g u p ?

    P a r t 3 g o e s t h r o u g h t h e R a s p b e r r y P i C a m e r a

    S u b s y s t e m a n d P a r t 4 d e s c r i b e s t h e s o f t w a r e

    s y s t e m u s e d f o r P r o j e c t C u r a c a o . A l l o f t h e c o d e

    u s e d i n t h i s a r t i c l e i s p o s t e d o n G i t H u b a t

    g i t h u b . c o m / p r o j e c t c u r a c a o .

    M o r e d i s c u s s i o n o n P r o j e c t C u r a c a o a t :

    h t t p : / / s w i t c h d o c . b l o g s p o t . c o m

    http://switchdoc.blogspot.com/http://goo.gl/aSTU0zhttp://www.milocreek.com/http://github.com/projectcuracaohttp://github.com/projectcuracao
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    18/442018

    SKILL LEVEL : BEGINNER

    Jacob Marsh

    M o d M y P i

    PHYSICAL COMPUTINGB r o u g h t t o y o u b y M o d M y P i

    Buttons and switches with theRaspberry Pi - part 3

    I n o u r p r e v i o u s t u t o r i a l w e b u i l t a s i m p l e b u t t o n c i r c u i t

    c o n n e c t e d t o o u r R a s p b e r r y P i v i a t h e G P I O p o r t s a n d

    p r o g r a m m e d a P y t h o n s c r i p t t o e x e c u t e a c o m m a n d w h e n

    t h e b u t t o n w a s p r e s s e d . W e t h e n e x p a n d e d o u r c i r c u i t

    w i t h a n L E D . I n t h i s t u t o r i a l , w e w i l l b e e x p a n d i n g o u r

    s c r i p t t o i n c l u d e t i m e r a n d t r i g g e r f u n c t i o n s f o r o u r L E D . A s

    t h e s c r i p t w i l l b e m o r e c o m p l i c a t e d , i t r e q u i r e s a p r o p e r

    c l e a n - u p f u n c t i o n . S t a r t b y r e a d i n g t h e p r e v i o u s t w o

    t u t o r i a l s f e a t u r e d i n I s s u e s 1 7 a n d 1 8 , b e f o r e t r y i n g t h i s

    o n e !

    Adding LED control code

    N o w t h a t o u r L E D e x p a n s i o n c i r c u i t h a s b e e n b u i l t , w e w i l l

    a d d s o m e c o d e i n t o o u r p r e v i o u s p r o g r a m . T h i s a d d i t i o n a l

    c o d e w i l l m a k e t h e L E D f l a s h o n a n d o f f w h e n t h e b u t t o n i s

    p r e s s e d . S t a r t b y b o o t i n g y o u r R a s p b e r r y P i t o t h e

    R a s p b i a n G U I ( s t a r t x ) . T h e n s t a r t I D L E 3 a n d o p e n t h e

    p r e v i o u s e x a m p l e p r o g r a m b u t t o n . p y . S a v e t h i s f i l e a s

    b u t t o n _ l e d . p y a n d o p e n i t .

    S i n c e w e w a n t t h e L E D t o f l a s h o n a n d o f f , w e w i l l n e e d t o

    a d d a t i m e f u n c t i o n t o a l l o w P y t h o n t o u n d e r s t a n d t h e

    c o n c e p t o f t i m e . W e t h e r e f o r e n e e d t o i m p o r t t h e t i m e

    m o d u l e , w h i c h a l l o w s v a r i o u s t i m e r e l a t e d f u n c t i o n a l i t y t o

    b e u s e d . A d d a n o t h e r l i n e o f c o d e u n d e r n e a t h l i n e 1 :

    i m p o r t t i m e

    N e x t , w e n e e d t o d e f i n e G P I O P 1 8 [ P i n 1 2 ] a s a n o u t p u t t o

    p o w e r o u r L E D . A d d t h i s t o o u r G P I O . s e t u p s e c t i o n ( l i n e

    4 ) , b e l o w t h e i n p u t p i n s e t u p l i n e :

    G P I O . s e t u p ( 1 8 , G P I O . O U T )

    O n c e G P I O P 1 8 [ P i n 1 2 ] h a s b e e n s e t a s a n o u t p u t , w e

    c a n t u r n t h e L E D o n w i t h t h e c o m m a n d G P I O . o u t p u t ( 1 8 ,

    T r u e ) . T h i s t r i g g e r s t h e p i n t o h i g h ( 3 . 3 V ) . S i n c e o u r L E D

    i s w i r e d d i r e c t l y t o t h i s o u t p u t p i n , i t s e n d s a c u r r e n t

    t h r o u g h t h e L E D t h a t u r n s i t o n . T h e p i n c a n a l s o b e

    t r i g g e r e d l o w ( 0 V ) t o t u r n t h e L E D o f f , b y t h e c o m m a n d

    G P I O . o u t p u t ( 1 8 , F a l s e ) .

    N o w w e d o n t j u s t w a n t o u r L E D t o t u r n o n a n d o f f ,

    o t h e r w i s e w e w o u l d h a v e s i m p l y w i r e d i t t o t h e b u t t o n a n d

    a p o w e r s u p p l y . W e w a n t t h e L E D t o d o s o m e t h i n g

    i n t e r e s t i n g v i a o u r R a s p b e r r y P i a n d P y t h o n c o d e . F o r

    e x a m p l e , l e t u s m a k e i t f l a s h b y t u r n i n g i t o n a n d o f f

    m u l t i p l e t i m e s w i t h a s i n g l e p r e s s o f t h e b u t t o n !

    I n o r d e r t o t u r n t h e L E D o n a n d o f f m u l t i p l e t i m e s w e a r e

    g o i n g t o u s e a f o r l o o p . W e w a n t t h e l o o p t o b e t r i g g e r e d

    w h e n t h e b u t t o n h a s b e e n p r e s s e d . T h e r e f o r e , i t n e e d s t o

    b e i n s e r t e d w i t h i n t h e i f c o n d i t i o n ' I f i n p u t _ v a l u e = = F a l s e : ' ,

    t h a t w e c r e a t e d i n o u r o r i g i n a l p r o g r a m . A d d t h e f o l l o w i n g

    b e l o w t h e l i n e ' p r i n t ( ' ' W h o p r e s s e d m y b u t t o n ! ) ' ( l i n e 9 ) ,

    m a k i n g s u r e t h e i n d e n t a t i o n i s t h e s a m e :

    f o r x i n r a n g e ( 0 , 3 ) :

    https://www.modmypi.com/shophttps://www.modmypi.com/shophttps://www.modmypi.com/shop
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    19/4419

    A n y c o d e b e l o w t h i s f u n c t i o n w i l l b e r e p e a t e d t h r e e t i m e s .

    H e r e t h e l o o p w i l l r u n f r o m 0 t o 2 , t h e r e f o r e r u n n i n g 3

    t i m e s . N o w w e w i l l a d d s o m e c o d e i n t h e l o o p , s u c h t h a t

    t h e L E D f l a s h e s o n a n d o f f :

    G P I O . o u t p u t ( 1 8 , T r u e )

    t i m e . s l e e p ( 1 )

    G P I O . o u t p u t ( 1 8 , F a l s e )

    t i m e . s l e e p ( 1 )

    T h e L E D i s t r i g g e r e d o n w i t h t h e c o m m a n d

    G P I O . o u t p u t ( 1 8 , T r u e ) . H o w e v e r , s i n c e w e d o n o t w a n t t o

    i m m e d i a t e l y t u r n i t b a c k o f f , w e u s e t h e f u n c t i o n

    t i m e . s l e e p ( 1 ) t o s l e e p f o r o n e s e c o n d . T h e n t h e L E D i s

    t r i g g e r e d o f f w i t h t h e G P I O . o u t p u t ( 1 8 , F a l s e ) c o m m a n d .

    W e u s e t h e t i m e . s l e e p ( 1 ) f u n c t i o n a g a i n t o w a i t b e f o r e t h e

    L E D i s t u r n e d b a c k o n a g a i n .

    T h e c o m p l e t e d p r o g r a m s h o u l d b e o f t h e f o r m :

    i m p o r t R P i . G P I O a s G P I O

    i m p o r t t i m e

    G P I O . s e t m o d e ( G P I O . B C M )

    G P I O . s e t u p ( 1 7 , G P I O . I N )

    G P I O . s e t u p ( 1 8 , G P I O . O U T )

    w h i l e T r u e :

    i n p u t _ v a l u e = G P I O . i n p u t ( 1 7 )

    i f i n p u t _ v a l u e = = F a l s e :

    p r i n t ( " W h o p r e s s e d m y b u t t o n ? " )

    f o r x i n r a n g e ( 0 , 3 ) :

    G P I O . o u t p u t ( 1 8 , T r u e )

    t i m e . s l e e p ( 1 )

    G P I O . o u t p u t ( 1 8 , F a l s e )

    t i m e . s l e e p ( 1 )

    w h i l e i n p u t _ v a l u e = = F a l s e :

    i n p u t _ v a l u e = G P I O . i n p u t ( 1 7 )

    S a v e t h e f i l e a n d o p e n a n e w t e r m i n a l w i n d o w . T h e n t y p e

    t h e f o l l o w i n g c o m m a n d :

    s u d o p y t h o n b u t t o n _ l e d . p y

    T h i s t i m e w h e n w e p r e s s t h e b u t t o n a m e s s a g e w i l l a p p e a r

    o n t h e s c r e e n a n d t h e L E D s h o u l d a l s o f l a s h o n a n d o f f

    t h r e e t i m e s !

    T o e x i t t h e p r o g r a m s c r i p t , s i m p l y t y p e C T R L + C o n t h e

    k e y b o a r d t o t e r m i n a t e i t . I f i t h a s n ' t w o r k e d d o n o t w o r r y .

    D o t h e s a m e c h e c k s w e d i d b e f o r e . F i r s t , c h e c k t h e c i r c u i t

    i s c o n n e c t e d c o r r e c t l y o n t h e b r e a d b o a r d . T h e n c h e c k t h a t

    t h e j u m p e r w i r e s a r e c o n n e c t e d t o t h e c o r r e c t p i n s o n t h e

    G P I O p o r t . D o u b l e c h e c k t h e L E D i s w i r e d t h e r i g h t w a y

    r o u n d . I f t h e p r o g r a m s t i l l f a i l s , d o u b l e c h e c k e a c h l i n e o f

    t h e p r o g r a m , r e m e m b e r i n g t h a t P y t h o n i s c a s e - s e n s i t i v e

    a n d c o r r e c t i n d e n t a t i o n i s n e e d e d .

    I f i s e v e r y t h i n g i s w o r k i n g a s e x p e c t e d , y o u c a n s t a r t

    p l a y i n g a r o u n d a b i t w i t h s o m e o f t h e v a r i a b l e s . T r y

    a d j u s t i n g t h e s p e e d t h e L E D f l a s h e s b y c h a n g i n g t h e v a l u e

    g i v e n t o t h e t i m e . s l e e p ( ) f u n c t i o n .

    Y o u c a n a l s o c h a n g e t h e n u m b e r o f t i m e s t h a t t h e L E D

    f l a s h e s b y a l t e r i n g t h e n u m b e r t i m e s t h a t t h e f o r l o o p i s

    r e p e a t e d . F o r e x a m p l e i f y o u w a n t e d t h e L E D t o f l a s h 3 0

    t i m e s , c h a n g e t h e l o o p t o : f o r x i n r a n g e ( 0 , 3 0 ) .

    H a v e a g o p l a y i n g a r o u n d w i t h b o t h t h e s e v a r i a b l e s a n d

    s e e w h a t h a p p e n s !

    Exiting a program cleanly

    W h e n a p r o g r a m i s t e r m i n a t e d ( d u e t o a n e r r o r , a k e y b o a r d

    i n t e r r u p t ( C T R L + C ) o r s i m p l y b e c a u s e i t ' s c o m e t o a n

    e n d ) , a n y G P I O p o r t s t h a t w e r e i n u s e w i l l c a r r y o n d o i n g

    w h a t t h e y w e r e d o i n g a t t h e t i m e o f t e r m i n a t i o n . T h e r e f o r e ,

    i f y o u t r y t o r u n t h e p r o g r a m a g a i n , a w a r n i n g m e s s a g e w i l l

    a p p e a r w h e n t h e p r o g r a m t r i e s t o ' s e t ' a p i n t h a t s a l r e a d y

    i n u s e f r o m t h e p r e v i o u s e x e c u t i o n o f t h e p r o g r a m . T h e

    p r o g r a m w i l l p r o b a b l y r u n f i n e , b u t i t i s g o o d p r a c t i c e t o

    a v o i d t h e s e s o r t s o f m e s s a g e s , e s p e c i a l l y a s y o u r

    p r o g r a m s b e c o m e l a r g e r a n d m o r e c o m p l e x !

    T o h e l p u s e x i t a p r o g r a m c l e a n l y w e a r e g o i n g t o u s e t h e

    c o m m a n d G P I O . c l e a n u p ( ) , w h i c h w i l l r e s e t a l l o f t h e G P I O

    p o r t s t o t h e i r d e f a u l t v a l u e s .

    F o r s o m e p r o g r a m s y o u c o u l d s i m p l y p l a c e t h e

    G P I O . c l e a n u p ( ) c o m m a n d a t t h e e n d o f y o u r p r o g r a m .

    T h i s w i l l c a u s e t h e G P I O p o r t s t o b e r e s e t w h e n t h e

    p r o g r a m f i n i s h e s . H o w e v e r , o u r p r o g r a m n e v e r e n d s b y

    i t s e l f s i n c e i t c o n s t a n t l y l o o p s t o c h e c k i f t h e b u t t o n h a s

    b e e n p r e s s e d . W e w i l l t h e r e f o r e u s e t h e t r y : a n d e x c e p t

    s y n t a x , s u c h t h a t w h e n o u r p r o g r a m i s t e r m i n a t e d b y a

    k e y b o a r d i n t e r r u p t i o n , t h e G P I O p o r t s a r e r e s e t

    a u t o m a t i c a l l y .

    T h e f o l l o w i n g P y t h o n i s a n e x a m p l e o f h o w t h e t r y : a n d

    e x c e p t c o m m a n d c a n b e u s e d t o g e t h e r t o e x i t a p r o g r a m

    c l e a n l y .

  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    20/4420

    # P l a c e a n y v a r i a b l e d e f i n i t i o n s a n d

    # G P I O s e t - u p s h e r e

    t r y :

    # P l a c e y o u r m a i n b l o c k o f c o d e o r

    # l o o p h e r e

    e x c e p t K e y b o a r d I n t e r r u p t :

    G P I O . c l e a n u p ( )

    # P r o g r a m w i l l e n d a n d G P I O p o r t s

    # c l e a n e d w h e n y o u h i t C T R L + C

    f i n a l l y :

    G P I O . c l e a n u p ( )

    N o t e t h a t P y t h o n w i l l i g n o r e a n y t e x t p l a c e d a f t e r h a s h t a g s

    ( # ) w i t h i n a s c r i p t . Y o u m a y c o m e a c r o s s t h i s a l o t w i t h i n

    P y t h o n , s i n c e i t i s a g o o d w a y o f a n n o t a t i n g p r o g r a m s w i t h

    n o t e s .

    A f t e r w e h a v e i m p o r t e d P y t h o n m o d u l e s , s e t u p o u r G P I O

    p i n s , w e n e e d t o p l a c e t h e m a i n b l o c k o f o u r c o d e w i t h i n

    t h e t r y : c o n d i t i o n . T h i s p a r t w i l l r u n a s u s u a l , e x c e p t w h e n

    a k e y b o a r d i n t e r r u p t i o n o c c u r s ( C T R L + C ) . I f a n

    i n t e r r u p t i o n o c c u r s t h e G P I O p o r t s w i l l b e r e s e t w h e n t h e

    p r o g r a m e x i t s . T h e f i n a l l y : c o n d i t i o n i s i n c l u d e d s u c h t h a t i f

    o u r p r o g r a m i s t e r m i n a t e d b y a c c i d e n t , i f t h e r e i s a n e r r o r

    w i t h o u t u s i n g o u r d e f i n e d k e y b o a r d f u n c t i o n , t h e n t h e

    G P I O p o r t s w i l l b e c l e a n e d b e f o r e e x i t .

    O p e n b u t t o n _ l e d . p y i n I D L E 3 a n d s a v e i t a s

    b u t t o n _ c l e a n u p . p y . N o w w e c a n a d d t h e c o d e p r e v i o u s l y

    d e s c r i b e d i n t o o u r s c r i p t . T h e f i n i s h e d p r o g r a m s h o u l d

    h a v e t h e f o r m :

    i m p o r t R P i . G P I O a s G P I O

    i m p o r t t i m e

    G P I O . s e t m o d e ( G P I O . B C M )

    G P I O . s e t u p ( 1 7 , G P I O . I N )

    G P I O . s e t u p ( 1 8 , G P I O . O U T )

    t r y :

    w h i l e T r u e :

    i n p u t _ v a l u e = G P I O . i n p u t ( 1 7 )

    i f i n p u t _ v a l u e = = F a l s e :

    p r i n t ( " W h o p r e s s e d m y b u t t o n ? " )

    f o r x i n r a n g e ( 0 , 3 ) :

    G P I O . o u t p u t ( 1 8 , T r u e )

    t i m e . s l e e p ( 1 )

    G P I O . o u t p u t ( 1 8 , F a l s e )

    t i m e . s l e e p ( 1 )

    w h i l e i n p u t _ v a l u e = = F a l s e :

    i n p u t _ v a l u e = G P I O . i n p u t ( 1 7 )

    e x c e p t K e y b o a r d I n t e r r u p t :

    G P I O . c l e a n u p ( )

    # P r o g r a m w i l l e n d a n d G P I O p o r t s c l e a n e d

    # w h e n y o u h i t C T R L + C

    f i n a l l y :

    G P I O . c l e a n u p ( )

    N o t i c e t h a t o n l y t h e l o o p p a r t o f t h e p r o g r a m i s w i t h i n t h e

    t r y : c o n d i t i o n . A l l o u r i m p o r t s a n d G P I O s e t - u p s a r e l e f t a t

    t h e t o p o f t h e s c r i p t . I t i s a l s o i m p o r t a n t t o m a k e s u r e t h a t

    a l l o f y o u r i n d e n t a t i o n s a r e c o r r e c t !

    S a v e t h e f i l e . T h e n r u n t h e p r o g r a m a s b e f o r e i n a t e r m i n a l

    w i n d o w t e r m i n a l :

    s u d o p y t h o n b u t t o n _ c l e a n u p . p y

    T h e f i r s t t i m e y o u r u n t h e f i l e , y o u m a y s e e t h e w a r n i n g

    m e s s a g e a p p e a r s i n c e t h e G P I O p o r t s h a v e n o t b e e n r e s e t

    y e t . E x i t t h e p r o g r a m w i t h a k e y b o a r d i n t e r r u p t i o n

    ( C T R L + X ) . T h e n r u n t h e p r o g r a m a g a i n a n d h o p e f u l l y t h i s

    t i m e n o w a r n i n g m e s s a g e s w i l l a p p e a r !

    T h i s e x t r a c o d e m a y s e e m l i k e a w a s t e o f t i m e b e c a u s e t h e

    p r o g r a m s t i l l r u n s f i n e w i t h o u t i t ! H o w e v e r , w h e n w e a r e

    p r o g r a m m i n g , w e a l w a y s w a n t t o t r y a n d b e i n c o n t r o l o f

    e v e r y t h i n g t h a t i s g o i n g o n . I t i s g o o d p r a c t i c e t o a d d t h i s

    c o d e , t o r e s e t t h e G P I O w h e n t h e p r o g a m i s t e r m i n a t e d .

    This article issponsored by

    ModMyPi

    A l l b r e a k o u t b o a r d s a n d a c c e s s o r i e s u s e d i n t h i s

    t u t o r i a l a r e a v a i l a b l e f o r w o r l d w i d e s h i p p i n g f r o m

    t h e M o d M y P i w e b s h o p a t w w w . m o d m y p i . c o m

    http://www.modmypi.com/http://www.modmypi.com/http://www.modmypi.com/http://www.modmypi.com/http://www.modmypi.com/http://www.modmypi.com/http://www.modmypi.com/http://www.modmypi.com/
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    21/44

    http://milocreek.com/
  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    22/4422

    SKILL LEVEL : INTERMEDIATE

    Fred Sonnenwald &Hamish Cunningham

    G u e s t W r i t e r s

    ELECTRONIC ARTU s i n g a n a c c e l e r o m e t e r

    Painting with XLoBorg

    Maker Culture meets Art

    T h e r e i s a n e w c o n j u n c t i o n e m e r g i n g a r o u n d o p e n

    h a r d w a r e , m a k e r c u l t u r e , a n d a r t . R e m e m b e r h o w p o p

    c u l t u r e c h a n g e d w i t h t h e a d v e n t o f p u n k i n t h e l a t e

    s e v e n t i e s ? W e s e e m t o b e w i t n e s s i n g a s i m i l a r e x p l o s i o n

    o f ' g a r a g e b a n d ' c r e a t i v i t y i n t h e t e n n i e s , a n d t h e R a s p b e r r y

    P i i s p r o u d l y l e a d i n g t h e c h a r g e f o r g e n e r a l p u r p o s e o p e n

    s o u r c e d e v i c e s i n t h i s c o n t e x t . ( T h e R a s p b e r r y P i

    F o u n d a t i o n e v e n h a s a n a r t i s t i n r e s i d e n c e R a c h e l

    R a y n s . )

    O p e n - s o u r c e h a r d w a r e a l l o w s p e o p l e t o m a k e t h e i r o w n

    r o b o t s , c a m e r a s o r e v e n e l e c t r o c a r d i a g r a p h m a c h i n e s b y

    d o w n l o a d i n g s c h e m a t i c s a n d i n c o r p o r a t i n g a n y c h a n g e s

    t h e y n e e d a n d , t y p i c a l l y , f r e e o p e n - s o u r c e s o f t w a r e i s

    a v a i l a b l e t o r u n t h e s e p r o j e c t s . 3 D p r i n t e r s , l a s e r c u t t e r s

    a n d C N C r o u t e r s h a v e h e l p e d t h i s a d o p t i o n o f t h e o p e n -

    e v e r y t h i n g e t h o s .

    T h e r e a r e s t a c k s o f D I Y p r o j e c t s b a s e d o n t h e R a s p b e r r y

    P i , a n d t h e f l o o d s h o w s n o s i g n o f s l o w i n g . T h e R a s p b e r r y

    P i i s t h e f i r s t g e n e r a l p u r p o s e d e v i c e ( i n c o n t r a s t t o t h e

    m a g n i f i c e n t , b u t m o r e s p e c i a l i s e d A r d u i n o ) , w h i c h i s v e r y

    e a s y t o c o b b l e t o g e t h e r w i t h a d d - o n e l e c t r o n i c s . T h e

    t h r i v i n g c o m m u n i t y t h a t h a s g r o w n u p a r o u n d t h e

    R a s p b e r r y P i ( i n c l u d i n g t h i s m a g a z i n e ) i s m a k i n g a h u g e

    i m p a c t , f r o m c h a n g e s i n t h e U K s c h o o l c u r r i c u l u m t o t h e

    O n i o n P i a n t i - s u r v e i l l a n c e d e v i c e a n d B r i c k P i ' s n e w b r a i n s

    f o r o l d l e g o R o b o t s .

    T h i s a r t i c l e d e s c r i b e s t h e P i B r u s h , a s i m p l e o n - s c r e e n

    p a i n t i n g s y s t e m t h a t u s e s t h e X L o B o r g m o t i o n a n d

    d i r e c t i o n s e n s o r a d d - o n b o a r d f r o m P i B o r g . T h e X L o B o r g

    a d d s a n a c c e l e r o m e t e r a n d a m a g n e t o m e t e r ( c o m p a s s ) t o

    y o u r P i a n d m a k e s a l l s o r t s o f m o t i o n - b a s e d i n t e r a c t i o n

    p o s s i b l e l i k e a K i n e c t , b u t f r e e a n d o p e n . T h e h e a d e r

    g r a p h i c f o r t h i s a r t i c l e w a s c r e a t e d w i t h t h e P i B r u s h .

    T h e P i B r u s h i s a n i n t e r a c t i v e a r t a n d t e c h n o l o g y e x h i b i t ( a

    r a t h e r g r a n d n a m e f o r s o m e t h i n g s o s m a l l ) t h a t s i m u l a t e s

    f l i c k i n g p a i n t o f f t h e e n d o f a p a i n t b r u s h o n t o c a n v a s a s

    J a c k s o n P o l l o c k f a m o u s l y d i d i n t h e 1 9 4 0 s a n d 5 0 s . T h e

    s e t u p i n c l u d e s t w o R a s p b e r r y P i s ( o n e M o d e l B a n d o n e

    M o d e l A ) , o n e X L o B o r g , a b a t t e r y p a c k , a d i s p l a y a n d t w o

    W i - F i d o n g l e s . T h e M o d e l A i s h e l d b y t h e u s e r a n d w a v e d

    a r o u n d . I t c o l l e c t s a c c e l e r a t i o n d a t a w i t h t h e X L o B o r g .

    T h e s e d a t a a r e t h e n t r a n s m i t t e d v i a W i - F i t o t h e M o d e l B ,

    w h i c h p r o c e s s e s t h e d a t a c o l l e c t e d i n t o p a i n t d r o p l e t s a n d

    d i s p l a y s t h e m o n t h e s c r e e n .

    F u n c t i o n a l l y i t l o o k s l i k e t h i s :

  • 8/13/2019 The Raspberry Pi Magazine.The MagPi. Issue 19

    23/4423

    a n d h e r e i s t h e h a r d w a r e :

    W e m a y i m p r o v e o n t h e e l a s t i c b a n d i n f u t u r e m o d e l s . T h e

    M o d e l A ( c l i e n t ) t o p a n d t h e M o d e l B ( s e r v e r ) b o t t o m ,

    w h e r e t h e r e d b u t t o n o n t h e s e r v e r s a v e s t h e p i c t u r e a n d

    s t a r t s a n e w p i c t u r e .

    The Code

    T h e h a r d w a r e f o r t h i s p r o j e c t i s f a i r l y s t r a i g h t f o r w a r d . T h e

    X L o B o r g p l u g s d i r e c t l y i n t o t h e G P I O p i n s a s a b l o c k o n

    t h e c l i e n t , a n d a p u s h b u t t o n s i m p l y c o n n e c t s a g r o u n d a n d

    G P I O p i n o n t h e s e r v e r . T h e s o f t w a r e i s w h e r e t h e

    c h a l l e n g e l i e s : i n t e r