Remco Dasselaar / Mbed 2 deprecated TotalControlEmg2

Dependencies:   HIDScope MODSERIAL QEI TextLCD mbed

Fork of TotalControlEmg2 by Remco Dasselaar

Files at this revision

API Documentation at this revision

Comitter:
Bartvaart
Date:
Tue Oct 06 12:02:22 2015 +0000
Child:
1:98be4152a539
Commit message:
Weet niet zeker of die van net goed ging dus nog een keer voor we op compile gaan drukken. Spannend!

Changed in this revision

Encoder.lib Show annotated file Show diff for this revision Revisions of this file
Filter.cpp Show annotated file Show diff for this revision Revisions of this file
Filter.h Show annotated file Show diff for this revision Revisions of this file
HIDScope.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder.lib	Tue Oct 06 12:02:22 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/vsluiter/code/Encoder/#3a226e3a4120
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Filter.cpp	Tue Oct 06 12:02:22 2015 +0000
@@ -0,0 +1,20 @@
+#include "Filter.h"
+
+double Filter(double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2){
+    // volgens direct form 2, uit de embedded filtering and control sheets van T.J.W. Lankhorst
+    // u = ingangssignaal (zonder filter)
+    // v = tussentap
+    // v1 = v bij u(x-1), dus v van 1 geleden
+    // v2 = v bij u(x-2), dus v van 2 geleden
+    // a1 en a2 variabele uit ASN filter, a0 =1
+    // b0, b1, b2 variabele uit ASN filter
+    // y = uitgangssignaal (gefilterd)
+    
+    double v = u - a1 * v1 - a2 * v2;
+    double y = b0 * v + b1 * v1 + b2 * v2
+    
+    v2 = v1; // signalen doorschuiven, zodat bij volgende input, de vorige waardes vsn v worden onthouden
+    v1 = v; 
+    
+    return y;
+    }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Filter.h	Tue Oct 06 12:02:22 2015 +0000
@@ -0,0 +1,3 @@
+#include "mbed.h"
+
+double filter(double u);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HIDScope.lib	Tue Oct 06 12:02:22 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/tomlankhorst/code/HIDScope/#5020a2c0934b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 06 12:02:22 2015 +0000
@@ -0,0 +1,73 @@
+#include "mbed.h"
+#include "HIDScope.h"
+#include "encoder.h"
+#include "Filter.h"
+
+AnalogIn    emg(A0); //Analog input van emg kabels
+Ticker      EMGticker;
+HIDScope    scope(2); //2 scopes
+
+// constante variabelen:
+
+//Sample frequentie
+Fs = 500; //Hz
+t = 1/ Fs; // voor EMGticker
+
+// Filter1a: 50Hz Notch
+double v1_50a = 0, v2_50a = 0;
+const double a1_50a = -1.61803398875, a2_50a = 1.00000000000;
+const double b0_50a = 1.00000000000, b1_50a = -1.61803398875, b2_50a = 1.00000000000;
+
+// Filter1b: 50Hz Notch
+double v1_50b = 0, v2_50b = 0;
+const double a1_50b = -1.61803398875, a2_50b = 1.00000000000;
+const double b0_50b = 1.00000000000, b1_50b = -1.61803398875, b2_50b = 1.00000000000;
+
+// Filter1: 20Hz HighPass
+double v1_HP = 0, v2_HP = 0;
+const double a1_HP = -0.76475499450, a2_HP = 0.27692273367;
+const double b0_HP = 1.00000000000, b1_HP = -2.00000000000, b2_HP = 1.00000000000;
+
+// Filter1: 5Hz LowPass
+double v1_LP = 0, v2_LP = 0;
+const double a1_LP = -1.16338171052, a2_LP = 0.42191097989;
+const double b0_LP = 1.00000000000, b1_LP = 2.00000000000, b2_LP = 1.00000000000;
+
+
+void EMGfilter()
+{
+    //u = input waarde
+    //y = output waarde
+
+    double u = emg.read(); // lees waarde van emg signaal uit
+    
+    // Op deze manier worden de waardes ingelezen in Filter. Zorg dus voor dezelfde volgorde, zodat de waardes goed uitgelezen worden!:
+    // Filter(double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2)
+    
+    // 50Hz Notch filter
+    double y50a = double Filter(u, v1_50a, v2_50a, a1_50a, a2_50a, b0_50a, b1_50a, b2_50a);
+    double y50b = double Filter(y50a, v1_50b, v2_50b, a1_50b, a2_50b, b0_50b, b1_50b, b2_50b);
+    
+    // High Pass filter. Tot 20Hz wordt weggefliterd
+    double yHP = double Filter(y50b, v1_HP, v2_HP, a1_HP, a2_HP, b0_HP, b1_HP, b2_HP);
+    
+    // Absolute waarde wordt genomen.
+    double y1 = fabs(yHP);
+    
+    // Low Pass filter. Alles vanaf 5Hz wordt weggefilterd 
+    double yLP = double Filter(y1, v1_LP, v2_LP, a1_LP, a2_LP, b0_LP, b1_LP, b2_LP);
+    
+    double y = yLP;
+    
+    // Plotten in HIDscope
+    scope.set(0,u); //ongefilterde waarde naar scope 1
+    scope.set(1,y); //gefilterde waarde naar scope 2
+    scope.send(); //stuur de waardes naar HIDscope
+}
+
+
+int main(){
+    EMGticker.attach(&EMGfilter, t) //500Hz
+    while(1){}
+    }
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Oct 06 12:02:22 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68
\ No newline at end of file