Matthew Maat / Mbed 2 deprecated EMG-RMS

Dependencies:   mbed HIDScope

Committer:
MatthewMaat
Date:
Fri Oct 11 11:59:59 2019 +0000
Revision:
21:4183ea24a735
Parent:
20:97059009a491
RMS output van de EMG

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"
MatthewMaat 21:4183ea24a735 3 #include <math.h>
vsluiter 0:32bb76391d89 4
vsluiter 4:8b298dfada81 5 //Define objects
tomlankhorst 19:2bf824669684 6 AnalogIn emg0( A0 );
tomlankhorst 19:2bf824669684 7 AnalogIn emg1( A1 );
tomlankhorst 19:2bf824669684 8
tomlankhorst 14:f83354387756 9 Ticker sample_timer;
tomlankhorst 19:2bf824669684 10 HIDScope scope( 2 );
tomlankhorst 18:21d8e7a81cf5 11 DigitalOut led(LED1);
vsluiter 2:e314bb3b2d99 12
tomlankhorst 14:f83354387756 13 /** Sample function
tomlankhorst 14:f83354387756 14 * this function samples the emg and sends it to HIDScope
tomlankhorst 14:f83354387756 15 **/
tomlankhorst 14:f83354387756 16 void sample()
vsluiter 2:e314bb3b2d99 17 {
MatthewMaat 21:4183ea24a735 18 static int count=0;
MatthewMaat 21:4183ea24a735 19 static float RMS_value=0;
MatthewMaat 21:4183ea24a735 20 static float HighPass_value=0;
MatthewMaat 21:4183ea24a735 21 count+=1;
MatthewMaat 21:4183ea24a735 22 static float RMS[150];
MatthewMaat 21:4183ea24a735 23 static float HighPass[30];
MatthewMaat 21:4183ea24a735 24 float I1;
MatthewMaat 21:4183ea24a735 25 float If;
MatthewMaat 21:4183ea24a735 26 I1=emg0.read(); //read signal
MatthewMaat 21:4183ea24a735 27 HighPass_value+=(I1-HighPass[count%30])/30.0;
MatthewMaat 21:4183ea24a735 28 HighPass[count%30]=I1;
MatthewMaat 21:4183ea24a735 29 If=pow(I1-HighPass_value,2.0f); // Highpass-filtered value squared
MatthewMaat 21:4183ea24a735 30 RMS_value+=(If-RMS[count%150])/150.0;
MatthewMaat 21:4183ea24a735 31 RMS[count%150]=If;
tomlankhorst 19:2bf824669684 32 /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
MatthewMaat 21:4183ea24a735 33 scope.set(0, sqrt(RMS_value) ); // send root mean squared
MatthewMaat 21:4183ea24a735 34 scope.set(1, emg0.read() );
tomlankhorst 19:2bf824669684 35 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
tomlankhorst 19:2bf824669684 36 * Ensure that enough channels are available (HIDScope scope( 2 ))
tomlankhorst 20:97059009a491 37 * Finally, send all channels to the PC at once */
vsluiter 11:ce72ec658a95 38 scope.send();
tomlankhorst 18:21d8e7a81cf5 39 /* To indicate that the function is working, the LED is toggled */
tomlankhorst 18:21d8e7a81cf5 40 led = !led;
vsluiter 2:e314bb3b2d99 41 }
vsluiter 0:32bb76391d89 42
vsluiter 0:32bb76391d89 43 int main()
tomlankhorst 19:2bf824669684 44 {
tomlankhorst 14:f83354387756 45 /**Attach the 'sample' function to the timer 'sample_timer'.
tomlankhorst 19:2bf824669684 46 * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
vsluiter 4:8b298dfada81 47 */
tomlankhorst 19:2bf824669684 48 sample_timer.attach(&sample, 0.002);
tomlankhorst 15:0da764eea774 49
tomlankhorst 14:f83354387756 50 /*empty loop, sample() is executed periodically*/
tomlankhorst 15:0da764eea774 51 while(1) {}
vsluiter 0:32bb76391d89 52 }