Changed some stuff

Fork of EMG by Kevin Hetterscheid

Committer:
dubbie
Date:
Thu Oct 29 10:43:46 2015 +0000
Revision:
28:5a12ce2fa441
Parent:
25:6d1b035f4838
Child:
29:373a2facb3ae
hoi kevin

Who changed what in which revision?

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