V001. 2017_11_30 10:21 Working code from Tuesday's lab session.

Dependencies:   mbed-rtos mbed

Fork of 2017_11_28_ELEC347_Coursework by Chris Hills

DSP Coursework ELEC347 2017-2018 Group members: Matthew Thewsey, Thomas Morris, Samuel Waggett, Christopher Hills .

Committer:
mwthewsey
Date:
Tue Dec 05 16:13:24 2017 +0000
Revision:
16:c91b29c9e9f6
Parent:
13:db76473a3d76
Variable (TESTEDandWORKING)

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