Meting EMG met alle filters voor biceps.

Dependencies:   MODSERIAL mbed

Committer:
DanAuhust
Date:
Fri Oct 18 07:25:14 2013 +0000
Revision:
0:49ab9907d70d
Child:
1:04e5611501f6
Test voor ??n meting EMG, eigenschappen filters nog niet optimaal.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DanAuhust 0:49ab9907d70d 1 #include "mbed.h"
DanAuhust 0:49ab9907d70d 2 #include "MODSERIAL.h"
DanAuhust 0:49ab9907d70d 3
DanAuhust 0:49ab9907d70d 4 //Define objects
DanAuhust 0:49ab9907d70d 5 AnalogIn emg_biceps(PTB0); //Analog input
DanAuhust 0:49ab9907d70d 6 PwmOut red(LED_RED); // EMG meting
DanAuhust 0:49ab9907d70d 7 // PwmOut blue(LED_BLUE); // uitgangssignaal controle
DanAuhust 0:49ab9907d70d 8 // PwmOut green(LED_GREEN);
DanAuhust 0:49ab9907d70d 9
DanAuhust 0:49ab9907d70d 10 Ticker timer;
DanAuhust 0:49ab9907d70d 11 MODSERIAL pc(USBTX,USBRX,64,1024);
DanAuhust 0:49ab9907d70d 12
DanAuhust 0:49ab9907d70d 13 #define gain_biceps 1
DanAuhust 0:49ab9907d70d 14 #define maxcount 50
DanAuhust 0:49ab9907d70d 15
DanAuhust 0:49ab9907d70d 16 #define NUM0 0.8841 // constante
DanAuhust 0:49ab9907d70d 17 #define NUM1 -3.53647 // z^-1
DanAuhust 0:49ab9907d70d 18 #define NUM2 5.3046 // z^-2etc.
DanAuhust 0:49ab9907d70d 19 #define NUM3 -3.5364
DanAuhust 0:49ab9907d70d 20 #define NUM4 0.8841
DanAuhust 0:49ab9907d70d 21
DanAuhust 0:49ab9907d70d 22 #define DEN0 1 // constante
DanAuhust 0:49ab9907d70d 23 #define DEN1 -3.7538
DanAuhust 0:49ab9907d70d 24 #define DEN2 5.2912
DanAuhust 0:49ab9907d70d 25 #define DEN3 -3.3189
DanAuhust 0:49ab9907d70d 26 #define DEN4 0.7816
DanAuhust 0:49ab9907d70d 27
DanAuhust 0:49ab9907d70d 28 /* hou in de gaten welke waarden globaal gedefinieerd moeten worden*/
DanAuhust 0:49ab9907d70d 29 float count = 0, square_biceps = 0, sum_biceps = 0, mean_biceps = 0.2;
DanAuhust 0:49ab9907d70d 30
DanAuhust 0:49ab9907d70d 31 void looper()
DanAuhust 0:49ab9907d70d 32 {
DanAuhust 0:49ab9907d70d 33 /*value0 is huidig, 1 is t-1, 2 is t-2 etc. Gebruik later aanduidingen ABCD. */
DanAuhust 0:49ab9907d70d 34 float mean;
DanAuhust 0:49ab9907d70d 35 static float in0 = 0, in1 = 0, in2 = 0, in3 = 0, in4 = 0;
DanAuhust 0:49ab9907d70d 36 static float out0 = 0, out1 = 0, out2 = 0, out3 = 0, out4 = 0;
DanAuhust 0:49ab9907d70d 37
DanAuhust 0:49ab9907d70d 38 in4 = in3; in3 = in4; in3 = in2; in2 = in1; in1 = in0;
DanAuhust 0:49ab9907d70d 39 in0 = emg_biceps.read();
DanAuhust 0:49ab9907d70d 40 red = in0;
DanAuhust 0:49ab9907d70d 41 /* rode led voor meting emg*/
DanAuhust 0:49ab9907d70d 42 out4 = out3; out3 = out2; out2 = out1; out1 = out0;
DanAuhust 0:49ab9907d70d 43 out0 = (NUM0*in0 + NUM1*in1 + NUM2*in2 + NUM3*in3 + NUM4*in4 - DEN1*out1 - DEN2*out2 - DEN3*out3 - DEN4*out4 ) / DEN0;
DanAuhust 0:49ab9907d70d 44
DanAuhust 0:49ab9907d70d 45 /*send value to PC. use 6 digits after decimal sign*/
DanAuhust 0:49ab9907d70d 46 //if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) // ! Testen filter: gebruik de if om de serial optimaal te gebruiken.
DanAuhust 0:49ab9907d70d 47 //pc.printf("%.6f\n",emg_out_biceps);
DanAuhust 0:49ab9907d70d 48 /**When not using the LED, the above could also have been done this way:
DanAuhust 0:49ab9907d70d 49 * pc.printf("%.6\n", emg0.read());
DanAuhust 0:49ab9907d70d 50 */
DanAuhust 0:49ab9907d70d 51 float emg_abs; // square, mean en count eerder gedefinieerd
DanAuhust 0:49ab9907d70d 52 emg_abs = fabs(out0);
DanAuhust 0:49ab9907d70d 53 mean = mean_biceps;
DanAuhust 0:49ab9907d70d 54 sum_biceps += out0;
DanAuhust 0:49ab9907d70d 55 square_biceps += (emg_abs - mean)*(emg_abs - mean); //neem absolute waarde, kwadrateer, voeg toe aan vorige square
DanAuhust 0:49ab9907d70d 56 // voeg rest EMG's toe, variabelen alleen _spier geven als het nodig is.
DanAuhust 0:49ab9907d70d 57 count += 1; // hou bij hoeveel squares er zijn opgeteld
DanAuhust 0:49ab9907d70d 58 }
DanAuhust 0:49ab9907d70d 59
DanAuhust 0:49ab9907d70d 60 int main()
DanAuhust 0:49ab9907d70d 61 {
DanAuhust 0:49ab9907d70d 62 /*setup baudrate. Choose the same in your program on PC side*/
DanAuhust 0:49ab9907d70d 63 pc.baud(115200);
DanAuhust 0:49ab9907d70d 64 /*set the period for the PWM to the red LED*/
DanAuhust 0:49ab9907d70d 65 red.period_ms(2); // periode pwm = 2*Fs , blijkbaar.
DanAuhust 0:49ab9907d70d 66 // blue.period_ms(2);
DanAuhust 0:49ab9907d70d 67 /**Here you attach the 'void looper(void)' function to the Ticker object0
DanAuhust 0:49ab9907d70d 68 * The looper() function will be called every 0.001 seconds.
DanAuhust 0:49ab9907d70d 69 * Please mind that the parentheses after looper are omitted when using attach.
DanAuhust 0:49ab9907d70d 70 */
DanAuhust 0:49ab9907d70d 71 float sig_out_biceps;
DanAuhust 0:49ab9907d70d 72 timer.attach(looper, 0.001);
DanAuhust 0:49ab9907d70d 73 while(1) // Loop
DanAuhust 0:49ab9907d70d 74 { if (count >= maxcount)
DanAuhust 0:49ab9907d70d 75 { sig_out_biceps = sqrt(square_biceps/count);
DanAuhust 0:49ab9907d70d 76 mean_biceps = sum_biceps/count;
DanAuhust 0:49ab9907d70d 77 count= 0; square_biceps = 0; sum_biceps = 0; // en neem de RMS als er genoeg zijn geteld, stuur die door, en reset sqaure en count
DanAuhust 0:49ab9907d70d 78 if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30)
DanAuhust 0:49ab9907d70d 79 pc.printf("%.6f\n",sig_out_biceps);
DanAuhust 0:49ab9907d70d 80 }
DanAuhust 0:49ab9907d70d 81
DanAuhust 0:49ab9907d70d 82 }
DanAuhust 0:49ab9907d70d 83 }