new fork sure

Dependencies:   HIDScope MODSERIAL PID QEI biquadFilter mbed

Fork of cpfromralph by Remi van Veen

Revision:
27:ca07f895f999
Parent:
26:c9ba45bdd5c9
Child:
28:ec0763106a2e
--- a/main.cpp	Thu Nov 03 15:20:48 2016 +0000
+++ b/main.cpp	Tue Oct 10 13:46:49 2017 +0000
@@ -7,13 +7,14 @@
     AnalogIn    emg1_in( A0 ); /* read out the signal */
     AnalogIn    emg2_in( A1 );
     AnalogIn    emg3_in( A2 );
-    DigitalIn   max_reader1( SW2 );
-    DigitalIn   max_reader3( SW3 );
+    AnalogIn    emg4_in( A3 );
+    DigitalIn   max_reader12( SW2 );
+    DigitalIn   max_reader34( SW3 );
 
     Ticker      main_timer;
     Ticker      max_read1;
     Ticker      max_read3;
-    HIDScope    scope( 5 );
+    HIDScope    scope( 4 );
     DigitalOut  red(LED_RED);
     DigitalOut  blue(LED_BLUE);
     DigitalOut  green(LED_GREEN);
@@ -28,6 +29,7 @@
     double emg1abs;
     double emg1lowfilter;
     double emg1peak;
+    double max1;
     double maxpart1;
     // Left Biceps
     double emg2;
@@ -36,9 +38,9 @@
     double emg2abs;
     double emg2lowfilter;
     double emg2peak;
-    double max1;
+    double max2;
     double maxpart2;
-    // Left Lower Arm OR Triceps
+    // Left Lower Arm
     double emg3;
     double emg3highfilter;
     double emg3notchfilter;
@@ -47,6 +49,15 @@
     double emg3peak;
     double max3;
     double maxpart3;
+    // Right Lower Arm
+    double emg4;
+    double emg4highfilter;
+    double emg4notchfilter;
+    double emg4abs;
+    double emg4lowfilter;
+    double emg4peak;
+    double max4;
+    double maxpart4;
 
 // BiQuad Filter Settings
     // Right Biceps
@@ -64,16 +75,21 @@
     BiQuad filternotch3(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01);
     BiQuad filterpeak3(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01);
     BiQuad filterlow3(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01);
+    // Right Lower Arm OR Triceps
+    BiQuad filterhigh4(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01);
+    BiQuad filternotch4(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01);
+    BiQuad filterpeak4(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01);
+    BiQuad filterlow4(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01);
     //
 
 // Finding max values for correct motor switch if the button is pressed
 void get_max1(){
-    if (max_reader1==0){
+    if (max_reader12==0){
             green = !green;
             red = 1;
             blue = 1;
-            
             for(int n=0;n<2000;n++){  /* measure 2000 samples and filter it */
+
             emg1 = emg1_in.read();      /* read out emg */
             emg1highfilter = filterhigh1.step(emg1); /* high pass filtered */
             emg1notchfilter = filternotch1.step(emg1highfilter); /* notch filtered */
@@ -81,20 +97,30 @@
             emg1lowfilter = filterlow1.step(emg1abs); /* low pass filtered */
             emg1peak = filterpeak1.step(emg1lowfilter); /* 4dB gain peak */
             
+            emg2 = emg2_in.read();      /* read out emg */
+            emg2highfilter = filterhigh2.step(emg2); /* high pass filtered */
+            emg2notchfilter = filternotch2.step(emg2highfilter); /* notch filtered */
+            emg2abs = fabs(emg2notchfilter); /* take the absolute value */
+            emg2lowfilter = filterlow2.step(emg2abs); /* low pass filtered */
+            emg2peak = filterpeak2.step(emg2lowfilter); /* 4dB gain peak */
+            
             if (max1<emg1peak){
                 max1 = emg1peak; /* set the max value at the highest measured value */
             }
+            if (max2<emg2peak){
+                max2 = emg2peak; /* set the max value at the highest measured value */                
+            }
             wait(0.001f); /* measure at 1000Hz */   
             }
             wait(0.2f);
             green = 1;
     }
-    maxpart1 = 0.25*max1; /* set cut off voltage at 25% of max for right biceps */
+    maxpart1 = 0.15*max1; /* set cut off voltage at 15% of max for right biceps */
     maxpart2 = 0.15*max1; /* set cut off votage at 15% of max for left biceps */
 }
 
 void get_max3(){
-    if (max_reader3==0){
+    if (max_reader34==0){
             green = 1;
             blue = 1;
             red = !red;
@@ -107,15 +133,26 @@
             emg3lowfilter = filterlow3.step(emg3abs);
             emg3peak = filterpeak3.step(emg3lowfilter);
             
+            emg4 = emg4_in.read();
+            emg4highfilter = filterhigh4.step(emg4);
+            emg4notchfilter = filternotch4.step(emg4highfilter);
+            emg4abs = fabs(emg4notchfilter);
+            emg4lowfilter = filterlow4.step(emg4abs);
+            emg4peak = filterpeak4.step(emg4lowfilter);
+            
             if (max3<emg3peak){
                 max3 = emg3peak; /* set the max value at the highest measured value */
             }
+            if (max4<emg4peak){
+                max4 = emg4peak; /* set the max value at the highest measured value */
+            }
             wait(0.001f);    
             }
             wait(0.2f);
             red = 1;
     }
-    maxpart3 = 0.25*max3; /* set cut off voltage at 25% of max for right biceps */
+    maxpart3 = 0.25*max3; /* set cut off voltage at 25% of max for left lower arm */
+    maxpart4 = 0.25*max4; /* set cut off voltage at 25% of max for right lower arm */
 }
 
 // Filtering & Scope
@@ -141,6 +178,13 @@
     emg3abs = fabs(emg3notchfilter);
     emg3lowfilter = filterlow3.step(emg3abs);
     emg3peak = filterpeak3.step(emg3lowfilter); /* Final Lower Arm values to be sent */
+    // Right Lower Arm OR Triceps
+    emg4 = emg4_in.read();
+    emg4highfilter = filterhigh4.step(emg4);
+    emg4notchfilter = filternotch4.step(emg4highfilter);
+    emg4abs = fabs(emg4notchfilter);
+    emg4lowfilter = filterlow4.step(emg4abs);
+    emg4peak = filterpeak4.step(emg4lowfilter); /* Final Lower Arm values to be sent */
     
     
     /* Compare measurement to the calibrated value to decide actions */
@@ -157,12 +201,17 @@
             }
         
     else {
-        if (maxpart3<emg3peak){ /* See if lower arm is contracting */
+        if (maxpart3<emg3peak){ /* See if lower left arm is contracting */
             red = 1;
             blue = 1;
             green = 0;
             }
-        
+    else {
+        if (maxpart4<emg3peak){ /* See if lower right arm is contracting */
+            red = 0;
+            blue = 0;
+            green = 0;
+            }
     else {
         red = 1; /* Shut down all led colors if no movement is registered */
         blue = 1;
@@ -170,12 +219,12 @@
         }
         }
         }
+        }
     /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
     scope.set(0, emg1peak ); /* plot Right biceps voltage */
     scope.set(1, emg2peak ); /* Plot Left biceps voltage */
-    scope.set(2, maxpart1 ); /* Show the line above which the motor should run for right biceps */
-    scope.set(3, emg3peak ); /* Plot Lower Arm voltage */
-    scope.set(4, maxpart3 ); /* Plot the line above which the motor should run for lower arm */
+    scope.set(2, emg3peak ); /* Plot Lower Left Arm voltage */
+    scope.set(3, emg4peak ); /* Plot Lower Right Arm Voltage */
  
     scope.send(); /* send everything to the HID scope */
 }