Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope MODSERIAL mbed
Fork of Signal Filter by
Diff: main.cpp
- Revision:
- 2:e30dbfec6d1e
- Parent:
- 1:f6110f80fa45
- Child:
- 3:04f3cca82b22
--- a/main.cpp Wed Sep 09 14:46:35 2015 +0000 +++ b/main.cpp Mon Sep 21 09:06:32 2015 +0000 @@ -1,45 +1,52 @@ #include "mbed.h" #include "MODSERIAL.h" +#include "math.h" #include "HIDScope.h" - -DigitalOut r_led(LED_RED); -DigitalOut g_led(LED_GREEN); -DigitalOut b_led(LED_BLUE); - -DigitalIn button(PTA4); + + +AnalogIn EMG(A0); MODSERIAL pc(USBTX, USBRX); -const int baudrate = 115200; +//const int baudrate = 115200; //const int ms_wait = 100; // Define the HIDScope and Ticker objects -HIDScope scope(2); -Ticker scopeTimer; -Ticker controllerTimer; - -const float period_led = 0.2; - // Two analog inputs to read from -AnalogIn a0(p2); -AnalogIn a1(p3); - -void controlAndMeasure() -{ - // Do some control - // ** control code here ** - - // Store values in the scope - scope.set(0,a0.read()); - scope.set(1,a1.read()); -} - +HIDScope scope(1); +Ticker scopeTimer; +// Define the storage variables and filter coeicients for two filters + + + // Define the storage variables and filter coeicients for two filters + double f1_v1 = 0, f1_v2 = 0, f2_v1 = 0, f2_v2 = 0; + const double f1_a1 = 1.0, f1_a2 = 2.0, f1_b0 = 1.0, f1_b1 = 3.0, f1_b2 = 4.0; + const double f2_a1 = 1.5, f2_a2 = 1.0, f2_b0 = 0.2, f2_b1 = 1.5, f2_b2 = 3.0; + + double biquad( double u, double &v1, double &v2, // Input and storage variables + 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; + } + + // This is your controller, call it using a Ticker + void myController() { + // double u1 = ..., u2 = ... ; + double y1 = biquad( u1, f1_v1, f1_v2, f1_a1, f1_a2, f1_b0, f1_b1, f1_b2 ); + double y2 = biquad( u2, f2_v1, f2_v2, f2_a1, f2_a2, f2_b0, f2_b1, f2_b2 ); + } + + + + +void scopeSend(){ +u1=EMG; +u2=y1 ; +scope.set(0,y2.read()); +scope.send(); + } int main() -{ - // Attach the HIDScope::send method from the scope object to the timer at 50Hz - scopeTimer.attach_us(&scope, &HIDScope::send, 2e4); - - // Attach the controlAndMeasure method to the controller timer at 1kHz - controllerTimer.attach_us(&controlAndMeasure, 1e3); - - while(1) { } +{ scopeTimer.attach_us(&scopeSend,1e4); + while(1){} } \ No newline at end of file