Eindelijk!!!!!

Fork of SignalNumber2 by Peter Knoben

Revision:
0:5f8dee4d4b09
Child:
1:15543c143a63
diff -r 000000000000 -r 5f8dee4d4b09 signalnumber.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/signalnumber.cpp	Mon Oct 30 10:41:44 2017 +0000
@@ -0,0 +1,163 @@
+#include "signalnumber.h"
+#include "mbed.h"
+
+
+//Constants for mean value
+const int n = 10;                   //Window size for the mean value
+float emg0_filtered[n] = {}; 
+int count = 0;
+int count2 = 0;
+//const float controller_TS = 0.1;
+float mean = 0.0;
+float sum = 0.0;
+
+
+//Constants EMG switch
+const float LeftFastmin=0.075;
+const float LeftFastmax=0.15;
+const float LeftSlowmin=0.2;
+const float LeftSlowmax=0.35;
+const float RightSlowmin=0.4;
+const float RightSlowmax=0.65;
+const float RightFastmin=0.7;
+const float RightFastmax=1.5;
+int Number = 0;
+//const int action =50;
+
+
+SignalNumber::SignalNumber(void)
+{
+
+}
+
+
+
+//Determine the sum of the last "n" amount of numbers.
+float SignalNumber::getsum(float array[], const int n){
+    float sum_math = 0.0;
+    for (int m=0 ; m<n ; m++ ){
+        sum_math = sum_math + array[m];
+    }
+    return sum_math;   
+}
+
+
+
+// Determine the mean value of the last "n" amount of numbers.
+float SignalNumber::getmean(const int n, float input){
+    emg0_filtered[count] = input;
+    count++;
+    if (count == n){
+        count = 0;
+    }
+    float mean_math = getsum(emg0_filtered,n)/n;
+    return mean_math;
+}
+
+
+float SignalNumber::calibrate(const int n, float input){
+    float offset = getmean(n, input);
+    return offset; 
+} 
+
+
+
+//Determine the right signal number
+/*
+There will be checked in which range the mean value lies. If the value lies 
+within this range for "action" samples the switch will be changed to the right
+mode. If the mean value changes outside of the region the switch will be set to 
+0, this is the resting state. Also the counter will be reset.
+*/
+int SignalNumber::getnumber(const int n, const int action, double input){
+    mean = getmean(n,input);
+    //Check first case
+    if( mean < LeftFastmin ) {
+        if (count2 <action){
+            mean = getmean(n, input);
+            if(mean < LeftFastmin){
+                count2++;
+            }
+            else{
+                count2=0;
+                Number=0;
+            } 
+        }
+        else{
+            Number = 0;
+            count2=0;
+        }
+    }
+     //Check second case  
+     else if(mean <= LeftFastmax and mean > LeftFastmin){
+        if (count2 <action){
+            mean = getmean(n,input);
+            if(mean <=LeftFastmax and mean>LeftFastmin){
+                count2++;
+            }
+            else{
+                count2=0;
+                Number=0;
+            } 
+        }
+        else{
+            Number = 1;
+            count2=0;
+        }
+    }
+     else if( mean <=LeftSlowmax and mean>LeftSlowmin) {
+        if (count2 <action){
+            mean = getmean(n,input);
+            if(mean <=LeftSlowmax and mean>LeftSlowmin){
+                count2++;
+            }
+            else{
+                count2=0;
+                Number=0;
+            } 
+        }
+        else{
+            Number = 2;
+            count2=0;
+        }
+     }
+     else if( mean <=RightSlowmax and mean>RightSlowmin) {
+        if (count2 <action){
+            mean = getmean(n,input);
+            if(mean <=RightSlowmax and mean>RightSlowmin){
+                count2++;
+            }
+            else{
+                count2=0;
+                Number=0;
+            } 
+        }
+        else{
+            Number = 3;
+            count2=0;
+        }
+     }
+     else if( mean <=RightFastmax and mean>RightFastmin ) {
+        if (count2 <action){
+            mean = getmean(n,input);
+            if(mean <=RightFastmax and mean>RightFastmin){
+                count2++;
+            }
+            else{
+                count2=0;
+                Number=0;
+            } 
+        }
+        else{
+            Number = 4;
+            count2=0;
+        }
+    }
+    else{
+        count2=0;
+        Number =0;
+    }
+    return Number;
+}       
+
+