the emg filtering part of the program

Dependencies:   HIDScope biquadFilter mbed MODSERIAL

Fork of EMG by Tom Tom

Committer:
RiP
Date:
Wed Oct 19 19:33:25 2016 +0000
Revision:
22:f38a15e851d2
Parent:
21:3aecd735319d
Child:
23:54d28f9eef53
go flag voor samplen toegevoegd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:32bb76391d89 1 #include "mbed.h"
vsluiter 11:ce72ec658a95 2 #include "HIDScope.h"
RiP 21:3aecd735319d 3 #include "BiQuad.h"
RiP 22:f38a15e851d2 4 #include "MODSERIAL.h"
vsluiter 0:32bb76391d89 5
vsluiter 4:8b298dfada81 6 //Define objects
RiP 21:3aecd735319d 7 AnalogIn emg1( A0 );
RiP 21:3aecd735319d 8 AnalogIn emg2( A1 );
RiP 21:3aecd735319d 9 AnalogIn emg3( A2 );
tomlankhorst 19:2bf824669684 10
tomlankhorst 14:f83354387756 11 Ticker sample_timer;
RiP 21:3aecd735319d 12 HIDScope scope( 6 );
RiP 22:f38a15e851d2 13 MODSERIAL pc(USBTX, USBRX);
tomlankhorst 18:21d8e7a81cf5 14 DigitalOut led(LED1);
vsluiter 2:e314bb3b2d99 15
RiP 22:f38a15e851d2 16 volatile bool sampletimer = false;
RiP 22:f38a15e851d2 17
RiP 22:f38a15e851d2 18
RiP 21:3aecd735319d 19 BiQuadChain bqc11;
RiP 21:3aecd735319d 20 BiQuadChain bqc13;
RiP 21:3aecd735319d 21 BiQuadChain bqc21;
RiP 21:3aecd735319d 22 BiQuadChain bqc23;
RiP 21:3aecd735319d 23 BiQuadChain bqc31;
RiP 21:3aecd735319d 24 BiQuadChain bqc33;
RiP 21:3aecd735319d 25 //BiQuad bq11( 9.87589e-01, -1.59795e+00, 9.87589e-01, -1.59795e+00, 9.75178e-01 ); //oude BiQuad waardes
RiP 21:3aecd735319d 26 /* BiQuads for filter emg1
RiP 21:3aecd735319d 27 notch filter*/
RiP 21:3aecd735319d 28 BiQuad bq111(0.9795, -1.5849, 0.9795, 1.0000, -1.5849, 0.9589);
RiP 21:3aecd735319d 29 BiQuad bq112(0.9833, -1.5912, 0.9833, 1.0000, -1.5793, 0.9787);
RiP 21:3aecd735319d 30 BiQuad bq113(0.9957, -1.6111, 0.9957, 1.0000, -1.6224, 0.9798);
RiP 21:3aecd735319d 31 /* High pass filter*/
RiP 21:3aecd735319d 32 BiQuad bq121( 9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01 );
RiP 21:3aecd735319d 33 /* low pass filter*/
RiP 21:3aecd735319d 34 BiQuad bq131( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
RiP 21:3aecd735319d 35
RiP 21:3aecd735319d 36
RiP 21:3aecd735319d 37 /* BiQuads for filter emg2
RiP 21:3aecd735319d 38 notch filter*/
RiP 21:3aecd735319d 39 BiQuad bq211(0.9795, -1.5849, 0.9795, 1.0000, -1.5849, 0.9589);
RiP 21:3aecd735319d 40 BiQuad bq212(0.9833, -1.5912, 0.9833, 1.0000, -1.5793, 0.9787);
RiP 21:3aecd735319d 41 BiQuad bq213(0.9957, -1.6111, 0.9957, 1.0000, -1.6224, 0.9798);
RiP 21:3aecd735319d 42 /* High pass filter*/
RiP 21:3aecd735319d 43 BiQuad bq221( 9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01 );
RiP 21:3aecd735319d 44 /* low pass filter*/
RiP 21:3aecd735319d 45 BiQuad bq231( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
RiP 21:3aecd735319d 46
RiP 21:3aecd735319d 47 /* BiQuads for filter emg3
RiP 21:3aecd735319d 48 notch filter*/
RiP 21:3aecd735319d 49 BiQuad bq311(0.9795, -1.5849, 0.9795, 1.0000, -1.5849, 0.9589);
RiP 21:3aecd735319d 50 BiQuad bq312(0.9833, -1.5912, 0.9833, 1.0000, -1.5793, 0.9787);
RiP 21:3aecd735319d 51 BiQuad bq313(0.9957, -1.6111, 0.9957, 1.0000, -1.6224, 0.9798);
RiP 21:3aecd735319d 52 /* High pass filter*/
RiP 21:3aecd735319d 53 BiQuad bq321( 9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01 );
RiP 21:3aecd735319d 54 /* low pass filter*/
RiP 21:3aecd735319d 55 BiQuad bq331( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
RiP 21:3aecd735319d 56
RiP 22:f38a15e851d2 57
RiP 22:f38a15e851d2 58 void sampleflag()
RiP 22:f38a15e851d2 59 {
RiP 22:f38a15e851d2 60 sampletimer=true;
RiP 22:f38a15e851d2 61 }
RiP 22:f38a15e851d2 62
RiP 22:f38a15e851d2 63
tomlankhorst 14:f83354387756 64 void sample()
vsluiter 2:e314bb3b2d99 65 {
RiP 21:3aecd735319d 66 /* Read the emg signals and filter it*/
RiP 22:f38a15e851d2 67
RiP 21:3aecd735319d 68 scope.set(0, emg1.read()); //original signal
RiP 21:3aecd735319d 69 double emg02=bqc13.step(fabs(bqc11.step(emg1.read())));
RiP 21:3aecd735319d 70 scope.set(1, emg02);
RiP 21:3aecd735319d 71 /* Read the second emg signal and filter it*/
RiP 21:3aecd735319d 72 scope.set(2, emg2.read()); //original signal
RiP 21:3aecd735319d 73 double emg12=bqc23.step(fabs(bqc21.step(emg2.read())));
RiP 21:3aecd735319d 74 scope.set(3, emg12);
RiP 21:3aecd735319d 75 /* Read the third emg signal and filter it*/
RiP 21:3aecd735319d 76 scope.set(4, emg3.read()); //original signal
RiP 21:3aecd735319d 77 double emg22=bqc33.step(fabs(bqc31.step(emg3.read())));
RiP 21:3aecd735319d 78 scope.set(5, emg22);
RiP 22:f38a15e851d2 79 //pc.printf("Hello World!\n");
RiP 21:3aecd735319d 80 /* Ensure that enough channels are available (HIDScope scope( 2 ))
RiP 21:3aecd735319d 81 * Finally, send all channels to the PC at once */
vsluiter 11:ce72ec658a95 82 scope.send();
RiP 21:3aecd735319d 83 /* To indicate that the function is working, the LED is toggled */
tomlankhorst 18:21d8e7a81cf5 84 led = !led;
RiP 22:f38a15e851d2 85 if (emg02>0.08||emg12>0.08||emg22>0.08) {
RiP 22:f38a15e851d2 86 pc.printf("het werkt\n");
RiP 22:f38a15e851d2 87 } else {
RiP 22:f38a15e851d2 88 pc.printf("het werkt niet\n");
RiP 22:f38a15e851d2 89 }
RiP 22:f38a15e851d2 90 sampletimer = false;
vsluiter 2:e314bb3b2d99 91 }
vsluiter 0:32bb76391d89 92
vsluiter 0:32bb76391d89 93 int main()
RiP 21:3aecd735319d 94 {
RiP 22:f38a15e851d2 95 pc.baud(115200);
RiP 22:f38a15e851d2 96
RiP 21:3aecd735319d 97 //de biquad chains instellen
RiP 21:3aecd735319d 98 bqc11.add( &bq111 ).add( &bq112 ).add( &bq113 ).add( &bq121 );
RiP 21:3aecd735319d 99 bqc13.add( &bq131);
RiP 21:3aecd735319d 100 bqc21.add( &bq211 ).add( &bq212 ).add( &bq213 ).add( &bq221 );
RiP 21:3aecd735319d 101 bqc23.add( &bq231);
RiP 21:3aecd735319d 102 bqc31.add( &bq311 ).add( &bq312 ).add( &bq313 ).add( &bq321 );
RiP 21:3aecd735319d 103 bqc33.add( &bq331);
RiP 21:3aecd735319d 104 /*Attach the 'sample' function to the timer 'sample_timer'.
RiP 21:3aecd735319d 105 this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
vsluiter 4:8b298dfada81 106 */
RiP 22:f38a15e851d2 107 sample_timer.attach(&sampleflag, 0.002);
tomlankhorst 15:0da764eea774 108
RiP 22:f38a15e851d2 109 while(1) {
RiP 22:f38a15e851d2 110 if (sampletimer==true) {
RiP 22:f38a15e851d2 111 sample();
RiP 22:f38a15e851d2 112
RiP 22:f38a15e851d2 113 }
RiP 22:f38a15e851d2 114 }
vsluiter 0:32bb76391d89 115 }