Project Paint / Mbed 2 deprecated EMG-Processing

Dependencies:   biquadFilter mbed

Revision:
1:984b6b6812c7
Parent:
0:44d3f99b08c1
Child:
2:8b790c03a760
--- a/calibrate.cpp	Wed Nov 02 11:08:53 2016 +0000
+++ b/calibrate.cpp	Wed Nov 02 13:19:42 2016 +0000
@@ -1,13 +1,16 @@
 #include "mbed.h"
 #include "BiQuad.h"
 
-InterruptIn button(SW2);
+DigitalIn calibrating(SW2);
+InterruptIn calibrateButton(SW3);
 AnalogIn calibrateEmg1(A0);
 AnalogIn calibrateEmg2(A1);
 Serial pc(USBTX, USBRX);
 
 DigitalOut led_red(LED_RED);
 DigitalOut led_green(LED_GREEN);
+DigitalOut led_blue(LED_BLUE);
+
 
 
 BiQuadChain calibrateBqc1;
@@ -19,7 +22,8 @@
 float calibrateEmgCache1[calibrateNumEmgCache]; //sorted from new to old;
 float calibrateEmgCache2[calibrateNumEmgCache]; //sorted from new to old;
 
-
+volatile float threshold1 = 0.2;
+volatile float threshold2 = 0.2;
 
 Ticker sampler;
 
@@ -96,20 +100,89 @@
     sampler.detach();
     average1 = meanC(calibrateEmgCache1, calibrateNumEmgCache);
     average2 = meanC(calibrateEmgCache2, calibrateNumEmgCache);
+    
     pc.printf ("(avg1, avg2) = (%f, %f)\r\n", average1, average2); //Why NaN? am I deviding by zero?
 }
 
-//int main()
-//{
-//    pc.baud(115200);
-//    led_red = false;
-//    led_green = true;
+void calibrate() {
+    while(calibrating) {
+        led_red = false;
+        wait(0.5);    
+        led_red = true;
+        wait(0.5);    
+    }
+    
+    // Button pressed for rest measurement
+    led_red = true;
+    sampler.attach(&sample, Ts);
+    led_blue = false;
+    wait(10);
+    // 10 seconds sampled
+    led_blue = true;
+    sampler.detach();
+    float restAvg1 = meanC(calibrateEmgCache1, calibrateNumEmgCache);
+    float restAvg2 = meanC(calibrateEmgCache2, calibrateNumEmgCache);
+    
+    int i =0;
+    while(i<3) {
+        led_green = false;
+        wait(0.5);    
+        led_green = true;
+        wait(0.5);    
+        i++;
+    }
+    led_green = true;
+    
+    while(calibrating) {
+        led_red = false;
+        wait(0.5);    
+        led_red = true;
+        wait(0.5);    
+    }
+    // Button pressed for contracted measurement    
+    led_red = true;
+    sampler.attach(&sample, Ts);
+    led_blue = false;
+    wait(10);
+    
+    // 10 seconds sampled
+    led_blue = true;
+    sampler.detach();
+    
+    i =0;
+    while(i<3) {
+        led_green = false;
+        wait(0.5);    
+        led_green = true;
+        wait(0.5);    
+        i++;
+    }
+    
+    float contAvg1 = meanC(calibrateEmgCache1, calibrateNumEmgCache);
+    float contAvg2 = meanC(calibrateEmgCache2, calibrateNumEmgCache);
+    
+    threshold1 = (contAvg1 + restAvg1)/2;
+    threshold2 = (contAvg2 + restAvg2)/2;    
+    pc.printf("threshold1: %f\tthreshold2:%f\n\r", threshold1, threshold2);
+
+}    
+
+int main()
+{
+    pc.baud(115200);
+    led_red = true;
+    led_green = true;
+    led_blue = true;
+    calibrateButton.fall(&calibrate);
+    
+    calibrate();
+    pc.printf("threshold1: %f\tthreshold2:%f\n\r", threshold1, threshold2);
+
 //
 //    calibrateBqc1.add( &calibrateBq11 );
 //    calibrateBqc2.add( &calibrateBq12 );
 //        
 //    button.rise(&onRelease);
-//    button.fall(&onPress);
 //
-//    while(true);
-//}
\ No newline at end of file
+    while(true);
+}
\ No newline at end of file