emg2

Dependencies:   HIDScope biquadFilter mbed QEI

Fork of EMG by Tom Tom

Revision:
28:433d12c52913
Parent:
27:6b4814ef266d
Child:
29:9010b7bdbfbb
--- a/main.cpp	Tue Oct 30 10:06:24 2018 +0000
+++ b/main.cpp	Tue Oct 30 10:23:46 2018 +0000
@@ -10,6 +10,10 @@
 AnalogIn emg1_in( A1 );
 AnalogIn emg2_in( A2 );
 
+DigitalOut led1(LED_BLUE);
+DigitalOut led2(LED_RED);
+DigitalOut led3(LED_GREEN);
+
 // Constants EMG
 const double m1 = 0.5000;
 const double m2 = -0.8090;
@@ -136,7 +140,80 @@
 
 
 
-void MovAg() //Void to make a moving average
+void Switching() //Void to switch between signals to calibrate
+{
+    g++;
+    if (g == 0) 
+    { //If g = 0, led is blue
+        led1=0;
+        led2=1;
+        led3=1;
+    } 
+    else if(g == 1) 
+    { //If g = 1, led is red
+        led1=1;
+        led2=0;
+        led3=1;
+
+    } 
+    else if(g == 2) 
+    { //If g = 2, led is green
+        led1=1;
+        led2=1;
+        led3=0;
+    } 
+    else 
+    { //If g > 3, led is white
+        led1=0;
+        led2=0;
+        led3=0;
+    }
+}
+void Calibration(void) //Void to calibrate the signals, depends on value g. While calibrating, maximal contraction is required
+
 {
-
+    switch (g) {
+        case 0: { //Case zero, calibrate EMG signal of right biceps
+            sum = 0.0;
+            for (int j = 0; j<=sizeCali-1; j++) {
+                //For statement to make an array of the latest datapoints of the filtered signal
+                StoreCali0[j] = low0; //Stores the latest datapoint in the first element of the array
+                sum+=StoreCali0[j]; //Sums the elements in the array
+                wait(0.001f);
+            }
+            Mean0 = sum/sizeCali; //Calculates the mean of the signal
+            Threshold0 = Mean0/2; //Determines the threshold (factor 0.5)
+            break;
+    }
+        case 1: { //Case one, calibrate EMG signal of left biceps
+            sum = 0.0;
+            for(int j=0; j<=sizeCali-1; j++) {
+                StoreCali1[j] = low1;
+                sum+=StoreCali1[j];
+                wait(0.001f);
+            }
+            Mean1 = sum/sizeCali;
+            Threshold1 = Mean1/2;
+            break;
+    }
+        case 2: { //case two, calibrate EMG signal of calf
+            sum = 0.0;
+            for(int j=0; j<=sizeCali-1; j++) {
+                StoreCali2[j] = low2;
+                sum+=StoreCali2[j];
+                wait(0.001f);
+            }   
+            Mean2 = sum/sizeCali;
+            Threshold2 = Mean2/2;
+            break;
+    }
+        case 3: { //Sets calibration value to 1; robot can be set to Home position
+            emg_calib=1;
+            wait(0.001f);
+            break;
+}
+default: { //Ensures nothing happens if g is not equal to 0,1 or 2.
+break;
+}
+}
 }
\ No newline at end of file