Het totale EMG script

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of EMGV5 by Laura Veenendaal

Revision:
18:12250e88037f
Parent:
16:9f7797ffd0fb
Child:
19:34456a1effc4
--- a/main.cpp	Tue Sep 22 07:00:54 2015 +0000
+++ b/main.cpp	Tue Oct 06 14:38:12 2015 +0000
@@ -2,31 +2,47 @@
 #include "HIDScope.h"
 
 //Define objects
-AnalogIn    emg(A0); //Analog input
-Ticker      sample_timer;
-HIDScope    scope(1);
+    AnalogIn    EMG_left(A0); //Analog input
+    AnalogIn    EMG_right(A1);   
+    Ticker      SampleEMG;
+    Ticker      ScopeTimer;
+    HIDScope    scope(1);
+
+// constant values
+    double EMG_L_f_v1 = 0, EMG_L_f_v2 = 0;
+
+// coëfficiënten
+    const double BiGainEMG_Lh = 1, BiGainEMG_Ll=1;
+    const double EMG_L_fh_a1 = -0.96608908283*BiGainEMG_Lh, EMG_L_fh_a2 = 0.0*BiGainEMG_Lh, EMG_L_fh_b0 = 1.0*BiGainEMG_Lh, EMG_L_fh_b1 = 1.0*BiGainEMG_Lh, EMG_L_fh_b2 = 0.0*BiGainEMG_Lh; //coefficients for high-pass filter
+    const double EMG_L_fl_a1 = -0.96608908283*BiGainEMG_Ll, EMG_L_fl_a2 = 0.0*BiGainEMG_Ll, EMG_L_fl_b0 = 1.0*BiGainEMG_Ll, EMG_L_fl_b1 = 1.0*BiGainEMG_Ll, EMG_L_fl_b2 = 0.0*BiGainEMG_Ll; // coefficients for low-pass filter
 
-/** Sample function
- * this function samples the emg and sends it to HIDScope
- **/
-void sample()
+// HIDScope
+    void ScopeSend()
+    {
+        scope.set(0, EMG_left.read());
+        scope.send();
+    }
+    
+// Biquad filter
+    double biquad( double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2 )
+    {
+        double v = u - a1*v1 - a2*v2;
+        double y = b0*v + b1*v1 + b2*v2;
+        v2 = v1; v1 = v;
+        return y;
+    }
+            
+// EMG filtering function
+void EMGfilter()
 {
-    /* First, sample the EMG using the 'read' method of the 'AnalogIn' variable named 'emg' */
-    double emg_value = emg.read();
-    /* Second, set the sampled emg value in channel zero (the first channel) in the 'HIDScope' variable named 'scope' */
-    scope.set(0,emg_value);
-    /* Repeat the step above if required for more channels (channel 0 up to 5 = 6 channels) */
-    /* Finally, send all channels to the PC at once */
-    scope.send();
+    double EMG_left_value = EMG_left.read();
+    double EMG_L_fh = biquad(EMG_left_value, EMG_L_f_v1, EMG_L_f_v2, EMG_L_fh_a1, EMG_L_fh_a2, EMG_L_fh_b0, EMG_L_fh_b1, EMG_L_fh_b2); 
+    double EMG_L_fhl = biquad(EMG_L_fh, EMG_L_f_v1, EMG_L_f_v2, EMG_L_fl_a1, EMG_L_fl_a2, EMG_L_fl_b0, EMG_L_fl_b1, EMG_L_fl_b2); 
 }
 
 int main()
 {
-    /**Attach the 'sample' function to the timer 'sample_timer'.
-    * this ensures that 'sample' is executed every... 0.002 seconds
-    */
-    sample_timer.attach(&sample, 0.002);
-
-    /*empty loop, sample() is executed periodically*/
+    SampleEMG.attach(&EMGfilter, 0.002);
+    ScopeTimer.attach(&ScopeSend, 0.002);
     while(1) {}
 }
\ No newline at end of file