EMG+filter
Dependencies: mbed HIDScope biquadFilter biquad
main.cpp@23:71103eda6fa2, 2019-10-15 (annotated)
- Committer:
- Rosalie
- Date:
- Tue Oct 15 13:28:03 2019 +0000
- Revision:
- 23:71103eda6fa2
- Parent:
- 22:782e4eac9796
- Child:
- 24:314fa9e0a4a2
BiQuad
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" |
Rosalie | 22:782e4eac9796 | 3 | #include "BiQuad.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 | |
Rosalie | 22:782e4eac9796 | 13 | BiQuadChain bqc; |
Rosalie | 23:71103eda6fa2 | 14 | BiQuad bq1(b0, b1, b2, a1, a2); //voor low-pass |
Rosalie | 23:71103eda6fa2 | 15 | BiQuad bq2(b00, b11, b22, a11, a22); // voor high-pass |
Rosalie | 23:71103eda6fa2 | 16 | BiQuad bq3(b000, b111, b222, a111, a222); //notch -> voor coëfficienten, zie matlab |
Rosalie | 22:782e4eac9796 | 17 | |
Rosalie | 22:782e4eac9796 | 18 | Ticker emgSampleTicker; |
Rosalie | 22:782e4eac9796 | 19 | AnalogIn emg(A0); //kloptA0 wel?? |
Rosalie | 22:782e4eac9796 | 20 | |
tomlankhorst | 14:f83354387756 | 21 | /** Sample function |
tomlankhorst | 14:f83354387756 | 22 | * this function samples the emg and sends it to HIDScope |
tomlankhorst | 14:f83354387756 | 23 | **/ |
tomlankhorst | 14:f83354387756 | 24 | void sample() |
vsluiter | 2:e314bb3b2d99 | 25 | { |
tomlankhorst | 19:2bf824669684 | 26 | /* 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 | 27 | scope.set(0, emg0.read() ); |
tomlankhorst | 19:2bf824669684 | 28 | scope.set(1, emg1.read() ); |
tomlankhorst | 19:2bf824669684 | 29 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
tomlankhorst | 19:2bf824669684 | 30 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
tomlankhorst | 20:97059009a491 | 31 | * Finally, send all channels to the PC at once */ |
vsluiter | 11:ce72ec658a95 | 32 | scope.send(); |
tomlankhorst | 18:21d8e7a81cf5 | 33 | /* To indicate that the function is working, the LED is toggled */ |
tomlankhorst | 18:21d8e7a81cf5 | 34 | led = !led; |
vsluiter | 2:e314bb3b2d99 | 35 | } |
vsluiter | 0:32bb76391d89 | 36 | |
Rosalie | 22:782e4eac9796 | 37 | |
Rosalie | 22:782e4eac9796 | 38 | |
Rosalie | 22:782e4eac9796 | 39 | |
Rosalie | 22:782e4eac9796 | 40 | |
Rosalie | 22:782e4eac9796 | 41 | void emgSample() |
Rosalie | 22:782e4eac9796 | 42 | { |
Rosalie | 22:782e4eac9796 | 43 | double emgFiltered=bqc.step(emg.read()); |
Rosalie | 22:782e4eac9796 | 44 | } |
Rosalie | 22:782e4eac9796 | 45 | |
Rosalie | 22:782e4eac9796 | 46 | |
vsluiter | 0:32bb76391d89 | 47 | int main() |
tomlankhorst | 19:2bf824669684 | 48 | { |
tomlankhorst | 14:f83354387756 | 49 | /**Attach the 'sample' function to the timer 'sample_timer'. |
tomlankhorst | 19:2bf824669684 | 50 | * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz |
vsluiter | 4:8b298dfada81 | 51 | */ |
WiesjeRoskamp | 21:7932900d6e3b | 52 | sample_timer.attach(&sample, 0.002f); |
Rosalie | 23:71103eda6fa2 | 53 | bqc.add(&bq1).add(&bq2).add(&bq3); |
Rosalie | 22:782e4eac9796 | 54 | emgSampleTicker.attach(&emgSample,0.01); |
tomlankhorst | 15:0da764eea774 | 55 | |
tomlankhorst | 14:f83354387756 | 56 | /*empty loop, sample() is executed periodically*/ |
WiesjeRoskamp | 21:7932900d6e3b | 57 | while(1) { |
WiesjeRoskamp | 21:7932900d6e3b | 58 | wait(0.1f); |
WiesjeRoskamp | 21:7932900d6e3b | 59 | } |
vsluiter | 0:32bb76391d89 | 60 | } |