EMG filtered

Dependencies:   HIDScope mbed

Fork of EMG by Tom Tom

Committer:
poephoofd
Date:
Mon Oct 09 10:10:15 2017 +0000
Revision:
22:c7ccfe13d168
Parent:
21:173af3b4367d
Child:
23:5c304175d791
New coefficients added

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"
vsluiter 0:32bb76391d89 3
vsluiter 4:8b298dfada81 4 //Define objects
john111222333 21:173af3b4367d 5 AnalogIn EMGData1( 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);
john111222333 21:173af3b4367d 11 volatile float EMGData_f1=0;
john111222333 21:173af3b4367d 12 double f_v1_EMG=0, f_v2_EMG=0; // set filter variables EMG 1
poephoofd 22:c7ccfe13d168 13 const double M1_F_A1_EMG=-1.561018075800718, M1_F_A2_EMG=0.641351538057563, M1_F_B0_EMG=0.800592403464570,
poephoofd 22:c7ccfe13d168 14 M1_F_B1_EMG=-1.601184806929141, M1_F_B2_EMG=0.800592403464570;// set EMG filter coefficients
vsluiter 2:e314bb3b2d99 15
tomlankhorst 14:f83354387756 16 /** Sample function
tomlankhorst 14:f83354387756 17 * this function samples the emg and sends it to HIDScope
tomlankhorst 14:f83354387756 18 **/
john111222333 21:173af3b4367d 19
john111222333 21:173af3b4367d 20 //IIRFilter
john111222333 21:173af3b4367d 21 double IIRFilter(double u, double &v1, double &v2, const double a1,
john111222333 21:173af3b4367d 22 const double a2, const double b0, const double b1, const double b2){
john111222333 21:173af3b4367d 23 double v = u-a1*v1-a2*v2;
john111222333 21:173af3b4367d 24 double y = b0*v + b1*v1 + b2*v2;
john111222333 21:173af3b4367d 25 v2=v1; v1=v;
john111222333 21:173af3b4367d 26 return y;
john111222333 21:173af3b4367d 27 }
john111222333 21:173af3b4367d 28
john111222333 21:173af3b4367d 29
tomlankhorst 14:f83354387756 30 void sample()
vsluiter 2:e314bb3b2d99 31 {
john111222333 21:173af3b4367d 32 EMGData_f1 = IIRFilter(EMGData1, f_v1_EMG, f_v2_EMG, M1_F_A1_EMG, M1_F_A2_EMG, M1_F_B0_EMG, M1_F_B1_EMG, M1_F_B2_EMG);
john111222333 21:173af3b4367d 33
tomlankhorst 19:2bf824669684 34 /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
john111222333 21:173af3b4367d 35 scope.set(0, EMGData1); // set scope channel 0 EMG
john111222333 21:173af3b4367d 36 scope.set(1, EMGData_f1); // set scope channel 1 EMG filtered
tomlankhorst 19:2bf824669684 37 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
tomlankhorst 19:2bf824669684 38 * Ensure that enough channels are available (HIDScope scope( 2 ))
tomlankhorst 20:97059009a491 39 * Finally, send all channels to the PC at once */
vsluiter 11:ce72ec658a95 40 scope.send();
tomlankhorst 18:21d8e7a81cf5 41 /* To indicate that the function is working, the LED is toggled */
tomlankhorst 18:21d8e7a81cf5 42 led = !led;
vsluiter 2:e314bb3b2d99 43 }
vsluiter 0:32bb76391d89 44
vsluiter 0:32bb76391d89 45 int main()
tomlankhorst 19:2bf824669684 46 {
tomlankhorst 14:f83354387756 47 /**Attach the 'sample' function to the timer 'sample_timer'.
tomlankhorst 19:2bf824669684 48 * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
vsluiter 4:8b298dfada81 49 */
john111222333 21:173af3b4367d 50 sample_timer.attach(&sample, 0.005);
tomlankhorst 15:0da764eea774 51
tomlankhorst 14:f83354387756 52 /*empty loop, sample() is executed periodically*/
tomlankhorst 15:0da764eea774 53 while(1) {}
vsluiter 0:32bb76391d89 54 }