Werkt. (hopelijk)

Dependencies:   MODSERIAL mbed

Committer:
DanAuhust
Date:
Mon Nov 04 13:26:18 2013 +0000
Revision:
1:1ffb9e3ae00f
Parent:
0:59e2ea255892
Child:
2:e609cd999fd2
Werkt nu

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 0:59e2ea255892 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 0:59e2ea255892 99 size=sizeof(keeper_flexoren)/sizeof(float);
DanAuhust 0:59e2ea255892 100 for(int i; 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 0:59e2ea255892 105 for(int i; 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 0:59e2ea255892 116 size=sizeof(keeper_extensoren)/sizeof(float);
DanAuhust 0:59e2ea255892 117 for(int i; 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 0:59e2ea255892 122 for(int i; 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 0:59e2ea255892 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 0:59e2ea255892 143 if(count_triceps>=size)
DanAuhust 0:59e2ea255892 144 count_triceps=0;
DanAuhust 0:59e2ea255892 145 break;
DanAuhust 0:59e2ea255892 146 } // einde switch
DanAuhust 0:59e2ea255892 147 } // einde else
DanAuhust 0:59e2ea255892 148 } // einde std_dev
DanAuhust 0:59e2ea255892 149
DanAuhust 0:59e2ea255892 150 float filter(int sig_number){
DanAuhust 0:59e2ea255892 151 float sig_out;
DanAuhust 0:59e2ea255892 152 // eerst variabelen definieren
DanAuhust 0:59e2ea255892 153
DanAuhust 0:59e2ea255892 154 //biceps
DanAuhust 0:59e2ea255892 155 //filter 1
DanAuhust 0:59e2ea255892 156 float in0_biceps =0;
DanAuhust 0:59e2ea255892 157 static float in1_biceps =0, in2_biceps = 0, in3_biceps = 0, in4_biceps = 0;
DanAuhust 0:59e2ea255892 158 static float out0_biceps = 0, out1_biceps = 0 , out2_biceps = 0, out3_biceps = 0, out4_biceps = 0;
DanAuhust 0:59e2ea255892 159
DanAuhust 0:59e2ea255892 160 //filter 3
DanAuhust 0:59e2ea255892 161 float in0_3_biceps =0;
DanAuhust 0:59e2ea255892 162 static float in1_3_biceps =0, in2_3_biceps = 0, in3_3_biceps = 0, in4_3_biceps = 0;
DanAuhust 0:59e2ea255892 163 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 164
DanAuhust 0:59e2ea255892 165
DanAuhust 0:59e2ea255892 166 //triceps
DanAuhust 0:59e2ea255892 167 //filter 1
DanAuhust 0:59e2ea255892 168 float in0_triceps =0;
DanAuhust 0:59e2ea255892 169 static float in1_triceps =0, in2_triceps = 0, in3_triceps = 0, in4_triceps = 0;
DanAuhust 0:59e2ea255892 170 static float out0_triceps = 0, out1_triceps = 0 , out2_triceps = 0, out3_triceps = 0, out4_triceps = 0;
DanAuhust 0:59e2ea255892 171
DanAuhust 0:59e2ea255892 172 //filter 3
DanAuhust 0:59e2ea255892 173 float in0_3_triceps =0;
DanAuhust 0:59e2ea255892 174 static float in1_3_triceps =0, in2_3_triceps = 0, in3_3_triceps = 0, in4_3_triceps = 0;
DanAuhust 0:59e2ea255892 175 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 176
DanAuhust 0:59e2ea255892 177
DanAuhust 0:59e2ea255892 178 //flexoren
DanAuhust 0:59e2ea255892 179 //filter 1
DanAuhust 0:59e2ea255892 180 float in0_flexoren =0;
DanAuhust 0:59e2ea255892 181 static float in1_flexoren =0, in2_flexoren = 0, in3_flexoren = 0, in4_flexoren = 0;
DanAuhust 0:59e2ea255892 182 static float out0_flexoren = 0, out1_flexoren = 0 , out2_flexoren = 0, out3_flexoren = 0, out4_flexoren = 0;
DanAuhust 0:59e2ea255892 183
DanAuhust 0:59e2ea255892 184 //filter 3
DanAuhust 0:59e2ea255892 185 float in0_3_flexoren =0;
DanAuhust 0:59e2ea255892 186 static float in1_3_flexoren =0, in2_3_flexoren = 0, in3_3_flexoren = 0, in4_3_flexoren = 0;
DanAuhust 0:59e2ea255892 187 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 188
DanAuhust 0:59e2ea255892 189
DanAuhust 0:59e2ea255892 190 //extensoren
DanAuhust 0:59e2ea255892 191 //filter 1
DanAuhust 0:59e2ea255892 192 float in0_extensoren =0;
DanAuhust 0:59e2ea255892 193 static float in1_extensoren =0, in2_extensoren = 0, in3_extensoren = 0, in4_extensoren = 0;
DanAuhust 0:59e2ea255892 194 static float out0_extensoren = 0, out1_extensoren = 0 , out2_extensoren = 0, out3_extensoren = 0, out4_extensoren = 0;
DanAuhust 0:59e2ea255892 195
DanAuhust 0:59e2ea255892 196 //filter 3
DanAuhust 0:59e2ea255892 197 float in0_3_extensoren =0;
DanAuhust 0:59e2ea255892 198 static float in1_3_extensoren =0, in2_3_extensoren = 0, in3_3_extensoren = 0, in4_3_extensoren = 0;
DanAuhust 0:59e2ea255892 199 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 200
DanAuhust 0:59e2ea255892 201
DanAuhust 0:59e2ea255892 202
DanAuhust 0:59e2ea255892 203 switch(sig_number){
DanAuhust 0:59e2ea255892 204 case 1:
DanAuhust 0:59e2ea255892 205 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 206 in4_biceps = in3_biceps; in3_biceps = in2_biceps; in2_biceps = in1_biceps; in1_biceps = in0_biceps;
DanAuhust 0:59e2ea255892 207 in0_biceps = emg_biceps.read() - offset_biceps;
DanAuhust 0:59e2ea255892 208 out4_biceps = out3_biceps; out3_biceps = out2_biceps; out2_biceps = out1_biceps; out1_biceps = out0_biceps;
DanAuhust 0:59e2ea255892 209 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 210 /*
DanAuhust 0:59e2ea255892 211 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 212 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 213 in0_3_biceps = abs(out0_biceps); // ruw - offset -> filter 1 -> stdev (-> filter 3)
DanAuhust 0:59e2ea255892 214 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 215 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 216 */
DanAuhust 0:59e2ea255892 217
DanAuhust 0:59e2ea255892 218 sig_out = out0_biceps;
DanAuhust 0:59e2ea255892 219 break;
DanAuhust 0:59e2ea255892 220 case 2:
DanAuhust 0:59e2ea255892 221 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 222 in4_triceps = in3_triceps; in3_triceps = in2_triceps; in2_triceps = in1_triceps; in1_triceps = in0_triceps;
DanAuhust 0:59e2ea255892 223 in0_triceps = emg_triceps.read() - offset_biceps;
DanAuhust 0:59e2ea255892 224 out4_triceps = out3_triceps; out3_triceps = out2_triceps; out2_triceps = out1_triceps; out1_triceps = out0_triceps;
DanAuhust 0:59e2ea255892 225 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 226
DanAuhust 0:59e2ea255892 227
DanAuhust 0:59e2ea255892 228 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 229 /*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 230 in0_3_triceps = abs(out0_triceps);
DanAuhust 0:59e2ea255892 231 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 232 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 233 */
DanAuhust 0:59e2ea255892 234
DanAuhust 0:59e2ea255892 235 sig_out = out0_triceps;
DanAuhust 0:59e2ea255892 236 break;
DanAuhust 0:59e2ea255892 237 case 3:
DanAuhust 0:59e2ea255892 238 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 239 in4_flexoren = in3_flexoren; in3_flexoren = in2_flexoren; in2_flexoren = in1_flexoren; in1_flexoren = in0_flexoren;
DanAuhust 0:59e2ea255892 240 in0_flexoren = emg_flexoren.read();
DanAuhust 0:59e2ea255892 241 out4_flexoren = out3_flexoren; out3_flexoren = out2_flexoren; out2_flexoren = out1_flexoren; out1_flexoren = out0_flexoren;
DanAuhust 0:59e2ea255892 242 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 243 /*
DanAuhust 0:59e2ea255892 244
DanAuhust 0:59e2ea255892 245 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 246 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 247 in0_3_flexoren = abs(out0_2_flexoren);
DanAuhust 0:59e2ea255892 248 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 249 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 250
DanAuhust 0:59e2ea255892 251
DanAuhust 0:59e2ea255892 252 */
DanAuhust 0:59e2ea255892 253 sig_out = out0_flexoren;
DanAuhust 0:59e2ea255892 254 break;
DanAuhust 0:59e2ea255892 255 case 4:
DanAuhust 0:59e2ea255892 256 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 257 in4_extensoren = in3_extensoren; in3_extensoren = in2_extensoren; in2_extensoren = in1_extensoren; in1_extensoren = in0_extensoren;
DanAuhust 0:59e2ea255892 258 in0_extensoren = emg_extensoren.read();
DanAuhust 0:59e2ea255892 259 out4_extensoren = out3_extensoren; out3_extensoren = out2_extensoren; out2_extensoren = out1_extensoren; out1_extensoren = out0_extensoren;
DanAuhust 0:59e2ea255892 260 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 261 /*
DanAuhust 0:59e2ea255892 262
DanAuhust 0:59e2ea255892 263 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 264 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 265 in0_3_extensoren = abs(out0_2_extensoren);
DanAuhust 0:59e2ea255892 266 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 267 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 268
DanAuhust 0:59e2ea255892 269
DanAuhust 0:59e2ea255892 270 */
DanAuhust 0:59e2ea255892 271 sig_out = out0_extensoren;
DanAuhust 0:59e2ea255892 272 break;
DanAuhust 0:59e2ea255892 273 }
DanAuhust 0:59e2ea255892 274 return sig_out;
DanAuhust 0:59e2ea255892 275 }
DanAuhust 0:59e2ea255892 276
DanAuhust 0:59e2ea255892 277 void looper()
DanAuhust 0:59e2ea255892 278 { float emg_value_biceps;
DanAuhust 0:59e2ea255892 279 float emg_value_triceps;
DanAuhust 0:59e2ea255892 280 float emg_value_flexoren;
DanAuhust 0:59e2ea255892 281 float emg_value_extensoren;
DanAuhust 0:59e2ea255892 282 float dy;
DanAuhust 0:59e2ea255892 283 //static int sig_count = 1;
DanAuhust 0:59e2ea255892 284 emg_value_biceps=std_dev(filter(1),1);
DanAuhust 0:59e2ea255892 285 emg_value_triceps=std_dev(filter(2),2);
DanAuhust 1:1ffb9e3ae00f 286 dy = emg_value_biceps-emg_value_triceps;
DanAuhust 0:59e2ea255892 287
DanAuhust 0:59e2ea255892 288
DanAuhust 0:59e2ea255892 289 /*emg_value_flexoren = (100*filter(3)-44);
DanAuhust 0:59e2ea255892 290 emg_value_extensoren = (100*filter(4)-44);*/
DanAuhust 0:59e2ea255892 291
DanAuhust 0:59e2ea255892 292 /*if(emg_value_biceps < 0.10){
DanAuhust 0:59e2ea255892 293 emg_value_biceps=0;
DanAuhust 0:59e2ea255892 294 }
DanAuhust 0:59e2ea255892 295 else {
DanAuhust 0:59e2ea255892 296 emg_value_biceps = emg_value_biceps;
DanAuhust 0:59e2ea255892 297 }
DanAuhust 0:59e2ea255892 298 if(emg_value_triceps < 0.10){
DanAuhust 0:59e2ea255892 299 emg_value_triceps=0;
DanAuhust 0:59e2ea255892 300 }
DanAuhust 0:59e2ea255892 301 else {
DanAuhust 0:59e2ea255892 302 emg_value_triceps=emg_value_triceps;
DanAuhust 0:59e2ea255892 303 }
DanAuhust 0:59e2ea255892 304 */
DanAuhust 0:59e2ea255892 305 //dy = emg_value_biceps-emg_value_triceps;
DanAuhust 0:59e2ea255892 306 if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30)
DanAuhust 1:1ffb9e3ae00f 307 pc.printf("%.6f, %.6f, %.6f\n",emg_value_biceps, emg_value_triceps, dy);
DanAuhust 0:59e2ea255892 308 /**When not using the LED, the above could also have been done this way:
DanAuhust 0:59e2ea255892 309 * pc.printf("%.6\n", emg0.read());
DanAuhust 0:59e2ea255892 310 */
DanAuhust 0:59e2ea255892 311 }
DanAuhust 0:59e2ea255892 312
DanAuhust 0:59e2ea255892 313 int main()
DanAuhust 0:59e2ea255892 314 {
DanAuhust 0:59e2ea255892 315 /*setup baudrate. Choose the same in your program on PC side*/
DanAuhust 0:59e2ea255892 316 pc.baud(115200);
DanAuhust 0:59e2ea255892 317 /*set the period for the PWM to the red LED*/
DanAuhust 0:59e2ea255892 318 red.period_ms(2);
DanAuhust 0:59e2ea255892 319 /**Here you attach the 'void looper(void)' function to the Ticker object
DanAuhust 0:59e2ea255892 320 * The looper() function will be called every 1/Fs seconds.
DanAuhust 0:59e2ea255892 321 * Please mind that the parentheses after looper are omitted when using attach.
DanAuhust 0:59e2ea255892 322 */
DanAuhust 0:59e2ea255892 323 timer.attach(looper,0.0032); //invullen in seconde. .0032 is niet eens afgerond, dus vandaar die frequentie.
DanAuhust 0:59e2ea255892 324 while(1) //Loop
DanAuhust 0:59e2ea255892 325 {
DanAuhust 0:59e2ea255892 326 /*Empty!*/
DanAuhust 0:59e2ea255892 327 /*Everything is handled by the interrupt routine now!*/
DanAuhust 0:59e2ea255892 328 }
DanAuhust 0:59e2ea255892 329 }