Dependencies:   mbed HIDScope biquadFilter

Committer:
hidde1104
Date:
Fri Oct 11 09:59:08 2019 +0000
Revision:
22:65c90d816235
Parent:
21:48b40b82d195
Child:
23:4c66541f00d3
EMG filter with calibration button (non-working);

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"
hidde1104 21:48b40b82d195 3 #include "BiQuad.h"
hidde1104 22:65c90d816235 4 InterruptIn but1(PTC6);
hidde1104 21:48b40b82d195 5
hidde1104 21:48b40b82d195 6 BiQuad bq1 (0.881889334678067, -1.76377866935613, 0.8818893346780671, -1.77069673005903, 0.797707797506027);
hidde1104 21:48b40b82d195 7
hidde1104 21:48b40b82d195 8 BiQuad bq2 (0.000198358203463849, 0.000396716406927699, 0.000198358203463849, -1.96262073248799, 0.963423352820821);
hidde1104 21:48b40b82d195 9
hidde1104 21:48b40b82d195 10
hidde1104 21:48b40b82d195 11 BiQuadChain bqc;
hidde1104 21:48b40b82d195 12
vsluiter 0:32bb76391d89 13
vsluiter 4:8b298dfada81 14 //Define objects
tomlankhorst 19:2bf824669684 15 AnalogIn emg0( A0 );
tomlankhorst 19:2bf824669684 16 AnalogIn emg1( A1 );
tomlankhorst 19:2bf824669684 17
tomlankhorst 14:f83354387756 18 Ticker sample_timer;
hidde1104 21:48b40b82d195 19 HIDScope scope( 3 );
hidde1104 22:65c90d816235 20 DigitalOut led(LED2);
vsluiter 2:e314bb3b2d99 21
tomlankhorst 14:f83354387756 22 /** Sample function
tomlankhorst 14:f83354387756 23 * this function samples the emg and sends it to HIDScope
tomlankhorst 14:f83354387756 24 **/
hidde1104 21:48b40b82d195 25
hidde1104 21:48b40b82d195 26 float sample_max = 0;
hidde1104 21:48b40b82d195 27 float sample_min = 1;
hidde1104 22:65c90d816235 28
hidde1104 22:65c90d816235 29 float emg0_value = emg0.read();
hidde1104 22:65c90d816235 30 float emg1_value = emg1.read();
hidde1104 22:65c90d816235 31
hidde1104 22:65c90d816235 32 float filter_value = fabs(bq2.step(fabs(bq1.step(emg0_value - emg1_value))));
hidde1104 22:65c90d816235 33
hidde1104 22:65c90d816235 34 void calibrate() {
hidde1104 22:65c90d816235 35
hidde1104 22:65c90d816235 36 filter_value = filter_value-sample_min;
hidde1104 22:65c90d816235 37 filter_value = filter_value/(sample_max-sample_min);
hidde1104 22:65c90d816235 38 }
hidde1104 22:65c90d816235 39
tomlankhorst 14:f83354387756 40 void sample()
vsluiter 2:e314bb3b2d99 41 {
tomlankhorst 19:2bf824669684 42 /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
hidde1104 21:48b40b82d195 43 float emg0_value = emg0.read();
hidde1104 21:48b40b82d195 44 float emg1_value = emg1.read();
hidde1104 21:48b40b82d195 45
hidde1104 21:48b40b82d195 46 //double filter_value = bqc.step(emg1_value);
hidde1104 21:48b40b82d195 47 float filter_value = fabs(bq2.step(fabs(bq1.step(emg0_value - emg1_value))));
hidde1104 21:48b40b82d195 48 if (filter_value > sample_max) {
hidde1104 21:48b40b82d195 49 sample_max = filter_value;
hidde1104 21:48b40b82d195 50 }
hidde1104 21:48b40b82d195 51 if (sample_min > filter_value) {
hidde1104 21:48b40b82d195 52 sample_min = filter_value;
hidde1104 21:48b40b82d195 53 }
hidde1104 21:48b40b82d195 54
hidde1104 22:65c90d816235 55 but1.mode(PullUp);
hidde1104 22:65c90d816235 56 but1.rise(&calibrate);
hidde1104 21:48b40b82d195 57
tomlankhorst 19:2bf824669684 58 scope.set(0, emg0.read() );
tomlankhorst 19:2bf824669684 59 scope.set(1, emg1.read() );
hidde1104 22:65c90d816235 60 //scope.set(2, filter_value);
tomlankhorst 19:2bf824669684 61 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
tomlankhorst 19:2bf824669684 62 * Ensure that enough channels are available (HIDScope scope( 2 ))
tomlankhorst 20:97059009a491 63 * Finally, send all channels to the PC at once */
vsluiter 11:ce72ec658a95 64 scope.send();
tomlankhorst 18:21d8e7a81cf5 65 /* To indicate that the function is working, the LED is toggled */
tomlankhorst 18:21d8e7a81cf5 66 led = !led;
vsluiter 2:e314bb3b2d99 67 }
vsluiter 0:32bb76391d89 68
hidde1104 22:65c90d816235 69
vsluiter 0:32bb76391d89 70 int main()
tomlankhorst 19:2bf824669684 71 {
hidde1104 21:48b40b82d195 72 bqc.add( &bq1);
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 */
tomlankhorst 19:2bf824669684 76 sample_timer.attach(&sample, 0.002);
tomlankhorst 14:f83354387756 77 /*empty loop, sample() is executed periodically*/
tomlankhorst 15:0da764eea774 78 while(1) {}
hidde1104 21:48b40b82d195 79 }