emg
Fork of EMG by
emg.cpp@19:34c129b055b6, 2015-10-20 (annotated)
- Committer:
- AeroKev
- Date:
- Tue Oct 20 08:00:52 2015 +0000
- Revision:
- 19:34c129b055b6
- Parent:
- main.cpp@18:e753220c7ba6
Added .h;
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 |
tomlankhorst | 14:f83354387756 | 6 | AnalogIn emg(A0); //Analog input |
tomlankhorst | 14:f83354387756 | 7 | Ticker sample_timer; |
AeroKev | 18:e753220c7ba6 | 8 | HIDScope scope(2); |
AeroKev | 18:e753220c7ba6 | 9 | |
AeroKev | 18:e753220c7ba6 | 10 | double highV[4]; |
AeroKev | 18:e753220c7ba6 | 11 | double lowV[4]; |
AeroKev | 19:34c129b055b6 | 12 | // An array which stores the last MOV_AVG_NUM outputs |
AeroKev | 18:e753220c7ba6 | 13 | double lastOutputs[MOV_AVG_NUM-1]; |
AeroKev | 19:34c129b055b6 | 14 | // The sum of the last MOV_AVG_NUM outputs |
AeroKev | 19:34c129b055b6 | 15 | double outputSum; |
AeroKev | 18:e753220c7ba6 | 16 | |
AeroKev | 18:e753220c7ba6 | 17 | double filter(double input, double coeff_input[], double coeff_output[], double prev_outputs[]) |
AeroKev | 18:e753220c7ba6 | 18 | { |
AeroKev | 18:e753220c7ba6 | 19 | double new_input = input; |
AeroKev | 18:e753220c7ba6 | 20 | for(int i=1; i<5; i++) |
AeroKev | 18:e753220c7ba6 | 21 | new_input -= coeff_input[i] * prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 22 | |
AeroKev | 18:e753220c7ba6 | 23 | double new_output = coeff_output[0] * new_input; |
AeroKev | 18:e753220c7ba6 | 24 | for(int i=1; i<5; i++) |
AeroKev | 18:e753220c7ba6 | 25 | new_output += coeff_output[i] * prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 26 | |
AeroKev | 18:e753220c7ba6 | 27 | // Set the new output as the first value of the 'recent outputs' |
AeroKev | 18:e753220c7ba6 | 28 | for(int i = 3; i > 0; i--) |
AeroKev | 18:e753220c7ba6 | 29 | prev_outputs[i] = prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 30 | prev_outputs[0] = new_input; |
AeroKev | 18:e753220c7ba6 | 31 | return new_output; |
AeroKev | 18:e753220c7ba6 | 32 | } |
AeroKev | 18:e753220c7ba6 | 33 | |
AeroKev | 18:e753220c7ba6 | 34 | double fh_b[]= {0.7602, -3.0406, 4.5609, -3.0406, 0.7602}; |
AeroKev | 18:e753220c7ba6 | 35 | double fh_a[]= {1, -3.4532, 4.5041, -2.6273, 0.5778}; |
AeroKev | 18:e753220c7ba6 | 36 | double highpass_filter(double u) |
AeroKev | 18:e753220c7ba6 | 37 | { |
AeroKev | 18:e753220c7ba6 | 38 | return filter(u, fh_a, fh_b, highV); |
AeroKev | 18:e753220c7ba6 | 39 | } |
AeroKev | 18:e753220c7ba6 | 40 | |
AeroKev | 18:e753220c7ba6 | 41 | double fl_b[]= {0.00000658, 0.00002631, 0.00003947, 0.00002631, 0.00000658}; |
AeroKev | 18:e753220c7ba6 | 42 | double fl_a[]= {1.0000, -3.7264, 5.2160, -3.2500, 0.7605}; |
AeroKev | 18:e753220c7ba6 | 43 | double lowpass_filter(double u) |
AeroKev | 18:e753220c7ba6 | 44 | { |
AeroKev | 18:e753220c7ba6 | 45 | return filter(u, fl_a, fl_b, lowV); |
AeroKev | 18:e753220c7ba6 | 46 | } |
vsluiter | 2:e314bb3b2d99 | 47 | |
tomlankhorst | 14:f83354387756 | 48 | void sample() |
vsluiter | 2:e314bb3b2d99 | 49 | { |
AeroKev | 18:e753220c7ba6 | 50 | double input = emg.read(); |
AeroKev | 18:e753220c7ba6 | 51 | double output1 = highpass_filter(input); |
AeroKev | 18:e753220c7ba6 | 52 | double output2 = fabs(output1); |
AeroKev | 18:e753220c7ba6 | 53 | double output3 = lowpass_filter(output2); |
AeroKev | 18:e753220c7ba6 | 54 | |
AeroKev | 19:34c129b055b6 | 55 | /* Calculate the average of the last MOV_AVG_NUM outputs and the new input */ |
AeroKev | 19:34c129b055b6 | 56 | outputSum = outputSum - lastOutputs[MOV_AVG_NUM-2] + output3; |
AeroKev | 18:e753220c7ba6 | 57 | for(int i=0; i<MOV_AVG_NUM-1; i++) { |
AeroKev | 18:e753220c7ba6 | 58 | if(i != 0) lastOutputs[i] = lastOutputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 59 | } |
AeroKev | 18:e753220c7ba6 | 60 | lastOutputs[0] = output3; |
AeroKev | 19:34c129b055b6 | 61 | output3 = outputSum/MOV_AVG_NUM; |
AeroKev | 18:e753220c7ba6 | 62 | |
tomlankhorst | 14:f83354387756 | 63 | /* Second, set the sampled emg value in channel zero (the first channel) in the 'HIDScope' variable named 'scope' */ |
AeroKev | 18:e753220c7ba6 | 64 | scope.set(0,input); |
AeroKev | 18:e753220c7ba6 | 65 | scope.set(1,output3); |
tomlankhorst | 16:9f7797ffd0fb | 66 | /* Repeat the step above if required for more channels (channel 0 up to 5 = 6 channels) */ |
tomlankhorst | 14:f83354387756 | 67 | /* Finally, send all channels to the PC at once */ |
vsluiter | 11:ce72ec658a95 | 68 | scope.send(); |
vsluiter | 2:e314bb3b2d99 | 69 | } |
vsluiter | 0:32bb76391d89 | 70 | |
AeroKev | 19:34c129b055b6 | 71 | /* |
vsluiter | 0:32bb76391d89 | 72 | int main() |
vsluiter | 0:32bb76391d89 | 73 | { |
AeroKev | 19:34c129b055b6 | 74 | //Attach the 'sample' function to the timer 'sample_timer'. this ensures that 'sample' is executed every... 0.002 seconds |
AeroKev | 19:34c129b055b6 | 75 | |
tomlankhorst | 14:f83354387756 | 76 | sample_timer.attach(&sample, 0.002); |
tomlankhorst | 15:0da764eea774 | 77 | |
AeroKev | 19:34c129b055b6 | 78 | //empty loop, sample() is executed periodically |
tomlankhorst | 15:0da764eea774 | 79 | while(1) {} |
AeroKev | 19:34c129b055b6 | 80 | }*/ |