Chris Verhoeven / Mbed 2 deprecated Filter

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of Filter by Rick Koetsier

Revision:
5:7c8176cdaa1c
Parent:
4:285fb7d84088
Child:
6:b9a84c1cb4f9
--- a/Calibrationscript.cpp	Tue Oct 31 10:17:15 2017 +0000
+++ b/Calibrationscript.cpp	Tue Oct 31 13:41:07 2017 +0000
@@ -2,6 +2,7 @@
 #include "HIDScope.h"                      //require the HIDScope library
 #include "MODSERIAL.h"
 #include "BiQuad.h"
+# include "math.h"
 
 //                       DEFINING                  
 //Define Inputs
@@ -20,7 +21,7 @@
 Ticker      sample_timer;                       // Taking samples
 Ticker      LED_timer;                          // Write the LED
 Ticker      calibration_timer;                  // Check when to start calibration
-//MODSERIAL pc(USBTX,USBRX);
+
 
 //Define HIDscope
 //HIDScope    scope(2);                           //instantize a 2-channel HIDScope object
@@ -32,24 +33,26 @@
 BiQuad bq2_high(0.96508, -1.93016, 0.96508, -1.92894, 0.93138);
 BiQuad bq3_notch(0.91859, -1.82269, 0.91859, -1.82269, 0.83718);
 
+double print; 
 double emgFiltered;
 double filteredAbs;
 double emg_value;
 double onoffsignal;
-double maxcal=0;        
-bool aboveTreshold=0;
+     
+bool check_calibration=0;
+double avg_emg;
 
 //                      FUNCTIONS                
 //function for filtering
 
 void filter(){                                      
-    if(aboveTreshold==1){
+    if(check_calibration==1){
         
         emg_value = emg.read();
         emgFiltered = bqc.step( emg_value );
         filteredAbs = abs( emgFiltered );
     
-        onoffsignal=filteredAbs/maxcal;             //divide the emg signal by the max EMG to calibrate the signal per person
+        onoffsignal=filteredAbs/avg_emg;             //divide the emg signal by the max EMG to calibrate the signal per person
 //        scope.set(0,emg_value);                     //set emg signal to scope in channel 1
  //       scope.set(1,onoffsignal);                   //set filtered signal to scope in channel 2
  //       scope.send();                               //send the signals to the scope
@@ -58,13 +61,13 @@
 }
 
 //function to check the threshold
-void checkthreshold(){                                  
-    if(aboveTreshold==1){                                      //if signal passes threshold value, red light goes on
-            if(onoffsignal<=0.25){
+void check_emg(){                                  
+    if(check_calibration==1){                                      //if signal passes threshold value, red light goes on
+            if(onoffsignal<=0.5){
                     led1.write(1);
                     led2.write(0);
             }
-            else if(onoffsignal >= 0.5){                //if signal does not pass threshold value, blue light goes on
+            else if(onoffsignal > 0.5){                //if signal does not pass threshold value, blue light goes on
                     led1.write(0);
                     led2.write(1);
             }
@@ -72,44 +75,61 @@
 }
 
 //function to calibrate
-void calibration(){
-    double sum_array;                                            
-    if(button1.read()==false){
+int calibration(){
+   pc.printf("check1\n\r");
+                                            
+   // if(button1.read()==false){
+    
+        
+        
         led3.write(0);
-        double signal_verzameling[5000];
-        for(int n =0; n<5000;n++){                          //read for 5000 samples as calibration
-            emg_value = emg.read();
-            emgFiltered = bqc.step( emg_value );
-            filteredAbs = abs( emgFiltered );
+       
+        double signal_verzameling = 0;
+        for(int n =0; n<5000;n++){   
            
-            double signalmeasure = filteredAbs;
-             signal_verzameling[n]= signalmeasure;
-            }
-            double lengte_array = sizeof(signal_verzameling);
-           
+                              //read for 5000 samples as calibration
+           emg_value = emg.read();
+            emgFiltered = bqc.step( emg_value );
+            filteredAbs = abs(emgFiltered);
+         
+          
+           // signal_verzameling[n]=  filteredAbs;
+             signal_verzameling = signal_verzameling + filteredAbs ;
+              pc.printf("signal_verzameling = %f \n\r",filteredAbs);
+              wait(0.0005);
+              if (n == 4999){
+                  avg_emg = signal_verzameling / n;
+                  pc.printf("avg_emg = %f\n\r",avg_emg);
+                  }
+            }  
+          
+             led3.write(1);
+           // double lengte_array = sizeof(signal_verzameling);
+      //     pc.printf("lengte_array = %f\n\r",lengte_array);
             
-            for(int i = 1; i < lengte_array; i++){
-       //^^should be 0
-       double temp_a = signal_verzameling[i];
-          sum_array += temp_a ;
-            }
-        double avg = sum_array / lengte_array;
+        //    for(int i = 0; i < lengte_array; i++){
+      
+     
+      //    sum_array = sum_array + signal_verzameling[i] ;
+      //      }
+//avg_emg = sum_array / lengte_array;
+     //   pc.printf("avg_emg = %f\n\r",avg_emg);
         
         
         
         
-        
-        aboveTreshold=1;
+        check_calibration=1;
         led3.write(1);
-    }  
+   // }
+    return 0;  
 }
  
 //                         MAIN                      
  
 int main(){
-//    pc.baud(115200);
+     pc.baud(115200);
 
-//    pc.printf("Lampjes zijn langs geweest");
+pc.printf("Lampjes zijn langs geweest");
     led1.write(1);
     led2.write(1);
     led3.write(1);
@@ -119,11 +139,11 @@
     bqc.add( &bq1_low ).add( &bq2_high ).add( &bq3_notch );
 
     sample_timer.attach(&filter, 0.002);                //continously execute the EMG reader and filter
-    LED_timer.attach(&checkthreshold, 0.002);         //continously execute the motor controller
-    calibration_timer.attach(&calibration, 0.002);              //ticker to check if EMG is being calibrated
+    LED_timer.attach(&check_emg, 0.002);         //continously execute the motor controller
+   //calibration_timer.attach(&calibration, 0.002);              //ticker to check if EMG is being calibrated
 //    pc.printf("%d",filteredAbs);
     
-    
+    calibration();
     
     while(1){     //while loop to keep system going