Changed some stuff
Fork of EMG by
emg.cpp@23:8647e71ca713, 2015-10-20 (annotated)
- Committer:
- AeroKev
- Date:
- Tue Oct 20 14:30:18 2015 +0000
- Revision:
- 23:8647e71ca713
- Parent:
- 22:8c9dda710584
- Child:
- 24:b7b3e87e0687
a
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" |
AeroKev | 19:34c129b055b6 | 3 | #include "emg.h" |
AeroKev | 18:e753220c7ba6 | 4 | |
vsluiter | 4:8b298dfada81 | 5 | //Define objects |
AeroKev | 21:49362a17495b | 6 | AnalogIn emgIn0(A0); //Analog input |
AeroKev | 21:49362a17495b | 7 | AnalogIn emgIn1(A1); //Analog input |
AeroKev | 21:49362a17495b | 8 | AnalogIn emgIn2(A2); //Analog input |
AeroKev | 18:e753220c7ba6 | 9 | |
AeroKev | 18:e753220c7ba6 | 10 | double highV[4]; |
AeroKev | 18:e753220c7ba6 | 11 | double lowV[4]; |
AeroKev | 18:e753220c7ba6 | 12 | |
AeroKev | 18:e753220c7ba6 | 13 | double filter(double input, double coeff_input[], double coeff_output[], double prev_outputs[]) |
AeroKev | 18:e753220c7ba6 | 14 | { |
AeroKev | 18:e753220c7ba6 | 15 | double new_input = input; |
AeroKev | 18:e753220c7ba6 | 16 | for(int i=1; i<5; i++) |
AeroKev | 18:e753220c7ba6 | 17 | new_input -= coeff_input[i] * prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 18 | |
AeroKev | 18:e753220c7ba6 | 19 | double new_output = coeff_output[0] * new_input; |
AeroKev | 18:e753220c7ba6 | 20 | for(int i=1; i<5; i++) |
AeroKev | 18:e753220c7ba6 | 21 | new_output += coeff_output[i] * prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 22 | |
AeroKev | 18:e753220c7ba6 | 23 | // Set the new output as the first value of the 'recent outputs' |
AeroKev | 18:e753220c7ba6 | 24 | for(int i = 3; i > 0; i--) |
AeroKev | 18:e753220c7ba6 | 25 | prev_outputs[i] = prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 26 | prev_outputs[0] = new_input; |
AeroKev | 18:e753220c7ba6 | 27 | return new_output; |
AeroKev | 18:e753220c7ba6 | 28 | } |
AeroKev | 18:e753220c7ba6 | 29 | |
AeroKev | 18:e753220c7ba6 | 30 | double fh_b[]= {0.7602, -3.0406, 4.5609, -3.0406, 0.7602}; |
AeroKev | 18:e753220c7ba6 | 31 | double fh_a[]= {1, -3.4532, 4.5041, -2.6273, 0.5778}; |
AeroKev | 18:e753220c7ba6 | 32 | double highpass_filter(double u) |
AeroKev | 18:e753220c7ba6 | 33 | { |
AeroKev | 18:e753220c7ba6 | 34 | return filter(u, fh_a, fh_b, highV); |
AeroKev | 18:e753220c7ba6 | 35 | } |
AeroKev | 18:e753220c7ba6 | 36 | |
AeroKev | 21:49362a17495b | 37 | double fl_b[]= {0.00001329, 0.00005317, 0.00007976, 0.00005317, 0.00001329}; |
AeroKev | 21:49362a17495b | 38 | double fl_a[]= {1.0000, -3.6717, 5.0680, -3.1160, 0.7199}; |
AeroKev | 18:e753220c7ba6 | 39 | double lowpass_filter(double u) |
AeroKev | 18:e753220c7ba6 | 40 | { |
AeroKev | 18:e753220c7ba6 | 41 | return filter(u, fl_a, fl_b, lowV); |
AeroKev | 18:e753220c7ba6 | 42 | } |
vsluiter | 2:e314bb3b2d99 | 43 | |
AeroKev | 21:49362a17495b | 44 | /** Sample function |
AeroKev | 21:49362a17495b | 45 | * this function samples the emg and sends it to HIDScope |
AeroKev | 21:49362a17495b | 46 | **/ |
AeroKev | 21:49362a17495b | 47 | double sample(int emgNum) |
vsluiter | 2:e314bb3b2d99 | 48 | { |
AeroKev | 21:49362a17495b | 49 | double input = 0.0; |
AeroKev | 21:49362a17495b | 50 | if(emgNum == 1) input = emgIn1.read(); |
AeroKev | 21:49362a17495b | 51 | if(emgNum == 2) input = emgIn2.read(); |
AeroKev | 21:49362a17495b | 52 | else input = emgIn0.read(); |
AeroKev | 18:e753220c7ba6 | 53 | double output1 = highpass_filter(input); |
AeroKev | 18:e753220c7ba6 | 54 | double output2 = fabs(output1); |
AeroKev | 18:e753220c7ba6 | 55 | double output3 = lowpass_filter(output2); |
AeroKev | 18:e753220c7ba6 | 56 | |
AeroKev | 23:8647e71ca713 | 57 | if(output3 >= 0 && output3 < 0.0507) |
AeroKev | 23:8647e71ca713 | 58 | output3 = 0.0; |
AeroKev | 23:8647e71ca713 | 59 | else if(output3 >= 0.0507 && output3 < 0.0966) |
AeroKev | 23:8647e71ca713 | 60 | output3 = 0.5; |
AeroKev | 23:8647e71ca713 | 61 | else if(output3 >= 0.0966 && output3 < 1) |
AeroKev | 23:8647e71ca713 | 62 | output3 = 1.0; |
AeroKev | 23:8647e71ca713 | 63 | |
AeroKev | 21:49362a17495b | 64 | return output3; |
AeroKev | 21:49362a17495b | 65 | } |