Code for emg filtering and thus controlling the robotic movement.

Dependencies:   HIDScope biquadFilter mbed

Fork of EMG by Tom Tom

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HIDScope.h"
00003 #include "biquadFilter.h"        // Require the HIDScope library
00004 
00005 //Define objects
00006 AnalogIn    emg(A0); //Analog input
00007 Ticker      sample_timer;
00008 Ticker      filter_timer
00009 HIDScope    scope(2);
00010 biquadFilter filterhigh1(-1.1430 0.4128 0.6389 -1.2779 0.6389);
00011 biquadFilter filterlow1(1.9556 0.9565 0.9780 1.9561 0.9780);
00012 biquadFilter notch(-1.1978e-16 0.9561 0.9780 -1.1978e-16 0.9780);
00013 biquadFilter filterlow2(-1.9645 0.9651 1.5515e-4 3.1030e-4 1.5515e-4);
00014 
00015 /** Sample function
00016  * this function samples the emg and sends it to HIDScope
00017  **/
00018 void sample()
00019 {
00020     /* First, sample the EMG using the 'read' method of the 'AnalogIn' variable named 'emg' */
00021     double emg_value = emg.read();
00022     /* Second, set the sampled emg value in channel zero (the first channel) in the 'HIDScope' variable named 'scope' */
00023     scope.set(0,emg_value);
00024     /* Repeat the step above if required for more channels (channel 0 up to 5 = 6 channels) */
00025     /* Finally, send all channels to the PC at once */
00026     scope.send();
00027 }
00028 void filtering()
00029 {
00030 double signalpart1 = filterhigh1(emg.read());
00031 double signalpart2 = abs(signalpart1);
00032 double signalpart3 = filterlow1(signalpart2);
00033 double signalpart4 = notch(signalpart3);
00034 double signalfinal = filterlow2(signalpart4);
00035 scope.set(1,signalfinal);
00036 int main()
00037 {
00038     /**Attach the 'sample' function to the timer 'sample_timer'.
00039     * this ensures that 'sample' is executed every... 0.002 seconds
00040     */
00041     sample_timer.attach(&sample, 0.002);
00042     filter_timer.attach(&filtering, 0.002);
00043 
00044     /*empty loop, sample() is executed periodically*/
00045     while(1) {}
00046 }