emg dingetje met moving avarage

Dependencies:   HIDScope biquadFilter circular_buffer mbed

Fork of EMG by Tom Tom

Committer:
Roytsg
Date:
Tue Oct 24 08:55:02 2017 +0000
Revision:
23:b5a09f96c2d7
Parent:
21:77998ce2c0dd
Child:
24:fe3825337233
added absolute value and a second scope;

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