![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Werkend EMG script
Dependencies: HIDScope MODSERIAL biquadFilter mbed
Fork of EMG by
Diff: main.cpp
- Revision:
- 19:34456a1effc4
- Parent:
- 18:12250e88037f
- Child:
- 20:a0642e983da9
--- a/main.cpp Tue Oct 06 14:38:12 2015 +0000 +++ b/main.cpp Wed Oct 07 18:26:10 2015 +0000 @@ -1,48 +1,71 @@ #include "mbed.h" #include "HIDScope.h" +#include "MODSERIAL.h" +#include "biquadFilter.h" //Define objects AnalogIn EMG_left(A0); //Analog input AnalogIn EMG_right(A1); Ticker SampleEMG; Ticker ScopeTimer; - HIDScope scope(1); + Ticker serial; + HIDScope scope(3); + DigitalOut led(LED_RED); + MODSERIAL pc(USBTX, USBRX); + -// constant values +// Declaring variables double EMG_L_f_v1 = 0, EMG_L_f_v2 = 0; + double EMG_L_fh=0; + double EMG_left_value; + double EMG_f1; + double EMG_f2; // coëfficiënten - const double BiGainEMG_Lh = 1, BiGainEMG_Ll=1; - const double EMG_L_fh_a1 = -0.96608908283*BiGainEMG_Lh, EMG_L_fh_a2 = 0.0*BiGainEMG_Lh, EMG_L_fh_b0 = 1.0*BiGainEMG_Lh, EMG_L_fh_b1 = 1.0*BiGainEMG_Lh, EMG_L_fh_b2 = 0.0*BiGainEMG_Lh; //coefficients for high-pass filter - const double EMG_L_fl_a1 = -0.96608908283*BiGainEMG_Ll, EMG_L_fl_a2 = 0.0*BiGainEMG_Ll, EMG_L_fl_b0 = 1.0*BiGainEMG_Ll, EMG_L_fl_b1 = 1.0*BiGainEMG_Ll, EMG_L_fl_b2 = 0.0*BiGainEMG_Ll; // coefficients for low-pass filter + const double BiGainEMG_Lh = 0.723601, BiGainEMG_Ll=0.983892; + const double EMGh_a1 = -1.74355513773*BiGainEMG_Lh, EMGh_a2 = 0.80079826172*BiGainEMG_Lh, EMGh_b0 = 1.0*BiGainEMG_Lh, EMGh_b1 = -1.99697722433*BiGainEMG_Lh, EMGh_b2 = 1.0*BiGainEMG_Lh; //coefficients for high-pass filter + const double EMGl_a1 = 1.96775103303*BiGainEMG_Ll, EMGl_a2 = 0.96827054038*BiGainEMG_Ll, EMGl_b0 = 1.0*BiGainEMG_Ll, EMGl_b1 = 1.99999993582*BiGainEMG_Ll, EMGl_b2 = 1.0*BiGainEMG_Ll; // coefficients for low-pass filter + biquadFilter EMG_highpass (EMGh_a1, EMGh_a2, EMGh_b0, EMGh_b1, EMGh_b2); // creates the high pass filter + biquadFilter EMG_lowpass (EMGl_a1, EMGl_a2, EMGl_b0, EMGl_b1, EMGl_b2); // creates the low pass filter + +// EMG filtering function +void EMGfilter() +{ + EMG_left_value = EMG_left.read(); + EMG_f1 = EMG_highpass.step(EMG_left_value); + EMG_f2 = EMG_lowpass.step(EMG_f1); +} // HIDScope void ScopeSend() { - scope.set(0, EMG_left.read()); + scope.set(0, EMG_left_value); + scope.set(1, EMG_f1); + scope.set(2, EMG_f2); scope.send(); } -// Biquad filter - double biquad( double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2 ) +// Serial communication + void serial_() { - double v = u - a1*v1 - a2*v2; - double y = b0*v + b1*v1 + b2*v2; - v2 = v1; v1 = v; - return y; + pc.baud(115200); + pc.printf("%d\n", EMG_L_fh); } - -// EMG filtering function -void EMGfilter() -{ - double EMG_left_value = EMG_left.read(); - double EMG_L_fh = biquad(EMG_left_value, EMG_L_f_v1, EMG_L_f_v2, EMG_L_fh_a1, EMG_L_fh_a2, EMG_L_fh_b0, EMG_L_fh_b1, EMG_L_fh_b2); - double EMG_L_fhl = biquad(EMG_L_fh, EMG_L_f_v1, EMG_L_f_v2, EMG_L_fl_a1, EMG_L_fl_a2, EMG_L_fl_b0, EMG_L_fl_b1, EMG_L_fl_b2); -} - + int main() { SampleEMG.attach(&EMGfilter, 0.002); ScopeTimer.attach(&ScopeSend, 0.002); - while(1) {} + while(1) + { + if (EMG_f2 > 0.08) + { + led.write(0); + } + + else + { + led.write(1); + } + } } \ No newline at end of file