EMG controlling calibration + filters biorobotics group 13 BMT
Dependencies: HIDScope biquadFilter mbed
main.cpp@2:e3259334299e, 2015-10-19 (annotated)
- Committer:
- RichellBooyink
- Date:
- Mon Oct 19 13:23:10 2015 +0000
- Revision:
- 2:e3259334299e
- Parent:
- 1:16165e207e70
- Child:
- 3:61f0fc41f3bc
Made the input of the biquad the output of the previous biquad. So y1=u2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RichellBooyink | 0:2ded24549b70 | 1 | #include "mbed.h" |
RichellBooyink | 0:2ded24549b70 | 2 | #include "HIDScope.h" |
RichellBooyink | 0:2ded24549b70 | 3 | |
RichellBooyink | 1:16165e207e70 | 4 | AnalogIn EMG(A0); |
RichellBooyink | 1:16165e207e70 | 5 | HIDScope scope (3); |
RichellBooyink | 1:16165e207e70 | 6 | |
RichellBooyink | 0:2ded24549b70 | 7 | // Standaard formule voor het biquad filter |
RichellBooyink | 0:2ded24549b70 | 8 | double biquad(double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2) |
RichellBooyink | 0:2ded24549b70 | 9 | |
RichellBooyink | 2:e3259334299e | 10 | { |
RichellBooyink | 0:2ded24549b70 | 11 | double v = u - a1*v1 - a2*v2; |
RichellBooyink | 0:2ded24549b70 | 12 | double y = b0*v + b1*v1 + b2*v2; |
RichellBooyink | 0:2ded24549b70 | 13 | v2=v1; |
RichellBooyink | 0:2ded24549b70 | 14 | v1=v; |
RichellBooyink | 0:2ded24549b70 | 15 | return y; |
RichellBooyink | 0:2ded24549b70 | 16 | } |
RichellBooyink | 0:2ded24549b70 | 17 | // Filter1 = High pass filter tot 20 Hz |
RichellBooyink | 0:2ded24549b70 | 18 | double fh1_v1=0, fh1_v2=0, fh2_v1=0, fh2_v2=0; |
RichellBooyink | 0:2ded24549b70 | 19 | const double fh1_a1=-0.84909054461, fh1_a2=0.00000000000, fh1_b0= 1, fh1_b1=-1, fh1_b2=0; |
RichellBooyink | 0:2ded24549b70 | 20 | const double fh2_a1=-1.82553264091, fh2_a2=0.85001416809, fh2_b0= 1, fh2_b1=-2, fh2_b2=1; |
RichellBooyink | 0:2ded24549b70 | 21 | // Filter2 = Low pass filter na 60 Hz |
RichellBooyink | 0:2ded24549b70 | 22 | double fl1_v1=0, fl1_v2=0, fl2_v1=0, fl2_v2=0; |
RichellBooyink | 0:2ded24549b70 | 23 | const double fl1_a1=-0.66979455390, fl1_a2=0.00000000000, fl1_b0= 1, fl1_b1=1, fl1_b2=0; |
RichellBooyink | 0:2ded24549b70 | 24 | const double fl2_a1=-1.55376616139, fl2_a2=0.68023470431, fl2_b0= 1, fl2_b1=2, fl2_b2=1; |
RichellBooyink | 0:2ded24549b70 | 25 | // Filter3 = Notch filter at 50 Hz |
RichellBooyink | 0:2ded24549b70 | 26 | double fno1_v1=0, fno1_v2=0, fno2_v1=0, fno2_v2=0, fno3_v1=0, fno3_v2=0; |
RichellBooyink | 0:2ded24549b70 | 27 | const double fno1_a1 = -1.87934916386, fno1_a2= 0.97731851355, fno1_b0= 1, fno1_b1= -1.90090686046, fno1_b2= 1; |
RichellBooyink | 0:2ded24549b70 | 28 | const double fno2_a1 = -1.88341028603, fno2_a2= 0.98825147717, fno2_b0= 1, fno2_b1= -1.90090686046, fno2_b2= 1; |
RichellBooyink | 0:2ded24549b70 | 29 | const double fno3_a1 = -1.89635403726, fno3_a2= 0.98894004849, fno3_b0= 1, fno3_b1= -1.90090686046, fno3_b2= 1; |
RichellBooyink | 2:e3259334299e | 30 | double y1, y2, y3, y4, y5, y6, y7, u1, u2, u3, u4, u5, u6, u7; |
RichellBooyink | 2:e3259334299e | 31 | void Filters() |
RichellBooyink | 0:2ded24549b70 | 32 | { |
RichellBooyink | 2:e3259334299e | 33 | u1 = EMG.read(); |
RichellBooyink | 2:e3259334299e | 34 | //Highpass |
RichellBooyink | 2:e3259334299e | 35 | y1 = u2; |
RichellBooyink | 2:e3259334299e | 36 | y1 = biquad (u1, fh1_v1, fh1_v2, fh1_a1, fh1_a2, fh1_b0, fh1_b1, fh1_b2); |
RichellBooyink | 2:e3259334299e | 37 | y2 = biquad (u2, fh2_v1, fh2_v2, fh2_a1, fh2_a2, fh2_b0, fh2_b1, fh2_b2); |
RichellBooyink | 2:e3259334299e | 38 | //Lowpass |
RichellBooyink | 2:e3259334299e | 39 | u3 = y2; |
RichellBooyink | 2:e3259334299e | 40 | u4 = y3; |
RichellBooyink | 2:e3259334299e | 41 | y3 = biquad (u3, fl1_v1, fl1_v2, fl1_a1, fl1_a2, fl1_b0, fl1_b1, fl1_b2); |
RichellBooyink | 2:e3259334299e | 42 | y4 = biquad (u4, fl2_v1, fl2_v2, fl2_a1, fl2_a2, fl2_b0, fl2_b1, fl2_b2); |
RichellBooyink | 2:e3259334299e | 43 | // Notch |
RichellBooyink | 2:e3259334299e | 44 | u5 = y4; |
RichellBooyink | 2:e3259334299e | 45 | u6 = y5; |
RichellBooyink | 2:e3259334299e | 46 | u7 = y6; |
RichellBooyink | 2:e3259334299e | 47 | y5 = biquad (u5, fno1_v1, fno1_v2, fno1_a1, fno1_a2, fno1_b0, fno1_b1, fno1_b2); |
RichellBooyink | 2:e3259334299e | 48 | y6 = biquad (u6, fno2_v1, fno2_v2, fno2_a1, fno2_a2, fno2_b0, fno2_b1, fno2_b2); |
RichellBooyink | 2:e3259334299e | 49 | y7 = biquad (u7, fno3_v1, fno3_v2, fno3_a1, fno3_a2, fno3_b0, fno3_b1, fno3_b2); |
RichellBooyink | 2:e3259334299e | 50 | scope.set (0, u1); |
RichellBooyink | 2:e3259334299e | 51 | scope.set (1, y2); |
RichellBooyink | 2:e3259334299e | 52 | scope.set (2, y4); |
RichellBooyink | 2:e3259334299e | 53 | scope.set (3, y7); |
RichellBooyink | 1:16165e207e70 | 54 | scope.send (); |
RichellBooyink | 1:16165e207e70 | 55 | } |
RichellBooyink | 2:e3259334299e | 56 | Ticker filter; |
RichellBooyink | 0:2ded24549b70 | 57 | int main () |
RichellBooyink | 2:e3259334299e | 58 | {filter.attach_us(Filters,1e3);} |
RichellBooyink | 0:2ded24549b70 | 59 | |
RichellBooyink | 0:2ded24549b70 | 60 | |
RichellBooyink | 0:2ded24549b70 | 61 | |
RichellBooyink | 2:e3259334299e | 62 | |
RichellBooyink | 2:e3259334299e | 63 |