Non-variable (TESTEDandWORKING)

Dependencies:   mbed-rtos mbed

Fork of ELEC347_Coursework by CMST

Committer:
chills
Date:
Tue Nov 28 15:42:35 2017 +0000
Revision:
9:91c53a683856
Parent:
8:192b376a6da7
Child:
10:3495fff88da7
Works Live Proven With Sound

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mwthewsey 0:d39a06ca6bf1 1 #include "mbed.h"
mwthewsey 0:d39a06ca6bf1 2 #include "rtos.h"
mwthewsey 0:d39a06ca6bf1 3 #include "Filter.hpp"
chills 6:a2737b51424c 4
chills 6:a2737b51424c 5
mwthewsey 0:d39a06ca6bf1 6 unsigned short ADC_DATA;
mwthewsey 0:d39a06ca6bf1 7
mwthewsey 0:d39a06ca6bf1 8 //Init values for difference equation
mwthewsey 0:d39a06ca6bf1 9 //MBED Class Instances follows
mwthewsey 0:d39a06ca6bf1 10 DigitalOut SampLED(LED1); //Digital Output (GREEN LED is PB_3, D13 You can use an Oscilloscope on This pin to confirm sample rate)
mwthewsey 0:d39a06ca6bf1 11
chills 7:6cb27cce4c50 12 //Analog Inputs
chills 8:192b376a6da7 13 AnalogIn QDEF(PA_7);
chills 8:192b376a6da7 14 AnalogIn BDEF(PA_6);
chills 8:192b376a6da7 15 AnalogIn FDEF(PA_5);
mwthewsey 0:d39a06ca6bf1 16 AnalogIn Ain(PA_1); //Analog Input (Signal Input 0 to +3 Volts)
mwthewsey 0:d39a06ca6bf1 17 AnalogOut Aout(PA_4); //Analog Output (Signal Input 0 to +3 Volts)
mwthewsey 0:d39a06ca6bf1 18
chills 7:6cb27cce4c50 19 //Declare Tickers
mwthewsey 0:d39a06ca6bf1 20 Ticker sample_timer;
chills 7:6cb27cce4c50 21 Ticker coeff_change;
chills 6:a2737b51424c 22
chills 8:192b376a6da7 23 //Declare Threads
chills 8:192b376a6da7 24 Thread t1;
chills 9:91c53a683856 25 Thread t2;
chills 8:192b376a6da7 26
chills 7:6cb27cce4c50 27 //Declare Ticker Rates
chills 7:6cb27cce4c50 28 float sample_rate = (1.0/35000); //Rate of sampling
chills 7:6cb27cce4c50 29 float coeff_rate = (1.0 / 20); //Rate of updating coefficients
mwthewsey 0:d39a06ca6bf1 30
chills 7:6cb27cce4c50 31 //Initial Input Value
mwthewsey 2:f6e49378dd8a 32 float input = 0.0;
chills 7:6cb27cce4c50 33
chills 7:6cb27cce4c50 34 //Declare Filter
chills 7:6cb27cce4c50 35 FILTER BP_filter(48000,10000,-16,2); //Create object of type Filter(Fs,Fo,Boost,Q)
mwthewsey 2:f6e49378dd8a 36
chills 7:6cb27cce4c50 37 //Declare Fixed Variables
chills 7:6cb27cce4c50 38 float Fo = 1;
chills 7:6cb27cce4c50 39 float Boost = 0;
chills 7:6cb27cce4c50 40 float Q = 1;
mwthewsey 0:d39a06ca6bf1 41
chills 7:6cb27cce4c50 42 //Declare Temporary Coefficient Values
chills 7:6cb27cce4c50 43 float Fo_Current;
chills 7:6cb27cce4c50 44 float Boost_Current;
chills 7:6cb27cce4c50 45 float Q_Current;
chills 7:6cb27cce4c50 46
chills 7:6cb27cce4c50 47 //Forward Declarations
mwthewsey 0:d39a06ca6bf1 48 void sampler(void);
chills 7:6cb27cce4c50 49 void coeff_update(void);
mwthewsey 0:d39a06ca6bf1 50
chills 8:192b376a6da7 51
mwthewsey 0:d39a06ca6bf1 52 int main()
mwthewsey 0:d39a06ca6bf1 53 {
chills 9:91c53a683856 54 t2.start(sampler);
chills 8:192b376a6da7 55 t1.start(coeff_update);
chills 7:6cb27cce4c50 56 printf("Hello");
mwthewsey 0:d39a06ca6bf1 57 }
mwthewsey 0:d39a06ca6bf1 58
chills 8:192b376a6da7 59
chills 7:6cb27cce4c50 60 void coeff_update(void)
chills 7:6cb27cce4c50 61 {
chills 8:192b376a6da7 62 while(1){
chills 8:192b376a6da7 63 Fo_Current = FDEF * 39999 + 1;
chills 8:192b376a6da7 64 if (abs(Fo_Current - BP_filter.Get_Fo()) > 50) //If Centre Frequency has changed significantly
chills 8:192b376a6da7 65 {
chills 8:192b376a6da7 66 BP_filter.Update_Fo(Fo_Current); //Update Centre Frequency
chills 8:192b376a6da7 67 }
chills 8:192b376a6da7 68 Boost_Current = BDEF * 100 - 50;
chills 8:192b376a6da7 69 if (abs(Boost_Current - BP_filter.Get_Boost()) > 2) //If Boost has changed significantly
chills 8:192b376a6da7 70 {
chills 8:192b376a6da7 71 BP_filter.Update_Boost(Boost_Current); //Update Boost
chills 8:192b376a6da7 72 }
chills 8:192b376a6da7 73 Q_Current = QDEF * 49 + 1;
chills 8:192b376a6da7 74 if (abs(Q_Current - BP_filter.Get_Q()) > 2) //If Q has changed significantly
chills 8:192b376a6da7 75 {
chills 8:192b376a6da7 76 BP_filter.Update_Q(Q_Current); //Update Q
chills 8:192b376a6da7 77 }
chills 8:192b376a6da7 78 BP_filter.Define_Filter(); //Calculate the coefficients
chills 9:91c53a683856 79 //BP_filter.Print_Filter(); //Print the ceofficients
chills 9:91c53a683856 80 printf("Q = %d \t Boost = %f \t Fo = %d\n\r", BP_filter.Get_Q(), BP_filter.Get_Boost(), BP_filter.Get_Fo());
chills 9:91c53a683856 81 Thread::wait(500);
chills 7:6cb27cce4c50 82 }
chills 7:6cb27cce4c50 83 }
mwthewsey 0:d39a06ca6bf1 84
mwthewsey 0:d39a06ca6bf1 85 void sampler(void)
mwthewsey 0:d39a06ca6bf1 86 {
chills 9:91c53a683856 87 while(1)
chills 9:91c53a683856 88 {
chills 9:91c53a683856 89 SampLED = 1; //LED Indicates start of sampling
mwthewsey 0:d39a06ca6bf1 90
chills 9:91c53a683856 91 input = Ain;
chills 9:91c53a683856 92 BP_filter.setvalue(input); //Input ADC. N.B. ADC in MBED is 0.0 to 1.0 float!!!!!!
chills 9:91c53a683856 93 Aout = BP_filter.getvalue();
chills 9:91c53a683856 94
chills 9:91c53a683856 95
chills 9:91c53a683856 96 SampLED = 0; //LED Indicates end of sampling
chills 9:91c53a683856 97 }
chills 7:6cb27cce4c50 98 }