Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ELEC347_Coursework by
main.cpp@12:d522a7a061d6, 2017-12-02 (annotated)
- Committer:
- thomasmorris
- Date:
- Sat Dec 02 17:09:34 2017 +0000
- Revision:
- 12:d522a7a061d6
- Parent:
- 11:7efb6e4759cc
- Child:
- 13:db76473a3d76
ELEC347 Final Working Version
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 |
thomasmorris | 11:7efb6e4759cc | 13 | AnalogIn QDEF(PA_7); // Q Factor |
thomasmorris | 11:7efb6e4759cc | 14 | AnalogIn BDEF(PA_6); // Boost Level |
thomasmorris | 11:7efb6e4759cc | 15 | AnalogIn FDEF(PA_5); //Centre Frequency |
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; |
thomasmorris | 11:7efb6e4759cc | 21 | |
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 | |
thomasmorris | 11:7efb6e4759cc | 37 | //Declare 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 | { |
thomasmorris | 11:7efb6e4759cc | 54 | t2.start(sampler);//Start the Sampler timed interrupt |
thomasmorris | 11:7efb6e4759cc | 55 | t1.start(coeff_update);//Update the coeffiecients of the filter |
thomasmorris | 12:d522a7a061d6 | 56 | |
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 | 10:3495fff88da7 | 63 | Fo_Current = FDEF * 22000 + 10; |
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()); |
thomasmorris | 11:7efb6e4759cc | 81 | Thread::wait(1000);//Sleep the thread for 1 second then re runs the coefficient calculation |
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 |
thomasmorris | 12:d522a7a061d6 | 97 | Thread::wait(sample_rate*1000);//Look at this |
chills | 9:91c53a683856 | 98 | } |
thomasmorris | 11:7efb6e4759cc | 99 | } |