Measure EMG, publish on HID Scope
Dependencies: HIDScope mbed mbed-dsp
Fork of EMG by
main.cpp.orig@16:24e992616cf6, 2014-09-24 (annotated)
- Committer:
- vsluiter
- Date:
- Wed Sep 24 08:31:30 2014 +0000
- Revision:
- 16:24e992616cf6
Added filter code (not tested)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 16:24e992616cf6 | 1 | #include "mbed.h" |
vsluiter | 16:24e992616cf6 | 2 | #include "MODSERIAL.h" |
vsluiter | 16:24e992616cf6 | 3 | |
vsluiter | 16:24e992616cf6 | 4 | //Define objects |
vsluiter | 16:24e992616cf6 | 5 | AnalogIn emg0(PTB1); //Analog input |
vsluiter | 16:24e992616cf6 | 6 | PwmOut red(LED_RED); //PWM output |
vsluiter | 16:24e992616cf6 | 7 | Ticker log_timer; |
vsluiter | 16:24e992616cf6 | 8 | MODSERIAL pc(USBTX,USBRX); |
vsluiter | 16:24e992616cf6 | 9 | |
vsluiter | 16:24e992616cf6 | 10 | typedef struct second_order_constants |
vsluiter | 16:24e992616cf6 | 11 | { |
vsluiter | 16:24e992616cf6 | 12 | float b[3]; |
vsluiter | 16:24e992616cf6 | 13 | float a[3]; |
vsluiter | 16:24e992616cf6 | 14 | } second_order_constants_t; |
vsluiter | 16:24e992616cf6 | 15 | |
vsluiter | 16:24e992616cf6 | 16 | typedef struct second_order_values |
vsluiter | 16:24e992616cf6 | 17 | { |
vsluiter | 16:24e992616cf6 | 18 | float x_1,x_2; |
vsluiter | 16:24e992616cf6 | 19 | float y_1,y_2; |
vsluiter | 16:24e992616cf6 | 20 | } second_order_values_t; |
vsluiter | 16:24e992616cf6 | 21 | |
vsluiter | 16:24e992616cf6 | 22 | //constants |
vsluiter | 16:24e992616cf6 | 23 | second_order_constants_t highpass= {{0.97803048, -1.95606096, 0.97803048},{1, -1.95557824, 0.95654368}}; |
vsluiter | 16:24e992616cf6 | 24 | //type for values |
vsluiter | 16:24e992616cf6 | 25 | second_order_values_t highpass_values; |
vsluiter | 16:24e992616cf6 | 26 | |
vsluiter | 16:24e992616cf6 | 27 | float second_order(float x, second_order_constants_t constants, second_order_values_t &values); |
vsluiter | 16:24e992616cf6 | 28 | |
vsluiter | 16:24e992616cf6 | 29 | |
vsluiter | 16:24e992616cf6 | 30 | /** Looper function |
vsluiter | 16:24e992616cf6 | 31 | * functions used for Ticker and Timeout should be of type void <name>(void) |
vsluiter | 16:24e992616cf6 | 32 | * i.e. no input arguments, no output arguments. |
vsluiter | 16:24e992616cf6 | 33 | * if you want to change a variable that you use in other places (for example in main) |
vsluiter | 16:24e992616cf6 | 34 | * you will have to make that variable global in order to be able to reach it both from |
vsluiter | 16:24e992616cf6 | 35 | * the function called at interrupt time, and in the main function. |
vsluiter | 16:24e992616cf6 | 36 | * To make a variable global, define it under the includes. |
vsluiter | 16:24e992616cf6 | 37 | * variables that are changed in the interrupt routine (written to) should be made |
vsluiter | 16:24e992616cf6 | 38 | * 'volatile' to let the compiler know that those values may change outside the current context. |
vsluiter | 16:24e992616cf6 | 39 | * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value" |
vsluiter | 16:24e992616cf6 | 40 | * in the example below, the variable is not re-used in the main function, and is thus declared |
vsluiter | 16:24e992616cf6 | 41 | * local in the looper function only. |
vsluiter | 16:24e992616cf6 | 42 | **/ |
vsluiter | 16:24e992616cf6 | 43 | void looper() |
vsluiter | 16:24e992616cf6 | 44 | { |
vsluiter | 16:24e992616cf6 | 45 | /*variable to store value in*/ |
vsluiter | 16:24e992616cf6 | 46 | uint16_t emg_value; |
vsluiter | 16:24e992616cf6 | 47 | float filtered_emg; |
vsluiter | 16:24e992616cf6 | 48 | /*put raw emg value both in red and in emg_value*/ |
vsluiter | 16:24e992616cf6 | 49 | red.write(emg0.read()); // read float value (0..1 = 0..3.3V) |
vsluiter | 16:24e992616cf6 | 50 | emg_value = emg0.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V) |
vsluiter | 16:24e992616cf6 | 51 | filtered_emg = second_order((float)emg_value,highpass, highpass_values); |
vsluiter | 16:24e992616cf6 | 52 | /*send value to PC. Line below is used to prevent buffer overrun */ |
vsluiter | 16:24e992616cf6 | 53 | if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) |
vsluiter | 16:24e992616cf6 | 54 | pc.printf("%u\t%f\n",emg_value, filtered_emg); |
vsluiter | 16:24e992616cf6 | 55 | /**When not using the LED, the above could also have been done this way: |
vsluiter | 16:24e992616cf6 | 56 | * pc.printf("%u\n", emg0.read_u16()); |
vsluiter | 16:24e992616cf6 | 57 | */ |
vsluiter | 16:24e992616cf6 | 58 | } |
vsluiter | 16:24e992616cf6 | 59 | |
vsluiter | 16:24e992616cf6 | 60 | int main() |
vsluiter | 16:24e992616cf6 | 61 | { |
vsluiter | 16:24e992616cf6 | 62 | /*setup baudrate. Choose the same in your program on PC side*/ |
vsluiter | 16:24e992616cf6 | 63 | pc.baud(115200); |
vsluiter | 16:24e992616cf6 | 64 | /*set the period for the PWM to the red LED*/ |
vsluiter | 16:24e992616cf6 | 65 | red.period_ms(2); |
vsluiter | 16:24e992616cf6 | 66 | /**Here you attach the 'void looper(void)' function to the Ticker object |
vsluiter | 16:24e992616cf6 | 67 | * The looper() function will be called every 0.01 seconds. |
vsluiter | 16:24e992616cf6 | 68 | * Please mind that the parentheses after looper are omitted when using attach. |
vsluiter | 16:24e992616cf6 | 69 | */ |
vsluiter | 16:24e992616cf6 | 70 | log_timer.attach(looper, 0.01); |
vsluiter | 16:24e992616cf6 | 71 | while(1) //Loop |
vsluiter | 16:24e992616cf6 | 72 | { |
vsluiter | 16:24e992616cf6 | 73 | /*Empty!*/ |
vsluiter | 16:24e992616cf6 | 74 | /*Everything is handled by the interrupt routine now!*/ |
vsluiter | 16:24e992616cf6 | 75 | } |
vsluiter | 16:24e992616cf6 | 76 | } |
vsluiter | 16:24e992616cf6 | 77 | |
vsluiter | 16:24e992616cf6 | 78 | float second_order(float x, second_order_constants_t constants, second_order_values_t &values) |
vsluiter | 16:24e992616cf6 | 79 | { |
vsluiter | 16:24e992616cf6 | 80 | float y = 0; |
vsluiter | 16:24e992616cf6 | 81 | float b_terms, a_terms; |
vsluiter | 16:24e992616cf6 | 82 | b_terms = (constants.b[0]*x) + (constants.b[1]*values.x_1) + (constants.b[2]*values.x_2); |
vsluiter | 16:24e992616cf6 | 83 | a_terms = (constants.a[1]*values.y_1) + (constants.a[2]*values.y_2); |
vsluiter | 16:24e992616cf6 | 84 | y=(1./constants.a[0])* (b_terms-a_terms); |
vsluiter | 16:24e992616cf6 | 85 | values.x_2 = values.x_1; |
vsluiter | 16:24e992616cf6 | 86 | values.x_1 = x; |
vsluiter | 16:24e992616cf6 | 87 | values.y_2 = values.y_1; |
vsluiter | 16:24e992616cf6 | 88 | values.y_1 = y; |
vsluiter | 16:24e992616cf6 | 89 | return y; |
vsluiter | 16:24e992616cf6 | 90 | } |