the emg filtering part of the program

Dependencies:   HIDScope biquadFilter mbed MODSERIAL

Fork of EMG by Tom Tom

Revision:
23:54d28f9eef53
Parent:
22:f38a15e851d2
Child:
24:01b4b51b5dc6
--- a/main.cpp	Wed Oct 19 19:33:25 2016 +0000
+++ b/main.cpp	Wed Oct 19 20:56:47 2016 +0000
@@ -2,6 +2,7 @@
 #include "HIDScope.h"
 #include "BiQuad.h"
 #include "MODSERIAL.h"
+#include <string.h>
 
 //Define objects
 AnalogIn    emg1( A0 );
@@ -14,7 +15,10 @@
 DigitalOut  led(LED1);
 
 volatile bool sampletimer = false;
+double threshold = 0.04;
+double samplefreq=0.002;
 
+typedef enum { STATE_CALIBRATION, STATE_PAUZE, STATE_X, STATE_X_NEG, STATE_Y, STATE_Y_NEG, STATE_XY, STATE_XY_NEG } states;
 
 BiQuadChain bqc11;
 BiQuadChain bqc13;
@@ -61,8 +65,9 @@
 }
 
 
-void sample()
+void sample(states &mystate)
 {
+
     /* Read the emg signals and filter it*/
 
     scope.set(0, emg1.read());    //original signal
@@ -82,11 +87,30 @@
     scope.send();
     /*   To indicate that the function is working, the LED is toggled */
     led = !led;
-    if (emg02>0.08||emg12>0.08||emg22>0.08) {
-        pc.printf("het werkt\n");
+    if (emg02>threshold) {
+        if (emg12>threshold&&emg22>threshold) {
+             mystate = STATE_XY_NEG;
+        } else if (emg12>threshold) {
+             mystate = STATE_X_NEG;
+
+        } else if (emg22>threshold) {
+             mystate = STATE_Y_NEG;
+        } else {
+             mystate = STATE_PAUZE;
+        }
     } else {
-        pc.printf("het werkt niet\n");
+        if (emg12>threshold&&emg22>threshold) {
+             mystate = STATE_XY;
+        } else if (emg12>threshold) {
+             mystate = STATE_X;
+
+        } else if (emg22>threshold) {
+             mystate = STATE_Y;
+        } else {
+             mystate = STATE_PAUZE;
+        }
     }
+
     sampletimer = false;
 }
 
@@ -94,6 +118,9 @@
 {
     pc.baud(115200);
 
+    // create a variable called 'state', define it
+    states mystate = STATE_CALIBRATION;
+
     //de biquad chains instellen
     bqc11.add( &bq111 ).add( &bq112 ).add( &bq113 ).add( &bq121 );
     bqc13.add( &bq131);
@@ -104,12 +131,40 @@
     /*Attach the 'sample' function to the timer 'sample_timer'.
       this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
     */
-    sample_timer.attach(&sampleflag, 0.002);
+    sample_timer.attach(&sampleflag, samplefreq);
 
     while(1) {
         if (sampletimer==true) {
-            sample();
+            sample(mystate);
 
+            // switch, case
+            switch (mystate) {
+                case STATE_CALIBRATION : // calibration
+                    pc.printf("calibration");
+                    wait(0.5f);
+                    break;
+                case STATE_X : // run
+                    pc.printf("X");
+                    break;
+                case STATE_X_NEG : // run
+                    pc.printf("Xneg");
+                    break;
+                case STATE_Y : // execute mode 1
+                    pc.printf("Y");
+                    break;
+                case STATE_Y_NEG : // execute mode 1
+                    pc.printf("Yneg");
+                    break;
+                case STATE_XY : // execute mode 2
+                    pc.printf("XY");
+                    break;
+                case STATE_XY_NEG : // execute mode 2
+                    pc.printf("XYneg");
+                    break;
+                case STATE_PAUZE : // default
+                    pc.printf("pauze");
+                    break;
+            }
         }
     }
 }
\ No newline at end of file