nu met gains, threshold en eerst opvullen

Dependencies:   MODSERIAL mbed

Fork of EMGStdevV3 by Jorick Leferink

Committer:
jorick92
Date:
Tue Nov 05 07:53:04 2013 +0000
Revision:
2:56230ff9fc8f
Parent:
1:1ffb9e3ae00f
std_dev v3

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
jorick92 2:56230ff9fc8f 41 double std_dev(float value, int number) {
jorick92 2:56230ff9fc8f 42 int n;
jorick92 2:56230ff9fc8f 43 float sum;
jorick92 2:56230ff9fc8f 44 float sq_sum;
jorick92 2:56230ff9fc8f 45 float mean;
jorick92 2:56230ff9fc8f 46 float variance;
DanAuhust 0:59e2ea255892 47 static int count_biceps=0;
DanAuhust 0:59e2ea255892 48 static int count_triceps=0;
DanAuhust 0:59e2ea255892 49 static int count_flexoren=0;
DanAuhust 0:59e2ea255892 50 static int count_extensoren=0;
DanAuhust 0:59e2ea255892 51 static float keeper_biceps[20];
DanAuhust 0:59e2ea255892 52 static float keeper_triceps[20];
DanAuhust 0:59e2ea255892 53 static float keeper_flexoren[20];
DanAuhust 0:59e2ea255892 54 static float keeper_extensoren[20];
DanAuhust 0:59e2ea255892 55
jorick92 2:56230ff9fc8f 56 switch (number){
DanAuhust 0:59e2ea255892 57 case 1:
jorick92 2:56230ff9fc8f 58 int n = sizeof(keeper_biceps)/sizeof(float);
jorick92 2:56230ff9fc8f 59 keeper_biceps[count_biceps]=value;
DanAuhust 0:59e2ea255892 60 count_biceps++;
jorick92 2:56230ff9fc8f 61 if ( count_biceps >= n)
jorick92 2:56230ff9fc8f 62 count_biceps = 0;
jorick92 2:56230ff9fc8f 63 double sum = 0;
jorick92 2:56230ff9fc8f 64 double sq_sum = 0;
jorick92 2:56230ff9fc8f 65 for(int i = 0; i < n; ++i) {
jorick92 2:56230ff9fc8f 66 sum += keeper_biceps[i];
jorick92 2:56230ff9fc8f 67 sq_sum += keeper_biceps[i] * keeper_biceps[i];
DanAuhust 0:59e2ea255892 68 }
jorick92 2:56230ff9fc8f 69 double mean = sum / n;
jorick92 2:56230ff9fc8f 70 double variance = sq_sum / n - mean * mean;
jorick92 2:56230ff9fc8f 71 return sqrt(variance);
DanAuhust 0:59e2ea255892 72 break;
DanAuhust 0:59e2ea255892 73 case 2:
jorick92 2:56230ff9fc8f 74 n = sizeof(keeper_triceps)/sizeof(float);
jorick92 2:56230ff9fc8f 75 keeper_triceps[count_triceps]=value;
DanAuhust 0:59e2ea255892 76 count_triceps++;
jorick92 2:56230ff9fc8f 77 if ( count_triceps >= n)
jorick92 2:56230ff9fc8f 78 count_triceps = 0;
jorick92 2:56230ff9fc8f 79 sum = 0;
jorick92 2:56230ff9fc8f 80 sq_sum = 0;
jorick92 2:56230ff9fc8f 81 for(int i = 0; i < n; ++i) {
jorick92 2:56230ff9fc8f 82 sum += keeper_triceps[i];
jorick92 2:56230ff9fc8f 83 sq_sum += keeper_triceps[i] * keeper_triceps[i];
DanAuhust 0:59e2ea255892 84 }
jorick92 2:56230ff9fc8f 85 mean = sum / n;
jorick92 2:56230ff9fc8f 86 variance = sq_sum / n - mean * mean;
jorick92 2:56230ff9fc8f 87 return sqrt(variance);
DanAuhust 0:59e2ea255892 88 break;
jorick92 2:56230ff9fc8f 89 case 3:
jorick92 2:56230ff9fc8f 90 n = sizeof(keeper_flexoren)/sizeof(float);
jorick92 2:56230ff9fc8f 91 keeper_flexoren[count_flexoren]=value;
DanAuhust 0:59e2ea255892 92 count_flexoren++;
jorick92 2:56230ff9fc8f 93 if ( count_flexoren >= n)
jorick92 2:56230ff9fc8f 94 count_flexoren = 0;
jorick92 2:56230ff9fc8f 95 sum = 0;
jorick92 2:56230ff9fc8f 96 sq_sum = 0;
jorick92 2:56230ff9fc8f 97 for(int i = 0; i < n; ++i) {
jorick92 2:56230ff9fc8f 98 sum += keeper_flexoren[i];
jorick92 2:56230ff9fc8f 99 sq_sum += keeper_flexoren[i] * keeper_flexoren[i];
DanAuhust 0:59e2ea255892 100 }
jorick92 2:56230ff9fc8f 101 mean = sum / n;
jorick92 2:56230ff9fc8f 102 variance = sq_sum / n - mean * mean;
jorick92 2:56230ff9fc8f 103 return sqrt(variance);
DanAuhust 0:59e2ea255892 104 break;
DanAuhust 0:59e2ea255892 105 case 4:
jorick92 2:56230ff9fc8f 106 n = sizeof(keeper_extensoren)/sizeof(float);
jorick92 2:56230ff9fc8f 107 keeper_triceps[count_extensoren]=value;
DanAuhust 0:59e2ea255892 108 count_extensoren++;
jorick92 2:56230ff9fc8f 109 if ( count_extensoren >= n)
jorick92 2:56230ff9fc8f 110 count_extensoren = 0;
jorick92 2:56230ff9fc8f 111 sum = 0;
jorick92 2:56230ff9fc8f 112 sq_sum = 0;
jorick92 2:56230ff9fc8f 113 for(int i = 0; i < n; ++i) {
jorick92 2:56230ff9fc8f 114 sum += keeper_extensoren[i];
jorick92 2:56230ff9fc8f 115 sq_sum += keeper_extensoren[i] * keeper_extensoren[i];
DanAuhust 0:59e2ea255892 116 }
jorick92 2:56230ff9fc8f 117 mean = sum / n;
jorick92 2:56230ff9fc8f 118 variance = sq_sum / n - mean * mean;
jorick92 2:56230ff9fc8f 119 return sqrt(variance);
jorick92 2:56230ff9fc8f 120 break;
jorick92 2:56230ff9fc8f 121 }
jorick92 2:56230ff9fc8f 122
jorick92 2:56230ff9fc8f 123
jorick92 2:56230ff9fc8f 124 }
jorick92 2:56230ff9fc8f 125
DanAuhust 0:59e2ea255892 126 float filter(int sig_number){
DanAuhust 0:59e2ea255892 127 float sig_out;
DanAuhust 0:59e2ea255892 128 // eerst variabelen definieren
DanAuhust 0:59e2ea255892 129
DanAuhust 0:59e2ea255892 130 //biceps
DanAuhust 0:59e2ea255892 131 //filter 1
DanAuhust 0:59e2ea255892 132 float in0_biceps =0;
DanAuhust 0:59e2ea255892 133 static float in1_biceps =0, in2_biceps = 0, in3_biceps = 0, in4_biceps = 0;
DanAuhust 0:59e2ea255892 134 static float out0_biceps = 0, out1_biceps = 0 , out2_biceps = 0, out3_biceps = 0, out4_biceps = 0;
DanAuhust 0:59e2ea255892 135
DanAuhust 0:59e2ea255892 136 //filter 3
DanAuhust 0:59e2ea255892 137 float in0_3_biceps =0;
DanAuhust 0:59e2ea255892 138 static float in1_3_biceps =0, in2_3_biceps = 0, in3_3_biceps = 0, in4_3_biceps = 0;
DanAuhust 0:59e2ea255892 139 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 140
DanAuhust 0:59e2ea255892 141
DanAuhust 0:59e2ea255892 142 //triceps
DanAuhust 0:59e2ea255892 143 //filter 1
DanAuhust 0:59e2ea255892 144 float in0_triceps =0;
DanAuhust 0:59e2ea255892 145 static float in1_triceps =0, in2_triceps = 0, in3_triceps = 0, in4_triceps = 0;
DanAuhust 0:59e2ea255892 146 static float out0_triceps = 0, out1_triceps = 0 , out2_triceps = 0, out3_triceps = 0, out4_triceps = 0;
DanAuhust 0:59e2ea255892 147
DanAuhust 0:59e2ea255892 148 //filter 3
DanAuhust 0:59e2ea255892 149 float in0_3_triceps =0;
DanAuhust 0:59e2ea255892 150 static float in1_3_triceps =0, in2_3_triceps = 0, in3_3_triceps = 0, in4_3_triceps = 0;
DanAuhust 0:59e2ea255892 151 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 152
DanAuhust 0:59e2ea255892 153
DanAuhust 0:59e2ea255892 154 //flexoren
DanAuhust 0:59e2ea255892 155 //filter 1
DanAuhust 0:59e2ea255892 156 float in0_flexoren =0;
DanAuhust 0:59e2ea255892 157 static float in1_flexoren =0, in2_flexoren = 0, in3_flexoren = 0, in4_flexoren = 0;
DanAuhust 0:59e2ea255892 158 static float out0_flexoren = 0, out1_flexoren = 0 , out2_flexoren = 0, out3_flexoren = 0, out4_flexoren = 0;
DanAuhust 0:59e2ea255892 159
DanAuhust 0:59e2ea255892 160 //filter 3
DanAuhust 0:59e2ea255892 161 float in0_3_flexoren =0;
DanAuhust 0:59e2ea255892 162 static float in1_3_flexoren =0, in2_3_flexoren = 0, in3_3_flexoren = 0, in4_3_flexoren = 0;
DanAuhust 0:59e2ea255892 163 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 164
DanAuhust 0:59e2ea255892 165
DanAuhust 0:59e2ea255892 166 //extensoren
DanAuhust 0:59e2ea255892 167 //filter 1
DanAuhust 0:59e2ea255892 168 float in0_extensoren =0;
DanAuhust 0:59e2ea255892 169 static float in1_extensoren =0, in2_extensoren = 0, in3_extensoren = 0, in4_extensoren = 0;
DanAuhust 0:59e2ea255892 170 static float out0_extensoren = 0, out1_extensoren = 0 , out2_extensoren = 0, out3_extensoren = 0, out4_extensoren = 0;
DanAuhust 0:59e2ea255892 171
DanAuhust 0:59e2ea255892 172 //filter 3
DanAuhust 0:59e2ea255892 173 float in0_3_extensoren =0;
DanAuhust 0:59e2ea255892 174 static float in1_3_extensoren =0, in2_3_extensoren = 0, in3_3_extensoren = 0, in4_3_extensoren = 0;
DanAuhust 0:59e2ea255892 175 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 176
DanAuhust 0:59e2ea255892 177
DanAuhust 0:59e2ea255892 178
DanAuhust 0:59e2ea255892 179 switch(sig_number){
DanAuhust 0:59e2ea255892 180 case 1:
DanAuhust 0:59e2ea255892 181 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 182 in4_biceps = in3_biceps; in3_biceps = in2_biceps; in2_biceps = in1_biceps; in1_biceps = in0_biceps;
DanAuhust 0:59e2ea255892 183 in0_biceps = emg_biceps.read() - offset_biceps;
DanAuhust 0:59e2ea255892 184 out4_biceps = out3_biceps; out3_biceps = out2_biceps; out2_biceps = out1_biceps; out1_biceps = out0_biceps;
DanAuhust 0:59e2ea255892 185 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;
jorick92 2:56230ff9fc8f 186
DanAuhust 0:59e2ea255892 187 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 188 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 189 in0_3_biceps = abs(out0_biceps); // ruw - offset -> filter 1 -> stdev (-> filter 3)
DanAuhust 0:59e2ea255892 190 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 191 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;
jorick92 2:56230ff9fc8f 192
DanAuhust 0:59e2ea255892 193
jorick92 2:56230ff9fc8f 194 sig_out = out0_3_biceps;
DanAuhust 0:59e2ea255892 195 break;
DanAuhust 0:59e2ea255892 196 case 2:
DanAuhust 0:59e2ea255892 197 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 198 in4_triceps = in3_triceps; in3_triceps = in2_triceps; in2_triceps = in1_triceps; in1_triceps = in0_triceps;
DanAuhust 0:59e2ea255892 199 in0_triceps = emg_triceps.read() - offset_biceps;
DanAuhust 0:59e2ea255892 200 out4_triceps = out3_triceps; out3_triceps = out2_triceps; out2_triceps = out1_triceps; out1_triceps = out0_triceps;
DanAuhust 0:59e2ea255892 201 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 202
DanAuhust 0:59e2ea255892 203
DanAuhust 0:59e2ea255892 204 //signaal filteren op 5Hz LOWPASS
jorick92 2:56230ff9fc8f 205 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 206 in0_3_triceps = abs(out0_triceps);
DanAuhust 0:59e2ea255892 207 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 208 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;
jorick92 2:56230ff9fc8f 209
DanAuhust 0:59e2ea255892 210
jorick92 2:56230ff9fc8f 211 sig_out = out0_3_triceps;
DanAuhust 0:59e2ea255892 212 break;
DanAuhust 0:59e2ea255892 213 case 3:
DanAuhust 0:59e2ea255892 214 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 215 in4_flexoren = in3_flexoren; in3_flexoren = in2_flexoren; in2_flexoren = in1_flexoren; in1_flexoren = in0_flexoren;
DanAuhust 0:59e2ea255892 216 in0_flexoren = emg_flexoren.read();
DanAuhust 0:59e2ea255892 217 out4_flexoren = out3_flexoren; out3_flexoren = out2_flexoren; out2_flexoren = out1_flexoren; out1_flexoren = out0_flexoren;
DanAuhust 0:59e2ea255892 218 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;
jorick92 2:56230ff9fc8f 219
DanAuhust 0:59e2ea255892 220
DanAuhust 0:59e2ea255892 221 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 222 in4_3_flexoren = in3_3_flexoren; in3_3_flexoren = in2_3_flexoren; in2_3_flexoren = in1_3_flexoren; in1_3_flexoren = in0_3_flexoren;
jorick92 2:56230ff9fc8f 223 in0_3_flexoren = abs(out0_flexoren);
DanAuhust 0:59e2ea255892 224 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 225 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 226
DanAuhust 0:59e2ea255892 227
jorick92 2:56230ff9fc8f 228
jorick92 2:56230ff9fc8f 229 sig_out = out0_3_flexoren;
DanAuhust 0:59e2ea255892 230 break;
DanAuhust 0:59e2ea255892 231 case 4:
DanAuhust 0:59e2ea255892 232 // signaal filteren op 15 Hz HIGHPASS
DanAuhust 0:59e2ea255892 233 in4_extensoren = in3_extensoren; in3_extensoren = in2_extensoren; in2_extensoren = in1_extensoren; in1_extensoren = in0_extensoren;
DanAuhust 0:59e2ea255892 234 in0_extensoren = emg_extensoren.read();
DanAuhust 0:59e2ea255892 235 out4_extensoren = out3_extensoren; out3_extensoren = out2_extensoren; out2_extensoren = out1_extensoren; out1_extensoren = out0_extensoren;
DanAuhust 0:59e2ea255892 236 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;
jorick92 2:56230ff9fc8f 237
DanAuhust 0:59e2ea255892 238
DanAuhust 0:59e2ea255892 239 //signaal filteren op 5Hz LOWPASS
DanAuhust 0:59e2ea255892 240 in4_3_extensoren = in3_3_extensoren; in3_3_extensoren = in2_3_extensoren; in2_3_extensoren = in1_3_extensoren; in1_3_extensoren = in0_3_extensoren;
jorick92 2:56230ff9fc8f 241 in0_3_extensoren = abs(out0_extensoren);
DanAuhust 0:59e2ea255892 242 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 243 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 244
DanAuhust 0:59e2ea255892 245
jorick92 2:56230ff9fc8f 246
jorick92 2:56230ff9fc8f 247 sig_out = out0_3_extensoren;
DanAuhust 0:59e2ea255892 248 break;
DanAuhust 0:59e2ea255892 249 }
DanAuhust 0:59e2ea255892 250 return sig_out;
DanAuhust 0:59e2ea255892 251 }
DanAuhust 0:59e2ea255892 252
DanAuhust 0:59e2ea255892 253 void looper()
DanAuhust 0:59e2ea255892 254 { float emg_value_biceps;
DanAuhust 0:59e2ea255892 255 float emg_value_triceps;
DanAuhust 0:59e2ea255892 256 float emg_value_flexoren;
DanAuhust 0:59e2ea255892 257 float emg_value_extensoren;
DanAuhust 0:59e2ea255892 258 float dy;
jorick92 2:56230ff9fc8f 259
DanAuhust 0:59e2ea255892 260 //static int sig_count = 1;
jorick92 2:56230ff9fc8f 261
jorick92 2:56230ff9fc8f 262 emg_value_biceps = filter(1);
jorick92 2:56230ff9fc8f 263 emg_value_triceps = filter(2);
jorick92 2:56230ff9fc8f 264 emg_value_extensoren = filter(3);
jorick92 2:56230ff9fc8f 265 emg_value_flexoren = filter(4);
jorick92 2:56230ff9fc8f 266
jorick92 2:56230ff9fc8f 267
jorick92 2:56230ff9fc8f 268
jorick92 2:56230ff9fc8f 269 float dx;
jorick92 2:56230ff9fc8f 270
DanAuhust 1:1ffb9e3ae00f 271 dy = emg_value_biceps-emg_value_triceps;
jorick92 2:56230ff9fc8f 272 dx = emg_value_extensoren - emg_value_flexoren;
DanAuhust 0:59e2ea255892 273
DanAuhust 0:59e2ea255892 274 /*emg_value_flexoren = (100*filter(3)-44);
DanAuhust 0:59e2ea255892 275 emg_value_extensoren = (100*filter(4)-44);*/
DanAuhust 0:59e2ea255892 276
DanAuhust 0:59e2ea255892 277 /*if(emg_value_biceps < 0.10){
DanAuhust 0:59e2ea255892 278 emg_value_biceps=0;
DanAuhust 0:59e2ea255892 279 }
DanAuhust 0:59e2ea255892 280 else {
DanAuhust 0:59e2ea255892 281 emg_value_biceps = emg_value_biceps;
DanAuhust 0:59e2ea255892 282 }
DanAuhust 0:59e2ea255892 283 if(emg_value_triceps < 0.10){
DanAuhust 0:59e2ea255892 284 emg_value_triceps=0;
DanAuhust 0:59e2ea255892 285 }
DanAuhust 0:59e2ea255892 286 else {
DanAuhust 0:59e2ea255892 287 emg_value_triceps=emg_value_triceps;
DanAuhust 0:59e2ea255892 288 }
DanAuhust 0:59e2ea255892 289 */
DanAuhust 0:59e2ea255892 290 //dy = emg_value_biceps-emg_value_triceps;
DanAuhust 0:59e2ea255892 291 if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30)
jorick92 2:56230ff9fc8f 292 pc.printf("%.6f\n",dy);
DanAuhust 0:59e2ea255892 293 /**When not using the LED, the above could also have been done this way:
DanAuhust 0:59e2ea255892 294 * pc.printf("%.6\n", emg0.read());
DanAuhust 0:59e2ea255892 295 */
DanAuhust 0:59e2ea255892 296 }
DanAuhust 0:59e2ea255892 297
DanAuhust 0:59e2ea255892 298 int main()
DanAuhust 0:59e2ea255892 299 {
DanAuhust 0:59e2ea255892 300 /*setup baudrate. Choose the same in your program on PC side*/
DanAuhust 0:59e2ea255892 301 pc.baud(115200);
DanAuhust 0:59e2ea255892 302 /*set the period for the PWM to the red LED*/
DanAuhust 0:59e2ea255892 303 red.period_ms(2);
DanAuhust 0:59e2ea255892 304 /**Here you attach the 'void looper(void)' function to the Ticker object
DanAuhust 0:59e2ea255892 305 * The looper() function will be called every 1/Fs seconds.
DanAuhust 0:59e2ea255892 306 * Please mind that the parentheses after looper are omitted when using attach.
DanAuhust 0:59e2ea255892 307 */
DanAuhust 0:59e2ea255892 308 timer.attach(looper,0.0032); //invullen in seconde. .0032 is niet eens afgerond, dus vandaar die frequentie.
DanAuhust 0:59e2ea255892 309 while(1) //Loop
DanAuhust 0:59e2ea255892 310 {
DanAuhust 0:59e2ea255892 311 /*Empty!*/
DanAuhust 0:59e2ea255892 312 /*Everything is handled by the interrupt routine now!*/
DanAuhust 0:59e2ea255892 313 }
DanAuhust 0:59e2ea255892 314 }