Non-variable (TESTEDandWORKING)

Dependencies:   mbed-rtos mbed

Fork of ELEC347_Coursework by CMST

Committer:
thomasmorris
Date:
Mon Dec 04 11:24:15 2017 +0000
Revision:
13:db76473a3d76
Parent:
12:d522a7a061d6
Child:
14:379796f5868e
ELEC347 Coursework Final Version Variable Parameters

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
mwthewsey 0:d39a06ca6bf1 5 unsigned short ADC_DATA;
mwthewsey 0:d39a06ca6bf1 6
mwthewsey 0:d39a06ca6bf1 7 //Init values for difference equation
mwthewsey 0:d39a06ca6bf1 8 //MBED Class Instances follows
mwthewsey 0:d39a06ca6bf1 9 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 10
chills 7:6cb27cce4c50 11 //Analog Inputs
mwthewsey 0:d39a06ca6bf1 12 AnalogIn Ain(PA_1); //Analog Input (Signal Input 0 to +3 Volts)
mwthewsey 0:d39a06ca6bf1 13 AnalogOut Aout(PA_4); //Analog Output (Signal Input 0 to +3 Volts)
mwthewsey 0:d39a06ca6bf1 14
chills 8:192b376a6da7 15 //Declare Threads
chills 8:192b376a6da7 16 Thread t1;
chills 9:91c53a683856 17 Thread t2;
chills 8:192b376a6da7 18
thomasmorris 13:db76473a3d76 19 AnalogIn QDEF(PA_7); // Q Factor
thomasmorris 13:db76473a3d76 20 AnalogIn BDEF(PA_6); // Boost Level
thomasmorris 13:db76473a3d76 21 AnalogIn FDEF(PA_5); //Centre Frequency
thomasmorris 13:db76473a3d76 22
chills 7:6cb27cce4c50 23 //Declare Ticker Rates
chills 7:6cb27cce4c50 24 float sample_rate = (1.0/35000); //Rate of sampling
chills 7:6cb27cce4c50 25 float coeff_rate = (1.0 / 20); //Rate of updating coefficients
mwthewsey 0:d39a06ca6bf1 26
chills 7:6cb27cce4c50 27 //Initial Input Value
mwthewsey 2:f6e49378dd8a 28 float input = 0.0;
chills 7:6cb27cce4c50 29
chills 7:6cb27cce4c50 30 float Fo_Current;
chills 7:6cb27cce4c50 31 float Boost_Current;
chills 7:6cb27cce4c50 32 float Q_Current;
chills 7:6cb27cce4c50 33
thomasmorris 13:db76473a3d76 34 //Declare Filter
thomasmorris 13:db76473a3d76 35 FILTER BP_filter(48000,10000,-16,2); //Create object of type Filter(Fs,Fo,Boost,Q)
thomasmorris 13:db76473a3d76 36
thomasmorris 13:db76473a3d76 37
chills 7:6cb27cce4c50 38 //Forward Declarations
mwthewsey 0:d39a06ca6bf1 39 void sampler(void);
thomasmorris 13:db76473a3d76 40 void update(void);
mwthewsey 0:d39a06ca6bf1 41
chills 8:192b376a6da7 42
mwthewsey 0:d39a06ca6bf1 43 int main()
mwthewsey 0:d39a06ca6bf1 44 {
thomasmorris 11:7efb6e4759cc 45 t2.start(sampler);//Start the Sampler timed interrupt
thomasmorris 13:db76473a3d76 46 t1.start(update);//Update the coeffiecients of the filter
mwthewsey 0:d39a06ca6bf1 47 }
mwthewsey 0:d39a06ca6bf1 48
thomasmorris 13:db76473a3d76 49 void update(void)
chills 7:6cb27cce4c50 50 {
chills 8:192b376a6da7 51 while(1){
thomasmorris 13:db76473a3d76 52 Fo_Current = FDEF * 22000 + 10;
thomasmorris 13:db76473a3d76 53 Boost_Current = BDEF * 100 - 50;
thomasmorris 13:db76473a3d76 54 Q_Current = QDEF * 49 + 1;
thomasmorris 13:db76473a3d76 55
thomasmorris 13:db76473a3d76 56 BP_filter.coeff_update(Fo_Current,Boost_Current,Q_Current);//Coefficient update
thomasmorris 13:db76473a3d76 57 printf("Q = %d \t Boost = %f \t Fo = %d\n\r", BP_filter.Get_Q(), BP_filter.Get_Boost(), BP_filter.Get_Fo());
thomasmorris 13:db76473a3d76 58 Thread::wait(1000);//Sleep the thread for 1 second then re runs the coefficient calculation
chills 7:6cb27cce4c50 59 }
chills 7:6cb27cce4c50 60 }
mwthewsey 0:d39a06ca6bf1 61
mwthewsey 0:d39a06ca6bf1 62 void sampler(void)
mwthewsey 0:d39a06ca6bf1 63 {
chills 9:91c53a683856 64 while(1)
chills 9:91c53a683856 65 {
thomasmorris 13:db76473a3d76 66 SampLED = 1;//LED Indicates start of sampling
thomasmorris 13:db76473a3d76 67 input = Ain;//Takes ADC input as a Varaible type float
thomasmorris 13:db76473a3d76 68 BP_filter.setvalue(input);//Input ADC. N.B. ADC in MBED is 0.0 to 1.0 float!!!!!!
thomasmorris 13:db76473a3d76 69 Aout = BP_filter.getvalue();//Sets the input value to the Class
thomasmorris 13:db76473a3d76 70 SampLED = 0;//LED Indicates end of sampling
thomasmorris 12:d522a7a061d6 71 Thread::wait(sample_rate*1000);//Look at this
chills 9:91c53a683856 72 }
thomasmorris 11:7efb6e4759cc 73 }