emg eind code

Dependencies:   MODSERIAL mbed

Fork of EMGStdevV3 by Dan August

Committer:
DanAuhust
Date:
Mon Nov 04 20:09:22 2013 +0000
Revision:
3:e609cd999fd2
Parent:
1:1ffb9e3ae00f
Child:
4:117c0bb09513
nu met gains en thresholds

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DanAuhust 0:59e2ea255892 1 #include "mbed.h"
DanAuhust 0:59e2ea255892 2 #include "MODSERIAL.h"
DanAuhust 0:59e2ea255892 3
DanAuhust 0:59e2ea255892 4 //Define objects
DanAuhust 0:59e2ea255892 5 AnalogIn emg_biceps(PTB0);
DanAuhust 0:59e2ea255892 6 AnalogIn emg_triceps(PTB1);
DanAuhust 0:59e2ea255892 7 AnalogIn emg_flexoren(PTB2);
DanAuhust 0:59e2ea255892 8 AnalogIn emg_extensoren(PTB3); //Analog input
DanAuhust 0:59e2ea255892 9 PwmOut red(LED_RED); //PWM output
DanAuhust 0:59e2ea255892 10 Ticker timer;
DanAuhust 0:59e2ea255892 11 MODSERIAL pc(USBTX,USBRX,64,1024);
DanAuhust 0:59e2ea255892 12
DanAuhust 0:59e2ea255892 13 #define offset_biceps 0 // offset ruwe invoer met adapter motoren, waar toepassen?
DanAuhust 0:59e2ea255892 14
DanAuhust 0:59e2ea255892 15 //high pass filter constantes 15Hz cutoff 4e orde, Fs = 312,5Hz (geeft een wat mooiere waarde voor periode en is geen veelvoud van 50Hz)
DanAuhust 0:59e2ea255892 16 #define NUM0 0.6731 // constante
DanAuhust 0:59e2ea255892 17 #define NUM1 -2.6925 // z^-1
DanAuhust 0:59e2ea255892 18 #define NUM2 4.0388 // z^-2etc.
DanAuhust 0:59e2ea255892 19 #define NUM3 -2.6925
DanAuhust 0:59e2ea255892 20 #define NUM4 0.6731
DanAuhust 0:59e2ea255892 21
DanAuhust 0:59e2ea255892 22 #define DEN0 1 // constante
DanAuhust 0:59e2ea255892 23 #define DEN1 -3.2133
DanAuhust 0:59e2ea255892 24 #define DEN2 3.9348
DanAuhust 0:59e2ea255892 25 #define DEN3 -2.1689
DanAuhust 0:59e2ea255892 26 #define DEN4 0.4531
DanAuhust 0:59e2ea255892 27
DanAuhust 1:1ffb9e3ae00f 28 //lowpass filter constantes 4Hz 4e orde, Fs = 312,5 Hz
DanAuhust 0:59e2ea255892 29 #define NUM0_3 0.00000236 // constante
DanAuhust 0:59e2ea255892 30 #define NUM1_3 0.00000944 // z^-1
DanAuhust 0:59e2ea255892 31 #define NUM2_3 0.00001415 // z^-2etc.
DanAuhust 0:59e2ea255892 32 #define NUM3_3 0.00000944
DanAuhust 0:59e2ea255892 33 #define NUM4_3 0.00000236
DanAuhust 0:59e2ea255892 34
DanAuhust 0:59e2ea255892 35 #define DEN0_3 1 // constante
DanAuhust 0:59e2ea255892 36 #define DEN1_3 -3.7899
DanAuhust 0:59e2ea255892 37 #define DEN2_3 5.3914
DanAuhust 0:59e2ea255892 38 #define DEN3_3 -3.4119
DanAuhust 0:59e2ea255892 39 #define DEN4_3 0.8104
DanAuhust 0:59e2ea255892 40
DanAuhust 0:59e2ea255892 41 float std_dev (float input, int sig_number){
DanAuhust 0:59e2ea255892 42 // define variables
DanAuhust 0:59e2ea255892 43 static int startcount=0;
DanAuhust 0:59e2ea255892 44 float sum;
DanAuhust 0:59e2ea255892 45 int size = 20;
DanAuhust 0:59e2ea255892 46 float sig_out;
DanAuhust 0:59e2ea255892 47 float mean;
DanAuhust 0:59e2ea255892 48 static int count_biceps=0;
DanAuhust 0:59e2ea255892 49 static int count_triceps=0;
DanAuhust 0:59e2ea255892 50 static int count_flexoren=0;
DanAuhust 0:59e2ea255892 51 static int count_extensoren=0;
DanAuhust 0:59e2ea255892 52 static float keeper_biceps[20];
DanAuhust 0:59e2ea255892 53 static float keeper_triceps[20];
DanAuhust 0:59e2ea255892 54 static float keeper_flexoren[20];
DanAuhust 0:59e2ea255892 55 static float keeper_extensoren[20];
DanAuhust 0:59e2ea255892 56
DanAuhust 0:59e2ea255892 57 if (startcount >= size)
DanAuhust 0:59e2ea255892 58 {//ja, dan gewoon std dev nemen, want keeper is al vol
DanAuhust 0:59e2ea255892 59 switch(sig_number){
DanAuhust 0:59e2ea255892 60 case 1:
DanAuhust 0:59e2ea255892 61 keeper_biceps[count_biceps]=input;
DanAuhust 0:59e2ea255892 62 count_biceps++;
DanAuhust 0:59e2ea255892 63 if(count_biceps >= size)
DanAuhust 0:59e2ea255892 64 count_biceps=0;
DanAuhust 0:59e2ea255892 65 //sizeof(keeper_biceps)/sizeof(float);
DanAuhust 1:1ffb9e3ae00f 66 for(int i=0; i < size; i++)
DanAuhust 0:59e2ea255892 67 {sum+=keeper_biceps[i];
DanAuhust 0:59e2ea255892 68 }
DanAuhust 0:59e2ea255892 69 mean=sum/size;
DanAuhust 0:59e2ea255892 70 sum=0;
DanAuhust 1:1ffb9e3ae00f 71 for(int i=0; i < size; i++)
DanAuhust 0:59e2ea255892 72 {sum+=(keeper_biceps[i]-mean)*(keeper_biceps[i]-mean);
DanAuhust 0:59e2ea255892 73 }
DanAuhust 0:59e2ea255892 74 sig_out=sqrt(sum/size);
DanAuhust 0:59e2ea255892 75 sum=0;
DanAuhust 0:59e2ea255892 76 break;
DanAuhust 0:59e2ea255892 77 case 2:
DanAuhust 0:59e2ea255892 78 keeper_triceps[count_triceps]=input;
DanAuhust 0:59e2ea255892 79 count_triceps++;
DanAuhust 0:59e2ea255892 80 if(count_triceps==size)
DanAuhust 0:59e2ea255892 81 count_triceps=0;
DanAuhust 0:59e2ea255892 82 //sizeof(keeper_triceps) / sizeof(float);
DanAuhust 1:1ffb9e3ae00f 83 for(int i=0; i < size; i++){
DanAuhust 0:59e2ea255892 84 sum+=keeper_triceps[i];
DanAuhust 0:59e2ea255892 85 }
DanAuhust 0:59e2ea255892 86 mean=sum/size;
DanAuhust 0:59e2ea255892 87 sum=0;
DanAuhust 1:1ffb9e3ae00f 88 for(int i=0; i < size; i++){
DanAuhust 0:59e2ea255892 89 sum+=(keeper_triceps[i]-mean)*(keeper_triceps[i]-mean);
DanAuhust 0:59e2ea255892 90 }
DanAuhust 0:59e2ea255892 91 sig_out=sqrt(sum/size);
DanAuhust 0:59e2ea255892 92 sum=0;
DanAuhust 0:59e2ea255892 93 break;
DanAuhust 3:e609cd999fd2 94 case 3:
DanAuhust 0:59e2ea255892 95 keeper_flexoren[count_flexoren]=input;
DanAuhust 0:59e2ea255892 96 count_flexoren++;
DanAuhust 0:59e2ea255892 97 if(count_flexoren==size) count_flexoren=0;
DanAuhust 0:59e2ea255892 98
DanAuhust 3:e609cd999fd2 99 //size=sizeof(keeper_flexoren)/sizeof(float);
DanAuhust 3:e609cd999fd2 100 for(int i=0; i < size; i++){
DanAuhust 0:59e2ea255892 101 sum+=keeper_flexoren[i];
DanAuhust 0:59e2ea255892 102 }
DanAuhust 0:59e2ea255892 103 mean=sum/size;
DanAuhust 0:59e2ea255892 104 sum=0;
DanAuhust 3:e609cd999fd2 105 for(int i=0; i < size; i++){
DanAuhust 0:59e2ea255892 106 sum+=(keeper_flexoren[i]-mean)*(keeper_flexoren[i]-mean);
DanAuhust 0:59e2ea255892 107 }
DanAuhust 0:59e2ea255892 108 sig_out=sqrt(sum/size);
DanAuhust 0:59e2ea255892 109 sum=0;
DanAuhust 0:59e2ea255892 110 break;
DanAuhust 0:59e2ea255892 111 case 4:
DanAuhust 0:59e2ea255892 112 keeper_extensoren[count_extensoren]=input;
DanAuhust 0:59e2ea255892 113 count_extensoren++;
DanAuhust 0:59e2ea255892 114 if(count_extensoren==size) count_extensoren=0;
DanAuhust 0:59e2ea255892 115
DanAuhust 3:e609cd999fd2 116 //size=sizeof(keeper_extensoren)/sizeof(float);
DanAuhust 3:e609cd999fd2 117 for(int i=0; i < size; i++){
DanAuhust 0:59e2ea255892 118 sum+=keeper_extensoren[i];
DanAuhust 0:59e2ea255892 119 }
DanAuhust 0:59e2ea255892 120 mean=sum/size;
DanAuhust 0:59e2ea255892 121 sum=0;
DanAuhust 3:e609cd999fd2 122 for(int i=0; i < size; i++){
DanAuhust 0:59e2ea255892 123 sum+=(keeper_extensoren[i]-mean)*(keeper_extensoren[i]-mean);
DanAuhust 0:59e2ea255892 124 }
DanAuhust 0:59e2ea255892 125 sig_out=sqrt(sum/size);
DanAuhust 0:59e2ea255892 126 sum=0;
DanAuhust 3:e609cd999fd2 127 break;
DanAuhust 0:59e2ea255892 128 } // einde switch
DanAuhust 0:59e2ea255892 129 return sig_out;
DanAuhust 0:59e2ea255892 130 } // einde if startcount ...
DanAuhust 0:59e2ea255892 131 else
DanAuhust 0:59e2ea255892 132 {startcount+=1;
DanAuhust 0:59e2ea255892 133 switch(sig_number){
DanAuhust 0:59e2ea255892 134 case 1:
DanAuhust 0:59e2ea255892 135 keeper_biceps[count_biceps]=input;
DanAuhust 0:59e2ea255892 136 count_biceps++;
DanAuhust 0:59e2ea255892 137 if(count_biceps >= size)
DanAuhust 0:59e2ea255892 138 count_biceps=0;
DanAuhust 0:59e2ea255892 139 break;
DanAuhust 0:59e2ea255892 140 case 2:
DanAuhust 0:59e2ea255892 141 keeper_triceps[count_triceps]=input;
DanAuhust 0:59e2ea255892 142 count_triceps++;
DanAuhust 3:e609cd999fd2 143 if(count_triceps >= size)
DanAuhust 0:59e2ea255892 144 count_triceps=0;
DanAuhust 0:59e2ea255892 145 break;
DanAuhust 3:e609cd999fd2 146 case 3:
DanAuhust 3:e609cd999fd2 147 keeper_flexoren[count_flexoren]=input;
DanAuhust 3:e609cd999fd2 148 count_flexoren++;
DanAuhust 3:e609cd999fd2 149 if(count_flexoren >= size)
DanAuhust 3:e609cd999fd2 150 count_flexoren=0;
DanAuhust 3:e609cd999fd2 151 break;
DanAuhust 3:e609cd999fd2 152 case 4:
DanAuhust 3:e609cd999fd2 153 keeper_extensoren[count_extensoren]=input;
DanAuhust 3:e609cd999fd2 154 count_extensoren++;
DanAuhust 3:e609cd999fd2 155 if(count_extensoren >= size)
DanAuhust 3:e609cd999fd2 156 count_extensoren=0;
DanAuhust 3:e609cd999fd2 157 break;
DanAuhust 0:59e2ea255892 158 } // einde switch
DanAuhust 0:59e2ea255892 159 } // einde else
DanAuhust 0:59e2ea255892 160 } // einde std_dev
DanAuhust 0:59e2ea255892 161
DanAuhust 0:59e2ea255892 162 float filter(int sig_number){
DanAuhust 0:59e2ea255892 163 float sig_out;
DanAuhust 0:59e2ea255892 164 // eerst variabelen definieren
DanAuhust 0:59e2ea255892 165
DanAuhust 0:59e2ea255892 166 //biceps
DanAuhust 0:59e2ea255892 167 //filter 1
DanAuhust 0:59e2ea255892 168 float in0_biceps =0;
DanAuhust 0:59e2ea255892 169 static float in1_biceps =0, in2_biceps = 0, in3_biceps = 0, in4_biceps = 0;
DanAuhust 0:59e2ea255892 170 static float out0_biceps = 0, out1_biceps = 0 , out2_biceps = 0, out3_biceps = 0, out4_biceps = 0;
DanAuhust 0:59e2ea255892 171
DanAuhust 0:59e2ea255892 172 //filter 3
DanAuhust 0:59e2ea255892 173 float in0_3_biceps =0;
DanAuhust 0:59e2ea255892 174 static float in1_3_biceps =0, in2_3_biceps = 0, in3_3_biceps = 0, in4_3_biceps = 0;
DanAuhust 0:59e2ea255892 175 static float out0_3_biceps = 0, out1_3_biceps = 0 , out2_3_biceps = 0, out3_3_biceps = 0, out4_3_biceps = 0;
DanAuhust 0:59e2ea255892 176
DanAuhust 0:59e2ea255892 177
DanAuhust 0:59e2ea255892 178 //triceps
DanAuhust 0:59e2ea255892 179 //filter 1
DanAuhust 0:59e2ea255892 180 float in0_triceps =0;
DanAuhust 0:59e2ea255892 181 static float in1_triceps =0, in2_triceps = 0, in3_triceps = 0, in4_triceps = 0;
DanAuhust 0:59e2ea255892 182 static float out0_triceps = 0, out1_triceps = 0 , out2_triceps = 0, out3_triceps = 0, out4_triceps = 0;
DanAuhust 0:59e2ea255892 183
DanAuhust 0:59e2ea255892 184 //filter 3
DanAuhust 0:59e2ea255892 185 float in0_3_triceps =0;
DanAuhust 0:59e2ea255892 186 static float in1_3_triceps =0, in2_3_triceps = 0, in3_3_triceps = 0, in4_3_triceps = 0;
DanAuhust 0:59e2ea255892 187 static float out0_3_triceps = 0, out1_3_triceps = 0 , out2_3_triceps = 0, out3_3_triceps = 0, out4_3_triceps = 0;
DanAuhust 0:59e2ea255892 188
DanAuhust 0:59e2ea255892 189
DanAuhust 0:59e2ea255892 190 //flexoren
DanAuhust 0:59e2ea255892 191 //filter 1
DanAuhust 0:59e2ea255892 192 float in0_flexoren =0;
DanAuhust 0:59e2ea255892 193 static float in1_flexoren =0, in2_flexoren = 0, in3_flexoren = 0, in4_flexoren = 0;
DanAuhust 0:59e2ea255892 194 static float out0_flexoren = 0, out1_flexoren = 0 , out2_flexoren = 0, out3_flexoren = 0, out4_flexoren = 0;
DanAuhust 0:59e2ea255892 195
DanAuhust 0:59e2ea255892 196 //filter 3
DanAuhust 0:59e2ea255892 197 float in0_3_flexoren =0;
DanAuhust 0:59e2ea255892 198 static float in1_3_flexoren =0, in2_3_flexoren = 0, in3_3_flexoren = 0, in4_3_flexoren = 0;
DanAuhust 0:59e2ea255892 199 static float out0_3_flexoren = 0, out1_3_flexoren = 0 , out2_3_flexoren = 0, out3_3_flexoren = 0, out4_3_flexoren = 0;
DanAuhust 0:59e2ea255892 200
DanAuhust 0:59e2ea255892 201
DanAuhust 0:59e2ea255892 202 //extensoren
DanAuhust 0:59e2ea255892 203 //filter 1
DanAuhust 0:59e2ea255892 204 float in0_extensoren =0;
DanAuhust 0:59e2ea255892 205 static float in1_extensoren =0, in2_extensoren = 0, in3_extensoren = 0, in4_extensoren = 0;
DanAuhust 0:59e2ea255892 206 static float out0_extensoren = 0, out1_extensoren = 0 , out2_extensoren = 0, out3_extensoren = 0, out4_extensoren = 0;
DanAuhust 0:59e2ea255892 207
DanAuhust 0:59e2ea255892 208 //filter 3
DanAuhust 0:59e2ea255892 209 float in0_3_extensoren =0;
DanAuhust 0:59e2ea255892 210 static float in1_3_extensoren =0, in2_3_extensoren = 0, in3_3_extensoren = 0, in4_3_extensoren = 0;
DanAuhust 0:59e2ea255892 211 static float out0_3_extensoren = 0, out1_3_extensoren = 0 , out2_3_extensoren = 0, out3_3_extensoren = 0, out4_3_extensoren = 0;
DanAuhust 0:59e2ea255892 212
DanAuhust 0:59e2ea255892 213
DanAuhust 0:59e2ea255892 214
DanAuhust 0:59e2ea255892 215 switch(sig_number){
DanAuhust 0:59e2ea255892 216 case 1:
DanAuhust 0:59e2ea255892 217 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 218 in4_biceps = in3_biceps; in3_biceps = in2_biceps; in2_biceps = in1_biceps; in1_biceps = in0_biceps;
DanAuhust 0:59e2ea255892 219 in0_biceps = emg_biceps.read() - offset_biceps;
DanAuhust 0:59e2ea255892 220 out4_biceps = out3_biceps; out3_biceps = out2_biceps; out2_biceps = out1_biceps; out1_biceps = out0_biceps;
DanAuhust 0:59e2ea255892 221 out0_biceps = (NUM0*in0_biceps + NUM1*in1_biceps + NUM2*in2_biceps + NUM3*in3_biceps + NUM4*in4_biceps - DEN1*out1_biceps - DEN2*out2_biceps - DEN3*out3_biceps - DEN4*out4_biceps ) / DEN0;
DanAuhust 0:59e2ea255892 222 /*
DanAuhust 0:59e2ea255892 223 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 224 in4_3_biceps = in3_3_biceps; in3_3_biceps = in2_3_biceps; in2_3_biceps = in1_3_biceps; in1_3_biceps = in0_3_biceps;
DanAuhust 0:59e2ea255892 225 in0_3_biceps = abs(out0_biceps); // ruw - offset -> filter 1 -> stdev (-> filter 3)
DanAuhust 0:59e2ea255892 226 out4_3_biceps = out3_3_biceps; out3_3_biceps = out2_3_biceps; out2_3_biceps = out1_3_biceps; out1_3_biceps = out0_3_biceps;
DanAuhust 0:59e2ea255892 227 out0_3_biceps = (NUM0_3*in0_3_biceps + NUM1_3*in1_3_biceps + NUM2_3*in2_3_biceps + NUM3_3*in3_3_biceps + NUM4_3*in4_3_biceps - DEN1_3*out1_3_biceps - DEN2_3*out2_3_biceps - DEN3_3*out3_3_biceps - DEN4_3*out4_3_biceps ) / DEN0_3;
DanAuhust 0:59e2ea255892 228 */
DanAuhust 0:59e2ea255892 229
DanAuhust 0:59e2ea255892 230 sig_out = out0_biceps;
DanAuhust 0:59e2ea255892 231 break;
DanAuhust 0:59e2ea255892 232 case 2:
DanAuhust 0:59e2ea255892 233 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 234 in4_triceps = in3_triceps; in3_triceps = in2_triceps; in2_triceps = in1_triceps; in1_triceps = in0_triceps;
DanAuhust 0:59e2ea255892 235 in0_triceps = emg_triceps.read() - offset_biceps;
DanAuhust 0:59e2ea255892 236 out4_triceps = out3_triceps; out3_triceps = out2_triceps; out2_triceps = out1_triceps; out1_triceps = out0_triceps;
DanAuhust 0:59e2ea255892 237 out0_triceps = (NUM0*in0_triceps + NUM1*in1_triceps + NUM2*in2_triceps + NUM3*in3_triceps + NUM4*in4_triceps - DEN1*out1_triceps - DEN2*out2_triceps - DEN3*out3_triceps - DEN4*out4_triceps ) / DEN0;
DanAuhust 0:59e2ea255892 238
DanAuhust 0:59e2ea255892 239
DanAuhust 0:59e2ea255892 240 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 241 /*in4_3_triceps = in3_3_triceps; in3_3_triceps = in2_3_triceps; in2_3_triceps = in1_3_triceps; in1_3_triceps = in0_3_triceps;
DanAuhust 0:59e2ea255892 242 in0_3_triceps = abs(out0_triceps);
DanAuhust 0:59e2ea255892 243 out4_3_triceps = out3_3_triceps; out3_3_triceps = out2_3_triceps; out2_3_triceps = out1_3_triceps; out1_3_triceps = out0_3_triceps;
DanAuhust 0:59e2ea255892 244 out0_3_triceps = (NUM0_3*in0_3_triceps + NUM1_3*in1_3_triceps + NUM2_3*in2_3_triceps + NUM3_3*in3_3_triceps + NUM4_3*in4_3_triceps - DEN1_3*out1_3_triceps - DEN2_3*out2_3_triceps - DEN3_3*out3_3_triceps - DEN4_3*out4_3_triceps ) / DEN0_3;
DanAuhust 0:59e2ea255892 245 */
DanAuhust 0:59e2ea255892 246
DanAuhust 0:59e2ea255892 247 sig_out = out0_triceps;
DanAuhust 0:59e2ea255892 248 break;
DanAuhust 0:59e2ea255892 249 case 3:
DanAuhust 0:59e2ea255892 250 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 251 in4_flexoren = in3_flexoren; in3_flexoren = in2_flexoren; in2_flexoren = in1_flexoren; in1_flexoren = in0_flexoren;
DanAuhust 0:59e2ea255892 252 in0_flexoren = emg_flexoren.read();
DanAuhust 0:59e2ea255892 253 out4_flexoren = out3_flexoren; out3_flexoren = out2_flexoren; out2_flexoren = out1_flexoren; out1_flexoren = out0_flexoren;
DanAuhust 0:59e2ea255892 254 out0_flexoren = (NUM0*in0_flexoren + NUM1*in1_flexoren + NUM2*in2_flexoren + NUM3*in3_flexoren + NUM4*in4_flexoren - DEN1*out1_flexoren - DEN2*out2_flexoren - DEN3*out3_flexoren - DEN4*out4_flexoren ) / DEN0;
DanAuhust 0:59e2ea255892 255 /*
DanAuhust 0:59e2ea255892 256
DanAuhust 0:59e2ea255892 257 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 258 in4_3_flexoren = in3_3_flexoren; in3_3_flexoren = in2_3_flexoren; in2_3_flexoren = in1_3_flexoren; in1_3_flexoren = in0_3_flexoren;
DanAuhust 0:59e2ea255892 259 in0_3_flexoren = abs(out0_2_flexoren);
DanAuhust 0:59e2ea255892 260 out4_3_flexoren = out3_3_flexoren; out3_3_flexoren = out2_3_flexoren; out2_3_flexoren = out1_3_flexoren; out1_3_flexoren = out0_3_flexoren;
DanAuhust 0:59e2ea255892 261 out0_3_flexoren = (NUM0_3*in0_3_flexoren + NUM1_3*in1_3_flexoren + NUM2_3*in2_3_flexoren + NUM3_3*in3_3_flexoren + NUM4_3*in4_3_flexoren - DEN1_3*out1_3_flexoren - DEN2_3*out2_3_flexoren - DEN3_3*out3_3_flexoren - DEN4_3*out4_3_flexoren ) / DEN0_3;
DanAuhust 0:59e2ea255892 262
DanAuhust 0:59e2ea255892 263
DanAuhust 0:59e2ea255892 264 */
DanAuhust 0:59e2ea255892 265 sig_out = out0_flexoren;
DanAuhust 0:59e2ea255892 266 break;
DanAuhust 0:59e2ea255892 267 case 4:
DanAuhust 0:59e2ea255892 268 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 269 in4_extensoren = in3_extensoren; in3_extensoren = in2_extensoren; in2_extensoren = in1_extensoren; in1_extensoren = in0_extensoren;
DanAuhust 0:59e2ea255892 270 in0_extensoren = emg_extensoren.read();
DanAuhust 0:59e2ea255892 271 out4_extensoren = out3_extensoren; out3_extensoren = out2_extensoren; out2_extensoren = out1_extensoren; out1_extensoren = out0_extensoren;
DanAuhust 0:59e2ea255892 272 out0_extensoren = (NUM0*in0_extensoren + NUM1*in1_extensoren + NUM2*in2_extensoren + NUM3*in3_extensoren + NUM4*in4_extensoren - DEN1*out1_extensoren - DEN2*out2_extensoren - DEN3*out3_extensoren - DEN4*out4_extensoren ) / DEN0;
DanAuhust 0:59e2ea255892 273 /*
DanAuhust 0:59e2ea255892 274
DanAuhust 0:59e2ea255892 275 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 276 in4_3_extensoren = in3_3_extensoren; in3_3_extensoren = in2_3_extensoren; in2_3_extensoren = in1_3_extensoren; in1_3_extensoren = in0_3_extensoren;
DanAuhust 0:59e2ea255892 277 in0_3_extensoren = abs(out0_2_extensoren);
DanAuhust 0:59e2ea255892 278 out4_3_extensoren = out3_3_extensoren; out3_3_extensoren = out2_3_extensoren; out2_3_extensoren = out1_3_extensoren; out1_3_extensoren = out0_3_extensoren;
DanAuhust 0:59e2ea255892 279 out0_3_extensoren = (NUM0_3*in0_3_extensoren + NUM1_3*in1_3_extensoren + NUM2_3*in2_3_extensoren + NUM3_3*in3_3_extensoren + NUM4_3*in4_3_extensoren - DEN1_3*out1_3_extensoren - DEN2_3*out2_3_extensoren - DEN3_3*out3_3_extensoren - DEN4_3*out4_3_extensoren ) / DEN0_3;
DanAuhust 0:59e2ea255892 280
DanAuhust 0:59e2ea255892 281
DanAuhust 0:59e2ea255892 282 */
DanAuhust 0:59e2ea255892 283 sig_out = out0_extensoren;
DanAuhust 0:59e2ea255892 284 break;
DanAuhust 0:59e2ea255892 285 }
DanAuhust 0:59e2ea255892 286 return sig_out;
DanAuhust 0:59e2ea255892 287 }
DanAuhust 0:59e2ea255892 288
DanAuhust 0:59e2ea255892 289 void looper()
DanAuhust 0:59e2ea255892 290 { float emg_value_biceps;
DanAuhust 0:59e2ea255892 291 float emg_value_triceps;
DanAuhust 0:59e2ea255892 292 float emg_value_flexoren;
DanAuhust 0:59e2ea255892 293 float emg_value_extensoren;
DanAuhust 0:59e2ea255892 294 float dy;
DanAuhust 0:59e2ea255892 295 //static int sig_count = 1;
DanAuhust 0:59e2ea255892 296 emg_value_biceps=std_dev(filter(1),1);
DanAuhust 3:e609cd999fd2 297 emg_value_triceps=std_dev(filter(2),2);
DanAuhust 3:e609cd999fd2 298 /*emg_value_flexoren = (100*filter(3)-44);
DanAuhust 3:e609cd999fd2 299 emg_value_extensoren = (100*filter(4)-44);*/
DanAuhust 3:e609cd999fd2 300
DanAuhust 3:e609cd999fd2 301 if(emg_value_biceps < 4.5)
DanAuhust 3:e609cd999fd2 302 emg_value_biceps=0;
DanAuhust 3:e609cd999fd2 303 else if (emg_value_biceps > 13)
DanAuhust 3:e609cd999fd2 304 emg_value_biceps = 13;
DanAuhust 3:e609cd999fd2 305 //emg_value_biceps = emg_value_biceps;
DanAuhust 0:59e2ea255892 306
DanAuhust 3:e609cd999fd2 307 if(emg_value_triceps < 2.5)
DanAuhust 0:59e2ea255892 308 emg_value_biceps=0;
DanAuhust 3:e609cd999fd2 309 else if (emg_value_biceps > 10)
DanAuhust 3:e609cd999fd2 310 emg_value_biceps = 10;
DanAuhust 3:e609cd999fd2 311 //emg_value_triceps = emg_value_triceps;
DanAuhust 3:e609cd999fd2 312
DanAuhust 3:e609cd999fd2 313 if(emg_value_flexoren < 2)
DanAuhust 3:e609cd999fd2 314 emg_value_flexoren=0;
DanAuhust 3:e609cd999fd2 315 else if (emg_value_flexoren > 8)
DanAuhust 3:e609cd999fd2 316 emg_value_flexoren = 8;
DanAuhust 3:e609cd999fd2 317 emg_value_flexoren = 2*emg_value_flexoren;
DanAuhust 3:e609cd999fd2 318
DanAuhust 3:e609cd999fd2 319 if(emg_value_extensoren < 5)
DanAuhust 3:e609cd999fd2 320 emg_value_extensoren=0;
DanAuhust 3:e609cd999fd2 321 else if (emg_value_extensoren > 13)
DanAuhust 3:e609cd999fd2 322 emg_value_extensoren = 13;
DanAuhust 3:e609cd999fd2 323
DanAuhust 3:e609cd999fd2 324 dy = emg_value_biceps - emg_value_triceps;
DanAuhust 3:e609cd999fd2 325 dx = 2*(emg_value_flexoren - emg_value_extensoren);
DanAuhust 3:e609cd999fd2 326
DanAuhust 0:59e2ea255892 327 if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30)
DanAuhust 1:1ffb9e3ae00f 328 pc.printf("%.6f, %.6f, %.6f\n",emg_value_biceps, emg_value_triceps, dy);
DanAuhust 0:59e2ea255892 329 /**When not using the LED, the above could also have been done this way:
DanAuhust 0:59e2ea255892 330 * pc.printf("%.6\n", emg0.read());
DanAuhust 0:59e2ea255892 331 */
DanAuhust 0:59e2ea255892 332 }
DanAuhust 0:59e2ea255892 333
DanAuhust 0:59e2ea255892 334 int main()
DanAuhust 0:59e2ea255892 335 {
DanAuhust 0:59e2ea255892 336 /*setup baudrate. Choose the same in your program on PC side*/
DanAuhust 0:59e2ea255892 337 pc.baud(115200);
DanAuhust 0:59e2ea255892 338 /*set the period for the PWM to the red LED*/
DanAuhust 0:59e2ea255892 339 red.period_ms(2);
DanAuhust 0:59e2ea255892 340 /**Here you attach the 'void looper(void)' function to the Ticker object
DanAuhust 0:59e2ea255892 341 * The looper() function will be called every 1/Fs seconds.
DanAuhust 0:59e2ea255892 342 * Please mind that the parentheses after looper are omitted when using attach.
DanAuhust 0:59e2ea255892 343 */
DanAuhust 0:59e2ea255892 344 timer.attach(looper,0.0032); //invullen in seconde. .0032 is niet eens afgerond, dus vandaar die frequentie.
DanAuhust 0:59e2ea255892 345 while(1) //Loop
DanAuhust 0:59e2ea255892 346 {
DanAuhust 0:59e2ea255892 347 /*Empty!*/
DanAuhust 0:59e2ea255892 348 /*Everything is handled by the interrupt routine now!*/
DanAuhust 0:59e2ea255892 349 }
DanAuhust 0:59e2ea255892 350 }