First Last / Mbed 2 deprecated EMG_HIDScope

Dependencies:   HIDScope mbed mbed-dsp

Fork of EMG by First Last

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 
00004 #include "arm_math.h"
00005 
00006 //Define objects
00007 AnalogIn    emg0(PTB1); //Analog input
00008 HIDScope scope(2);
00009 
00010 arm_biquad_casd_df1_inst_f32 lowpass;
00011 //constants for 5Hz lowpass
00012 float lowpass_const[] = {0.02008337 , 0.04016673 , 0.02008337 , 1.56101808 , -0.64135154};
00013 //state values
00014 float lowpass_states[4];
00015 arm_biquad_casd_df1_inst_f32 highpass;
00016 //constants for 0.5Hz highpass
00017 float highpass_const[] = {0.97803048, -1.95606096,  0.97803048, 1.95557824 , -0.95654368};
00018 //state values
00019 float highpass_states[4];
00020 
00021 
00022 /** Looper function
00023 * functions used for Ticker and Timeout should be of type void <name>(void)
00024 * i.e. no input arguments, no output arguments.
00025 * if you want to change a variable that you use in other places (for example in main)
00026 * you will have to make that variable global in order to be able to reach it both from
00027 * the function called at interrupt time, and in the main function.
00028 * To make a variable global, define it under the includes.
00029 * variables that are changed in the interrupt routine (written to) should be made
00030 * 'volatile' to let the compiler know that those values may change outside the current context.
00031 * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value"
00032 * in the example below, the variable is not re-used in the main function, and is thus declared
00033 * local in the looper function only.
00034 **/
00035 void looper()
00036 {
00037     /*variable to store value in*/    
00038     uint16_t emg_value;
00039     float filtered_emg;
00040     float emg_value_f32;
00041     /*put raw emg value both in red and in emg_value*/
00042     emg_value = emg0.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
00043     emg_value_f32 = emg0.read();
00044 
00045     //process emg
00046     arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32, &filtered_emg, 1 );
00047     filtered_emg = fabs(filtered_emg);
00048     arm_biquad_cascade_df1_f32(&lowpass, &filtered_emg, &filtered_emg, 1 );
00049     
00050     /*send value to PC. */
00051     scope.set(0,emg_value);     //uint value
00052     scope.set(1,filtered_emg);  //processed float
00053     scope.send();
00054 
00055 }
00056 
00057 int main()
00058 {
00059     Ticker log_timer;
00060    //set up filters. Use external array for constants
00061     arm_biquad_cascade_df1_init_f32(&lowpass,1 , lowpass_const, lowpass_states);
00062     arm_biquad_cascade_df1_init_f32(&highpass,1 ,highpass_const,highpass_states);
00063 
00064     /**Here you attach the 'void looper(void)' function to the Ticker object
00065     * The looper() function will be called every 0.01 seconds.
00066     * Please mind that the parentheses after looper are omitted when using attach.
00067     */
00068     log_timer.attach(looper, 0.005);
00069     while(1) //Loop
00070     {
00071       /*Empty!*/
00072       /*Everything is handled by the interrupt routine now!*/
00073     }
00074 }