What we cover today?
Hardware you will use in this week lab - don’t memorize this part,iit is more of a referenceMore on ADC and how to process ADC output signalLoops in C - you will need this for the lab
Arthur Roberts ENGR101: Lecture 10 March 2017 2 / 29
Hardware: Raspberry Pi
Full Linux computer$5 - $50Programmable in C, PythonetcNo hard drive, SD card insteadNo on-board ADC
Display
SD card
MouseKeyboard
Arthur Roberts ENGR101: Lecture 10 March 2017 3 / 29
Hardware: Our addition to Raspberry Pi
ADC ADCinputs(A0-A8)
GROUND
+5V
POWER INON_OFF
digitalinputs/outputs
MOTORCONTROL
Additions:10 bits ADC, 8 channels (likewe have 8 ADCs).Power supply output +5V forsensors.Digital input/output, 8channels.Motor control circuitry - candrive 2 motors.
Arthur Roberts ENGR101: Lecture 10 March 2017 4 / 29
Hardware: RPI start screen
Figure: On RPi start
Connect display, keyboard,mouseSwitch power onIt takes about 30 sec for RPI tostartIf nothing is happening (blackscreen) it is likely that SD cardis not working. Ask forinstructions.
Arthur Roberts ENGR101: Lecture 10 March 2017 5 / 29
Hardware: Common programs
Figure: Applications
Utility programs (underAccessories):
File managerText editorcommand terminal
Arthur Roberts ENGR101: Lecture 10 March 2017 6 / 29
Hardware: Programming RPI
We will use "Geany" again (for the time being, anyway).
It works almost same way as on PC. There is one subtle differencethough.
Arthur Roberts ENGR101: Lecture 10 March 2017 7 / 29
Hardware: Geany tuning for our hardware
add -le101
add -le101
To work with our custom hardwarewe need to use C library (moreon what library is in next lecture)To connect the library please clickBuild− > SetBuildCommands inGeany menuAdd -le101 to Compile and Buildat the end of command strings(Use library e101 when compiling)Add sudo at the beginning ofExecute command string (Thisprogram will run withadministrator/super usercredentials)
Arthur Roberts ENGR101: Lecture 10 March 2017 8 / 29
Hardware: How to copy files?RPi is connected to Victoria WiFi (no or very limited Internet though). Itcan be used to transfer files to your usual folders.
SERVER: greta-pt.ecs.vuw.ac.nz
Your ECS user name and password
Port: 22( SFTP)
Files on RPi Files in your homefolder
Drag over to copy
Under Intenet startFileZilaEnter server name,your ECS user nameand passwordPort 22Click Quick connectOK in pop-up dialogYou can copy files
SFTP? Wait until network lecture...Arthur Roberts ENGR101: Lecture 10 March 2017 9 / 29
Hardware: Where our analog signal is coming from?
We use infra-red distancesensorNarrow Field Of ViewOutput voltage changes ifdistance to obstacle changes.Manufacturer and modelnumber - printed on outer caseHave a look at sensorspecifications: Always helps toknow the answer in advanceSignal is changing very slowSignal is noisy
SENSOR GROUND
+5 V
SIGNAL
Arthur Roberts ENGR101: Lecture 10 March 2017 10 / 29
Reminder on ADC
There is an input signal.It can take any value ony-axis(amplitude) and is continousin time.
Arthur Roberts ENGR101: Lecture 10 March 2017 11 / 29
Reminder on ADC
We take measurements of thesignal at certain moments of time.We ignore what is happening withthe signal between measurements.OK as long as our measurementsare done fast enough.Measured values still can take anyvalue.Signal becomes discrete in time.
Arthur Roberts ENGR101: Lecture 10 March 2017 12 / 29
Reminder on ADC
We round the signal to nearestvalue which can represented asbinary number. Signal is discretein time and amplitude.
Figure:
Arthur Roberts ENGR101: Lecture 10 March 2017 13 / 29
More on ADCs: Noisy?
We measure voltage, i.e. howmany electrons are in signalwireMore electrons on wire -higher measured voltage.Electrons can not staystill(unless it is really cold).Changes in voltage are calledthermal noise - if you put wirein liquid helium then there willbe no more noise.Electrons do not conspireagainst you and have nomemory - thermal noise israndom.
Arthur Roberts ENGR101: Lecture 10 March 2017 14 / 29
More on ADCs: Noise?
Signal is there but we can notsee pure signal - only withnoise addedSignal is changing slowNoise is truly random:chances of one noise samplebeing positive(or negative) are50%If that is the case we can addour measurements togetherand hope that positive oneswill cancel eachother
Arthur Roberts ENGR101: Lecture 10 March 2017 15 / 29
More on ADCs: Averaging!
True value is 10. Randomnoise is added.Add all measurement resulttogether:11 + 9.2 + 9.2 + 10.5 + 10.5 =50.4... divide by number ofmeasurements to get average:50.4/5 = 10.0810.08 is closer to true value(10) than any specificmeasurement:11,9,2,9.2,10.5,10.5
Arthur Roberts ENGR101: Lecture 10 March 2017 16 / 29
More on ADCs: Filter
What we just did is called digital filtering.Algorithm for our filter is simple:
1 Take N measurements of the signal.2 Window is N samples wide.3 Calculate average value4 Average becomes new (hopefully improved) measurement
Of course, it is slower. It takes N times more as compared with singlemeasurement.
Arthur Roberts ENGR101: Lecture 10 March 2017 17 / 29
Losing information in filter
Signal changes fast - stepIn window2 two samples are ofold signal value and one is ofnew valueFilter output for window2 doesnot reproduce input signal
input signal
9
11
10
WINDOW 1
output signal
WINDOW 2WINDOW 3
time
Our filter is not really smart. It removes noise but it distorts the signalas well.
Arthur Roberts ENGR101: Lecture 10 March 2017 18 / 29
C programming: To read from ADC
Listing 1: Ex1.c#include <stdio.h>#include <time.h>#include "E101.h" \\this is our custom ENGR101 C library.
int main(){//This sets up the RPi hardware and ensures//everything is working correctlyinit();//We declare an integer variable to store the ADC dataint adc_reading;//Reads from Analog Pin 0 (A0)adc_reading = read_analog(0);//Prints read analog valueprintf("%d\n",adc_reading);//Waits for 0.5 seconds (500000 microseconds)sleep1(0,500000);
return 0;}
Arthur Roberts ENGR101: Lecture 10 March 2017 19 / 29
Remainder on C programming...
1 main() - starting point of the program. Program starts executionthere.
2 #include - it includes. Includes libraries.3 stdio.h - standard input output. Standard means display and
keyboard (Mouse is a pest). h stands for header - list of this libraryfunctions available. Let’s have a look...
4 "E101.h" - library for our hardware
Arthur Roberts ENGR101: Lecture 10 March 2017 20 / 29
You will need to run some code several times whencreating filter. Repetitions in CSometimes we want to repeat execution of some part of the codeseveral times. If we know in advance how many times we want torepeat we can use for operator.
int i;for (i=0; i<5;i=i+1){ printf("%d\n",i);}
1 2TRUE
3
4
FALSE
There are 3 parts in operatorfor(Part1 ; Part2 ; Part3)Part1 is executed whenoperator starts - alwaysPart2 is condition. If conditionis:
I TRUE - do whatever isbetween curly brackets andgo to Part3
I FALSE - abort for, go toclosing curly bracket andcontinue
Arthur Roberts ENGR101: Lecture 10 March 2017 21 / 29
Repetitions
int i;for (i=0; i<5;i=i+1){ printf("%d\n",i);}
1 2TRUE
3
4
FALSEExecute Part3Go to Part2, againRinse and repeat
It goes around and around, so it is called looping
Arthur Roberts ENGR101: Lecture 10 March 2017 22 / 29
Simple repetition
Listing 2: Ex1.c#include <stdio.h>int main(){int i;for (i=0; i<5;i=i+1){printf("%d\n",i);
}}
And result is...01234
Arthur Roberts ENGR101: Lecture 10 March 2017 23 / 29
Quiz time (0.5 minutes)
What is result of that?
Listing 3: Ex1.c#include <stdio.h>int main(){int i;for (i=0; i<5;i=i+2){printf("%d\n",i);
}}
1 0 1 2 3 42 0 2 43 0 1 3
Arthur Roberts ENGR101: Lecture 10 March 2017 24 / 29
Quiz time again(0.5 minutes)
What is result of that?
Listing 4: Ex1.c#include <stdio.h>
int main(){
int i;for (i=10; i<5;i=i+1){
printf("%d\n",i);}
}
1 10 9 8 7 6 52
3 5 6 7 8 9
Arthur Roberts ENGR101: Lecture 10 March 2017 25 / 29
Another way to loop
Using for() is good if you know in advance how many times you wantto loop. There is another way, can be simpler.
Program checks condition inthe brackets of while().If condition estimates to TRUEthen code between openingcurly bracket andcorresponding closing one isexecuted.And this is it.
int i = 0;while ( i<10){ // ...}
TRUE
FALSE
Too simple, isn’t it?
Arthur Roberts ENGR101: Lecture 10 March 2017 26 / 29
Yet another quiz!!! What is an output?
0 1 2 3 4 5 6 7 8 90 0 0 0 0 0 0 0 0 0 0 0...10 10 10 101 10 10 10
Listing 5: while.c#include <stdio.h>
int main(){
int i = 0;while (i<10){
printf("%d\n",i);}
}
Arthur Roberts ENGR101: Lecture 10 March 2017 27 / 29
Corrected
Listing 6: correct while#include <stdio.h>
int main(){int i = 0;while (i<10){printf("%d\n",i);i = i +1;
}}
It is surprisingly common error.
Arthur Roberts ENGR101: Lecture 10 March 2017 28 / 29