script zover

Dependencies:   HIDScope MODSERIAL mbed

Revision:
2:620ff9f02d62
Parent:
1:bc2db3bff4bb
Child:
3:9a7ed524bb30
Child:
5:9415e5be8235
--- a/main.cpp	Mon Oct 13 11:02:05 2014 +0000
+++ b/main.cpp	Wed Oct 15 13:35:09 2014 +0000
@@ -2,42 +2,59 @@
 #include "MODSERIAL.h"
 #include "HIDScope.h"
 
+#define A1LP    0.181269246922018
+#define B1LP    -0.818730753077982
+
 #define A1HP -1.999214978283642
-#define A0HP 0.999214978283642
+#define A0HP   0.999214978283642
 #define B1HP -1.943446330323158
 #define B0HP 0.945001709500376
-#define TSAMP 0.01
 
+#define TSAMP 0.0001
 
 AnalogIn    emg0(PTB1);
 MODSERIAL pc(USBTX,USBRX);
-HIDScope scope(2);
+HIDScope scope(5);
 Ticker timer;
 float filter(float);
 float emg_value;
-static float y,x1=0,y1=0,x2=0,y2=0;
 volatile bool looptimerflag;
-
+float ysum = 0, yave=0 ;
 
-void read()
+uint16_t readEMG()
 {
-    emg_value=emg0.read_u16();
+
+    return emg0.read_u16();
 }
 
-float filter(float emg0_value)
+float hpfilter(float emg0_value)
 {
+    static float y,x1=0,y1=0,x2=0,y2=0;
     y = y1*B1HP-y2*B0HP-x1*A1HP+x2*A0HP;
     x2 = x1;
     x1 = emg0_value;
     y2 = y1;
     y1 = y;
-    return y;
+    return yhp1;
+}
+
+float lpfilter(float yhp1)
+{
+    static float y,x1=0,y1=0,x=0;
+    y = A1LP*x1lp+y1lp*B1LP;
+    y1 = y;
+    x1 = x;
+    x = y;
+    return ylp1;
 }
 
 void viewer()
 {
     scope.set(0,emg_value);
-    scope.set(1,y);
+    scope.set(1,yhp);
+    scope.set(2,ylp);
+    scope.set(3,ysum);
+    scope.set(4,yave);
     scope.send();
 }
 
@@ -50,12 +67,27 @@
 {
     pc.baud(115200);
     timer.attach(setlooptimerflag,TSAMP);
-    //timer.attach(scope,0.05);
+    int n;
     while(1) {
-        while(!looptimerflag)
-            looptimerflag = false;
-        read();
-        y = filter(emg_value);
-        viewer();
+
+        while(!looptimerflag) /* do nothing */;
+        looptimerflag = false;
+        //read();                     //waardes inladen
+        emg_value =  readEMG();
+        yhp1 = hpfilter(emg_value);  //functie hpfilter
+        ylp1 = lpfilter(yhp1);         //functie filter
+        ysum = ysum+abs(ylp);
+        n++;
+
+        if(n==1000) {
+            yave = ysum/1000;
+            yave = yave;
+            ysum = 0;
+            n = 0;
+        }
+
+
+        viewer();                  //functie hidscope
+
     }
 }
\ No newline at end of file