emg dingetje met moving avarage

Dependencies:   HIDScope biquadFilter circular_buffer mbed

Fork of EMG by Tom Tom

Committer:
Roytsg
Date:
Tue Oct 24 12:58:41 2017 +0000
Revision:
27:674193a62e06
Parent:
26:97a8adc9b895
Project groep 14, use at own risk

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 27:674193a62e06 18 int P= 200;
Roytsg 27:674193a62e06 19 double A[200];
Roytsg 26:97a8adc9b895 20
Roytsg 24:fe3825337233 21
tomlankhorst 14:f83354387756 22 void sample()
vsluiter 2:e314bb3b2d99 23 {
tomlankhorst 19:2bf824669684 24 /* 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 25 scope.set(0, emg0.read() );
tomlankhorst 19:2bf824669684 26 scope.set(1, emg1.read() );
tomlankhorst 19:2bf824669684 27 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
tomlankhorst 19:2bf824669684 28 * Ensure that enough channels are available (HIDScope scope( 2 ))
tomlankhorst 20:97059009a491 29 * Finally, send all channels to the PC at once */
vsluiter 11:ce72ec658a95 30 scope.send();
tomlankhorst 18:21d8e7a81cf5 31 /* To indicate that the function is working, the LED is toggled */
tomlankhorst 18:21d8e7a81cf5 32 led = !led;
vsluiter 2:e314bb3b2d99 33 }
vsluiter 0:32bb76391d89 34
Roytsg 21:77998ce2c0dd 35 BiQuadChain bqc;
Roytsg 21:77998ce2c0dd 36 BiQuad bq1( 0.6844323315947305,1.368864663189461, 0.6844323315947305,1.2243497755555954,0.5133795508233265);
Roytsg 21:77998ce2c0dd 37 BiQuad bq2( 0.6844323315947306, -1.3688646631894612, 0.6844323315947306, -1.2243497755555959, 0.5133795508233266);
Roytsg 21:77998ce2c0dd 38 BiQuad bq3( 0.7566897754116633, -1.2243497755555959, 0.7566897754116633, -1.2243497755555959, 0.5133795508233266);
Roytsg 21:77998ce2c0dd 39
Roytsg 21:77998ce2c0dd 40 Ticker emgSampleTicker;
Roytsg 21:77998ce2c0dd 41 AnalogIn emg( A0 );
Roytsg 21:77998ce2c0dd 42
Roytsg 21:77998ce2c0dd 43 void emgSample() {
Roytsg 21:77998ce2c0dd 44
Roytsg 24:fe3825337233 45 double emgFiltered = bqc.step( emg.read() );
Roytsg 24:fe3825337233 46 double emgabs = abs(emgFiltered);
Roytsg 24:fe3825337233 47 scope.set(0, emgFiltered );
Roytsg 24:fe3825337233 48 scope.set(1, emgabs );
Roytsg 24:fe3825337233 49
Roytsg 27:674193a62e06 50 for(int i = P-1; i >= 0; i--){
Roytsg 26:97a8adc9b895 51 if (i == 0) {
Roytsg 26:97a8adc9b895 52 A[i] = emgabs;
Roytsg 26:97a8adc9b895 53 }
Roytsg 26:97a8adc9b895 54 else {
Roytsg 26:97a8adc9b895 55 A[i] = A[i-1];
Roytsg 26:97a8adc9b895 56 }
Roytsg 26:97a8adc9b895 57 }
Roytsg 26:97a8adc9b895 58 double sum = 0;
Roytsg 27:674193a62e06 59 for (int n = 0; n < P-1; n++) {
Roytsg 26:97a8adc9b895 60 sum = sum + A[n];
Roytsg 26:97a8adc9b895 61 }
Roytsg 24:fe3825337233 62
Roytsg 27:674193a62e06 63 double movmean = sum/P;
Roytsg 24:fe3825337233 64
Roytsg 26:97a8adc9b895 65 scope.set(2, movmean);
Roytsg 21:77998ce2c0dd 66 scope.send();
Roytsg 21:77998ce2c0dd 67 }
Roytsg 21:77998ce2c0dd 68
Roytsg 21:77998ce2c0dd 69
vsluiter 0:32bb76391d89 70 int main()
tomlankhorst 19:2bf824669684 71 {
Roytsg 21:77998ce2c0dd 72 bqc.add( &bq1 ).add( &bq2 ).add( &bq3 );
Roytsg 21:77998ce2c0dd 73 emgSampleTicker.attach( &emgSample, 0.002 );
tomlankhorst 14:f83354387756 74 /**Attach the 'sample' function to the timer 'sample_timer'.
tomlankhorst 19:2bf824669684 75 * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
vsluiter 4:8b298dfada81 76 */
Roytsg 21:77998ce2c0dd 77 //sample_timer.attach(&sample, 0.01);
tomlankhorst 15:0da764eea774 78
tomlankhorst 14:f83354387756 79 /*empty loop, sample() is executed periodically*/
tomlankhorst 15:0da764eea774 80 while(1) {}
vsluiter 0:32bb76391d89 81 }