Dependencies: mbed HIDScope biquadFilter
main.cpp@21:48b40b82d195, 2019-10-11 (annotated)
- Committer:
- hidde1104
- Date:
- Fri Oct 11 09:12:11 2019 +0000
- Revision:
- 21:48b40b82d195
- Parent:
- 20:97059009a491
- Child:
- 22:65c90d816235
- Child:
- 27:c53dd84c38aa
EMG filter;
Who changed what in which revision?
User | Revision | Line number | New 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 | 21:48b40b82d195 | 4 | |
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 ); |
tomlankhorst | 18:21d8e7a81cf5 | 20 | DigitalOut led(LED1); |
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; |
tomlankhorst | 14:f83354387756 | 28 | void sample() |
vsluiter | 2:e314bb3b2d99 | 29 | { |
tomlankhorst | 19:2bf824669684 | 30 | /* 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 | 31 | float emg0_value = emg0.read(); |
hidde1104 | 21:48b40b82d195 | 32 | float emg1_value = emg1.read(); |
hidde1104 | 21:48b40b82d195 | 33 | |
hidde1104 | 21:48b40b82d195 | 34 | //double filter_value = bqc.step(emg1_value); |
hidde1104 | 21:48b40b82d195 | 35 | float filter_value = fabs(bq2.step(fabs(bq1.step(emg0_value - emg1_value)))); |
hidde1104 | 21:48b40b82d195 | 36 | if (filter_value > sample_max) { |
hidde1104 | 21:48b40b82d195 | 37 | sample_max = filter_value; |
hidde1104 | 21:48b40b82d195 | 38 | } |
hidde1104 | 21:48b40b82d195 | 39 | if (sample_min > filter_value) { |
hidde1104 | 21:48b40b82d195 | 40 | sample_min = filter_value; |
hidde1104 | 21:48b40b82d195 | 41 | } |
hidde1104 | 21:48b40b82d195 | 42 | |
hidde1104 | 21:48b40b82d195 | 43 | filter_value = filter_value-sample_min; |
hidde1104 | 21:48b40b82d195 | 44 | filter_value = filter_value/(sample_max-sample_min); |
hidde1104 | 21:48b40b82d195 | 45 | |
tomlankhorst | 19:2bf824669684 | 46 | scope.set(0, emg0.read() ); |
tomlankhorst | 19:2bf824669684 | 47 | scope.set(1, emg1.read() ); |
hidde1104 | 21:48b40b82d195 | 48 | scope.set(2, filter_value); |
tomlankhorst | 19:2bf824669684 | 49 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
tomlankhorst | 19:2bf824669684 | 50 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
tomlankhorst | 20:97059009a491 | 51 | * Finally, send all channels to the PC at once */ |
vsluiter | 11:ce72ec658a95 | 52 | scope.send(); |
tomlankhorst | 18:21d8e7a81cf5 | 53 | /* To indicate that the function is working, the LED is toggled */ |
tomlankhorst | 18:21d8e7a81cf5 | 54 | led = !led; |
vsluiter | 2:e314bb3b2d99 | 55 | } |
vsluiter | 0:32bb76391d89 | 56 | |
vsluiter | 0:32bb76391d89 | 57 | int main() |
tomlankhorst | 19:2bf824669684 | 58 | { |
hidde1104 | 21:48b40b82d195 | 59 | bqc.add( &bq1); |
tomlankhorst | 14:f83354387756 | 60 | /**Attach the 'sample' function to the timer 'sample_timer'. |
tomlankhorst | 19:2bf824669684 | 61 | * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz |
vsluiter | 4:8b298dfada81 | 62 | */ |
tomlankhorst | 19:2bf824669684 | 63 | sample_timer.attach(&sample, 0.002); |
tomlankhorst | 15:0da764eea774 | 64 | |
tomlankhorst | 14:f83354387756 | 65 | /*empty loop, sample() is executed periodically*/ |
tomlankhorst | 15:0da764eea774 | 66 | while(1) {} |
hidde1104 | 21:48b40b82d195 | 67 | } |