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