emg trial
Dependencies: FastPWM HIDScope MODSERIAL mbed biquadFilter
main.cpp@2:0189c5170834, 2018-10-19 (annotated)
- Committer:
- s1680897
- Date:
- Fri Oct 19 14:04:19 2018 +0000
- Revision:
- 2:0189c5170834
- Parent:
- 1:e81ca29ae626
- Child:
- 3:b96d952083be
EMG filter implemented, not checked. Want Hidscope liet maar ??n channel zien.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s1680897 | 0:94b5c70f818b | 1 | #include "mbed.h" |
s1680897 | 1:e81ca29ae626 | 2 | #include "HIDScope.h" |
s1680897 | 0:94b5c70f818b | 3 | #include "MODSERIAL.h" |
s1680897 | 2:0189c5170834 | 4 | #include "BiQuad.h" |
s1680897 | 0:94b5c70f818b | 5 | |
s1680897 | 1:e81ca29ae626 | 6 | |
s1680897 | 1:e81ca29ae626 | 7 | // Two analog inputs to read from |
s1680897 | 1:e81ca29ae626 | 8 | AnalogIn a0(A0); |
s1680897 | 1:e81ca29ae626 | 9 | AnalogIn a1(A1); |
s1680897 | 1:e81ca29ae626 | 10 | DigitalOut ledr(LED_RED); |
s1680897 | 0:94b5c70f818b | 11 | MODSERIAL pc(USBTX, USBRX); |
s1680897 | 0:94b5c70f818b | 12 | |
s1680897 | 0:94b5c70f818b | 13 | |
s1680897 | 1:e81ca29ae626 | 14 | // Define the HIDScope and Ticker objects |
s1680897 | 2:0189c5170834 | 15 | HIDScope scope(2); |
s1680897 | 1:e81ca29ae626 | 16 | Ticker scopeTimer; |
s1680897 | 2:0189c5170834 | 17 | Ticker EMGTicker; |
s1680897 | 2:0189c5170834 | 18 | |
s1680897 | 2:0189c5170834 | 19 | // BiQuad filter |
s1680897 | 2:0189c5170834 | 20 | BiQuad HP_emg(1,-2,1,1.142980502539901,0.412801598096189); //Highpassfilter |
s1680897 | 2:0189c5170834 | 21 | |
s1680897 | 2:0189c5170834 | 22 | double EMGdata; |
s1680897 | 2:0189c5170834 | 23 | |
s1680897 | 2:0189c5170834 | 24 | void ReadEMG() |
s1680897 | 2:0189c5170834 | 25 | { |
s1680897 | 2:0189c5170834 | 26 | EMGdata = a0.read(); // store values in the scope |
s1680897 | 2:0189c5170834 | 27 | } |
s1680897 | 2:0189c5170834 | 28 | |
s1680897 | 2:0189c5170834 | 29 | double EMG_HP(double EMGdata) |
s1680897 | 1:e81ca29ae626 | 30 | { |
s1680897 | 2:0189c5170834 | 31 | double HP_abs_EMGdata = HP_emg.step(EMGdata); |
s1680897 | 2:0189c5170834 | 32 | |
s1680897 | 2:0189c5170834 | 33 | return fabs(HP_abs_EMGdata); |
s1680897 | 1:e81ca29ae626 | 34 | } |
s1680897 | 0:94b5c70f818b | 35 | |
s1680897 | 2:0189c5170834 | 36 | double Moving_mean(double HP_abs_EMGdata) |
s1680897 | 1:e81ca29ae626 | 37 | { |
s1680897 | 2:0189c5170834 | 38 | int P = 50; |
s1680897 | 2:0189c5170834 | 39 | double EMGarray[P]; |
s1680897 | 2:0189c5170834 | 40 | |
s1680897 | 2:0189c5170834 | 41 | for(int i = P-1; i >= 0; i--) |
s1680897 | 2:0189c5170834 | 42 | { |
s1680897 | 2:0189c5170834 | 43 | if (i == 0) |
s1680897 | 2:0189c5170834 | 44 | { |
s1680897 | 2:0189c5170834 | 45 | EMGarray[i] = HP_abs_EMGdata; |
s1680897 | 2:0189c5170834 | 46 | } |
s1680897 | 2:0189c5170834 | 47 | else |
s1680897 | 2:0189c5170834 | 48 | { |
s1680897 | 2:0189c5170834 | 49 | EMGarray[i] = EMGarray[i-1]; |
s1680897 | 2:0189c5170834 | 50 | } |
s1680897 | 2:0189c5170834 | 51 | } |
s1680897 | 2:0189c5170834 | 52 | |
s1680897 | 2:0189c5170834 | 53 | double sum = 0.0; |
s1680897 | 2:0189c5170834 | 54 | |
s1680897 | 2:0189c5170834 | 55 | for(int j=0; j==P-1; j++) |
s1680897 | 2:0189c5170834 | 56 | { |
s1680897 | 2:0189c5170834 | 57 | sum += EMGarray[j]; |
s1680897 | 2:0189c5170834 | 58 | } |
s1680897 | 2:0189c5170834 | 59 | |
s1680897 | 2:0189c5170834 | 60 | double EMG_filt = sum / (double)P; |
s1680897 | 2:0189c5170834 | 61 | |
s1680897 | 2:0189c5170834 | 62 | return(EMG_filt); |
s1680897 | 1:e81ca29ae626 | 63 | } |
s1680897 | 0:94b5c70f818b | 64 | |
s1680897 | 2:0189c5170834 | 65 | void EMG_filtering () |
s1680897 | 2:0189c5170834 | 66 | { |
s1680897 | 2:0189c5170834 | 67 | ReadEMG(); |
s1680897 | 2:0189c5170834 | 68 | scope.set(0, EMGdata); |
s1680897 | 2:0189c5170834 | 69 | |
s1680897 | 2:0189c5170834 | 70 | volatile double HP_abs_EMGdata = EMG_HP(EMGdata); |
s1680897 | 2:0189c5170834 | 71 | volatile double EMG_filt = Moving_mean(HP_abs_EMGdata); |
s1680897 | 2:0189c5170834 | 72 | |
s1680897 | 2:0189c5170834 | 73 | scope.set(1, EMG_filt); |
s1680897 | 2:0189c5170834 | 74 | //pc.printf("EMG filt= %f \n\r", EMG_filt); |
s1680897 | 2:0189c5170834 | 75 | } |
s1680897 | 1:e81ca29ae626 | 76 | |
s1680897 | 0:94b5c70f818b | 77 | int main() |
s1680897 | 0:94b5c70f818b | 78 | { |
s1680897 | 0:94b5c70f818b | 79 | pc.baud(115200); |
s1680897 | 1:e81ca29ae626 | 80 | // Attach the HIDScope::send method from the scope object to the timer at 50Hz |
s1680897 | 1:e81ca29ae626 | 81 | scopeTimer.attach_us(&scope, &HIDScope::send, 2e4); |
s1680897 | 2:0189c5170834 | 82 | EMGTicker.attach_us(EMG_filtering, 2e4); |
s1680897 | 0:94b5c70f818b | 83 | |
s1680897 | 2:0189c5170834 | 84 | while(1) {} |
s1680897 | 1:e81ca29ae626 | 85 | |
s1680897 | 0:94b5c70f818b | 86 | } |