Chris Hills
/
2017_11_28_ELEC347_Coursework
Test
Fork of ELEC347_CourseWork by
main.cpp@8:192b376a6da7, 2017-11-28 (annotated)
- Committer:
- chills
- Date:
- Tue Nov 28 15:19:08 2017 +0000
- Revision:
- 8:192b376a6da7
- Parent:
- 7:6cb27cce4c50
- Child:
- 9:91c53a683856
Print Coefficients is Working Live
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:192b376a6da7 | 25 | |
chills | 7:6cb27cce4c50 | 26 | //Declare Ticker Rates |
chills | 7:6cb27cce4c50 | 27 | float sample_rate = (1.0/35000); //Rate of sampling |
chills | 7:6cb27cce4c50 | 28 | float coeff_rate = (1.0 / 20); //Rate of updating coefficients |
mwthewsey | 0:d39a06ca6bf1 | 29 | |
chills | 7:6cb27cce4c50 | 30 | //Initial Input Value |
mwthewsey | 2:f6e49378dd8a | 31 | float input = 0.0; |
chills | 7:6cb27cce4c50 | 32 | |
chills | 7:6cb27cce4c50 | 33 | //Declare Filter |
chills | 7:6cb27cce4c50 | 34 | FILTER BP_filter(48000,10000,-16,2); //Create object of type Filter(Fs,Fo,Boost,Q) |
mwthewsey | 2:f6e49378dd8a | 35 | |
chills | 7:6cb27cce4c50 | 36 | //Declare Fixed Variables |
chills | 7:6cb27cce4c50 | 37 | float Fo = 1; |
chills | 7:6cb27cce4c50 | 38 | float Boost = 0; |
chills | 7:6cb27cce4c50 | 39 | float Q = 1; |
mwthewsey | 0:d39a06ca6bf1 | 40 | |
chills | 7:6cb27cce4c50 | 41 | //Declare Temporary Coefficient Values |
chills | 7:6cb27cce4c50 | 42 | float Fo_Current; |
chills | 7:6cb27cce4c50 | 43 | float Boost_Current; |
chills | 7:6cb27cce4c50 | 44 | float Q_Current; |
chills | 7:6cb27cce4c50 | 45 | |
chills | 7:6cb27cce4c50 | 46 | //Forward Declarations |
mwthewsey | 0:d39a06ca6bf1 | 47 | void sampler(void); |
chills | 7:6cb27cce4c50 | 48 | void coeff_update(void); |
mwthewsey | 0:d39a06ca6bf1 | 49 | |
chills | 8:192b376a6da7 | 50 | |
mwthewsey | 0:d39a06ca6bf1 | 51 | int main() |
mwthewsey | 0:d39a06ca6bf1 | 52 | { |
chills | 8:192b376a6da7 | 53 | sample_timer.attach(&sampler,sample_rate); |
chills | 8:192b376a6da7 | 54 | t1.start(coeff_update); |
chills | 7:6cb27cce4c50 | 55 | printf("Hello"); |
mwthewsey | 0:d39a06ca6bf1 | 56 | } |
mwthewsey | 0:d39a06ca6bf1 | 57 | |
chills | 8:192b376a6da7 | 58 | |
chills | 7:6cb27cce4c50 | 59 | void coeff_update(void) |
chills | 7:6cb27cce4c50 | 60 | { |
chills | 8:192b376a6da7 | 61 | while(1){ |
chills | 8:192b376a6da7 | 62 | Fo_Current = FDEF * 39999 + 1; |
chills | 8:192b376a6da7 | 63 | if (abs(Fo_Current - BP_filter.Get_Fo()) > 50) //If Centre Frequency has changed significantly |
chills | 8:192b376a6da7 | 64 | { |
chills | 8:192b376a6da7 | 65 | BP_filter.Update_Fo(Fo_Current); //Update Centre Frequency |
chills | 8:192b376a6da7 | 66 | } |
chills | 8:192b376a6da7 | 67 | Boost_Current = BDEF * 100 - 50; |
chills | 8:192b376a6da7 | 68 | if (abs(Boost_Current - BP_filter.Get_Boost()) > 2) //If Boost has changed significantly |
chills | 8:192b376a6da7 | 69 | { |
chills | 8:192b376a6da7 | 70 | BP_filter.Update_Boost(Boost_Current); //Update Boost |
chills | 8:192b376a6da7 | 71 | } |
chills | 8:192b376a6da7 | 72 | Q_Current = QDEF * 49 + 1; |
chills | 8:192b376a6da7 | 73 | if (abs(Q_Current - BP_filter.Get_Q()) > 2) //If Q has changed significantly |
chills | 8:192b376a6da7 | 74 | { |
chills | 8:192b376a6da7 | 75 | BP_filter.Update_Q(Q_Current); //Update Q |
chills | 8:192b376a6da7 | 76 | } |
chills | 8:192b376a6da7 | 77 | BP_filter.Define_Filter(); //Calculate the coefficients |
chills | 8:192b376a6da7 | 78 | BP_filter.Print_Filter(); //Print the ceofficients |
chills | 7:6cb27cce4c50 | 79 | } |
chills | 7:6cb27cce4c50 | 80 | } |
mwthewsey | 0:d39a06ca6bf1 | 81 | |
mwthewsey | 0:d39a06ca6bf1 | 82 | void sampler(void) |
mwthewsey | 0:d39a06ca6bf1 | 83 | { |
mwthewsey | 0:d39a06ca6bf1 | 84 | SampLED = 1; //LED Indicates start of sampling |
mwthewsey | 0:d39a06ca6bf1 | 85 | |
mwthewsey | 2:f6e49378dd8a | 86 | input = Ain; |
chills | 7:6cb27cce4c50 | 87 | BP_filter.setvalue(input); //Input ADC. N.B. ADC in MBED is 0.0 to 1.0 float!!!!!! |
mwthewsey | 0:d39a06ca6bf1 | 88 | Aout = BP_filter.getvalue(); |
mwthewsey | 0:d39a06ca6bf1 | 89 | |
mwthewsey | 0:d39a06ca6bf1 | 90 | |
mwthewsey | 0:d39a06ca6bf1 | 91 | SampLED = 0; //LED Indicates end of sampling |
chills | 7:6cb27cce4c50 | 92 | } |