test

Dependencies:   HIDScope MODSERIAL mbed-dsp mbed

Fork of emg_filter2 by BMT M9 Groep01

Committer:
Tanja2211
Date:
Mon Oct 20 10:26:11 2014 +0000
Revision:
54:f3a9fa5f2b0e
Parent:
53:d90e54fba7d8
Child:
55:f215d954533c
triceps veranderd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s1340735 22:dc630dbb1dcd 1 #include "mbed.h"
s1340735 22:dc630dbb1dcd 2 #include "HIDScope.h"
s1340735 23:1c51af8386c9 3 #include "MODSERIAL.h"
s1340735 22:dc630dbb1dcd 4 #include "arm_math.h"
s1340735 22:dc630dbb1dcd 5
s1340735 29:f54123765a47 6
s1340735 29:f54123765a47 7
s1340735 29:f54123765a47 8 // ****** emg filter shizzle ******
s1340735 29:f54123765a47 9
s1340735 22:dc630dbb1dcd 10 //Define objects
s1340735 22:dc630dbb1dcd 11 AnalogIn emgB(PTB0); //Analog input bicep
s1340735 22:dc630dbb1dcd 12 AnalogIn emgT(PTB1); //Analog input tricep
s1340735 22:dc630dbb1dcd 13
s1340735 23:1c51af8386c9 14 float filtered_emgB;
s1340735 27:24e73fd36859 15 float filtered_emgT;
Tanja2211 51:809e38348c91 16 float B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, MOVAVG_B;
Tanja2211 51:809e38348c91 17 float T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, MOVAVG_T;
Tanja2211 54:f3a9fa5f2b0e 18 float drempelwaardeB1, drempelwaardeB2, drempelwaardeB3, drempelwaardeT1, drempelwaardeT2;
s1340735 23:1c51af8386c9 19
s1340735 23:1c51af8386c9 20 MODSERIAL pc(USBTX,USBRX);
s1340735 23:1c51af8386c9 21
s1340735 26:b93c82fb6e1d 22 HIDScope scope(4);//uitgang scherm
s1340735 22:dc630dbb1dcd 23
s1340735 22:dc630dbb1dcd 24 arm_biquad_casd_df1_inst_f32 lowpass;
s1340735 22:dc630dbb1dcd 25 //constants for 50Hz lowpass
s1340735 30:5d8e6f0fabc1 26 float lowpass_const[] = {0.2928920553, 0.5857841107, 0.2928920554, -0, -0.17156822136};//{a0 a1 a2 -b1 -b2} van online calculator
s1340735 22:dc630dbb1dcd 27 //state values
s1340735 26:b93c82fb6e1d 28 float lowpass_states[4];
s1340735 22:dc630dbb1dcd 29
s1340735 22:dc630dbb1dcd 30 arm_biquad_casd_df1_inst_f32 highpass;
s1340735 22:dc630dbb1dcd 31 //constants for 10Hz highpass
s1340735 26:b93c82fb6e1d 32 float highpass_const[] = {0.8005910267, -1.6011820533, 0.8005910267, 1.5610153913, -0.6413487154};//{a0 a1 a2 -b1 -b2}
s1340735 22:dc630dbb1dcd 33 //state values
s1340735 26:b93c82fb6e1d 34 float highpass_states[4];
s1340735 22:dc630dbb1dcd 35
s1340735 22:dc630dbb1dcd 36
s1340735 22:dc630dbb1dcd 37 /** Looper function
s1340735 22:dc630dbb1dcd 38 * functions used for Ticker and Timeout should be of type void <name>(void)
s1340735 22:dc630dbb1dcd 39 * i.e. no input arguments, no output arguments.
s1340735 22:dc630dbb1dcd 40 * if you want to change a variable that you use in other places (for example in main)
s1340735 22:dc630dbb1dcd 41 * you will have to make that variable global in order to be able to reach it both from
s1340735 22:dc630dbb1dcd 42 * the function called at interrupt time, and in the main function.
s1340735 22:dc630dbb1dcd 43 * To make a variable global, define it under the includes.
s1340735 22:dc630dbb1dcd 44 * variables that are changed in the interrupt routine (written to) should be made
s1340735 22:dc630dbb1dcd 45 * 'volatile' to let the compiler know that those values may change outside the current context.
s1340735 22:dc630dbb1dcd 46 * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value"
s1340735 22:dc630dbb1dcd 47 * in the example below, the variable is not re-used in the main function, and is thus declared
s1340735 22:dc630dbb1dcd 48 * local in the looper function only.
s1340735 22:dc630dbb1dcd 49 **/
s1340735 22:dc630dbb1dcd 50
s1340735 22:dc630dbb1dcd 51 //BICEP EMG LEZEN
s1340735 22:dc630dbb1dcd 52 void looperB()
s1340735 22:dc630dbb1dcd 53 {
s1340735 23:1c51af8386c9 54 /*variable to store value in*/
s1340735 22:dc630dbb1dcd 55 uint16_t emg_valueB;
s1340735 30:5d8e6f0fabc1 56
s1340735 22:dc630dbb1dcd 57 float emg_value_f32B;
s1340735 22:dc630dbb1dcd 58 /*put raw emg value both in red and in emg_value*/
s1340735 22:dc630dbb1dcd 59 emg_valueB = emgB.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
s1340735 22:dc630dbb1dcd 60 emg_value_f32B = emgB.read();
s1340735 22:dc630dbb1dcd 61
s1340735 22:dc630dbb1dcd 62 //process emg
s1340735 22:dc630dbb1dcd 63 arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32B, &filtered_emgB, 1 );
s1340735 22:dc630dbb1dcd 64 filtered_emgB = fabs(filtered_emgB);
s1340735 22:dc630dbb1dcd 65 arm_biquad_cascade_df1_f32(&lowpass, &filtered_emgB, &filtered_emgB, 1 );
s1340735 23:1c51af8386c9 66
s1340735 22:dc630dbb1dcd 67 /*send value to PC. */
s1340735 22:dc630dbb1dcd 68 scope.set(0,emg_valueB); //uint value
s1340735 22:dc630dbb1dcd 69 scope.set(1,filtered_emgB); //processed float
s1340735 22:dc630dbb1dcd 70 scope.send();
Tanja2211 45:7950fa411107 71
Tanja2211 44:b47f559826ba 72 // Moving Average Filter Biceps
s1340735 22:dc630dbb1dcd 73
Tanja2211 51:809e38348c91 74
Tanja2211 36:82fd9d862266 75 {
Tanja2211 32:1bc34d137942 76 B0=filtered_emgB;
Tanja2211 42:d49b766ffdc7 77 MOVAVG_B=B0*0.1+B1*0.1+B2*0.1+B3*0.1+B4*0.1+B5*0.1+B6*0.1+B7*0.1+B8*0.1+B9*0.1;
Tanja2211 32:1bc34d137942 78 B9=B8;
Tanja2211 32:1bc34d137942 79 B8=B7;
Tanja2211 32:1bc34d137942 80 B7=B6;
Tanja2211 32:1bc34d137942 81 B6=B5;
Tanja2211 32:1bc34d137942 82 B5=B4;
Tanja2211 32:1bc34d137942 83 B4=B3;
Tanja2211 32:1bc34d137942 84 B3=B2;
Tanja2211 32:1bc34d137942 85 B2=B1;
Tanja2211 32:1bc34d137942 86 B1=B0;
s1340735 30:5d8e6f0fabc1 87
s1340735 30:5d8e6f0fabc1 88 }
Tanja2211 45:7950fa411107 89 }
Tanja2211 36:82fd9d862266 90
Tanja2211 36:82fd9d862266 91
Tanja2211 44:b47f559826ba 92 // Triceps EMG lezen
Tanja2211 45:7950fa411107 93 void looperT()
Tanja2211 45:7950fa411107 94 {
Tanja2211 45:7950fa411107 95 /*variable to store value in*/
Tanja2211 45:7950fa411107 96 uint16_t emg_valueT;
Tanja2211 31:b6f7ba4938d4 97
Tanja2211 45:7950fa411107 98 float emg_value_f32T;
Tanja2211 45:7950fa411107 99 /*put raw emg value both in red and in emg_value*/
Tanja2211 45:7950fa411107 100 emg_valueT = emgT.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
Tanja2211 45:7950fa411107 101 emg_value_f32T = emgT.read();
s1340735 22:dc630dbb1dcd 102
Tanja2211 45:7950fa411107 103 //process emg
Tanja2211 45:7950fa411107 104 arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32T, &filtered_emgT, 1 );
Tanja2211 45:7950fa411107 105 filtered_emgT = fabs(filtered_emgT);
Tanja2211 45:7950fa411107 106 arm_biquad_cascade_df1_f32(&lowpass, &filtered_emgT, &filtered_emgT, 1 );
s1340735 22:dc630dbb1dcd 107
Tanja2211 45:7950fa411107 108 /*send value to PC. */
Tanja2211 45:7950fa411107 109 scope.set(2,emg_valueT); //uint value
Tanja2211 45:7950fa411107 110 scope.set(3,filtered_emgT); //processed float
Tanja2211 45:7950fa411107 111 scope.send();
Tanja2211 38:7ed04177892b 112
Tanja2211 45:7950fa411107 113 // Moving Average Filter Triceps
Tanja2211 38:7ed04177892b 114
Tanja2211 51:809e38348c91 115
Tanja2211 50:650e8e45b870 116 {
Tanja2211 45:7950fa411107 117 T0=filtered_emgT;
Tanja2211 45:7950fa411107 118 MOVAVG_T=T0*0.1+T1*0.1+T2*0.1+T3*0.1+T4*0.1+T5*0.1+T6*0.1+T7*0.1+T8*0.1+T9*0.1;
Tanja2211 38:7ed04177892b 119
Tanja2211 45:7950fa411107 120 T9=T8;
Tanja2211 45:7950fa411107 121 T8=T7;
Tanja2211 45:7950fa411107 122 T7=T6;
Tanja2211 45:7950fa411107 123 T6=T5;
Tanja2211 45:7950fa411107 124 T5=T4;
Tanja2211 45:7950fa411107 125 T4=T3;
Tanja2211 45:7950fa411107 126 T3=T2;
Tanja2211 45:7950fa411107 127 T2=T1;
Tanja2211 45:7950fa411107 128 T1=T0;
Tanja2211 38:7ed04177892b 129
Tanja2211 45:7950fa411107 130 }
Tanja2211 50:650e8e45b870 131 }
Tanja2211 49:b103e9ed5ef2 132
Tanja2211 36:82fd9d862266 133
Tanja2211 45:7950fa411107 134 int main()
Tanja2211 45:7950fa411107 135 {
Tanja2211 45:7950fa411107 136 Ticker log_timer;
Tanja2211 45:7950fa411107 137 //set up filters. Use external array for constants
Tanja2211 45:7950fa411107 138 arm_biquad_cascade_df1_init_f32(&lowpass,1 , lowpass_const, lowpass_states);
Tanja2211 45:7950fa411107 139 arm_biquad_cascade_df1_init_f32(&highpass,1 ,highpass_const, highpass_states);
s1340735 22:dc630dbb1dcd 140
Tanja2211 45:7950fa411107 141 /**Here you attach the 'void looper(void)' function to the Ticker object
Tanja2211 45:7950fa411107 142 * The looper() function will be called every 0.01 seconds.
Tanja2211 45:7950fa411107 143 * Please mind that the parentheses after looper are omitted when using attach.
Tanja2211 45:7950fa411107 144 */
Tanja2211 45:7950fa411107 145 log_timer.attach(looperB, 0.005);//??
Tanja2211 45:7950fa411107 146 log_timer.attach(looperT, 0.005);//??
Tanja2211 45:7950fa411107 147 while(1) { //Loop
Tanja2211 45:7950fa411107 148 /*Empty!*/
Tanja2211 45:7950fa411107 149 /*Everything is handled by the interrupt routine now!*/
s1340735 23:1c51af8386c9 150 }
Tanja2211 45:7950fa411107 151 }
s1340735 23:1c51af8386c9 152
s1340735 22:dc630dbb1dcd 153 //filtered_emgB
s1340735 22:dc630dbb1dcd 154 //filtered_emgT
s1340735 22:dc630dbb1dcd 155
Tanja2211 52:cce270e0753a 156 void AntwoordT()
Tanja2211 45:7950fa411107 157 {
Tanja2211 54:f3a9fa5f2b0e 158 drempelwaardeT1=4.99;
Tanja2211 54:f3a9fa5f2b0e 159 drempelwaardeT2=7;
Tanja2211 54:f3a9fa5f2b0e 160 int yT1, yT2;
Tanja2211 54:f3a9fa5f2b0e 161
Tanja2211 54:f3a9fa5f2b0e 162 if (MOVAVG_T > drempelwaardeT1) {
Tanja2211 54:f3a9fa5f2b0e 163 yT1=1;
Tanja2211 54:f3a9fa5f2b0e 164 if (MOVAVG_T > drempelwaardeT2) {
Tanja2211 54:f3a9fa5f2b0e 165 yT2=1;
Tanja2211 54:f3a9fa5f2b0e 166 } else {
Tanja2211 54:f3a9fa5f2b0e 167 yT2=0; }
Tanja2211 54:f3a9fa5f2b0e 168 } else {
Tanja2211 54:f3a9fa5f2b0e 169 yT1=0;
Tanja2211 54:f3a9fa5f2b0e 170 }
Tanja2211 54:f3a9fa5f2b0e 171
Tanja2211 54:f3a9fa5f2b0e 172 int positie;
Tanja2211 54:f3a9fa5f2b0e 173
Tanja2211 54:f3a9fa5f2b0e 174 positie=yT1+yT2;
Tanja2211 54:f3a9fa5f2b0e 175 if (positie==0) {
Tanja2211 54:f3a9fa5f2b0e 176 pc.printf("Motor 2 beweegt niet\n");
Tanja2211 54:f3a9fa5f2b0e 177 } else {
Tanja2211 54:f3a9fa5f2b0e 178 pc.printf("Motor 2 gaat beweegen\n"); }
Tanja2211 54:f3a9fa5f2b0e 179 if (snelheidsstand==1) {
Tanja2211 54:f3a9fa5f2b0e 180 pc.printf("Motor 2 beweegt naar positie 1\n");
Tanja2211 54:f3a9fa5f2b0e 181 } else {
Tanja2211 54:f3a9fa5f2b0e 182 pc.printf("Motor 1 beweegt niet met snelheid 2\n");
Tanja2211 54:f3a9fa5f2b0e 183 }
Tanja2211 54:f3a9fa5f2b0e 184 if (snelheidsstand==3) {
Tanja2211 54:f3a9fa5f2b0e 185 pc.printf("Motor 1 beweegt met snelheid 3\n");
Tanja2211 54:f3a9fa5f2b0e 186 } else {
Tanja2211 54:f3a9fa5f2b0e 187 pc.printf("Motor 1 beweegt niet met snelheid 3\n");
Tanja2211 54:f3a9fa5f2b0e 188 }
Tanja2211 54:f3a9fa5f2b0e 189 if (yT1==1) {
Tanja2211 45:7950fa411107 190 pc.printf("Motor 2 beweegt\n");
Tanja2211 45:7950fa411107 191 } else {
Tanja2211 45:7950fa411107 192 pc.printf("Motor 2 beweegt niet\n");
Tanja2211 45:7950fa411107 193 }
Tanja2211 52:cce270e0753a 194 }
Tanja2211 38:7ed04177892b 195
Tanja2211 52:cce270e0753a 196 void AntwoordB() {
Tanja2211 52:cce270e0753a 197 drempelwaardeB1=4.99;
Tanja2211 52:cce270e0753a 198 drempelwaardeB2=6;
Tanja2211 52:cce270e0753a 199 drempelwaardeB3=10;
Tanja2211 45:7950fa411107 200 int yB1;
Tanja2211 45:7950fa411107 201 int yB2;
Tanja2211 45:7950fa411107 202 int yB3;
Tanja2211 38:7ed04177892b 203
Tanja2211 52:cce270e0753a 204 if (MOVAVG_B > drempelwaardeB1) {
Tanja2211 45:7950fa411107 205 yB1=1;
Tanja2211 52:cce270e0753a 206 if (MOVAVG_B > drempelwaardeB2) {
Tanja2211 45:7950fa411107 207 yB2=1;
Tanja2211 53:d90e54fba7d8 208 if (MOVAVG_B > drempelwaardeB3) {
Tanja2211 53:d90e54fba7d8 209 yB3=1;}
Tanja2211 53:d90e54fba7d8 210 else {
Tanja2211 53:d90e54fba7d8 211 yB3=0;}
Tanja2211 45:7950fa411107 212 } else {
Tanja2211 53:d90e54fba7d8 213 yB2=0; }
Tanja2211 53:d90e54fba7d8 214 } else {
Tanja2211 45:7950fa411107 215 yB1=0;
Tanja2211 45:7950fa411107 216 }
Tanja2211 53:d90e54fba7d8 217
Tanja2211 45:7950fa411107 218 int snelheidsstand;
Tanja2211 53:d90e54fba7d8 219
Tanja2211 45:7950fa411107 220 snelheidsstand=yB1+yB2+yB3;
Tanja2211 45:7950fa411107 221 if (snelheidsstand==1) {
Tanja2211 45:7950fa411107 222 pc.printf("Motor 1 beweegt met snelheid 1\n");
Tanja2211 45:7950fa411107 223 } else {
Tanja2211 48:5a270ba60008 224 pc.printf("Motor 1 beweegt niet met snelheid 1\n"); }
Tanja2211 45:7950fa411107 225 if (snelheidsstand==2) {
Tanja2211 45:7950fa411107 226 pc.printf("Motor 1 beweegt met snelheid 2\n");
Tanja2211 45:7950fa411107 227 } else {
Tanja2211 48:5a270ba60008 228 pc.printf("Motor 1 beweegt niet met snelheid 2\n");
Tanja2211 38:7ed04177892b 229 }
Tanja2211 45:7950fa411107 230 if (snelheidsstand==3) {
Tanja2211 45:7950fa411107 231 pc.printf("Motor 1 beweegt met snelheid 3\n");
Tanja2211 45:7950fa411107 232 } else {
Tanja2211 48:5a270ba60008 233 pc.printf("Motor 1 beweegt niet met snelheid 3\n");
Tanja2211 44:b47f559826ba 234 }
Tanja2211 45:7950fa411107 235 }