state switch test, works but switches 2 times

Dependencies:   EMGFilter HIDScope MODSERIAL biquadFilter mbed

Revision:
1:90041f90ecf2
Parent:
0:f49bda586fc5
--- a/main.cpp	Thu Oct 26 10:09:33 2017 +0000
+++ b/main.cpp	Fri Oct 27 08:13:15 2017 +0000
@@ -11,6 +11,7 @@
 enum r_states {R_HORIZONTAL, R_VERTICAL, R_BASE};
 r_states state;
 
+
 //Define EMG inputs
 EMG_filter emg1(A0);
 EMG_filter emg2(A1);
@@ -25,7 +26,8 @@
 
 //Define ticker1
 Ticker mainticker;      //Ticker that calls every function. Functions are called by means of a variable named go, e.g. go_EMG
-Timeout calibrationgo;      // Timeout that will determine the duration of the calibration.
+Timeout maintimeout;      // Timeout that will determine the duration of the calibration.
+Timeout testtimeout;
 
 //Define ticker variables
 bool go_EMG;                    // Boolean to put on/off the EMG readout
@@ -70,44 +72,53 @@
 {
 
 }
-
+/*
+void r_processStateSwitchGO()   // function will be runned after 3 seconds.
+{
+    
+    go_switch = false;          // then it will stop again
+    ledstatedef = 1;            // now you are definitely in a state and motor controll will begin again.
+    ledstateswitch = 0;   
+}
+*/
 void r_processStateSwitch()
 {
-    while(go_switch) {
+    
+    if(go_switch == true) {
         switch(state) {
             case R_HORIZONTAL:
                 state = R_VERTICAL;
                 ledblue = 1;
                 ledred = 1;
                 ledgreen = 0;
-                pc.printf("state is vertical");
+                //pc.printf("state is vertical\r\n");
                 break;
             case R_VERTICAL:
                 state = R_BASE;
                 ledgreen = 1;
                 ledblue = 1;
                 ledred = 0;
-                pc.printf("state is base");
+                //pc.printf("state is base\r\n");
                 break;
             case R_BASE:
                 state = R_HORIZONTAL;
                 ledgreen = 1;
                 ledred = 1;
                 ledblue = 0;
-                pc.printf("state is horizontal");
+                //pc.printf("state is horizontal\r\n");
                 break;
-                }
-                wait(3.0f);
-                go_switch = false;
-                ledstatedef = 1;
-                ledstateswitch = 0;
-        }   
+        }
+            go_switch = false;                      // otherwise it will see a lot of values over 0.7 so it will switch states 100 times per peak. 
+            ledstatedef = 1;            // now you are definitely in a state and motor controll will begin again.
+            ledstateswitch = 0; 
+    }
 }
 
 void processEMG ()
 {
-    r_processStateSwitch();
+
     if (go_EMG) {
+     
         //go_EMG = false;                   //set the variable to false --> misschien nodig?
         emg1.filter();                      //filter the incoming emg signal
         emg2.filter();
@@ -124,13 +135,13 @@
 
 int main()
 {
-    pc.baud(115200);                    // Set baudrate for proper communication
+    pc.baud(9600);                    // Set baudrate for proper communication
     go_EMG = true;                      // Setting ticker variables
     go_calibration = true;              // Setting the timeout variable
-    calibrationgo.attach(&calibrationGO, 5.0);     // Attach calibration timeout to calibration function
-    mainticker.attach(&calibrationEMG, 0.001f);      // Attach ticker to the calibration function
-    wait(5.0);                                         // Wait for 5.0 seconds to make time for calibration
-    mainticker.attach(&processEMG, 0.001f);         // Attaching mainticker to function processEMG, calling  the function 1000 times a second
+    maintimeout.attach(&calibrationGO, 5.0);     // Attach calibration timeout to calibration function
+    mainticker.attach(&calibrationEMG, 0.001f);    // Attach ticker to the calibration function
+    wait(5.0);                                     // Wait for 5.0 seconds to make time for calibration
+    mainticker.attach(&processEMG, 0.001f);        // Attaching mainticker to function processEMG, calling  the function 1000 times a second
 
     while (true) {
         ledstatedef = 1;
@@ -138,10 +149,14 @@
             ledstateswitch = 1;
             ledstatedef = 0;
             go_switch = true;
-
+            r_processStateSwitch(); 
+            wait(2.0f);
+        
+             //r_processStateSwitchGO();           
         }
+        pc.printf("go_Switch = %d, emg1 = %f, emg2 = %f \r\n", go_switch, emg1.normalized, emg2.normalized);
 
-
+        state = R_VERTICAL;
         switch(state) {
             case R_HORIZONTAL:
                 r_movehorizontal();
@@ -152,7 +167,7 @@
             case R_BASE:
                 r_movebase();
                 break;
-                       }
+        }
 
 
     }