![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Measure EMG, publish on HID Scope
Dependencies: HIDScope mbed mbed-dsp
Fork of EMG by
main.cpp@21:133e9390a42a, 2014-10-01 (annotated)
- Committer:
- vsluiter
- Date:
- Wed Oct 01 12:25:06 2014 +0000
- Revision:
- 21:133e9390a42a
- Parent:
- 20:f7d281e3112b
Removed all code for serial data, moved Ticker to local scope
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 11:ce72ec658a95 | 2 | #include "HIDScope.h" |
vsluiter | 0:32bb76391d89 | 3 | |
vsluiter | 18:fed07cc1f8f6 | 4 | #include "arm_math.h" |
vsluiter | 18:fed07cc1f8f6 | 5 | |
vsluiter | 4:8b298dfada81 | 6 | //Define objects |
vsluiter | 7:3396c3e33928 | 7 | AnalogIn emg0(PTB1); //Analog input |
vsluiter | 11:ce72ec658a95 | 8 | HIDScope scope(2); |
vsluiter | 2:e314bb3b2d99 | 9 | |
vsluiter | 18:fed07cc1f8f6 | 10 | arm_biquad_casd_df1_inst_f32 lowpass; |
vsluiter | 19:dbc1bca498e3 | 11 | //constants for 5Hz lowpass |
vsluiter | 18:fed07cc1f8f6 | 12 | float lowpass_const[] = {0.02008337 , 0.04016673 , 0.02008337 , 1.56101808 , -0.64135154}; |
vsluiter | 20:f7d281e3112b | 13 | //state values |
vsluiter | 20:f7d281e3112b | 14 | float lowpass_states[4]; |
vsluiter | 18:fed07cc1f8f6 | 15 | arm_biquad_casd_df1_inst_f32 highpass; |
vsluiter | 19:dbc1bca498e3 | 16 | //constants for 0.5Hz highpass |
vsluiter | 18:fed07cc1f8f6 | 17 | float highpass_const[] = {0.97803048, -1.95606096, 0.97803048, 1.95557824 , -0.95654368}; |
vsluiter | 20:f7d281e3112b | 18 | //state values |
vsluiter | 20:f7d281e3112b | 19 | float highpass_states[4]; |
vsluiter | 15:d3a849ab3697 | 20 | |
vsluiter | 15:d3a849ab3697 | 21 | |
vsluiter | 4:8b298dfada81 | 22 | /** Looper function |
vsluiter | 4:8b298dfada81 | 23 | * functions used for Ticker and Timeout should be of type void <name>(void) |
vsluiter | 4:8b298dfada81 | 24 | * i.e. no input arguments, no output arguments. |
vsluiter | 4:8b298dfada81 | 25 | * if you want to change a variable that you use in other places (for example in main) |
vsluiter | 4:8b298dfada81 | 26 | * you will have to make that variable global in order to be able to reach it both from |
vsluiter | 4:8b298dfada81 | 27 | * the function called at interrupt time, and in the main function. |
vsluiter | 4:8b298dfada81 | 28 | * To make a variable global, define it under the includes. |
vsluiter | 4:8b298dfada81 | 29 | * variables that are changed in the interrupt routine (written to) should be made |
vsluiter | 4:8b298dfada81 | 30 | * 'volatile' to let the compiler know that those values may change outside the current context. |
vsluiter | 8:8a17f65622b4 | 31 | * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value" |
vsluiter | 4:8b298dfada81 | 32 | * in the example below, the variable is not re-used in the main function, and is thus declared |
vsluiter | 4:8b298dfada81 | 33 | * local in the looper function only. |
vsluiter | 4:8b298dfada81 | 34 | **/ |
vsluiter | 2:e314bb3b2d99 | 35 | void looper() |
vsluiter | 2:e314bb3b2d99 | 36 | { |
vsluiter | 4:8b298dfada81 | 37 | /*variable to store value in*/ |
vsluiter | 8:8a17f65622b4 | 38 | uint16_t emg_value; |
vsluiter | 15:d3a849ab3697 | 39 | float filtered_emg; |
vsluiter | 18:fed07cc1f8f6 | 40 | float emg_value_f32; |
vsluiter | 4:8b298dfada81 | 41 | /*put raw emg value both in red and in emg_value*/ |
vsluiter | 12:768048d7f742 | 42 | 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 | 43 | emg_value_f32 = emg0.read(); |
vsluiter | 19:dbc1bca498e3 | 44 | |
vsluiter | 19:dbc1bca498e3 | 45 | //process emg |
vsluiter | 18:fed07cc1f8f6 | 46 | arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32, &filtered_emg, 1 ); |
vsluiter | 18:fed07cc1f8f6 | 47 | filtered_emg = fabs(filtered_emg); |
vsluiter | 18:fed07cc1f8f6 | 48 | arm_biquad_cascade_df1_f32(&lowpass, &filtered_emg, &filtered_emg, 1 ); |
vsluiter | 19:dbc1bca498e3 | 49 | |
vsluiter | 19:dbc1bca498e3 | 50 | /*send value to PC. */ |
vsluiter | 19:dbc1bca498e3 | 51 | scope.set(0,emg_value); //uint value |
vsluiter | 19:dbc1bca498e3 | 52 | scope.set(1,filtered_emg); //processed float |
vsluiter | 11:ce72ec658a95 | 53 | scope.send(); |
vsluiter | 16:24e992616cf6 | 54 | |
vsluiter | 2:e314bb3b2d99 | 55 | } |
vsluiter | 0:32bb76391d89 | 56 | |
vsluiter | 0:32bb76391d89 | 57 | int main() |
vsluiter | 0:32bb76391d89 | 58 | { |
vsluiter | 21:133e9390a42a | 59 | Ticker log_timer; |
vsluiter | 19:dbc1bca498e3 | 60 | //set up filters. Use external array for constants |
vsluiter | 20:f7d281e3112b | 61 | arm_biquad_cascade_df1_init_f32(&lowpass,1 , lowpass_const, lowpass_states); |
vsluiter | 20:f7d281e3112b | 62 | arm_biquad_cascade_df1_init_f32(&highpass,1 ,highpass_const,highpass_states); |
vsluiter | 19:dbc1bca498e3 | 63 | |
vsluiter | 4:8b298dfada81 | 64 | /**Here you attach the 'void looper(void)' function to the Ticker object |
vsluiter | 9:d33e7b175ad7 | 65 | * The looper() function will be called every 0.01 seconds. |
vsluiter | 4:8b298dfada81 | 66 | * Please mind that the parentheses after looper are omitted when using attach. |
vsluiter | 4:8b298dfada81 | 67 | */ |
vsluiter | 13:18d4cef1fdb4 | 68 | log_timer.attach(looper, 0.005); |
ArvidKeemink | 1:db54d9412d18 | 69 | while(1) //Loop |
vsluiter | 0:32bb76391d89 | 70 | { |
vsluiter | 4:8b298dfada81 | 71 | /*Empty!*/ |
vsluiter | 4:8b298dfada81 | 72 | /*Everything is handled by the interrupt routine now!*/ |
vsluiter | 0:32bb76391d89 | 73 | } |
vsluiter | 19:dbc1bca498e3 | 74 | } |