script zover

Dependencies:   HIDScope MODSERIAL mbed

Committer:
wiesdat
Date:
Fri Oct 17 10:23:42 2014 +0000
Revision:
5:9415e5be8235
Parent:
2:620ff9f02d62
Child:
6:6f0bc2e465b0
SCRIPT WERKT, SCOPES NIET???

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 5:9415e5be8235 4 #include <iostream>
wiesdat 0:bed29da02e8b 5
wiesdat 5:9415e5be8235 6 #define A1LP 0.221199216928595
wiesdat 5:9415e5be8235 7 #define B1LP -0.778800783071405
wiesdat 2:620ff9f02d62 8
wiesdat 5:9415e5be8235 9 #define A1HP -1.999801878951505
wiesdat 5:9415e5be8235 10 #define A0HP 0.999801878951505
wiesdat 5:9415e5be8235 11 #define B1HP -1.971717601075000
wiesdat 5:9415e5be8235 12 #define B0HP 0.972111984032897
wiesdat 1:bc2db3bff4bb 13
wiesdat 5:9415e5be8235 14 #define TSAMP 0.001
wiesdat 0:bed29da02e8b 15
wiesdat 0:bed29da02e8b 16 AnalogIn emg0(PTB1);
wiesdat 0:bed29da02e8b 17 MODSERIAL pc(USBTX,USBRX);
wiesdat 2:620ff9f02d62 18 HIDScope scope(5);
wiesdat 0:bed29da02e8b 19 Ticker timer;
wiesdat 5:9415e5be8235 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 5:9415e5be8235 24
wiesdat 5:9415e5be8235 25 float readEMG()
wiesdat 0:bed29da02e8b 26 {
wiesdat 2:620ff9f02d62 27
wiesdat 5:9415e5be8235 28 emg_value=emg0.read();
wiesdat 5:9415e5be8235 29 return emg_value;
wiesdat 0:bed29da02e8b 30 }
wiesdat 0:bed29da02e8b 31
wiesdat 5:9415e5be8235 32 float hpfilter1(float emg_value)
wiesdat 0:bed29da02e8b 33 {
wiesdat 5:9415e5be8235 34 static float y,x1=0,y1=0,x2=0, y2=0;
wiesdat 5:9415e5be8235 35 yhp1 = y1*B1HP-y2*B0HP-x1*A1HP+x2*A0HP;
wiesdat 0:bed29da02e8b 36 x2 = x1;
wiesdat 5:9415e5be8235 37 x1 = emg_value;
wiesdat 0:bed29da02e8b 38 y2 = y1;
wiesdat 0:bed29da02e8b 39 y1 = y;
wiesdat 2:620ff9f02d62 40 return yhp1;
wiesdat 2:620ff9f02d62 41 }
wiesdat 2:620ff9f02d62 42
wiesdat 5:9415e5be8235 43 float lpfilter1(float yhp1)
wiesdat 2:620ff9f02d62 44 {
wiesdat 2:620ff9f02d62 45 static float y,x1=0,y1=0,x=0;
wiesdat 5:9415e5be8235 46 ylp1 = A1LP*x1+y1*B1LP;
wiesdat 2:620ff9f02d62 47 y1 = y;
wiesdat 2:620ff9f02d62 48 x1 = x;
wiesdat 5:9415e5be8235 49 x = yhp1;
wiesdat 2:620ff9f02d62 50 return ylp1;
wiesdat 0:bed29da02e8b 51 }
wiesdat 0:bed29da02e8b 52
wiesdat 0:bed29da02e8b 53 void viewer()
wiesdat 0:bed29da02e8b 54 {
wiesdat 0:bed29da02e8b 55 scope.set(0,emg_value);
wiesdat 5:9415e5be8235 56 scope.set(1,yhp1);
wiesdat 5:9415e5be8235 57 scope.set(2,ylp1);
wiesdat 5:9415e5be8235 58 scope.set(3,yave);
wiesdat 5:9415e5be8235 59 scope.set(4,ysum);
wiesdat 0:bed29da02e8b 60 scope.send();
wiesdat 0:bed29da02e8b 61 }
wiesdat 0:bed29da02e8b 62
wiesdat 1:bc2db3bff4bb 63 void setlooptimerflag(void)
wiesdat 1:bc2db3bff4bb 64 {
wiesdat 1:bc2db3bff4bb 65 looptimerflag = true;
wiesdat 1:bc2db3bff4bb 66 }
wiesdat 1:bc2db3bff4bb 67
wiesdat 0:bed29da02e8b 68 int main()
wiesdat 0:bed29da02e8b 69 {
wiesdat 0:bed29da02e8b 70 pc.baud(115200);
wiesdat 1:bc2db3bff4bb 71 timer.attach(setlooptimerflag,TSAMP);
wiesdat 5:9415e5be8235 72 int n = 0;
wiesdat 0:bed29da02e8b 73 while(1) {
wiesdat 2:620ff9f02d62 74
wiesdat 2:620ff9f02d62 75 while(!looptimerflag) /* do nothing */;
wiesdat 2:620ff9f02d62 76 looptimerflag = false;
wiesdat 2:620ff9f02d62 77 emg_value = readEMG();
wiesdat 5:9415e5be8235 78 yhp1 = hpfilter1(emg_value); //function hpfilter
wiesdat 5:9415e5be8235 79 ylp1 = lpfilter1(emg_value); //function filter
wiesdat 5:9415e5be8235 80 ysum = ysum+yhp1;
wiesdat 2:620ff9f02d62 81 n++;
wiesdat 5:9415e5be8235 82
wiesdat 5:9415e5be8235 83 if(n==100) {
wiesdat 5:9415e5be8235 84 yave = ysum/100;
wiesdat 2:620ff9f02d62 85 ysum = 0;
wiesdat 2:620ff9f02d62 86 n = 0;
wiesdat 5:9415e5be8235 87 }
wiesdat 2:620ff9f02d62 88
wiesdat 5:9415e5be8235 89 viewer(); //functie hidscope
wiesdat 5:9415e5be8235 90
wiesdat 2:620ff9f02d62 91
wiesdat 1:bc2db3bff4bb 92 }
wiesdat 0:bed29da02e8b 93 }