Jelle Bakker
/
EMG
EMG filtered
Fork of EMG by
main.cpp@23:5c304175d791, 2017-10-09 (annotated)
- Committer:
- poephoofd
- Date:
- Mon Oct 09 10:32:30 2017 +0000
- Revision:
- 23:5c304175d791
- Parent:
- 22:c7ccfe13d168
Added abs and Low Pass Filter
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 11:ce72ec658a95 | 2 | #include "HIDScope.h" |
vsluiter | 0:32bb76391d89 | 3 | |
vsluiter | 4:8b298dfada81 | 4 | //Define objects |
poephoofd | 23:5c304175d791 | 5 | AnalogIn EMGData_1( A0 ); |
tomlankhorst | 19:2bf824669684 | 6 | AnalogIn emg1( A1 ); |
tomlankhorst | 19:2bf824669684 | 7 | |
tomlankhorst | 14:f83354387756 | 8 | Ticker sample_timer; |
tomlankhorst | 19:2bf824669684 | 9 | HIDScope scope( 2 ); |
tomlankhorst | 18:21d8e7a81cf5 | 10 | DigitalOut led(LED1); |
poephoofd | 23:5c304175d791 | 11 | |
poephoofd | 23:5c304175d791 | 12 | volatile float EMGData_1_HiFil=0; //EMG Hi Filtered |
poephoofd | 23:5c304175d791 | 13 | volatile float EMGData_1_HiFilAbs=0; //EMG Hi Filtered abs |
poephoofd | 23:5c304175d791 | 14 | volatile float EMGData_1_HiLoFil=0; //EMG HiLo Filtered |
poephoofd | 23:5c304175d791 | 15 | |
poephoofd | 23:5c304175d791 | 16 | double f_v1_EMG_1_Hi=0, f_v2_EMG_1_Hi=0; // set Hi filter variables EMG 1 |
poephoofd | 23:5c304175d791 | 17 | const double M1_F_A1_EMG_Hi=-1.561018075800718, M1_F_A2_EMG_Hi=0.641351538057563, M1_F_B0_EMG_Hi=0.800592403464570, |
poephoofd | 23:5c304175d791 | 18 | M1_F_B1_EMG_Hi=-1.601184806929141, M1_F_B2_EMG_Hi=0.800592403464570;// set EMG filter coefficients |
poephoofd | 23:5c304175d791 | 19 | |
poephoofd | 23:5c304175d791 | 20 | double f_v1_EMG_1_Lo=0, f_v2_EMG_1_Lo=0; // set Lo filter variables EMG 1 |
poephoofd | 23:5c304175d791 | 21 | const double M1_F_A1_EMG_Lo=-1.561018075800718, M1_F_A2_EMG_Lo=0.641351538057563, M1_F_B0_EMG_Lo=0.800592403464570, |
poephoofd | 23:5c304175d791 | 22 | M1_F_B1_EMG_Lo=-1.601184806929141, M1_F_B2_EMG_Lo=0.800592403464570; // set EMG filter coefficients |
vsluiter | 2:e314bb3b2d99 | 23 | |
tomlankhorst | 14:f83354387756 | 24 | /** Sample function |
tomlankhorst | 14:f83354387756 | 25 | * this function samples the emg and sends it to HIDScope |
tomlankhorst | 14:f83354387756 | 26 | **/ |
john111222333 | 21:173af3b4367d | 27 | |
john111222333 | 21:173af3b4367d | 28 | //IIRFilter |
john111222333 | 21:173af3b4367d | 29 | double IIRFilter(double u, double &v1, double &v2, const double a1, |
john111222333 | 21:173af3b4367d | 30 | const double a2, const double b0, const double b1, const double b2){ |
john111222333 | 21:173af3b4367d | 31 | double v = u-a1*v1-a2*v2; |
john111222333 | 21:173af3b4367d | 32 | double y = b0*v + b1*v1 + b2*v2; |
john111222333 | 21:173af3b4367d | 33 | v2=v1; v1=v; |
john111222333 | 21:173af3b4367d | 34 | return y; |
john111222333 | 21:173af3b4367d | 35 | } |
john111222333 | 21:173af3b4367d | 36 | |
john111222333 | 21:173af3b4367d | 37 | |
tomlankhorst | 14:f83354387756 | 38 | void sample() |
vsluiter | 2:e314bb3b2d99 | 39 | { |
poephoofd | 23:5c304175d791 | 40 | EMGData_1_HiFil = IIRFilter(EMGData_1, f_v1_EMG_1_Hi, f_v2_EMG_1_Hi, M1_F_A1_EMG_Hi, M1_F_A2_EMG_Hi, M1_F_B0_EMG_Hi, M1_F_B1_EMG_Hi, M1_F_B2_EMG_Hi); |
poephoofd | 23:5c304175d791 | 41 | EMGData_1_HiFilAbs = abs(EMGData_1_HiFil); |
poephoofd | 23:5c304175d791 | 42 | EMGData_1_HiLoFil = IIRFilter(EMGData_1_HiFilAbs, f_v1_EMG_1_Lo, f_v2_EMG_1_Lo, M1_F_A1_EMG_Lo, M1_F_A2_EMG_Lo, M1_F_B0_EMG_Lo, M1_F_B1_EMG_Lo, M1_F_B2_EMG_Lo); |
tomlankhorst | 19:2bf824669684 | 43 | /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ |
poephoofd | 23:5c304175d791 | 44 | scope.set(0, EMGData_1); // set scope channel 0 EMG |
poephoofd | 23:5c304175d791 | 45 | scope.set(1, EMGData_1_HiLoFil); // set scope channel 1 EMG filtered |
tomlankhorst | 19:2bf824669684 | 46 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
tomlankhorst | 19:2bf824669684 | 47 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
tomlankhorst | 20:97059009a491 | 48 | * Finally, send all channels to the PC at once */ |
vsluiter | 11:ce72ec658a95 | 49 | scope.send(); |
tomlankhorst | 18:21d8e7a81cf5 | 50 | /* To indicate that the function is working, the LED is toggled */ |
tomlankhorst | 18:21d8e7a81cf5 | 51 | led = !led; |
vsluiter | 2:e314bb3b2d99 | 52 | } |
vsluiter | 0:32bb76391d89 | 53 | |
vsluiter | 0:32bb76391d89 | 54 | int main() |
tomlankhorst | 19:2bf824669684 | 55 | { |
tomlankhorst | 14:f83354387756 | 56 | /**Attach the 'sample' function to the timer 'sample_timer'. |
tomlankhorst | 19:2bf824669684 | 57 | * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz |
vsluiter | 4:8b298dfada81 | 58 | */ |
john111222333 | 21:173af3b4367d | 59 | sample_timer.attach(&sample, 0.005); |
tomlankhorst | 15:0da764eea774 | 60 | |
tomlankhorst | 14:f83354387756 | 61 | /*empty loop, sample() is executed periodically*/ |
tomlankhorst | 15:0da764eea774 | 62 | while(1) {} |
vsluiter | 0:32bb76391d89 | 63 | } |