Measure EMG, publish on HID Scope
Dependencies: HIDScope mbed mbed-dsp
Fork of EMG by
main.cpp@18:fed07cc1f8f6, 2014-09-29 (annotated)
- Committer:
- vsluiter
- Date:
- Mon Sep 29 11:42:37 2014 +0000
- Revision:
- 18:fed07cc1f8f6
- Parent:
- 17:1388f1a2d7b2
- Child:
- 19:dbc1bca498e3
Using arm_math functions for IIR filters;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 6:80c13d99aa55 | 2 | #include "MODSERIAL.h" |
vsluiter | 11:ce72ec658a95 | 3 | #include "HIDScope.h" |
vsluiter | 0:32bb76391d89 | 4 | |
vsluiter | 18:fed07cc1f8f6 | 5 | #include "arm_math.h" |
vsluiter | 18:fed07cc1f8f6 | 6 | |
vsluiter | 4:8b298dfada81 | 7 | //Define objects |
vsluiter | 7:3396c3e33928 | 8 | AnalogIn emg0(PTB1); //Analog input |
ArvidKeemink | 1:db54d9412d18 | 9 | PwmOut red(LED_RED); //PWM output |
vsluiter | 8:8a17f65622b4 | 10 | Ticker log_timer; |
vsluiter | 8:8a17f65622b4 | 11 | MODSERIAL pc(USBTX,USBRX); |
vsluiter | 11:ce72ec658a95 | 12 | HIDScope scope(2); |
vsluiter | 2:e314bb3b2d99 | 13 | |
vsluiter | 18:fed07cc1f8f6 | 14 | arm_biquad_casd_df1_inst_f32 lowpass; |
vsluiter | 18:fed07cc1f8f6 | 15 | float lowpass_const[] = {0.02008337 , 0.04016673 , 0.02008337 , 1.56101808 , -0.64135154}; |
vsluiter | 18:fed07cc1f8f6 | 16 | float lowpass_states[4]; |
vsluiter | 18:fed07cc1f8f6 | 17 | |
vsluiter | 18:fed07cc1f8f6 | 18 | arm_biquad_casd_df1_inst_f32 highpass; |
vsluiter | 18:fed07cc1f8f6 | 19 | float highpass_const[] = {0.97803048, -1.95606096, 0.97803048, 1.95557824 , -0.95654368}; |
vsluiter | 18:fed07cc1f8f6 | 20 | volatile float highpass_states[4]; |
vsluiter | 18:fed07cc1f8f6 | 21 | |
vsluiter | 18:fed07cc1f8f6 | 22 | /* |
vsluiter | 15:d3a849ab3697 | 23 | typedef struct second_order_constants |
vsluiter | 15:d3a849ab3697 | 24 | { |
vsluiter | 15:d3a849ab3697 | 25 | float b[3]; |
vsluiter | 15:d3a849ab3697 | 26 | float a[3]; |
vsluiter | 15:d3a849ab3697 | 27 | } second_order_constants_t; |
vsluiter | 15:d3a849ab3697 | 28 | |
vsluiter | 15:d3a849ab3697 | 29 | typedef struct second_order_values |
vsluiter | 15:d3a849ab3697 | 30 | { |
vsluiter | 15:d3a849ab3697 | 31 | float x_1,x_2; |
vsluiter | 15:d3a849ab3697 | 32 | float y_1,y_2; |
vsluiter | 15:d3a849ab3697 | 33 | } second_order_values_t; |
vsluiter | 15:d3a849ab3697 | 34 | |
vsluiter | 15:d3a849ab3697 | 35 | //constants |
vsluiter | 15:d3a849ab3697 | 36 | second_order_constants_t highpass= {{0.97803048, -1.95606096, 0.97803048},{1, -1.95557824, 0.95654368}}; |
vsluiter | 17:1388f1a2d7b2 | 37 | second_order_constants_t lowpass={{ 0.02008337 , 0.04016673 , 0.02008337},{1.0, -1.56101808, 0.64135154}}; |
vsluiter | 15:d3a849ab3697 | 38 | //type for values |
vsluiter | 17:1388f1a2d7b2 | 39 | second_order_values_t highpass_values, lowpass_values; |
vsluiter | 15:d3a849ab3697 | 40 | |
vsluiter | 18:fed07cc1f8f6 | 41 | |
vsluiter | 18:fed07cc1f8f6 | 42 | //function definition |
vsluiter | 15:d3a849ab3697 | 43 | float second_order(float x, second_order_constants_t constants, second_order_values_t &values); |
vsluiter | 18:fed07cc1f8f6 | 44 | */ |
vsluiter | 15:d3a849ab3697 | 45 | |
vsluiter | 4:8b298dfada81 | 46 | /** Looper function |
vsluiter | 4:8b298dfada81 | 47 | * functions used for Ticker and Timeout should be of type void <name>(void) |
vsluiter | 4:8b298dfada81 | 48 | * i.e. no input arguments, no output arguments. |
vsluiter | 4:8b298dfada81 | 49 | * if you want to change a variable that you use in other places (for example in main) |
vsluiter | 4:8b298dfada81 | 50 | * you will have to make that variable global in order to be able to reach it both from |
vsluiter | 4:8b298dfada81 | 51 | * the function called at interrupt time, and in the main function. |
vsluiter | 4:8b298dfada81 | 52 | * To make a variable global, define it under the includes. |
vsluiter | 4:8b298dfada81 | 53 | * variables that are changed in the interrupt routine (written to) should be made |
vsluiter | 4:8b298dfada81 | 54 | * 'volatile' to let the compiler know that those values may change outside the current context. |
vsluiter | 8:8a17f65622b4 | 55 | * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value" |
vsluiter | 4:8b298dfada81 | 56 | * in the example below, the variable is not re-used in the main function, and is thus declared |
vsluiter | 4:8b298dfada81 | 57 | * local in the looper function only. |
vsluiter | 4:8b298dfada81 | 58 | **/ |
vsluiter | 2:e314bb3b2d99 | 59 | void looper() |
vsluiter | 2:e314bb3b2d99 | 60 | { |
vsluiter | 4:8b298dfada81 | 61 | /*variable to store value in*/ |
vsluiter | 8:8a17f65622b4 | 62 | uint16_t emg_value; |
vsluiter | 15:d3a849ab3697 | 63 | float filtered_emg; |
vsluiter | 18:fed07cc1f8f6 | 64 | float emg_value_f32; |
vsluiter | 4:8b298dfada81 | 65 | /*put raw emg value both in red and in emg_value*/ |
vsluiter | 10:09b8424a7b39 | 66 | red.write(emg0.read()); // read float value (0..1 = 0..3.3V) |
vsluiter | 12:768048d7f742 | 67 | emg_value = emg0.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V) |
vsluiter | 18:fed07cc1f8f6 | 68 | emg_value_f32 = emg0.read(); |
vsluiter | 18:fed07cc1f8f6 | 69 | //filtered_emg = second_order((float)emg_value,highpass, highpass_values); |
vsluiter | 18:fed07cc1f8f6 | 70 | arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32, &filtered_emg, 1 ); |
vsluiter | 18:fed07cc1f8f6 | 71 | filtered_emg = fabs(filtered_emg); |
vsluiter | 18:fed07cc1f8f6 | 72 | arm_biquad_cascade_df1_f32(&lowpass, &filtered_emg, &filtered_emg, 1 ); |
vsluiter | 8:8a17f65622b4 | 73 | /*send value to PC. Line below is used to prevent buffer overrun */ |
vsluiter | 11:ce72ec658a95 | 74 | scope.set(0,emg_value); |
vsluiter | 18:fed07cc1f8f6 | 75 | //scope.set(1,second_order(fabs(filtered_emg), lowpass, lowpass_values)); |
vsluiter | 18:fed07cc1f8f6 | 76 | scope.set(1,filtered_emg); |
vsluiter | 11:ce72ec658a95 | 77 | scope.send(); |
vsluiter | 16:24e992616cf6 | 78 | |
vsluiter | 5:4dacb7b72109 | 79 | /**When not using the LED, the above could also have been done this way: |
vsluiter | 8:8a17f65622b4 | 80 | * pc.printf("%u\n", emg0.read_u16()); |
vsluiter | 5:4dacb7b72109 | 81 | */ |
vsluiter | 2:e314bb3b2d99 | 82 | } |
vsluiter | 0:32bb76391d89 | 83 | |
vsluiter | 0:32bb76391d89 | 84 | int main() |
vsluiter | 0:32bb76391d89 | 85 | { |
vsluiter | 4:8b298dfada81 | 86 | /*setup baudrate. Choose the same in your program on PC side*/ |
vsluiter | 14:94a4a5863689 | 87 | //pc.baud(115200); |
vsluiter | 4:8b298dfada81 | 88 | /*set the period for the PWM to the red LED*/ |
vsluiter | 2:e314bb3b2d99 | 89 | red.period_ms(2); |
vsluiter | 4:8b298dfada81 | 90 | /**Here you attach the 'void looper(void)' function to the Ticker object |
vsluiter | 9:d33e7b175ad7 | 91 | * The looper() function will be called every 0.01 seconds. |
vsluiter | 4:8b298dfada81 | 92 | * Please mind that the parentheses after looper are omitted when using attach. |
vsluiter | 4:8b298dfada81 | 93 | */ |
vsluiter | 18:fed07cc1f8f6 | 94 | //set up filters |
vsluiter | 18:fed07cc1f8f6 | 95 | arm_biquad_cascade_df1_init_f32(&lowpass,1 , lowpass_const, lowpass_states); |
vsluiter | 18:fed07cc1f8f6 | 96 | arm_biquad_cascade_df1_init_f32(&highpass,1 ,highpass_const, (float *)highpass_states); |
vsluiter | 13:18d4cef1fdb4 | 97 | log_timer.attach(looper, 0.005); |
ArvidKeemink | 1:db54d9412d18 | 98 | while(1) //Loop |
vsluiter | 0:32bb76391d89 | 99 | { |
vsluiter | 4:8b298dfada81 | 100 | /*Empty!*/ |
vsluiter | 4:8b298dfada81 | 101 | /*Everything is handled by the interrupt routine now!*/ |
vsluiter | 0:32bb76391d89 | 102 | } |
vsluiter | 15:d3a849ab3697 | 103 | } |
vsluiter | 15:d3a849ab3697 | 104 | |
vsluiter | 18:fed07cc1f8f6 | 105 | |
vsluiter | 18:fed07cc1f8f6 | 106 | /* |
vsluiter | 15:d3a849ab3697 | 107 | float second_order(float x, second_order_constants_t constants, second_order_values_t &values) |
vsluiter | 15:d3a849ab3697 | 108 | { |
vsluiter | 15:d3a849ab3697 | 109 | float y = 0; |
vsluiter | 15:d3a849ab3697 | 110 | float b_terms, a_terms; |
vsluiter | 15:d3a849ab3697 | 111 | b_terms = (constants.b[0]*x) + (constants.b[1]*values.x_1) + (constants.b[2]*values.x_2); |
vsluiter | 15:d3a849ab3697 | 112 | a_terms = (constants.a[1]*values.y_1) + (constants.a[2]*values.y_2); |
vsluiter | 15:d3a849ab3697 | 113 | y=(1./constants.a[0])* (b_terms-a_terms); |
vsluiter | 15:d3a849ab3697 | 114 | values.x_2 = values.x_1; |
vsluiter | 15:d3a849ab3697 | 115 | values.x_1 = x; |
vsluiter | 15:d3a849ab3697 | 116 | values.y_2 = values.y_1; |
vsluiter | 15:d3a849ab3697 | 117 | values.y_1 = y; |
vsluiter | 15:d3a849ab3697 | 118 | return y; |
vsluiter | 18:fed07cc1f8f6 | 119 | }*/ |