Het totale EMG script
Dependencies: HIDScope MODSERIAL biquadFilter mbed
Fork of EMGV5 by
main.cpp@20:a0642e983da9, 2015-10-08 (annotated)
- Committer:
- Margreeth95
- Date:
- Thu Oct 08 09:50:11 2015 +0000
- Revision:
- 20:a0642e983da9
- Parent:
- 19:34456a1effc4
- Child:
- 22:207d314523ef
EMG script
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 11:ce72ec658a95 | 2 | #include "HIDScope.h" |
Margreeth95 | 19:34456a1effc4 | 3 | #include "MODSERIAL.h" |
Margreeth95 | 19:34456a1effc4 | 4 | #include "biquadFilter.h" |
vsluiter | 0:32bb76391d89 | 5 | |
vsluiter | 4:8b298dfada81 | 6 | //Define objects |
Margreeth95 | 18:12250e88037f | 7 | AnalogIn EMG_left(A0); //Analog input |
Margreeth95 | 18:12250e88037f | 8 | AnalogIn EMG_right(A1); |
Margreeth95 | 18:12250e88037f | 9 | Ticker SampleEMG; |
Margreeth95 | 18:12250e88037f | 10 | Ticker ScopeTimer; |
Margreeth95 | 19:34456a1effc4 | 11 | Ticker serial; |
Margreeth95 | 19:34456a1effc4 | 12 | HIDScope scope(3); |
Margreeth95 | 19:34456a1effc4 | 13 | DigitalOut led(LED_RED); |
Margreeth95 | 19:34456a1effc4 | 14 | MODSERIAL pc(USBTX, USBRX); |
Margreeth95 | 19:34456a1effc4 | 15 | |
Margreeth95 | 18:12250e88037f | 16 | |
Margreeth95 | 19:34456a1effc4 | 17 | // Declaring variables |
Margreeth95 | 18:12250e88037f | 18 | double EMG_L_f_v1 = 0, EMG_L_f_v2 = 0; |
Margreeth95 | 19:34456a1effc4 | 19 | double EMG_L_fh=0; |
Margreeth95 | 19:34456a1effc4 | 20 | double EMG_left_value; |
Margreeth95 | 19:34456a1effc4 | 21 | double EMG_f1; |
Margreeth95 | 20:a0642e983da9 | 22 | double EMG_f2; |
Margreeth95 | 20:a0642e983da9 | 23 | double Threshold = 0.08; |
Margreeth95 | 18:12250e88037f | 24 | |
Margreeth95 | 18:12250e88037f | 25 | // coëfficiënten |
Margreeth95 | 19:34456a1effc4 | 26 | const double BiGainEMG_Lh = 0.723601, BiGainEMG_Ll=0.983892; |
Margreeth95 | 19:34456a1effc4 | 27 | 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 |
Margreeth95 | 19:34456a1effc4 | 28 | 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 |
Margreeth95 | 20:a0642e983da9 | 29 | |
Margreeth95 | 20:a0642e983da9 | 30 | // Filter creation |
Margreeth95 | 19:34456a1effc4 | 31 | biquadFilter EMG_highpass (EMGh_a1, EMGh_a2, EMGh_b0, EMGh_b1, EMGh_b2); // creates the high pass filter |
Margreeth95 | 19:34456a1effc4 | 32 | biquadFilter EMG_lowpass (EMGl_a1, EMGl_a2, EMGl_b0, EMGl_b1, EMGl_b2); // creates the low pass filter |
Margreeth95 | 19:34456a1effc4 | 33 | |
Margreeth95 | 19:34456a1effc4 | 34 | // EMG filtering function |
Margreeth95 | 19:34456a1effc4 | 35 | void EMGfilter() |
Margreeth95 | 19:34456a1effc4 | 36 | { |
Margreeth95 | 19:34456a1effc4 | 37 | EMG_left_value = EMG_left.read(); |
Margreeth95 | 19:34456a1effc4 | 38 | EMG_f1 = EMG_highpass.step(EMG_left_value); |
Margreeth95 | 19:34456a1effc4 | 39 | EMG_f2 = EMG_lowpass.step(EMG_f1); |
Margreeth95 | 19:34456a1effc4 | 40 | } |
vsluiter | 2:e314bb3b2d99 | 41 | |
Margreeth95 | 18:12250e88037f | 42 | // HIDScope |
Margreeth95 | 18:12250e88037f | 43 | void ScopeSend() |
Margreeth95 | 18:12250e88037f | 44 | { |
Margreeth95 | 19:34456a1effc4 | 45 | scope.set(0, EMG_left_value); |
Margreeth95 | 19:34456a1effc4 | 46 | scope.set(1, EMG_f1); |
Margreeth95 | 19:34456a1effc4 | 47 | scope.set(2, EMG_f2); |
Margreeth95 | 18:12250e88037f | 48 | scope.send(); |
Margreeth95 | 18:12250e88037f | 49 | } |
Margreeth95 | 18:12250e88037f | 50 | |
Margreeth95 | 19:34456a1effc4 | 51 | // Serial communication |
Margreeth95 | 19:34456a1effc4 | 52 | void serial_() |
Margreeth95 | 18:12250e88037f | 53 | { |
Margreeth95 | 19:34456a1effc4 | 54 | pc.baud(115200); |
Margreeth95 | 19:34456a1effc4 | 55 | pc.printf("%d\n", EMG_L_fh); |
Margreeth95 | 18:12250e88037f | 56 | } |
Margreeth95 | 19:34456a1effc4 | 57 | |
vsluiter | 0:32bb76391d89 | 58 | int main() |
vsluiter | 0:32bb76391d89 | 59 | { |
Margreeth95 | 18:12250e88037f | 60 | SampleEMG.attach(&EMGfilter, 0.002); |
Margreeth95 | 18:12250e88037f | 61 | ScopeTimer.attach(&ScopeSend, 0.002); |
Margreeth95 | 19:34456a1effc4 | 62 | while(1) |
Margreeth95 | 19:34456a1effc4 | 63 | { |
Margreeth95 | 20:a0642e983da9 | 64 | if (fabs(EMG_f2) > Threshold) |
Margreeth95 | 19:34456a1effc4 | 65 | { |
Margreeth95 | 19:34456a1effc4 | 66 | led.write(0); |
Margreeth95 | 19:34456a1effc4 | 67 | } |
Margreeth95 | 19:34456a1effc4 | 68 | |
Margreeth95 | 19:34456a1effc4 | 69 | else |
Margreeth95 | 19:34456a1effc4 | 70 | { |
Margreeth95 | 19:34456a1effc4 | 71 | led.write(1); |
Margreeth95 | 19:34456a1effc4 | 72 | } |
Margreeth95 | 19:34456a1effc4 | 73 | } |
vsluiter | 0:32bb76391d89 | 74 | } |