emg2

Dependencies:   HIDScope biquadFilter mbed QEI

Fork of EMG by Tom Tom

Revision:
26:1eafb6111ae8
Parent:
25:02f183b944ed
Child:
27:6b4814ef266d
--- a/main.cpp	Tue Oct 30 08:25:31 2018 +0000
+++ b/main.cpp	Tue Oct 30 09:20:55 2018 +0000
@@ -58,6 +58,26 @@
 BiQuad L2( c1, c2, d0, d1, d2);
 BiQuad L3( c1, c2, d0, d1, d2);
 
+// EMG
+const int sizeMovAg = 150; //Size of array over which the moving average (MovAg) is calculated
+double sum, sum1, sum2, sum3; //Variables used in calibration and MovAg to sum the elements in the array
+double StoreArray0[sizeMovAg] = {}, StoreArray1[sizeMovAg] = {}, StoreArray2[sizeMovAg] = {};
+
+//Empty arrays to calculate MovAgs
+double Average0, Average1, Average2; //Outcome of MovAg
+const int sizeCali = 2000; //Size of array over which the Threshold will be calculated
+double StoreCali0[sizeCali] = {}, StoreCali1[sizeCali] = {}, StoreCali2[sizeCali] = {};
+//Empty arrays to calculate means in calibration
+
+double Mean0, Mean1, Mean2; //Mean of maximum contraction, calculated in the calibration
+double Threshold0 = 1, Threshold1 = 1, Threshold2 = 1; //Thresholds for muscles 0 to 2
+int g = 0; //Part of the switch void, where the current state can be changed
+int emg_calib=0; //After calibration this value will be 1, enabling the
+
+//EMG
+Ticker Filter_tick;
+Ticker MovAg_tick;
+
 // Filter of the first EMG signal
 void filtering()
 {
@@ -77,6 +97,27 @@
     low1 = L2.step(absolute1);
     low2 = L3.step(absolute2); 
     
+    for (int i = sizeMovAg-1; i>=0; i--) {
+//For statement to make an array of the last datapoints of the filtered signal
+StoreArray0[i] = StoreArray0[i-1]; //Shifts the i'th element one place to the right
+StoreArray1[i] = StoreArray1[i-1];
+StoreArray2[i] = StoreArray2[i-1];
+}
+StoreArray0[0] = low0; //Stores the latest datapoint in the first element of the array
+StoreArray1[0] = low1;
+StoreArray2[0] = low2;
+sum1 = 0.0;
+sum2 = 0.0;
+sum3 = 0.0;
+for (int a = 0; a<=sizeMovAg-1; a++) { //For statement to sum the elements in the array
+sum1+=StoreArray0[a];
+sum2+=StoreArray1[a];
+sum3+=StoreArray2[a];
+}
+Average0 = sum1/sizeMovAg; //Calculates an average over the datapoints in the array
+Average1 = sum2/sizeMovAg;
+Average2 = sum3/sizeMovAg;
+    
     scope.set( 0, emg0); // Sending the signal to the HIDScope 
     scope.set( 1, low0); // Change the numer of inputs on the top when necessary
     scope.set( 2, emg1);
@@ -91,4 +132,11 @@
     sample_timer.attach( &filtering, 0.002);
 
     while(1) {}
+}
+
+
+
+void MovAg() //Void to make a moving average
+{
+
 }
\ No newline at end of file