State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Revision:
35:38a5af0afee8
Child:
36:691ea4660f29
diff -r 720a410c4980 -r 38a5af0afee8 EMG_calibration.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EMG_calibration.h	Thu Nov 01 08:50:20 2018 +0000
@@ -0,0 +1,114 @@
+#pragma once
+
+#include "mbed.h"
+#include "MODSERIAL.h"
+#include "Screen.h"
+#include "EMGFilter.h"
+
+//Define objects
+InterruptIn Button1(D2);            // Button on interface
+Ticker      calibration_timer;
+Timeout     calibration_bicep;      // Timeout to get to relax state 
+Timeout     end_of_calibration;     // Timeout to get to final state
+Screen screen(D14, D15, D9);
+
+//Variables
+    volatile float EMG_signal1;
+    volatile float EMG_min_relax=1.000;
+    volatile float EMG_min_tense=1.000;
+    volatile float EMG_max_relax=0.000;
+    volatile float EMG_max_tense=0.000;
+    volatile float EMG_threshold; 
+// IN STATE MACHINE:
+EMGFilter emg_input = EMGFilter(A0);         // Voor kalibratie van bicep 1
+//EMGFilter emg_input = EMGFilter(A1);         // Voor kalibratie van bicep 2
+
+void tense_calibration()
+{
+    EMG_signal1 = emg_input.get_envelope_emg();
+    if (EMG_max_tense < EMG_signal1)
+    {
+        EMG_max_tense=EMG_signal1;
+    }
+    if (EMG_min_tense > EMG_signal1)
+    {
+        EMG_min_tense=EMG_signal1;
+    }
+    screen.get_screen_handle()->setTextCursor(0, 0);    
+    screen.get_screen_handle()->printf("Tense                ");
+    screen.get_screen_handle()->printf("EMG maximum= %.6f",EMG_max_tense);
+    screen.get_screen_handle()->printf("EMG minimum= %.6f",EMG_min_tense);
+    screen.get_screen_handle()->printf("                     ",EMG_min_tense);
+    screen.display();
+}
+
+void relax_calibration()
+{
+    EMG_signal1 = emg_input.get_envelope_emg();
+    if (EMG_max_relax < EMG_signal1)
+    {
+        EMG_max_relax=EMG_signal1;
+    }
+    if (EMG_min_relax > EMG_signal1)
+    {
+        EMG_min_relax=EMG_signal1;
+    }
+    EMG_threshold=(((EMG_min_tense-EMG_max_relax)/2)+EMG_max_relax);
+    screen.get_screen_handle()->setTextCursor(0, 0);    
+    screen.get_screen_handle()->printf("Relax                ");
+    screen.get_screen_handle()->printf("EMG maximum= %.6f",EMG_max_relax);
+    screen.get_screen_handle()->printf("EMG minimum= %.6f",EMG_min_relax);
+    screen.get_screen_handle()->printf("threshold= %.6f",EMG_threshold);
+    screen.display();
+}
+
+void TheFinalCalibration()
+    {
+        calibration_bicep.detach();
+        calibration_timer.detach();
+        screen.get_screen_handle()->setTextCursor(0, 0);    
+        screen.get_screen_handle()->printf("threshold= %.6f",EMG_threshold);
+        screen.get_screen_handle()->printf("                     ");
+        screen.get_screen_handle()->printf("                     "); 
+        screen.get_screen_handle()->printf("                     "); 
+        screen.display();
+        emg_input.set_threshold(EMG_threshold);
+    }
+
+void relax_measure()
+{
+    calibration_timer.detach(); 
+    screen.get_screen_handle()->setTextCursor(0, 0);    
+    screen.get_screen_handle()->printf("Relax muscle         ",EMG_max_relax);    
+    screen.get_screen_handle()->printf("                     ",EMG_min_tense);
+    screen.get_screen_handle()->printf("                     ",EMG_min_tense);
+    screen.get_screen_handle()->printf("                     ",EMG_min_tense);
+    screen.display();
+    wait(2.5f);
+    calibration_timer.attach(&relax_calibration, 0.1);
+    end_of_calibration.attach(TheFinalCalibration,5.0f);
+}
+
+void tense_measure()
+{
+    calibration_timer.detach(); 
+    screen.get_screen_handle()->setTextCursor(0, 0);    
+    screen.get_screen_handle()->printf("Tense muscle         ",EMG_max_relax);
+    screen.get_screen_handle()->printf("                     ",EMG_min_tense);
+    screen.get_screen_handle()->printf("                     ",EMG_min_tense);
+    screen.get_screen_handle()->printf("                     ",EMG_min_tense);    
+    screen.display();
+    wait(2.5f);
+    calibration_timer.attach(&tense_calibration, 0.1);  
+    calibration_bicep.attach(&relax_measure,5);
+}
+
+int main()
+{   
+    emg_input.start(0.005);
+    while(1)
+    {
+        Button1.fall(&tense_measure);
+    }
+//    return 0;
+}
\ No newline at end of file