Richell Booyink / Mbed 2 deprecated EMG_Controlling

Dependencies:   HIDScope biquadFilter mbed

Committer:
RichellBooyink
Date:
Tue Oct 20 10:38:37 2015 +0000
Revision:
4:8db85182a00d
Parent:
3:61f0fc41f3bc
Child:
5:e78295b978ab
Filter including rectifier and smoothing the signal ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RichellBooyink 0:2ded24549b70 1 #include "mbed.h"
RichellBooyink 0:2ded24549b70 2 #include "HIDScope.h"
RichellBooyink 0:2ded24549b70 3
RichellBooyink 4:8db85182a00d 4 AnalogIn EMG_bicepsleft(A0);
RichellBooyink 4:8db85182a00d 5 AnalogIn EMG_bicepsright (A1);
RichellBooyink 4:8db85182a00d 6 AnalogIn EMG_legleft (A2);
RichellBooyink 4:8db85182a00d 7 AnalogIn EMG_legright (A3);
RichellBooyink 3:61f0fc41f3bc 8 HIDScope scope(4);
RichellBooyink 0:2ded24549b70 9
RichellBooyink 0:2ded24549b70 10 // Filter1 = High pass filter tot 20 Hz
RichellBooyink 0:2ded24549b70 11 double fh1_v1=0, fh1_v2=0, fh2_v1=0, fh2_v2=0;
RichellBooyink 0:2ded24549b70 12 const double fh1_a1=-0.84909054461, fh1_a2=0.00000000000, fh1_b0= 1, fh1_b1=-1, fh1_b2=0;
RichellBooyink 0:2ded24549b70 13 const double fh2_a1=-1.82553264091, fh2_a2=0.85001416809, fh2_b0= 1, fh2_b1=-2, fh2_b2=1;
RichellBooyink 0:2ded24549b70 14 // Filter2 = Low pass filter na 60 Hz
RichellBooyink 0:2ded24549b70 15 double fl1_v1=0, fl1_v2=0, fl2_v1=0, fl2_v2=0;
RichellBooyink 0:2ded24549b70 16 const double fl1_a1=-0.66979455390, fl1_a2=0.00000000000, fl1_b0= 1, fl1_b1=1, fl1_b2=0;
RichellBooyink 0:2ded24549b70 17 const double fl2_a1=-1.55376616139, fl2_a2=0.68023470431, fl2_b0= 1, fl2_b1=2, fl2_b2=1;
RichellBooyink 0:2ded24549b70 18 // Filter3 = Notch filter at 50 Hz
RichellBooyink 0:2ded24549b70 19 double fno1_v1=0, fno1_v2=0, fno2_v1=0, fno2_v2=0, fno3_v1=0, fno3_v2=0;
RichellBooyink 0:2ded24549b70 20 const double fno1_a1 = -1.87934916386, fno1_a2= 0.97731851355, fno1_b0= 1, fno1_b1= -1.90090686046, fno1_b2= 1;
RichellBooyink 0:2ded24549b70 21 const double fno2_a1 = -1.88341028603, fno2_a2= 0.98825147717, fno2_b0= 1, fno2_b1= -1.90090686046, fno2_b2= 1;
RichellBooyink 0:2ded24549b70 22 const double fno3_a1 = -1.89635403726, fno3_a2= 0.98894004849, fno3_b0= 1, fno3_b1= -1.90090686046, fno3_b2= 1;
RichellBooyink 4:8db85182a00d 23 // Filter4 = Lowpass filter at 5 Hz
RichellBooyink 4:8db85182a00d 24 double flp1_v1=0, flp1_v2=0, flp2_v1=0, flp2_v2=0;
RichellBooyink 4:8db85182a00d 25 const double flp1_a1=-0.97922725527, flp1_a2=0.00000000000, flp1_b0= 1, flp1_b1=1, flp1_b2=0;
RichellBooyink 4:8db85182a00d 26 const double flp2_a1=-1.97879353121, flp2_a2=0.97922951943, flp2_b0= 1, flp2_b1=2, flp2_b2=1;
RichellBooyink 2:e3259334299e 27 double y1, y2, y3, y4, y5, y6, y7, u1, u2, u3, u4, u5, u6, u7;
RichellBooyink 3:61f0fc41f3bc 28
RichellBooyink 3:61f0fc41f3bc 29 // Standaard formule voor het biquad filter
RichellBooyink 3:61f0fc41f3bc 30 double biquad(double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2)
RichellBooyink 3:61f0fc41f3bc 31
RichellBooyink 3:61f0fc41f3bc 32 {
RichellBooyink 3:61f0fc41f3bc 33 double v = u - a1*v1 - a2*v2;
RichellBooyink 3:61f0fc41f3bc 34 double y = b0*v + b1*v1 + b2*v2;
RichellBooyink 3:61f0fc41f3bc 35 v2=v1;
RichellBooyink 3:61f0fc41f3bc 36 v1=v;
RichellBooyink 3:61f0fc41f3bc 37 return y;
RichellBooyink 3:61f0fc41f3bc 38 }
RichellBooyink 3:61f0fc41f3bc 39
RichellBooyink 4:8db85182a00d 40 void Filters_bicepleft()
RichellBooyink 0:2ded24549b70 41 {
RichellBooyink 4:8db85182a00d 42 u1 = EMG_bicepsleft.read();
RichellBooyink 2:e3259334299e 43 //Highpass
RichellBooyink 3:61f0fc41f3bc 44 y1 = biquad (u1, fh1_v1, fh1_v2, fh1_a1, fh1_a2, fh1_b0*0.924547, fh1_b1*0.924547, fh1_b2*0.924547);
RichellBooyink 3:61f0fc41f3bc 45 u2 = y1;
RichellBooyink 3:61f0fc41f3bc 46 y2 = biquad (u2, fh2_v1, fh2_v2, fh2_a1, fh2_a2, fh2_b0*0.918885, fh2_b1*0.918885, fh2_b2*0.918885);
RichellBooyink 2:e3259334299e 47 //Lowpass
RichellBooyink 2:e3259334299e 48 u3 = y2;
RichellBooyink 3:61f0fc41f3bc 49 y3 = biquad (u3, fl1_v1, fl1_v2, fl1_a1, fl1_a2, fl1_b0*0.165103, fl1_b1*0.165103, fl1_b2*0.165103);
RichellBooyink 2:e3259334299e 50 u4 = y3;
RichellBooyink 3:61f0fc41f3bc 51 y4 = biquad (u4, fl2_v1, fl2_v2, fl2_a1, fl2_a2, fl2_b0*0.031617, fl2_b1*0.031617, fl2_b2*0.031617);
RichellBooyink 2:e3259334299e 52 // Notch
RichellBooyink 2:e3259334299e 53 u5 = y4;
RichellBooyink 3:61f0fc41f3bc 54 y5 = biquad (u5, fno1_v1, fno1_v2, fno1_a1, fno1_a2, fno1_b0*1.004206, fno1_b1*1.004206, fno1_b2*1.004206);
RichellBooyink 2:e3259334299e 55 u6 = y5;
RichellBooyink 3:61f0fc41f3bc 56 y6 = biquad (u6, fno2_v1, fno2_v2, fno2_a1, fno2_a2, fno2_b0, fno2_b1, fno2_b2);
RichellBooyink 2:e3259334299e 57 u7 = y6;
RichellBooyink 3:61f0fc41f3bc 58 y7 = biquad (u7, fno3_v1, fno3_v2, fno3_a1, fno3_a2, fno3_b0*0.973227, fno3_b1*0.973227, fno3_b2*0.973227);
RichellBooyink 4:8db85182a00d 59 // Rectify sample
RichellBooyink 4:8db85182a00d 60 y8 = fabs(y7);
RichellBooyink 4:8db85182a00d 61 // Make it smooth
RichellBooyink 4:8db85182a00d 62 u8 = y8;
RichellBooyink 4:8db85182a00d 63 y9 = biquad (u8, flp1_v1, flp1_v2, flp1_a1, flp1_a2, flp1_b0* 0.010386, flp1_b1* 0.010386, flp1_b2* 0.010386);
RichellBooyink 4:8db85182a00d 64 u9 = y9;
RichellBooyink 4:8db85182a00d 65 final_filter = biquad(u9, flp2_v1, flp2_v2, flp2_a1, flp2_a2, flp2_b0*0.000109, flp2_b1*0.000109, flp2_b2*0.000109);
RichellBooyink 4:8db85182a00d 66 // Send it to scope
RichellBooyink 2:e3259334299e 67 scope.set (0, u1);
RichellBooyink 4:8db85182a00d 68 scope.set (1, y7);
RichellBooyink 4:8db85182a00d 69 scope.set (2, y8);
RichellBooyink 4:8db85182a00d 70 scope.set (3, final_filter);
RichellBooyink 1:16165e207e70 71 scope.send ();
RichellBooyink 1:16165e207e70 72 }
RichellBooyink 4:8db85182a00d 73
RichellBooyink 2:e3259334299e 74 Ticker filter;
RichellBooyink 0:2ded24549b70 75 int main ()
RichellBooyink 2:e3259334299e 76 {filter.attach_us(Filters,1e3);}
RichellBooyink 0:2ded24549b70 77
RichellBooyink 0:2ded24549b70 78
RichellBooyink 0:2ded24549b70 79
RichellBooyink 2:e3259334299e 80
RichellBooyink 2:e3259334299e 81