![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
script zover
Dependencies: HIDScope MODSERIAL mbed
main.cpp@2:620ff9f02d62, 2014-10-15 (annotated)
- Committer:
- wiesdat
- Date:
- Wed Oct 15 13:35:09 2014 +0000
- Revision:
- 2:620ff9f02d62
- Parent:
- 1:bc2db3bff4bb
- Child:
- 3:9a7ed524bb30
- Child:
- 5:9415e5be8235
na hulp wc 15/10;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wiesdat | 0:bed29da02e8b | 1 | #include "mbed.h" |
wiesdat | 0:bed29da02e8b | 2 | #include "MODSERIAL.h" |
wiesdat | 0:bed29da02e8b | 3 | #include "HIDScope.h" |
wiesdat | 0:bed29da02e8b | 4 | |
wiesdat | 2:620ff9f02d62 | 5 | #define A1LP 0.181269246922018 |
wiesdat | 2:620ff9f02d62 | 6 | #define B1LP -0.818730753077982 |
wiesdat | 2:620ff9f02d62 | 7 | |
wiesdat | 0:bed29da02e8b | 8 | #define A1HP -1.999214978283642 |
wiesdat | 2:620ff9f02d62 | 9 | #define A0HP 0.999214978283642 |
wiesdat | 0:bed29da02e8b | 10 | #define B1HP -1.943446330323158 |
wiesdat | 0:bed29da02e8b | 11 | #define B0HP 0.945001709500376 |
wiesdat | 1:bc2db3bff4bb | 12 | |
wiesdat | 2:620ff9f02d62 | 13 | #define TSAMP 0.0001 |
wiesdat | 0:bed29da02e8b | 14 | |
wiesdat | 0:bed29da02e8b | 15 | AnalogIn emg0(PTB1); |
wiesdat | 0:bed29da02e8b | 16 | MODSERIAL pc(USBTX,USBRX); |
wiesdat | 2:620ff9f02d62 | 17 | HIDScope scope(5); |
wiesdat | 0:bed29da02e8b | 18 | Ticker timer; |
wiesdat | 0:bed29da02e8b | 19 | float filter(float); |
wiesdat | 0:bed29da02e8b | 20 | float emg_value; |
wiesdat | 1:bc2db3bff4bb | 21 | volatile bool looptimerflag; |
wiesdat | 2:620ff9f02d62 | 22 | float ysum = 0, yave=0 ; |
wiesdat | 0:bed29da02e8b | 23 | |
wiesdat | 2:620ff9f02d62 | 24 | uint16_t readEMG() |
wiesdat | 0:bed29da02e8b | 25 | { |
wiesdat | 2:620ff9f02d62 | 26 | |
wiesdat | 2:620ff9f02d62 | 27 | return emg0.read_u16(); |
wiesdat | 0:bed29da02e8b | 28 | } |
wiesdat | 0:bed29da02e8b | 29 | |
wiesdat | 2:620ff9f02d62 | 30 | float hpfilter(float emg0_value) |
wiesdat | 0:bed29da02e8b | 31 | { |
wiesdat | 2:620ff9f02d62 | 32 | static float y,x1=0,y1=0,x2=0,y2=0; |
wiesdat | 0:bed29da02e8b | 33 | y = y1*B1HP-y2*B0HP-x1*A1HP+x2*A0HP; |
wiesdat | 0:bed29da02e8b | 34 | x2 = x1; |
wiesdat | 0:bed29da02e8b | 35 | x1 = emg0_value; |
wiesdat | 0:bed29da02e8b | 36 | y2 = y1; |
wiesdat | 0:bed29da02e8b | 37 | y1 = y; |
wiesdat | 2:620ff9f02d62 | 38 | return yhp1; |
wiesdat | 2:620ff9f02d62 | 39 | } |
wiesdat | 2:620ff9f02d62 | 40 | |
wiesdat | 2:620ff9f02d62 | 41 | float lpfilter(float yhp1) |
wiesdat | 2:620ff9f02d62 | 42 | { |
wiesdat | 2:620ff9f02d62 | 43 | static float y,x1=0,y1=0,x=0; |
wiesdat | 2:620ff9f02d62 | 44 | y = A1LP*x1lp+y1lp*B1LP; |
wiesdat | 2:620ff9f02d62 | 45 | y1 = y; |
wiesdat | 2:620ff9f02d62 | 46 | x1 = x; |
wiesdat | 2:620ff9f02d62 | 47 | x = y; |
wiesdat | 2:620ff9f02d62 | 48 | return ylp1; |
wiesdat | 0:bed29da02e8b | 49 | } |
wiesdat | 0:bed29da02e8b | 50 | |
wiesdat | 0:bed29da02e8b | 51 | void viewer() |
wiesdat | 0:bed29da02e8b | 52 | { |
wiesdat | 0:bed29da02e8b | 53 | scope.set(0,emg_value); |
wiesdat | 2:620ff9f02d62 | 54 | scope.set(1,yhp); |
wiesdat | 2:620ff9f02d62 | 55 | scope.set(2,ylp); |
wiesdat | 2:620ff9f02d62 | 56 | scope.set(3,ysum); |
wiesdat | 2:620ff9f02d62 | 57 | scope.set(4,yave); |
wiesdat | 0:bed29da02e8b | 58 | scope.send(); |
wiesdat | 0:bed29da02e8b | 59 | } |
wiesdat | 0:bed29da02e8b | 60 | |
wiesdat | 1:bc2db3bff4bb | 61 | void setlooptimerflag(void) |
wiesdat | 1:bc2db3bff4bb | 62 | { |
wiesdat | 1:bc2db3bff4bb | 63 | looptimerflag = true; |
wiesdat | 1:bc2db3bff4bb | 64 | } |
wiesdat | 1:bc2db3bff4bb | 65 | |
wiesdat | 0:bed29da02e8b | 66 | int main() |
wiesdat | 0:bed29da02e8b | 67 | { |
wiesdat | 0:bed29da02e8b | 68 | pc.baud(115200); |
wiesdat | 1:bc2db3bff4bb | 69 | timer.attach(setlooptimerflag,TSAMP); |
wiesdat | 2:620ff9f02d62 | 70 | int n; |
wiesdat | 0:bed29da02e8b | 71 | while(1) { |
wiesdat | 2:620ff9f02d62 | 72 | |
wiesdat | 2:620ff9f02d62 | 73 | while(!looptimerflag) /* do nothing */; |
wiesdat | 2:620ff9f02d62 | 74 | looptimerflag = false; |
wiesdat | 2:620ff9f02d62 | 75 | //read(); //waardes inladen |
wiesdat | 2:620ff9f02d62 | 76 | emg_value = readEMG(); |
wiesdat | 2:620ff9f02d62 | 77 | yhp1 = hpfilter(emg_value); //functie hpfilter |
wiesdat | 2:620ff9f02d62 | 78 | ylp1 = lpfilter(yhp1); //functie filter |
wiesdat | 2:620ff9f02d62 | 79 | ysum = ysum+abs(ylp); |
wiesdat | 2:620ff9f02d62 | 80 | n++; |
wiesdat | 2:620ff9f02d62 | 81 | |
wiesdat | 2:620ff9f02d62 | 82 | if(n==1000) { |
wiesdat | 2:620ff9f02d62 | 83 | yave = ysum/1000; |
wiesdat | 2:620ff9f02d62 | 84 | yave = yave; |
wiesdat | 2:620ff9f02d62 | 85 | ysum = 0; |
wiesdat | 2:620ff9f02d62 | 86 | n = 0; |
wiesdat | 2:620ff9f02d62 | 87 | } |
wiesdat | 2:620ff9f02d62 | 88 | |
wiesdat | 2:620ff9f02d62 | 89 | |
wiesdat | 2:620ff9f02d62 | 90 | viewer(); //functie hidscope |
wiesdat | 2:620ff9f02d62 | 91 | |
wiesdat | 1:bc2db3bff4bb | 92 | } |
wiesdat | 0:bed29da02e8b | 93 | } |