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