script zover
Dependencies: HIDScope MODSERIAL mbed
main.cpp@4:92e5c7220897, 2014-10-17 (annotated)
- Committer:
- wiesdat
- Date:
- Fri Oct 17 08:53:56 2014 +0000
- Revision:
- 4:92e5c7220897
- Parent:
- 3:9a7ed524bb30
SAFETY
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 | 3:9a7ed524bb30 | 13 | #define TSAMP 0.001 |
wiesdat | 0:bed29da02e8b | 14 | |
wiesdat | 0:bed29da02e8b | 15 | AnalogIn emg0(PTB1); |
wiesdat | 0:bed29da02e8b | 16 | MODSERIAL pc(USBTX,USBRX); |
wiesdat | 4:92e5c7220897 | 17 | HIDScope scope(4); |
wiesdat | 0:bed29da02e8b | 18 | Ticker timer; |
wiesdat | 3:9a7ed524bb30 | 19 | float emg_value, ylp1, yhp1; |
wiesdat | 1:bc2db3bff4bb | 20 | volatile bool looptimerflag; |
wiesdat | 2:620ff9f02d62 | 21 | float ysum = 0, yave=0 ; |
wiesdat | 0:bed29da02e8b | 22 | |
wiesdat | 3:9a7ed524bb30 | 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 | 4:92e5c7220897 | 30 | float hpfilter1(float emg_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 | 4:92e5c7220897 | 35 | x1 = emg_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 | 3:9a7ed524bb30 | 41 | float lpfilter1(float yhp1) |
wiesdat | 2:620ff9f02d62 | 42 | { |
wiesdat | 2:620ff9f02d62 | 43 | static float y,x1=0,y1=0,x=0; |
wiesdat | 3:9a7ed524bb30 | 44 | y = A1LP*x1+y1*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 | 3:9a7ed524bb30 | 54 | scope.set(1,yhp1); |
wiesdat | 3:9a7ed524bb30 | 55 | scope.set(2,ylp1); |
wiesdat | 3:9a7ed524bb30 | 56 | scope.set(3,yave); |
wiesdat | 0:bed29da02e8b | 57 | scope.send(); |
wiesdat | 0:bed29da02e8b | 58 | } |
wiesdat | 0:bed29da02e8b | 59 | |
wiesdat | 1:bc2db3bff4bb | 60 | void setlooptimerflag(void) |
wiesdat | 1:bc2db3bff4bb | 61 | { |
wiesdat | 1:bc2db3bff4bb | 62 | looptimerflag = true; |
wiesdat | 1:bc2db3bff4bb | 63 | } |
wiesdat | 1:bc2db3bff4bb | 64 | |
wiesdat | 0:bed29da02e8b | 65 | int main() |
wiesdat | 0:bed29da02e8b | 66 | { |
wiesdat | 0:bed29da02e8b | 67 | pc.baud(115200); |
wiesdat | 1:bc2db3bff4bb | 68 | timer.attach(setlooptimerflag,TSAMP); |
wiesdat | 2:620ff9f02d62 | 69 | int n; |
wiesdat | 0:bed29da02e8b | 70 | while(1) { |
wiesdat | 2:620ff9f02d62 | 71 | |
wiesdat | 2:620ff9f02d62 | 72 | while(!looptimerflag) /* do nothing */; |
wiesdat | 2:620ff9f02d62 | 73 | looptimerflag = false; |
wiesdat | 2:620ff9f02d62 | 74 | emg_value = readEMG(); |
wiesdat | 3:9a7ed524bb30 | 75 | yhp1 = hpfilter1(emg_value); //function hpfilter |
wiesdat | 3:9a7ed524bb30 | 76 | ylp1 = lpfilter1(yhp1); //function filter |
wiesdat | 3:9a7ed524bb30 | 77 | ysum = ysum+abs(ylp1); |
wiesdat | 2:620ff9f02d62 | 78 | n++; |
wiesdat | 2:620ff9f02d62 | 79 | |
wiesdat | 3:9a7ed524bb30 | 80 | if(n==100) { |
wiesdat | 3:9a7ed524bb30 | 81 | yave = ysum/100; |
wiesdat | 2:620ff9f02d62 | 82 | ysum = 0; |
wiesdat | 2:620ff9f02d62 | 83 | n = 0; |
wiesdat | 2:620ff9f02d62 | 84 | } |
wiesdat | 2:620ff9f02d62 | 85 | |
wiesdat | 2:620ff9f02d62 | 86 | |
wiesdat | 2:620ff9f02d62 | 87 | viewer(); //functie hidscope |
wiesdat | 2:620ff9f02d62 | 88 | |
wiesdat | 1:bc2db3bff4bb | 89 | } |
wiesdat | 0:bed29da02e8b | 90 | } |