script zover

Dependencies:   HIDScope MODSERIAL mbed

Committer:
wiesdat
Date:
Thu Oct 16 10:55:57 2014 +0000
Revision:
3:9a7ed524bb30
Parent:
2:620ff9f02d62
Child:
4:92e5c7220897
voor bas

Who changed what in which revision?

UserRevisionLine numberNew 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 2:620ff9f02d62 17 HIDScope scope(5);
wiesdat 0:bed29da02e8b 18 Ticker timer;
wiesdat 0:bed29da02e8b 19 float filter(float);
wiesdat 3:9a7ed524bb30 20 float emg_value, ylp1, yhp1;
wiesdat 1:bc2db3bff4bb 21 volatile bool looptimerflag;
wiesdat 2:620ff9f02d62 22 float ysum = 0, yave=0 ;
wiesdat 0:bed29da02e8b 23
wiesdat 3:9a7ed524bb30 24
wiesdat 2:620ff9f02d62 25 uint16_t readEMG()
wiesdat 0:bed29da02e8b 26 {
wiesdat 2:620ff9f02d62 27
wiesdat 2:620ff9f02d62 28 return emg0.read_u16();
wiesdat 0:bed29da02e8b 29 }
wiesdat 0:bed29da02e8b 30
wiesdat 3:9a7ed524bb30 31 float hpfilter1(float emg0_value)
wiesdat 0:bed29da02e8b 32 {
wiesdat 2:620ff9f02d62 33 static float y,x1=0,y1=0,x2=0,y2=0;
wiesdat 0:bed29da02e8b 34 y = y1*B1HP-y2*B0HP-x1*A1HP+x2*A0HP;
wiesdat 0:bed29da02e8b 35 x2 = x1;
wiesdat 0:bed29da02e8b 36 x1 = emg0_value;
wiesdat 0:bed29da02e8b 37 y2 = y1;
wiesdat 0:bed29da02e8b 38 y1 = y;
wiesdat 2:620ff9f02d62 39 return yhp1;
wiesdat 2:620ff9f02d62 40 }
wiesdat 2:620ff9f02d62 41
wiesdat 3:9a7ed524bb30 42 float lpfilter1(float yhp1)
wiesdat 2:620ff9f02d62 43 {
wiesdat 2:620ff9f02d62 44 static float y,x1=0,y1=0,x=0;
wiesdat 3:9a7ed524bb30 45 y = A1LP*x1+y1*B1LP;
wiesdat 2:620ff9f02d62 46 y1 = y;
wiesdat 2:620ff9f02d62 47 x1 = x;
wiesdat 2:620ff9f02d62 48 x = y;
wiesdat 2:620ff9f02d62 49 return ylp1;
wiesdat 0:bed29da02e8b 50 }
wiesdat 0:bed29da02e8b 51
wiesdat 0:bed29da02e8b 52 void viewer()
wiesdat 0:bed29da02e8b 53 {
wiesdat 0:bed29da02e8b 54 scope.set(0,emg_value);
wiesdat 3:9a7ed524bb30 55 scope.set(1,yhp1);
wiesdat 3:9a7ed524bb30 56 scope.set(2,ylp1);
wiesdat 3:9a7ed524bb30 57 scope.set(3,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 emg_value = readEMG();
wiesdat 3:9a7ed524bb30 76 yhp1 = hpfilter1(emg_value); //function hpfilter
wiesdat 3:9a7ed524bb30 77 ylp1 = lpfilter1(yhp1); //function filter
wiesdat 3:9a7ed524bb30 78 ysum = ysum+abs(ylp1);
wiesdat 2:620ff9f02d62 79 n++;
wiesdat 2:620ff9f02d62 80
wiesdat 3:9a7ed524bb30 81 if(n==100) {
wiesdat 3:9a7ed524bb30 82 yave = ysum/100;
wiesdat 2:620ff9f02d62 83 yave = yave;
wiesdat 2:620ff9f02d62 84 ysum = 0;
wiesdat 2:620ff9f02d62 85 n = 0;
wiesdat 2:620ff9f02d62 86 }
wiesdat 2:620ff9f02d62 87
wiesdat 2:620ff9f02d62 88
wiesdat 2:620ff9f02d62 89 viewer(); //functie hidscope
wiesdat 2:620ff9f02d62 90
wiesdat 1:bc2db3bff4bb 91 }
wiesdat 0:bed29da02e8b 92 }