This repository has been archived by the owner on Feb 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handson.tex
618 lines (495 loc) · 23.2 KB
/
handson.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
\documentclass[a4paper]{article}
% \usepackage[a4paper]{geometry}
\usepackage{a4wide}
\usepackage[dutch]{babel}
\usepackage{titling}
\pretitle{\begin{center}\LARGE\sffamily\bfseries}
\posttitle{\end{center}}
\predate{\begin{center}\sffamily\bfseries}
\postdate{\end{center}}
\usepackage{sectsty}
\allsectionsfont{\normalfont\sffamily\bfseries}
\usepackage[breaklinks]{hyperref}
\usepackage{courier}
\usepackage{color}
\definecolor{consolegray}{gray}{0.9}
\usepackage{listings}
\usepackage[parfill]{parskip}
\usepackage{graphicx}
\usepackage{subfig}
\title{Hands-on met de Raspberry Pi}
\begin{document}
\maketitle
\tableofcontents
\section{Inleiding}
In dit document wordt eerst beschreven hoe je de Raspberry Pi moet
verbinden met de verschillende randapparatuur, hoe je de Raspberry
Pi aanzet, en hoe je de Raspberry Pi de eerste keer configureert.
Daarna wordt beschreven hoe je met de camera en de afstandssensor aan
de slag kan.
Lees ook Sectie~\ref{sec:opruimen}, zelfs als je niet klaar bent met
de opdrachten. Daar staat immers beschreven hoe je de Raspberry Pi
klaar maakt voor de volgende groep indien je in de eerste groep zit,
en hoe je de software ontwerp labo's netjes achterlaat.
Dit document staat online op \url{http://goo.gl/oSvdBP}.
\subsection{Benodigdheden}
De volgende items zouden zeker in je kit moeten zitten:
\begin{enumerate}
\item Raspberry Pi Model A
\item HDMI naar DVI kabel
\item SD kaart
\item Muis \& toetsenbord
\item USB Hub
\item USB WiFi dongle
\item Raspberry Pi camera
\item Micro USB adapter
\item HC SR04 Afstandssensor
\item 4 female to female jumper kabeltjes
\end{enumerate}
\section{De Raspberry Pi opstarten}
Dit hoofdstuk beschrijft hoe je de Raspberry Pi moet aansluiten op
alle randapparatuur. Daarna zal de Raspberry Pi opstarten. Indien
het de eerste keer is dat je Raspbian opstart moeten er een aantal
zaken geconfigureerd worden.
\subsection{De Raspberry Pi connecteren}
Voor je de Raspberry Pi voor het eerst opstart moet je het
toestel connecteren met alle randapparatuur. De Raspberry Pi heeft
geen aan/uit knop. De Pi zal opstarten zodra er stroom is.
\paragraph{SD kaart} Het slot voor de SD kaart zit onderaan de
Raspberry Pi. Schuif de SD kaart met de contactpunten naar boven in
het slot.
\paragraph{Monitor} Kies bij voorkeur een monitor met een vrije
DVI ingang. Op die manier hoef je geen kabels uit te trekken. Kies
de DVI ingang als \emph{source} voor de monitor.
\paragraph{USB toestellen} Sluit de USB Hub aan op de USB poort
van de Raspberry Pi. Je gebruikt hiervoor de stekker waaruit twee
draden vertrekken. De Wifi dongle, het toetsenbord en de muis sluit
je aan op de USB Hub.
\paragraph{Micro USB adapter} Connecteer de Micro USB adapter
met de Raspberry Pi. De Raspberry Pi zal nu opstarten. Je ziet een
gekleurd vierkant op het scherm verschijnen gevolgd door de Linux boot
sequentie.
\subsection{Initi\"ele configuratie}
Wanneer je Raspbian voor het eerst opstart kom je na de start-up
in de \texttt{raspi-config} configuratie tool. Met deze tool kan je
een aantal Raspberry Pi specifieke configuratie instellingen wijzigen.
Als Raspbian reeds is opgestart van op deze SD kaart zal
\texttt{raspi-config} niet geladen worden en zal je in de plaats
daarvan het login scherm zien. Normaal gezien zijn alle instellingen
reeds in orde. Je kan inloggen zoals beschreven in
Sectie~\ref{sec:inloggen}. Moest je later toch nog instellingen
willen wijzigen kan dat door \texttt{raspi-config} manueel te starten
zoals hieronder beschreven.
Als je de \texttt{raspi-config} tool per ongeluk verlaat kan je
deze terug starten door het volgende commando in de console te typen:
\lstset{emph={pi@raspberrypi,~,$},emphstyle={\textbf},basicstyle={\ttfamily},backgroundcolor=\color{consolegray},framesep=4pt,framexleftmargin=6pt,frame=tb,framerule=0pt}
\begin{lstlisting}
pi@raspberrypi ~ $ sudo raspi-config
\end{lstlisting}
\paragraph{Filesystem uitbreiden}
Wanneer de Raspbian software op de SD kaart wordt gezet neemt
deze niet de ganse SD kaart in beslag. Om meer plaats te hebben in je
filesystem kan je het uitbreiden.
\begin{enumerate}
\item Kies optie \texttt{1 Expand Filesystem}.
\item Even later verschijnt normaal de bevestiging dat de root partitie
groter is gemaakt.
\item Druk op \emph{enter}.
\end{enumerate}
\paragraph{Correcte toetsenbord layout kiezen}
De standaard instellingen voor de toetsenbord indeling komen niet
overeen met het toetsenbord dat jullie gekregen hebben. Je moet het
juiste toetsenbord selecteren door je instelling steeds verder te
verfijnen.
\begin{enumerate}
\item Kies optie \texttt{4 Internationalisation Options}.
\item Kies vervolgens \texttt{I3 Change Keyboard Layout}.
\item Selecteer \texttt{Generic 105-key (Intl) PC} in de lijst met modellen.
\item Selecteer \texttt{Other} in de lijst met keyboard layouts.
\item Selecteer \texttt{English (US)}.
\item Selecteer \texttt{English (US)} (helemaal bovenaan).
\item Selecteer \texttt{The default for the keyboard layout}.
\item Selecteer \texttt{No compose key}.
\item Selecteer \texttt{<Yes>} om via
\texttt{Control+Alt+Backspace} de X server te kunnen stoppen.
\end{enumerate}
De nieuwe instellingen zijn onmiddelijk van kracht.
\paragraph{Camera activeren}
Om later de camera te kunnen gebruiken moet deze eerst
geconfigureerd worden. Deze wijziging zal pas actief zijn na een
reboot.
\begin{enumerate}
\item Kies optie \texttt{5 Enable Camera}.
\item In de volgende dialoog kies je voor \texttt{<Enable>}.
\end{enumerate}
\paragraph{SSH activeren}
Op termijn is het eenvoudiger om op de Raspberry Pi in te loggen
via SSH. Op die manier kan je van op je eigen computer werken en
kunnen meerdere gebruikers tegelijk de Raspberry Pi gebruiken.
\begin{enumerate}
\item Kies \texttt{8 Advance Options}.
\item Kies \texttt{A4 SSH}.
\item Kies \texttt{<Enable>}.
\item Je krijgt de bevestiging dat de SSH server vanaf nu
geactiveerd is.
\end{enumerate}
\paragraph{Rebooten} Nadat je de nodige configuratie
instellingen hebt aangepast kan je best je Raspberry Pi rebooten.
Selecteer \texttt{<Finish>} om \texttt{raspi-config} te be\"eindigen.
Kies nadien voor \texttt{<Yes>} om te rebooten.
\subsection{Inloggen}\label{sec:inloggen}
De Raspbian distributie komt met een standaard gebruiker:
\texttt{\textbf pi} en paswoord \texttt{\textbf raspberry}. Na het inloggen kom je in
de standaard Linux console. Je kan de grafische gebruikers interface
starten met:
\begin{lstlisting}
pi@raspberrypi ~ $ startx
\end{lstlisting}
\subsection{WiFi configureren}
Om je WiFi connectie te configureren kan je een grafische
configuratie tool gebruiken (\texttt{wpa\_gui}). De snelkoppeling op je bureaublad werkt
echter niet. De tool heeft \texttt{root} rechten nodig.
Open \emph{LXTerminal} via de snelkoppeling op je bureaublad. Je
krijgt een console te zien. Je kan de configuratie tool als
\texttt{root} starten met het volgende commando:
\begin{lstlisting}
pi@raspberrypi ~ $ sudo wpa_gui
\end{lstlisting}
In het venster dat opent klik je op de \emph{Scan} knop onderaan.
Er opent zich een nieuw venster \emph{Scan results}. Hier moet je
weer op de knop \emph{Scan} drukken om te scannen naar beschikbare
WiFi netwerken.
Kies het \emph{eduroam} netwerk in de lijst door er op te
dubbelklikken. Dit opent een configuratie venster voor het
\emph{eduroam} network.
Op \url{https://admin.kuleuven.be/icts/english/wifi/eduroam-ubuntu} vind
je hoe je het eduroam netwerk kan instellen op een Ubuntu Linux. Het
merendeel van de instellingen is identiek op Raspbian.
Tabel~\ref{table:eduroam} geeft een overzicht van de configuratie
nodig voor eduroam.
\begin{table}[h!]
\centering
\begin{tabular}{ll}
\hline
SSID & eduroam \\
Authentication & WPA2-Enterprise (EAP) \\
Encryption & CCMP \\
EAP method & PEAP \\
Identity & xxxxxxxxx@kuleuven.be \\
Password & your-password \\
CA certificat & \emph{blanco laten} \\
\hline
\end{tabular}
\caption{Configuratie voor het eduroam netwerk in
\texttt{wpa\_gui}. Het \emph{CA certificat} veld mag blanco blijven.}
\label{table:eduroam}
\end{table}
\section{Opdrachten}
Dit hoofdstuk beschrijft een aantal opdrachten die je helpen
vertrouwd te worden met de camera en de afstandssensor.
\subsection{De Raspberry Pi Camera module}
Je kan foto's en video's nemen met de Raspberry Pi camera module
met behulp van respectievelijk het \texttt{raspistill} en
\texttt{raspivid} commando.
\subsubsection{De camera module connecteren}
De kabel komende van de camera module moet verbonden worden met de
connector vlak naast de HDMI aansluiting. De kant met het blauwe
kleeflint moet \emph{weg} wijzen van HDMI connector. De kant met de metalen
connectors moet \emph{naar} de HDMI connector wijzen
(Figuur~\ref{fig:camera}).
\begin{figure}[h!]
\centering
\subfloat{
\includegraphics[width=.48\textwidth]{images/camera-2.jpg}
}
\subfloat{
\includegraphics[width=.48\textwidth]{images/camera-1.jpg}
}
\caption{De kant met het blauwe kleeflint moet \emph{weg} wijzen van
HDMI connector. De kant met de metalen connectors moet \emph{naar} de
HDMI connector wijzen.}
\label{fig:camera}
\end{figure}
Nadat je de camera hebt aangesloten moet je de Raspberry Pi
herstarten. Dit kan je doen door in een console het volgende commando
te geven:
\begin{lstlisting}
pi@raspberrypi ~ $ sudo reboot
\end{lstlisting}
Dit roept het \texttt{reboot} commando aan als \texttt{root}
gebruiker.
Wanneer de Raspberry Pi opnieuw is opgestart moet je opnieuw
inloggen en de grafische omgeving starten met \texttt{startx}.
\subsubsection{Foto's nemen met de camera module}
Met het commandline programma \texttt{raspistill} kan je foto's
nemen.
\begin{lstlisting}
pi@raspberrypi ~ $ raspistill -o image.jpg
\end{lstlisting}
Na het geven van dit commando komt er op het scherm een preview van
de foto. Na 5 seconden preview wordt er een foto genomen en bewaard
in de \texttt{image.jpg} file in je huidige directory.
Je kan de foto nu bekijken met het volgende commando:
\begin{lstlisting}
pi@raspberrypi ~ $ gpicview image.jpg
\end{lstlisting}
of door in een file browser naar het foto bestand te navigeren en
het zo te openen.
Het \texttt{raspistill} commando heeft erg veel opties. Een
overzicht van alle opties krijg je door het volgende commando uit te
voeren.
\begin{lstlisting}
pi@raspberrypi ~ $ raspistill --help
\end{lstlisting}
\subsubsection{Video's maken met de camera module}
Met het commandline programma \texttt{raspivid} kan je video's
opnemen. Het volgende commando maakt een video van 10 seconden en
schrijft deze video weg in het \texttt{video.h264} bestand.
\begin{lstlisting}
pi@raspberrypi ~ $ raspivid -o video.h264 -t 10000
\end{lstlisting}
Je kan de video bekijken met de \texttt{omxplayer} player:
\begin{lstlisting}
pi@raspberrypi ~ $ omxplayer video.h264
\end{lstlisting}
Net als \texttt{raspistill} heeft \texttt{raspivid} veel
configuratie opties. Je krijg een overzicht van al deze opties met
het commando:
\begin{lstlisting}
pi@raspberrypi ~ $ raspivid --help
\end{lstlisting}
\subsection{De afstandssensor}
In dit deel wordt beschreven hoe je waarden van de afstandssensor
uitleest met behulp van Python of Java. Om te werken met de
afstandssensor moet je gebruik maken van de GPIO pinnen.
\subsubsection{Werking van de GPIO pinnen}
De Raspberry Pi heeft in totaal 26 GPIO pinnen. Hiervan kunnen
er 17 gebruikt worden voor algemene in- en uitvoer.
Figuur~\ref{fig:gpio} toont de verschillende pinnen.
\begin{figure}[h!]
\centering
\includegraphics[width=.8\textwidth]{images/gpio.png}
\caption{De verschillende GPIO pinnen. De pinnen worden op
twee verschillende manieren benoemd: op basis van hun fysieke locatie
(nummer in de cirkels) of op basis van hun connectie met de CPU (de
GPIOXX labels in het groen).}
\label{fig:gpio}
\end{figure}
Pin 1, links onderaan Figuur~\ref{fig:gpio}, heeft onderaan een
rechthoekig symbool. Pin 1 op de Raspberry Pi heeft hetzelfde symbool.
De pinnen kunnen op twee manieren benoemd worden. Op basis van
de fysieke locatie van de pin en op basis van de connectie met de CPU.
De fysieke locatie wordt als volgt bepaald: De pin met symbool,
onderaan links is pin 1. De pin daarboven is pin 2. De pin rechts van
pin 1 is pin 3. De pin boven pin 3 is pin 4. En zo verder. De
fysieke pin nummers worden getoond in Figuur~\ref{fig:gpio:physical}.
\begin{figure}[h!]
\centering
\includegraphics[width=.8\textwidth]{images/gpio-physical.png}
\caption{De fysieke pin nummering}
\label{fig:gpio:physical}
\end{figure}
De alternatieve benaming voor de pinnen is gebaseerd op hun
connectie met de CPU. Zo is de pin op plaats 3 verbonden met ingang 2
van de CPU, en de pin op plaats 5 is verbonden met ingang 3 van de
CPU. Deze pinnen krijgen dus respectievelijk de labels \texttt{GPIO2}
en \texttt{GPIO3}.
Figuur~\ref{fig:gpio} toont de mapping tussen beide benamingen.
Enkel de pinnen met een groen label zijn verbonden met de CPU en
hebben een \texttt{GPIO} label. Enkel deze pinnen kunnen gebruikt
worden voor in en uitvoer.
De GPIO pinnen kunnen werken in twee verschillende modi. Ze
werken als invoer of als uitvoer. Van een pin in invoer modus kan je
in je software lezen of er spanning ten opzichte van de aarding op
staat. Op een pin in uitvoer modus kan je een spanning ten opzichte
van de aarding plaatsen.
De GPIO pinnen, de groene pinnen in Figuur~\ref{fig:gpio}, mogen
niet gebruikt worden om stroom te leveren. Ze dienen enkel maar om
een signaal te geven. Omwille van hun rechtstreekse verbinding met de
CPU kan een te grote stroom doorheen deze pinnen leiden tot schade aan
de CPU.
De overige pinnen, de \texttt{3V3}, \texttt{5V} en
\texttt{Ground} pinnen mogen wel gebruikt worden om stroom aan
sensoren te leveren. Ze kunnen echter enkel de door de Raspberry Pi
ongebruikte stroom leveren. Als de Micro USB adapter 500 mA levert en
de Raspberry Pi 300 mA verbruikt kunnen de pinnen samen maar 200 mA
leveren. Voor sensoren zoals de afstandssensor is dit voldoende, voor
het aansturen van een motor is dit wellicht niet voldoende.
Er bestaan voor verschillende programmeertalen libraries die het
gebruik van de GPIO pinnen mogelijk maken. Voor Python is er
WiringPi\footnote{\url{https://projects.drogon.net/raspberry-pi/wiringpi/}}
en RPi.GPIO\footnote{\url{https://pypi.python.org/pypi/RPi.GPIO}}.
Voor Java is er Pi4J\footnote{\url{http://pi4j.com/}}. Naast
bibliotheken om de GPIO pinnen te gebruiken kan je ook het
\texttt{\\sysfs} bestandssysteem gebruiken.
\subsubsection{Werking van de afstandssensor}
De HC SR04 afstandssensor heeft 4 pinnen, gelabeled \emph{Vcc},
\emph{Trig}, \emph{Echo} en \emph{Gnd}. De buitenste twee, \emph{Vcc}
en \emph{Gnd}, dienen om de sensor van stroom te voorzien. De
binnenste twee, \emph{Trig} en \emph{Echo}, dienen respectievelijk om
de sensor te activeren en uit te lezen.
Wanneer via een GPIO pin in uitvoer modus een signaal gegeven
wordt aan de \emph{Trig} pin zal de sensor een korte ultrasone puls
verzenden. Nadien zal de sensor een spanning plaatsen op de
\emph{Echo} pin. Deze spanning kan gedetecteerd worden met een GPIO
pin in invoer modus. De duur van de spanning komt overeen met de tijd
die het signaal nodig had om het dichtstbijzijnde object te bereiken,
te weerkaatsen en terug te keren naar
sensor\footnote{url: \url{http://elecfreaks.com/store/download/HC-SR04.pdf}}.
Door de lengte van het signaal te meten ($\Delta t$) kan je dus de afstand tot het
object berekenen ($d$) met de formule:
\begin{equation}
d = \frac{\Delta t * v_{snd}}{2}
\end{equation}
De snelheid van het geluid, $v_{snd}$ in de formule, is 340 m/s.
Om de afstand in centimeters te bereken op basis van een tijdsduur in
microseconden ($\mu s$) volstaat het dus $\Delta t$ te
vermenigvuldigen met $0.017$.
\subsubsection{Connecteren van de afstandssensor}
Connecteer de \emph{Vcc} pin van de sensor met pin 2
(\texttt{5V}) van de
Raspberry Pi. Connecteer de \emph{Gnd} pin van de sensor met pin 25
(\texttt{Ground}) van de Raspberry Pi. Connecteer de \emph{Trig} pin van de
sensor met pin 7 (\texttt{GPIO4}) van de Raspberry Pi. En connecteer tot
slot de \emph{Echo} pin van de sensor met pin 11 (\texttt{GPIO17}).
De uiteindelijke connecties moeten overeenkomen met
Figuur~\ref{fig:sensor}.
\begin{figure}[h!]
\centering
\subfloat{
\includegraphics[width=.48\textwidth]{images/sensor.pdf}
}
\subfloat{
\includegraphics[width=.48\textwidth]{images/sensor.jpg}
}
\caption{De verbinding tussen de afstandssensor en de Raspberry Pi}
\label{fig:sensor}
\end{figure}
\subsubsection{Sensor waarden uitlezen met Python}
Listing~\ref{lst:distance.py} toont de code nodig om de
afstandssensor uit te lezen op de Pi. Je kan deze code ook op het
internet bekijken op \url{https://gist.github.com/rutgerclaes/6701244}.
Om de code snel op de Pi te krijgen kan je volgend command gebruiken:
\begin{lstlisting}
pi@raspberrypi ~ $ wget -O distance.py http://goo.gl/X9ot8j
\end{lstlisting}
\lstinputlisting[numbers=left,numberstyle=\tiny,backgroundcolor=,captionpos=b,language=Python,basicstyle={\footnotesize\ttfamily},label={lst:distance.py},caption={distance.py},showstringspaces=false]{code/distance.py}
Je kan het \texttt{distance.py} programma aanpassen met een editor
zoals \texttt{leafpad}. Nadien moet je het script uitvoerbaar maken
en uitvoeren als \texttt{root}.
\begin{lstlisting}
pi@raspberrypi ~ $ chmod +x distance.py
pi@raspberrypi ~ $ sudo ./distance.py
\end{lstlisting}
Van zodra het programma gestart is zie je de afstandswaarden voorbij
komen. Je kan het programma afsluiten met \texttt{Ctrl+c}. Als je
het programma voor de tweede keer opstart zal je een foutboodschap te
zien krijgen: \texttt{RuntimeWarning: This channel is already in use
...}. De GPIO pinnen zijn reeds ge\"initializeerd en de RPi.GPIO
geeft daarom een waarschuwing. Je mag deze foutboodschap negeren.
\subsubsection{Sensor waarden uitlezen met Java}
De Java JDK is standaard niet ge\"installeerd op Raspbian.
Indien je de eerste gebruiker bent van deze Raspbian installatie zal je
die zelf moeten installeren. Dit kan je doen met het volgende commando:
\begin{lstlisting}
pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install openjdk-7-jdk
pi@raspberrypi ~ $ java -version
\end{lstlisting}
Het installeren van de nodige software zal een tijdje duren.
Nadien zou het \texttt{java} commando moeten werken.
In toekomstige versies van Raspbian zal de \emph{Oracle Java JDK}
standaard vooraf ge\"installeerd
zijn\footnote{\url{http://www.raspberrypi.org/archives/4920}}.
Ook vanuit Java kan je de GPIO pinnen gebruiken. De library die
dit mogelijk maakt is Pi4J. Om de library te gebruiken moet je code
downloaden met het volgende commando:
\begin{lstlisting}
pi@raspberrypi ~ $ wget pi4j.googlecode.com/files/pi4j-0.0.5.zip
pi@raspberrypi ~ $ unzip pi4j-0.0.5.zip
\end{lstlisting}
Listing~\ref{lst:distance.java} toont voorbeeld code die gebruik
maakt van Pi4J om de afstandssensor uit te lezen\footnote{Je kan deze
code ook vinden op \url{http://goo.gl/dpOsxb}}. PI4J gebruikt
\emph{nog} een andere manier om de pinnen te adresseren.
Figuur~\ref{fig:pi4j} toont de mapping in Pi4J. Zowel de naam als de
nummer in Figuur~\ref{fig:pi4j} zijn Pi4J specifiek. Je kan de
fysieke pin nummer gebruiken om de mapping te maken zoals in
Tabel~\ref{table:mapping}.
\begin{figure}[h!]
\centering
\includegraphics[width=.5\textwidth]{images/gpio-pi4j.png}
\caption{De pin nummering voor PI4J. Onder \texttt{Pin \#} staat de
nummer die PI4J gebruikt. Onder \texttt{Name} de naam die PI4J
gebruikt. Naast de pinnen staat ten slotte nog de fysieke pin
nummer.}
\label{fig:pi4j}
\end{figure}
\begin{table}[h!]
\centering
\begin{tabular}{l|ll}
\hline
\textbf{Fysieke pin nr.} & \textbf{RPi.GPIO label} & \textbf{Pi4J label} \\
\hline
7 & \texttt{GPIO04} & \texttt{RaspiPin.GPIO07} \\
11 & \texttt{GPIO17} & \texttt{RaspiPin.GPIO00} \\
\hline
\end{tabular}
\caption{Mapping tussen fysieke pin, RPi.GPIO en Pi4J labels.}
\label{table:mapping}
\end{table}
\lstinputlisting[numbers=left,numberstyle=\tiny,backgroundcolor=,captionpos=b,language=Java,basicstyle={\footnotesize\ttfamily},label={lst:distance.java},caption={DistanceMonitor.java},showstringspaces=false]{code/DistanceMonitor.java}
Je kan bovenstaande code compileren en uitvoeren met de volgende
twee commandos. Het eerste zorgt voor de compilatie. Het tweede
voert het programma uit en schrijft de waarden naar een
\texttt{distance.csv} file.
\lstset{basicstyle=\footnotesize\ttfamily}
\begin{lstlisting}
pi@raspberrypi ~ $ javac -cp pi4j-0.0.5/lib/pi4j-core.jar DistanceMonitor.java
pi@raspberrypi ~ $ sudo java -cp pi4j-0.0.5/lib/pi4j-core.jar:. DistanceMonitor > distance.csv
\end{lstlisting}
\paragraph{Visualiseren van de sensor waarden.} Om de data te
visualiseren kan je R gebruiken. Je kan R installeren op de Raspberry
Pi met het volgende commando. R is een erg uitgebreid pakket, de
installatie kan even duren.
\begin{lstlisting}
pi@raspberrypi ~ $ sudo apt-get install r-base
\end{lstlisting}
Eens R is ge\"installeerd kan je een sessie starten door in je
console het volgende te typen:
\begin{lstlisting}
pi@raspberrypi ~ $ R
\end{lstlisting}
In de R console geef je de volgende commando's. De eerste twee
installeren nuttige R bibliotheken. Het downloaden en installeren kan
lang duren. Bij het installeren van de bibliotheken kan het zijn dat
R vraagt waar hij ze moet downloaden (CRAN Mirror). Hier kan je
kiezen voor \emph{Belgium}.
\begin{lstlisting}
> install.packages( 'ggplot2' )
> install.packages( 'zoo' )
> distance.data = read.csv( 'distance.csv' )
> colnames( distance.data ) = c( 'time', 'distance' )
> distance.data$time = ( distance.data$time - min( distance.data$time ) ) / 1000
> distance.data$smooth = rollmedian( distance.data$distance, 9, na.pad = T )
> plot = ggplot( data = distance.data, aes( x = time ) )
> plot = plot + geom_point( aes( y = distance ) )
> plot = plot + geom_line( aes( y = smooth ) )
> print( plot )
\end{lstlisting}
\section{Alles netjes achterlaten}\label{sec:opruimen}
Het eduroam paswoord dat je gebruikte om de WiFi te connecteren wordt
in plaintext opgeslagen. Als je niet wil dat de studenten van de
volgende groep het kunnen lezen verwijder je best de configuratie
files.
\begin{lstlisting}
pi@raspberrypi ~ $ sudo rm /etc/wpa_supplicant/wpa_supplicant.conf
\end{lstlisting}
Daarnaast kan je ook de code die je hebt geschreven of gedownload
verwijderen. Zeker als je in de eerste groep studenten zit.
Ten slotte zet je de Raspberry Pi uit door de Micro USB connectie te
verbreken. Verwijder ook alle randapparatuur van de Raspberry Pi.
Zet de invoer van de schermen terug op displayport en kijk na of ze
een Ubuntu login scherm tonen.
\end{document}
% vim: tw=70 nocindent expandtab