Dit is alleen het EMG gedeelte

Dependencies:   mbed HIDScope biquadFilter MODSERIAL FXOS8700Q

Revision:
28:59e8266f4633
Parent:
27:f18da01093c9
Child:
29:f51683a6cbbf
diff -r f18da01093c9 -r 59e8266f4633 main.cpp
--- a/main.cpp	Mon Oct 28 13:37:21 2019 +0000
+++ b/main.cpp	Mon Oct 28 14:27:21 2019 +0000
@@ -218,10 +218,10 @@
         double emg3_rectify = fabs( emg3_hp );           // Rectify
         emg3_env = bqc3_low.step( emg3_rectify ); // Filter lowpass (completes envelope)
 
-        scope.set(0, emg1_n);
-        scope.set(1, emg2_n);
-
-        scope.set(2, emg1_env );
+        // Set HIDScope outputs
+        scope.set(0, emg1_n ); // One notch filtered EMG to check 50Hz disturbance
+        scope.set(1, emg1_env );
+        scope.set(2, emg2_env );
         scope.set(3, emg2_env );
         scope.send();
 
@@ -324,27 +324,19 @@
 // Run calibration of EMG
 void do_emg_cal()
 {
+    // Entry functions
     if ( emg_state_changed == true ) {
-        emg_state_changed = false;
-        // pc.printf("Starting calibration :");
+        emg_state_changed = false; // Disable entry functions
         led_b = 0; // Turn on calibration led
+
         timerCalibration.reset();
-        timerCalibration.start();
+        timerCalibration.start(); // Sets up timer to stop calibration after Tcal seconds
         sampleNow = true; // Enable signal sampling in sampleSignal()
-        calibrateNow = true;
+        calibrateNow = true; // Enable calibration vector functionality in sampleSignal()
 
-        emg1_cal.reserve(Fs * Tcal);
+        emg1_cal.reserve(Fs * Tcal); // Initialize vector lengths to prevent memory overflow
         emg2_cal.reserve(Fs * Tcal);
         emg3_cal.reserve(Fs * Tcal);
-
-        switch( emg_curr_state ) {
-            case emg_cal_MVC:
-                // pc.printf("MVC\r\n");
-                break;
-            case emg_cal_rest:
-                // pc.printf("Rest\r\n");
-                break;
-        }
     }
 
     // Allemaal dingen doen tot de end conditions true zijn
@@ -367,9 +359,9 @@
 {
     // Entry function
     if ( emg_state_changed == true ) {
-        emg_state_changed = false;
+        emg_state_changed = false; // Disable entry functions
         double margin_percentage = 10; // Set up % margin for rest
-        
+
         emg1_factor = 1 / emg1_MVC; // Factor to normalize MVC
         emg1_th = emg1_rest * emg1_factor + margin_percentage/100; // Set normalized rest threshold
         emg2_factor = 1 / emg2_MVC; // Factor to normalize MVC
@@ -379,19 +371,22 @@
     }
 
     // Do stuff until end condition is met
-    
-    double emg1_norm = emg1_env * emg1_factor;
+    double emg1_norm = emg1_env * emg1_factor; // Normalize EMG signal with calibrated factor
     double emg2_norm = emg2_env * emg2_factor;
     double emg3_norm = emg3_env * emg3_factor;
-    
-    if ( emg1_norm < emg1_th ) {
+
+    // Set normalized EMG output signal (CAN BE MOVED TO EXTERNAL FUNCTION BECAUSE IT IS REPEATED 3 TIMES)
+    if ( emg1_norm < emg1_th ) { // If below threshold, emg_out = 0 (ignored)
         emg1_out = 0.0;
-    } else if ( emg1_norm > 1.0f ) {
+    } else if ( emg1_norm > 1.0f ) { // If above MVC (due to filtering perhaps), emg_out = 1 (max value)
         emg1_out = 1.0;
-    } else {
+    } else { // If in between threshold and MVC, scale EMG signal accordingly
+        // Inputs may be in range       [emg_th, 1]
+        // Outputs are scaled to range  [0,      1]
         emg1_out = rescale(emg1_norm, emg1_th, 1, 0, 1);
     }
-    
+
+    // Idem for emg2
     if ( emg2_norm < emg2_th ) {
         emg2_out = 0.0;
     } else if ( emg2_norm > 1.0f ) {
@@ -399,7 +394,8 @@
     } else {
         emg2_out = rescale(emg2_norm, emg2_th, 1, 0, 1);
     }
-    
+
+    // Idem for emg3
     if ( emg3_norm < emg3_th ) {
         emg3_out = 0.0;
     } else if ( emg3_norm > 1.0f ) {
@@ -408,11 +404,18 @@
         emg3_out = rescale(emg3_norm, emg3_th, 1, 0, 1);
     }
 
+    // Set HIDScope outputs
+    scope.set(0, emg1_env ); // One envelope for reference
+    scope.set(1, emg1_out );
+    scope.set(2, emg2_out );
+    scope.set(3, emg3_out );
+    scope.send();
+
+
     // State transition guard
     if ( false ) {
-        emg_curr_state = emg_wait;
-        emg_state_changed = true;
-        // More functions
+        emg_curr_state = emg_wait; // Set next state
+        emg_state_changed = true; // Enable entry function
     }
 }