Dependencies: HIDScope MODSERIAL biquadFilter mbed
Fork of EMG_converter_code by
main.cpp@1:9913e3886643, 2015-10-23 (annotated)
- Committer:
- Technical_Muffin
- Date:
- Fri Oct 23 08:36:59 2015 +0000
- Revision:
- 1:9913e3886643
- Parent:
- 0:1883d922ada8
- Child:
- 2:83659da3e5fe
EMG code adapted to calibrate signal to patients specific amplitude
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 | 1:9913e3886643 | 14 | double emg_value; |
Technical_Muffin | 1:9913e3886643 | 15 | double signalpart1; |
Technical_Muffin | 1:9913e3886643 | 16 | double signalpart2; |
Technical_Muffin | 1:9913e3886643 | 17 | double signalpart3; |
Technical_Muffin | 1:9913e3886643 | 18 | double signalpart4; |
Technical_Muffin | 1:9913e3886643 | 19 | double signalfinal; |
Technical_Muffin | 1:9913e3886643 | 20 | |
Technical_Muffin | 0:1883d922ada8 | 21 | /* |
Technical_Muffin | 0:1883d922ada8 | 22 | */ |
Technical_Muffin | 0:1883d922ada8 | 23 | void filter() |
Technical_Muffin | 0:1883d922ada8 | 24 | { |
Technical_Muffin | 1:9913e3886643 | 25 | emg_value = emg.read();//read the emg value from the elektrodes |
Technical_Muffin | 1:9913e3886643 | 26 | signalpart1 = filterhigh1.step(emg_value);//Highpass filter for removing offset and artifacts |
Technical_Muffin | 1:9913e3886643 | 27 | signalpart2 = abs(signalpart1);//rectify the filtered signal |
Technical_Muffin | 1:9913e3886643 | 28 | signalpart3 = filterlow1.step(signalpart2);//low pass filter to envelope the emg |
Technical_Muffin | 1:9913e3886643 | 29 | signalpart4 = notch.step(signalpart3);//notch filter to remove 50Hz signal |
Technical_Muffin | 1:9913e3886643 | 30 | signalfinal = filterlow2.step(signalpart4);//2nd low pass filter to envelope the emg |
Technical_Muffin | 1:9913e3886643 | 31 | scope.set(0,emg_value);//set emg signal to scope in channel 1 |
Technical_Muffin | 1:9913e3886643 | 32 | scope.set(1,signalfinal);//set filtered signal to scope in channel 2 |
Technical_Muffin | 1:9913e3886643 | 33 | scope.send();//send the signals to the scope |
Technical_Muffin | 0:1883d922ada8 | 34 | } |
Technical_Muffin | 0:1883d922ada8 | 35 | |
Technical_Muffin | 0:1883d922ada8 | 36 | int main() |
Technical_Muffin | 0:1883d922ada8 | 37 | { |
Technical_Muffin | 1:9913e3886643 | 38 | int caldone=0; |
Technical_Muffin | 1:9913e3886643 | 39 | double maxValue = 0;//define the max value to start with |
Technical_Muffin | 1:9913e3886643 | 40 | while(1){ |
Technical_Muffin | 1:9913e3886643 | 41 | while(PTC6){//as long as the button is pressed record the emg signal |
Technical_Muffin | 1:9913e3886643 | 42 | double signalmeasure=emg.read();//read the emg values to check for max |
Technical_Muffin | 1:9913e3886643 | 43 | if(signalmeasure > maxValue){ |
Technical_Muffin | 1:9913e3886643 | 44 | maxValue = signalmeasure; |
Technical_Muffin | 1:9913e3886643 | 45 | } |
Technical_Muffin | 1:9913e3886643 | 46 | caldone=1; |
Technical_Muffin | 1:9913e3886643 | 47 | } |
Technical_Muffin | 1:9913e3886643 | 48 | double maxcal=maxValue; |
Technical_Muffin | 1:9913e3886643 | 49 | if(caldone==1){ |
Technical_Muffin | 1:9913e3886643 | 50 | sample_timer.attach(&filter, 0.002);//continously execute the EMG reader and filter |
Technical_Muffin | 1:9913e3886643 | 51 | double onoffsignal=signalfinal/maxcal;//divide the emg signal by the max EMG to calibrate the signal per person |
Technical_Muffin | 1:9913e3886643 | 52 | } |
Technical_Muffin | 1:9913e3886643 | 53 | } |
Technical_Muffin | 0:1883d922ada8 | 54 | } |