emg dingetje met moving avarage

Dependencies:   HIDScope biquadFilter circular_buffer mbed

Fork of EMG by Tom Tom

Committer:
Roytsg
Date:
Tue Oct 24 10:03:16 2017 +0000
Revision:
24:fe3825337233
Parent:
23:b5a09f96c2d7
Child:
25:9c2fc98da6b3
moving average with 5 samples;

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"
Roytsg 21:77998ce2c0dd 3 #include "BiQuad.h"
Roytsg 23:b5a09f96c2d7 4 #include "math.h"
vsluiter 0:32bb76391d89 5
Roytsg 24:fe3825337233 6
vsluiter 4:8b298dfada81 7 //Define objects
tomlankhorst 19:2bf824669684 8 AnalogIn emg0( A0 );
tomlankhorst 19:2bf824669684 9 AnalogIn emg1( A1 );
tomlankhorst 19:2bf824669684 10
tomlankhorst 14:f83354387756 11 Ticker sample_timer;
Roytsg 24:fe3825337233 12 HIDScope scope( 3 );
tomlankhorst 18:21d8e7a81cf5 13 DigitalOut led(LED1);
vsluiter 2:e314bb3b2d99 14
tomlankhorst 14:f83354387756 15 /** Sample function
tomlankhorst 14:f83354387756 16 * this function samples the emg and sends it to HIDScope
tomlankhorst 14:f83354387756 17 **/
Roytsg 24:fe3825337233 18
Roytsg 24:fe3825337233 19 double a1 = 0;
Roytsg 24:fe3825337233 20 double a2 = 0;
Roytsg 24:fe3825337233 21 double a3 = 0;
Roytsg 24:fe3825337233 22 double a4 = 0;
Roytsg 24:fe3825337233 23 double a5 = 0;
Roytsg 24:fe3825337233 24
Roytsg 24:fe3825337233 25
tomlankhorst 14:f83354387756 26 void sample()
vsluiter 2:e314bb3b2d99 27 {
tomlankhorst 19:2bf824669684 28 /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
tomlankhorst 19:2bf824669684 29 scope.set(0, emg0.read() );
tomlankhorst 19:2bf824669684 30 scope.set(1, emg1.read() );
tomlankhorst 19:2bf824669684 31 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
tomlankhorst 19:2bf824669684 32 * Ensure that enough channels are available (HIDScope scope( 2 ))
tomlankhorst 20:97059009a491 33 * Finally, send all channels to the PC at once */
vsluiter 11:ce72ec658a95 34 scope.send();
tomlankhorst 18:21d8e7a81cf5 35 /* To indicate that the function is working, the LED is toggled */
tomlankhorst 18:21d8e7a81cf5 36 led = !led;
vsluiter 2:e314bb3b2d99 37 }
vsluiter 0:32bb76391d89 38
Roytsg 21:77998ce2c0dd 39 BiQuadChain bqc;
Roytsg 21:77998ce2c0dd 40 BiQuad bq1( 0.6844323315947305,1.368864663189461, 0.6844323315947305,1.2243497755555954,0.5133795508233265);
Roytsg 21:77998ce2c0dd 41 BiQuad bq2( 0.6844323315947306, -1.3688646631894612, 0.6844323315947306, -1.2243497755555959, 0.5133795508233266);
Roytsg 21:77998ce2c0dd 42 BiQuad bq3( 0.7566897754116633, -1.2243497755555959, 0.7566897754116633, -1.2243497755555959, 0.5133795508233266);
Roytsg 21:77998ce2c0dd 43
Roytsg 21:77998ce2c0dd 44 Ticker emgSampleTicker;
Roytsg 21:77998ce2c0dd 45 AnalogIn emg( A0 );
Roytsg 21:77998ce2c0dd 46
Roytsg 21:77998ce2c0dd 47 void emgSample() {
Roytsg 21:77998ce2c0dd 48
Roytsg 24:fe3825337233 49 double emgFiltered = bqc.step( emg.read() );
Roytsg 24:fe3825337233 50 double emgabs = abs(emgFiltered);
Roytsg 24:fe3825337233 51 scope.set(0, emgFiltered );
Roytsg 24:fe3825337233 52 scope.set(1, emgabs );
Roytsg 24:fe3825337233 53
Roytsg 24:fe3825337233 54
Roytsg 24:fe3825337233 55 a5 = a4;
Roytsg 24:fe3825337233 56 a4 = a3;
Roytsg 24:fe3825337233 57 a3 = a2;
Roytsg 24:fe3825337233 58 a2 = a1;
Roytsg 24:fe3825337233 59 a1 = emgabs;
Roytsg 24:fe3825337233 60
Roytsg 24:fe3825337233 61 double moving = a1 + a2 + a3 + a4 + a5;
Roytsg 24:fe3825337233 62 double movmean = moving/5;
Roytsg 24:fe3825337233 63 scope.set(2, movmean );
Roytsg 24:fe3825337233 64
Roytsg 21:77998ce2c0dd 65 scope.send();
Roytsg 21:77998ce2c0dd 66 }
Roytsg 21:77998ce2c0dd 67
Roytsg 21:77998ce2c0dd 68
vsluiter 0:32bb76391d89 69 int main()
tomlankhorst 19:2bf824669684 70 {
Roytsg 21:77998ce2c0dd 71 bqc.add( &bq1 ).add( &bq2 ).add( &bq3 );
Roytsg 21:77998ce2c0dd 72 emgSampleTicker.attach( &emgSample, 0.002 );
tomlankhorst 14:f83354387756 73 /**Attach the 'sample' function to the timer 'sample_timer'.
tomlankhorst 19:2bf824669684 74 * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
vsluiter 4:8b298dfada81 75 */
Roytsg 21:77998ce2c0dd 76 //sample_timer.attach(&sample, 0.01);
tomlankhorst 15:0da764eea774 77
tomlankhorst 14:f83354387756 78 /*empty loop, sample() is executed periodically*/
tomlankhorst 15:0da764eea774 79 while(1) {}
vsluiter 0:32bb76391d89 80 }