Combination code of movement and emg code with small changes for 2 motors.
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of EMG_converter_code by
main.cpp@0:1883d922ada8, 2015-10-21 (annotated)
- Committer:
- Technical_Muffin
- Date:
- Wed Oct 21 15:23:36 2015 +0000
- Revision:
- 0:1883d922ada8
- Child:
- 1:9913e3886643
Code for converting emg signal to usable code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Technical_Muffin | 0:1883d922ada8 | 1 | #include "mbed.h" |
Technical_Muffin | 0:1883d922ada8 | 2 | #include "HIDScope.h" |
Technical_Muffin | 0:1883d922ada8 | 3 | #include "biquadFilter.h" // Require the HIDScope library |
Technical_Muffin | 0:1883d922ada8 | 4 | |
Technical_Muffin | 0:1883d922ada8 | 5 | //Define objects |
Technical_Muffin | 0:1883d922ada8 | 6 | AnalogIn emg(A0); //Analog of EMG input |
Technical_Muffin | 0:1883d922ada8 | 7 | Ticker sample_timer; |
Technical_Muffin | 0:1883d922ada8 | 8 | HIDScope scope(2); // Instantize a 2-channel HIDScope object |
Technical_Muffin | 0:1883d922ada8 | 9 | /*The biquad filters required to transform the EMG signal into an usable signal*/ |
Technical_Muffin | 0:1883d922ada8 | 10 | biquadFilter filterhigh1(-1.1430, 0.4128, 0.6389, -1.2779, 0.6389); |
Technical_Muffin | 0:1883d922ada8 | 11 | biquadFilter filterlow1(1.9556, 0.9565, 0.9780, 1.9561, 0.9780); |
Technical_Muffin | 0:1883d922ada8 | 12 | biquadFilter notch(-1.1978e-16, 0.9561, 0.9780, -1.1978e-16, 0.9780); |
Technical_Muffin | 0:1883d922ada8 | 13 | biquadFilter filterlow2(-1.9645, 0.9651, 1.5515e-4, 3.1030e-4, 1.5515e-4); |
Technical_Muffin | 0:1883d922ada8 | 14 | |
Technical_Muffin | 0:1883d922ada8 | 15 | /* |
Technical_Muffin | 0:1883d922ada8 | 16 | */ |
Technical_Muffin | 0:1883d922ada8 | 17 | void filter() |
Technical_Muffin | 0:1883d922ada8 | 18 | { |
Technical_Muffin | 0:1883d922ada8 | 19 | /* Sample the EMG using the 'read' method of the 'AnalogIn' variable named 'emg' */ |
Technical_Muffin | 0:1883d922ada8 | 20 | double emg_value = emg.read(); |
Technical_Muffin | 0:1883d922ada8 | 21 | double signalpart1 = filterhigh1.step(emg_value);//Highpass filter for removing offset and artifacts |
Technical_Muffin | 0:1883d922ada8 | 22 | double signalpart2 = abs(signalpart1);//rectify the filtered signal |
Technical_Muffin | 0:1883d922ada8 | 23 | double signalpart3 = filterlow1.step(signalpart2);//low pass filter to envelope the emg |
Technical_Muffin | 0:1883d922ada8 | 24 | double signalpart4 = notch.step(signalpart3);//notch filter to remove 50Hz signal |
Technical_Muffin | 0:1883d922ada8 | 25 | double signalfinal = filterlow2.step(signalpart4);//2nd low pass filter to envelope the emg |
Technical_Muffin | 0:1883d922ada8 | 26 | scope.set(0,emg_value);//emg_value |
Technical_Muffin | 0:1883d922ada8 | 27 | scope.set(1,signalfinal); |
Technical_Muffin | 0:1883d922ada8 | 28 | /* Second, set the sampled emg value in channel zero (the first channel) in the 'HIDScope' variable named 'scope' */ |
Technical_Muffin | 0:1883d922ada8 | 29 | /* Repeat the step above if required for more channels (channel 0 up to 5 = 6 channels) */ |
Technical_Muffin | 0:1883d922ada8 | 30 | /* Finally, send all channels to the PC at once */ |
Technical_Muffin | 0:1883d922ada8 | 31 | scope.send(); |
Technical_Muffin | 0:1883d922ada8 | 32 | } |
Technical_Muffin | 0:1883d922ada8 | 33 | |
Technical_Muffin | 0:1883d922ada8 | 34 | int main() |
Technical_Muffin | 0:1883d922ada8 | 35 | { |
Technical_Muffin | 0:1883d922ada8 | 36 | /**Attach the 'sample' function to the timer 'sample_timer'. |
Technical_Muffin | 0:1883d922ada8 | 37 | * this ensures that 'sample' is executed every... 0.002 seconds |
Technical_Muffin | 0:1883d922ada8 | 38 | */ |
Technical_Muffin | 0:1883d922ada8 | 39 | sample_timer.attach(&filter, 0.002); |
Technical_Muffin | 0:1883d922ada8 | 40 | |
Technical_Muffin | 0:1883d922ada8 | 41 | /*empty loop, sample() is executed periodically*/ |
Technical_Muffin | 0:1883d922ada8 | 42 | while(1) {} |
Technical_Muffin | 0:1883d922ada8 | 43 | } |