Changed some stuff
Fork of EMG by
emg.cpp@25:6d1b035f4838, 2015-10-27 (annotated)
- Committer:
- dubbie
- Date:
- Tue Oct 27 12:03:21 2015 +0000
- Revision:
- 25:6d1b035f4838
- Parent:
- 24:b7b3e87e0687
- Child:
- 27:25d0ee8cd7b7
- Child:
- 28:5a12ce2fa441
I am very committed.
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 | 24:b7b3e87e0687 | 3 | |
AeroKev | 24:b7b3e87e0687 | 4 | #define MOV_AVG_NUM 100 |
AeroKev | 18:e753220c7ba6 | 5 | |
vsluiter | 4:8b298dfada81 | 6 | //Define objects |
AeroKev | 24:b7b3e87e0687 | 7 | AnalogIn b_emg(A0); //Analog input bicep |
AeroKev | 24:b7b3e87e0687 | 8 | AnalogIn t_emg(A1); //Analog input tricep |
AeroKev | 24:b7b3e87e0687 | 9 | AnalogIn p_emg(A2); //Analog input bicep for push |
AeroKev | 18:e753220c7ba6 | 10 | |
AeroKev | 24:b7b3e87e0687 | 11 | double b_highV[4]; |
AeroKev | 24:b7b3e87e0687 | 12 | double b_lowV[4]; |
AeroKev | 24:b7b3e87e0687 | 13 | double t_highV[4]; |
AeroKev | 24:b7b3e87e0687 | 14 | double t_lowV[4]; |
AeroKev | 24:b7b3e87e0687 | 15 | double p_highV[4]; |
AeroKev | 24:b7b3e87e0687 | 16 | double p_lowV[4]; |
AeroKev | 24:b7b3e87e0687 | 17 | |
AeroKev | 24:b7b3e87e0687 | 18 | //double lastOutputs[MOV_AVG_NUM-1]; |
AeroKev | 18:e753220c7ba6 | 19 | |
AeroKev | 18:e753220c7ba6 | 20 | double filter(double input, double coeff_input[], double coeff_output[], double prev_outputs[]) |
AeroKev | 18:e753220c7ba6 | 21 | { |
AeroKev | 18:e753220c7ba6 | 22 | double new_input = input; |
AeroKev | 18:e753220c7ba6 | 23 | for(int i=1; i<5; i++) |
AeroKev | 18:e753220c7ba6 | 24 | new_input -= coeff_input[i] * prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 25 | |
AeroKev | 18:e753220c7ba6 | 26 | double new_output = coeff_output[0] * new_input; |
AeroKev | 18:e753220c7ba6 | 27 | for(int i=1; i<5; i++) |
AeroKev | 18:e753220c7ba6 | 28 | new_output += coeff_output[i] * prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 29 | |
AeroKev | 18:e753220c7ba6 | 30 | // Set the new output as the first value of the 'recent outputs' |
AeroKev | 18:e753220c7ba6 | 31 | for(int i = 3; i > 0; i--) |
AeroKev | 18:e753220c7ba6 | 32 | prev_outputs[i] = prev_outputs[i-1]; |
AeroKev | 18:e753220c7ba6 | 33 | prev_outputs[0] = new_input; |
AeroKev | 18:e753220c7ba6 | 34 | return new_output; |
AeroKev | 18:e753220c7ba6 | 35 | } |
AeroKev | 18:e753220c7ba6 | 36 | double fh_b[]= {0.7602, -3.0406, 4.5609, -3.0406, 0.7602}; |
AeroKev | 18:e753220c7ba6 | 37 | double fh_a[]= {1, -3.4532, 4.5041, -2.6273, 0.5778}; |
AeroKev | 24:b7b3e87e0687 | 38 | double b_highpass_filter(double u) |
AeroKev | 24:b7b3e87e0687 | 39 | { |
AeroKev | 24:b7b3e87e0687 | 40 | return filter(u, fh_a, fh_b, t_highV); |
AeroKev | 24:b7b3e87e0687 | 41 | } |
AeroKev | 24:b7b3e87e0687 | 42 | double t_highpass_filter(double u) |
AeroKev | 18:e753220c7ba6 | 43 | { |
AeroKev | 24:b7b3e87e0687 | 44 | return filter(u, fh_a, fh_b, b_highV); |
AeroKev | 24:b7b3e87e0687 | 45 | } |
AeroKev | 24:b7b3e87e0687 | 46 | double p_highpass_filter(double u) |
AeroKev | 24:b7b3e87e0687 | 47 | { |
AeroKev | 24:b7b3e87e0687 | 48 | return filter(u, fh_a, fh_b, p_highV); |
AeroKev | 18:e753220c7ba6 | 49 | } |
AeroKev | 18:e753220c7ba6 | 50 | |
AeroKev | 21:49362a17495b | 51 | double fl_b[]= {0.00001329, 0.00005317, 0.00007976, 0.00005317, 0.00001329}; |
AeroKev | 21:49362a17495b | 52 | double fl_a[]= {1.0000, -3.6717, 5.0680, -3.1160, 0.7199}; |
AeroKev | 24:b7b3e87e0687 | 53 | double b_lowpass_filter(double u) |
AeroKev | 24:b7b3e87e0687 | 54 | { |
AeroKev | 24:b7b3e87e0687 | 55 | return filter(u, fl_a, fl_b, t_lowV); |
AeroKev | 24:b7b3e87e0687 | 56 | } |
AeroKev | 24:b7b3e87e0687 | 57 | double t_lowpass_filter(double u) |
AeroKev | 18:e753220c7ba6 | 58 | { |
AeroKev | 24:b7b3e87e0687 | 59 | |
AeroKev | 24:b7b3e87e0687 | 60 | return filter(u, fl_a, fl_b, b_lowV); |
AeroKev | 24:b7b3e87e0687 | 61 | } |
AeroKev | 24:b7b3e87e0687 | 62 | double p_lowpass_filter(double u) |
AeroKev | 24:b7b3e87e0687 | 63 | { |
AeroKev | 24:b7b3e87e0687 | 64 | |
AeroKev | 24:b7b3e87e0687 | 65 | return filter(u, fl_a, fl_b, p_lowV); |
AeroKev | 18:e753220c7ba6 | 66 | } |
vsluiter | 2:e314bb3b2d99 | 67 | |
AeroKev | 24:b7b3e87e0687 | 68 | double last_biceps = 0; |
AeroKev | 24:b7b3e87e0687 | 69 | double last_triceps = 0; |
AeroKev | 24:b7b3e87e0687 | 70 | double last_push = 0; |
AeroKev | 21:49362a17495b | 71 | /** Sample function |
AeroKev | 21:49362a17495b | 72 | * this function samples the emg and sends it to HIDScope |
AeroKev | 21:49362a17495b | 73 | **/ |
AeroKev | 24:b7b3e87e0687 | 74 | void sample(double& bicout, double& tricout, double& pushout) |
vsluiter | 2:e314bb3b2d99 | 75 | { |
AeroKev | 24:b7b3e87e0687 | 76 | double b_input = b_emg.read(); |
AeroKev | 24:b7b3e87e0687 | 77 | double output1 = b_highpass_filter(b_input); |
AeroKev | 18:e753220c7ba6 | 78 | double output2 = fabs(output1); |
AeroKev | 24:b7b3e87e0687 | 79 | double output3 = b_lowpass_filter(output2); |
AeroKev | 24:b7b3e87e0687 | 80 | |
AeroKev | 24:b7b3e87e0687 | 81 | double t_input = t_emg.read(); |
AeroKev | 24:b7b3e87e0687 | 82 | double output4 = t_highpass_filter(t_input); |
AeroKev | 24:b7b3e87e0687 | 83 | double output5 = fabs(output4); |
AeroKev | 24:b7b3e87e0687 | 84 | double output6 = t_lowpass_filter(output5); |
AeroKev | 24:b7b3e87e0687 | 85 | |
AeroKev | 24:b7b3e87e0687 | 86 | double p_input = p_emg.read(); |
AeroKev | 24:b7b3e87e0687 | 87 | double output7 = p_highpass_filter(p_input); |
AeroKev | 24:b7b3e87e0687 | 88 | double output8 = fabs(output7); |
AeroKev | 24:b7b3e87e0687 | 89 | double output9 = p_lowpass_filter(output8); |
AeroKev | 24:b7b3e87e0687 | 90 | |
AeroKev | 24:b7b3e87e0687 | 91 | /*double tot = output3; |
AeroKev | 24:b7b3e87e0687 | 92 | for(int i=0; i<MOV_AVG_NUM-1; i++) { |
AeroKev | 24:b7b3e87e0687 | 93 | tot += lastOutputs[i]; |
AeroKev | 24:b7b3e87e0687 | 94 | if(i != 0) lastOutputs[i] = lastOutputs[i-1]; |
AeroKev | 24:b7b3e87e0687 | 95 | } |
AeroKev | 24:b7b3e87e0687 | 96 | lastOutputs[0] = output3; |
AeroKev | 24:b7b3e87e0687 | 97 | output3 = tot/MOV_AVG_NUM;*/ |
AeroKev | 18:e753220c7ba6 | 98 | |
AeroKev | 24:b7b3e87e0687 | 99 | /* Second, set the sampled emg value in channel zero (the first channel) in the 'HIDScope' variable named 'scope' */ |
AeroKev | 24:b7b3e87e0687 | 100 | |
AeroKev | 24:b7b3e87e0687 | 101 | if (last_biceps == 0 and output3 > 0.06) { |
AeroKev | 24:b7b3e87e0687 | 102 | bicout = 1; |
AeroKev | 24:b7b3e87e0687 | 103 | } else if (last_biceps == 1 and output3 < 0.045) { |
AeroKev | 24:b7b3e87e0687 | 104 | bicout = 0; |
AeroKev | 24:b7b3e87e0687 | 105 | } else { |
AeroKev | 24:b7b3e87e0687 | 106 | bicout = last_biceps; |
AeroKev | 24:b7b3e87e0687 | 107 | } |
AeroKev | 24:b7b3e87e0687 | 108 | last_biceps = bicout; |
AeroKev | 24:b7b3e87e0687 | 109 | |
AeroKev | 24:b7b3e87e0687 | 110 | /*if (output3>0.06) {//This is the output for the right bicep |
AeroKev | 24:b7b3e87e0687 | 111 | bicout=1; |
AeroKev | 24:b7b3e87e0687 | 112 | } else if(output3>0.04) { |
AeroKev | 24:b7b3e87e0687 | 113 | bicout=0.5; |
AeroKev | 24:b7b3e87e0687 | 114 | } else { |
AeroKev | 24:b7b3e87e0687 | 115 | bicout=0; |
AeroKev | 24:b7b3e87e0687 | 116 | }*/ |
AeroKev | 23:8647e71ca713 | 117 | |
AeroKev | 24:b7b3e87e0687 | 118 | /*if (output6>0.0561) {//this is the output for the right tricep |
AeroKev | 24:b7b3e87e0687 | 119 | tricout=1; |
AeroKev | 24:b7b3e87e0687 | 120 | } else if(output6>0.0380) { |
AeroKev | 24:b7b3e87e0687 | 121 | tricout=0.5; |
AeroKev | 24:b7b3e87e0687 | 122 | } else { |
AeroKev | 24:b7b3e87e0687 | 123 | tricout=0; |
AeroKev | 24:b7b3e87e0687 | 124 | }*/ |
AeroKev | 24:b7b3e87e0687 | 125 | |
AeroKev | 24:b7b3e87e0687 | 126 | if (last_triceps == 0 and output6 > 0.045) { |
AeroKev | 24:b7b3e87e0687 | 127 | tricout = 1; |
AeroKev | 24:b7b3e87e0687 | 128 | } else if(last_triceps == 1 and output6 < 0.04) { |
AeroKev | 24:b7b3e87e0687 | 129 | tricout = 0; |
AeroKev | 24:b7b3e87e0687 | 130 | } else { |
AeroKev | 24:b7b3e87e0687 | 131 | tricout = last_triceps; |
AeroKev | 24:b7b3e87e0687 | 132 | } |
AeroKev | 24:b7b3e87e0687 | 133 | last_triceps = tricout; |
AeroKev | 24:b7b3e87e0687 | 134 | |
dubbie | 25:6d1b035f4838 | 135 | if (last_push ==0 and output9>0.06) {//this is the output for the left bicep (the push motion) |
AeroKev | 24:b7b3e87e0687 | 136 | pushout=1; |
dubbie | 25:6d1b035f4838 | 137 | } else if(last_triceps == 1 and output9<0.045) { |
dubbie | 25:6d1b035f4838 | 138 | pushout=0; |
AeroKev | 24:b7b3e87e0687 | 139 | } else { |
dubbie | 25:6d1b035f4838 | 140 | pushout = last_push; |
AeroKev | 24:b7b3e87e0687 | 141 | } |
dubbie | 25:6d1b035f4838 | 142 | last_push = pushout; |
AeroKev | 21:49362a17495b | 143 | } |