Code for emg filtering and thus controlling the robotic movement.
Dependencies: HIDScope biquadFilter mbed
Fork of EMG by
main.cpp@18:3175fbe48b40, 2015-10-21 (annotated)
- Committer:
- Technical_Muffin
- Date:
- Wed Oct 21 06:45:08 2015 +0000
- Revision:
- 18:3175fbe48b40
- Parent:
- 16:9f7797ffd0fb
implemented biquad code, not yet tested if working
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" |
Technical_Muffin | 18:3175fbe48b40 | 3 | #include "biquadFilter.h" // Require the HIDScope library |
vsluiter | 0:32bb76391d89 | 4 | |
vsluiter | 4:8b298dfada81 | 5 | //Define objects |
tomlankhorst | 14:f83354387756 | 6 | AnalogIn emg(A0); //Analog input |
tomlankhorst | 14:f83354387756 | 7 | Ticker sample_timer; |
Technical_Muffin | 18:3175fbe48b40 | 8 | Ticker filter_timer |
Technical_Muffin | 18:3175fbe48b40 | 9 | HIDScope scope(2); |
Technical_Muffin | 18:3175fbe48b40 | 10 | biquadFilter filterhigh1(-1.1430 0.4128 0.6389 -1.2779 0.6389); |
Technical_Muffin | 18:3175fbe48b40 | 11 | biquadFilter filterlow1(1.9556 0.9565 0.9780 1.9561 0.9780); |
Technical_Muffin | 18:3175fbe48b40 | 12 | biquadFilter notch(-1.1978e-16 0.9561 0.9780 -1.1978e-16 0.9780); |
Technical_Muffin | 18:3175fbe48b40 | 13 | biquadFilter filterlow2(-1.9645 0.9651 1.5515e-4 3.1030e-4 1.5515e-4); |
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 | **/ |
tomlankhorst | 14:f83354387756 | 18 | void sample() |
vsluiter | 2:e314bb3b2d99 | 19 | { |
tomlankhorst | 14:f83354387756 | 20 | /* First, sample the EMG using the 'read' method of the 'AnalogIn' variable named 'emg' */ |
tomlankhorst | 14:f83354387756 | 21 | double emg_value = emg.read(); |
tomlankhorst | 14:f83354387756 | 22 | /* Second, set the sampled emg value in channel zero (the first channel) in the 'HIDScope' variable named 'scope' */ |
vsluiter | 11:ce72ec658a95 | 23 | scope.set(0,emg_value); |
tomlankhorst | 16:9f7797ffd0fb | 24 | /* Repeat the step above if required for more channels (channel 0 up to 5 = 6 channels) */ |
tomlankhorst | 14:f83354387756 | 25 | /* Finally, send all channels to the PC at once */ |
vsluiter | 11:ce72ec658a95 | 26 | scope.send(); |
vsluiter | 2:e314bb3b2d99 | 27 | } |
Technical_Muffin | 18:3175fbe48b40 | 28 | void filtering() |
Technical_Muffin | 18:3175fbe48b40 | 29 | { |
Technical_Muffin | 18:3175fbe48b40 | 30 | double signalpart1 = filterhigh1(emg.read()); |
Technical_Muffin | 18:3175fbe48b40 | 31 | double signalpart2 = abs(signalpart1); |
Technical_Muffin | 18:3175fbe48b40 | 32 | double signalpart3 = filterlow1(signalpart2); |
Technical_Muffin | 18:3175fbe48b40 | 33 | double signalpart4 = notch(signalpart3); |
Technical_Muffin | 18:3175fbe48b40 | 34 | double signalfinal = filterlow2(signalpart4); |
Technical_Muffin | 18:3175fbe48b40 | 35 | scope.set(1,signalfinal); |
vsluiter | 0:32bb76391d89 | 36 | int main() |
vsluiter | 0:32bb76391d89 | 37 | { |
tomlankhorst | 14:f83354387756 | 38 | /**Attach the 'sample' function to the timer 'sample_timer'. |
tomlankhorst | 14:f83354387756 | 39 | * this ensures that 'sample' is executed every... 0.002 seconds |
vsluiter | 4:8b298dfada81 | 40 | */ |
tomlankhorst | 14:f83354387756 | 41 | sample_timer.attach(&sample, 0.002); |
Technical_Muffin | 18:3175fbe48b40 | 42 | filter_timer.attach(&filtering, 0.002); |
tomlankhorst | 15:0da764eea774 | 43 | |
tomlankhorst | 14:f83354387756 | 44 | /*empty loop, sample() is executed periodically*/ |
tomlankhorst | 15:0da764eea774 | 45 | while(1) {} |
vsluiter | 0:32bb76391d89 | 46 | } |