![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Non-variable (TESTEDandWORKING)
Fork of ELEC347_Coursework by
main.cpp@7:6cb27cce4c50, 2017-11-28 (annotated)
- Committer:
- chills
- Date:
- Tue Nov 28 14:37:13 2017 +0000
- Revision:
- 7:6cb27cce4c50
- Parent:
- 6:a2737b51424c
- Child:
- 8:192b376a6da7
Finished Writing Active Updating Code; Needs Testing; Did not print to board upon initial testing;
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 | 7:6cb27cce4c50 | 13 | AnalogIn QDEF(PA_2); |
chills | 7:6cb27cce4c50 | 14 | AnalogIn BDEF(PA_7); |
chills | 7:6cb27cce4c50 | 15 | AnalogIn FDEF(PA_6); |
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 | 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 | //Declare Filter |
chills | 7:6cb27cce4c50 | 31 | FILTER BP_filter(48000,10000,-16,2); //Create object of type Filter(Fs,Fo,Boost,Q) |
mwthewsey | 2:f6e49378dd8a | 32 | |
chills | 7:6cb27cce4c50 | 33 | //Declare Fixed Variables |
chills | 7:6cb27cce4c50 | 34 | float Fo = 1; |
chills | 7:6cb27cce4c50 | 35 | float Boost = 0; |
chills | 7:6cb27cce4c50 | 36 | float Q = 1; |
mwthewsey | 0:d39a06ca6bf1 | 37 | |
chills | 7:6cb27cce4c50 | 38 | //Declare Temporary Coefficient Values |
chills | 7:6cb27cce4c50 | 39 | float Fo_Current; |
chills | 7:6cb27cce4c50 | 40 | float Boost_Current; |
chills | 7:6cb27cce4c50 | 41 | float Q_Current; |
chills | 7:6cb27cce4c50 | 42 | |
chills | 7:6cb27cce4c50 | 43 | //Forward Declarations |
mwthewsey | 0:d39a06ca6bf1 | 44 | void sampler(void); |
chills | 7:6cb27cce4c50 | 45 | void coeff_update(void); |
mwthewsey | 0:d39a06ca6bf1 | 46 | |
mwthewsey | 0:d39a06ca6bf1 | 47 | int main() |
mwthewsey | 0:d39a06ca6bf1 | 48 | { |
chills | 6:a2737b51424c | 49 | //sample_timer.attach(&sampler,sample_rate); |
chills | 7:6cb27cce4c50 | 50 | coeff_change.attach(&coeff_update,coeff_rate); |
chills | 7:6cb27cce4c50 | 51 | printf("Hello"); |
mwthewsey | 0:d39a06ca6bf1 | 52 | } |
mwthewsey | 0:d39a06ca6bf1 | 53 | |
chills | 7:6cb27cce4c50 | 54 | void coeff_update(void) |
chills | 7:6cb27cce4c50 | 55 | { |
chills | 7:6cb27cce4c50 | 56 | Fo_Current = FDEF * 39999 + 1; |
chills | 7:6cb27cce4c50 | 57 | if (abs(Fo_Current - BP_filter.Get_Fo()) > 50) //If Centre Frequency has changed significantly |
chills | 7:6cb27cce4c50 | 58 | { |
chills | 7:6cb27cce4c50 | 59 | BP_filter.Update_Fo(Fo_Current); //Update Centre Frequency |
chills | 7:6cb27cce4c50 | 60 | } |
chills | 7:6cb27cce4c50 | 61 | Boost_Current = BDEF * 100 - 50; |
chills | 7:6cb27cce4c50 | 62 | if (abs(Boost_Current - BP_filter.Get_Boost()) > 2) //If Boost has changed significantly |
chills | 7:6cb27cce4c50 | 63 | { |
chills | 7:6cb27cce4c50 | 64 | BP_filter.Update_Boost(Boost_Current); //Update Boost |
chills | 7:6cb27cce4c50 | 65 | } |
chills | 7:6cb27cce4c50 | 66 | Q_Current = QDEF * 49 + 1; |
chills | 7:6cb27cce4c50 | 67 | if (abs(Q_Current - BP_filter.Get_Q()) > 2) //If Q has changed significantly |
chills | 7:6cb27cce4c50 | 68 | { |
chills | 7:6cb27cce4c50 | 69 | BP_filter.Update_Q(Q_Current); //Update Q |
chills | 7:6cb27cce4c50 | 70 | } |
chills | 7:6cb27cce4c50 | 71 | BP_filter.Define_Filter(); //Calculate the coefficients |
chills | 7:6cb27cce4c50 | 72 | //BP_filter.Print_Filter(); //Print the ceofficients |
chills | 7:6cb27cce4c50 | 73 | } |
mwthewsey | 0:d39a06ca6bf1 | 74 | |
mwthewsey | 0:d39a06ca6bf1 | 75 | void sampler(void) |
mwthewsey | 0:d39a06ca6bf1 | 76 | { |
mwthewsey | 0:d39a06ca6bf1 | 77 | SampLED = 1; //LED Indicates start of sampling |
mwthewsey | 0:d39a06ca6bf1 | 78 | |
mwthewsey | 2:f6e49378dd8a | 79 | input = Ain; |
chills | 7:6cb27cce4c50 | 80 | BP_filter.setvalue(input); //Input ADC. N.B. ADC in MBED is 0.0 to 1.0 float!!!!!! |
mwthewsey | 0:d39a06ca6bf1 | 81 | Aout = BP_filter.getvalue(); |
mwthewsey | 0:d39a06ca6bf1 | 82 | |
mwthewsey | 0:d39a06ca6bf1 | 83 | |
mwthewsey | 0:d39a06ca6bf1 | 84 | SampLED = 0; //LED Indicates end of sampling |
chills | 7:6cb27cce4c50 | 85 | } |