pour daan
Dependencies: HIDScope mbed-dsp mbed
Fork of Project5-filteringBiceps by
main.cpp@31:6b6f60f64cb6, 2014-10-20 (annotated)
- Committer:
- DominiqueC
- Date:
- Mon Oct 20 08:40:39 2014 +0000
- Revision:
- 31:6b6f60f64cb6
- Parent:
- 30:2162f7a82fc7
toevoeging movag
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Daanmk | 0:68c8cf9a1f96 | 1 | /***************************************/ |
Daanmk | 0:68c8cf9a1f96 | 2 | /* */ |
Daanmk | 0:68c8cf9a1f96 | 3 | /* BRONCODE GROEP 5, MODULE 9, 2014 */ |
Daanmk | 0:68c8cf9a1f96 | 4 | /* *****-THE SLAP-****** */ |
Daanmk | 0:68c8cf9a1f96 | 5 | /* */ |
Daanmk | 0:68c8cf9a1f96 | 6 | /* -Dominique Clevers */ |
Daanmk | 0:68c8cf9a1f96 | 7 | /* -Rianne van Dommelen */ |
Daanmk | 0:68c8cf9a1f96 | 8 | /* -Daan de Muinck Keizer */ |
Daanmk | 0:68c8cf9a1f96 | 9 | /* -David den Houting */ |
Daanmk | 0:68c8cf9a1f96 | 10 | /* -Marjolein Thijssen */ |
Daanmk | 0:68c8cf9a1f96 | 11 | /***************************************/ |
Daanmk | 0:68c8cf9a1f96 | 12 | #include "mbed.h" |
Daanmk | 0:68c8cf9a1f96 | 13 | #include "HIDScope.h" |
Daanmk | 0:68c8cf9a1f96 | 14 | #include "arm_math.h" |
DominiqueC | 31:6b6f60f64cb6 | 15 | #define TSAMP 0.005 // sampletijd 200 Hz |
Daanmk | 4:c8ec56d87a5b | 16 | |
Daanmk | 0:68c8cf9a1f96 | 17 | //Define objects |
Daanmk | 0:68c8cf9a1f96 | 18 | AnalogIn emg0(PTB0); //Biceps |
Daanmk | 0:68c8cf9a1f96 | 19 | AnalogIn emg1(PTB1); //Triceps |
Daanmk | 10:1efe8b0cddd0 | 20 | HIDScope scope(5); |
DominiqueC | 31:6b6f60f64cb6 | 21 | int const windowsamples = 60; //aantal samples waaruit het window voor MOVAG bestaat |
Daanmk | 4:c8ec56d87a5b | 22 | |
DominiqueC | 20:3ae43944846a | 23 | arm_biquad_casd_df1_inst_f32 notch; |
Daanmk | 4:c8ec56d87a5b | 24 | //constants for 50Hz notch |
DominiqueC | 20:3ae43944846a | 25 | float notch_const[] = {0.9695312529087462, -0.0, 0.9695312529087462, 0.0, -0.9390625058174924}; |
Daanmk | 0:68c8cf9a1f96 | 26 | //state values |
DominiqueC | 20:3ae43944846a | 27 | float notch_states[4]; |
DominiqueC | 18:2523eba53f16 | 28 | arm_biquad_casd_df1_inst_f32 highpass; |
DominiqueC | 26:d73e33ca524d | 29 | //constants for 20Hz highpass |
DominiqueC | 25:a84afc4598fe | 30 | float highpass_const[] = {0.638945525159022, -1.277891050318045, 0.638945525159022, 1.142980502539901, -0.412801598096189}; |
Daanmk | 0:68c8cf9a1f96 | 31 | //state values |
DominiqueC | 18:2523eba53f16 | 32 | float highpass_states[4]; |
DominiqueC | 12:8a30e43337bb | 33 | //constants for 80Hz lowpass |
DominiqueC | 20:3ae43944846a | 34 | arm_biquad_casd_df1_inst_f32 lowpass; |
DominiqueC | 28:c40dc343033f | 35 | float lowpass_const[] = {0.638945525159022, 1.277891050318045, 0.638945525159022, -1.142980502539901, -0.412801598096189}; |
DominiqueC | 12:8a30e43337bb | 36 | //state values |
DominiqueC | 20:3ae43944846a | 37 | float lowpass_states[4]; |
DominiqueC | 11:a598cb27907f | 38 | |
DominiqueC | 31:6b6f60f64cb6 | 39 | //state values looper |
DominiqueC | 31:6b6f60f64cb6 | 40 | float emg_value_f32,filtered_emg_notch,filtered_emg_notch_highpass,filtered_emg_notch_highpass_lowpass,filtered_emg_eindsignaal_abs; |
DominiqueC | 31:6b6f60f64cb6 | 41 | float xpos_max,xpos_min,xneg_max,xneg_min; // kalibratiewaardes |
DominiqueC | 31:6b6f60f64cb6 | 42 | float emg_biceps [windowsamples]; |
DominiqueC | 31:6b6f60f64cb6 | 43 | |
Daanmk | 0:68c8cf9a1f96 | 44 | void looper() |
Daanmk | 0:68c8cf9a1f96 | 45 | { |
Daanmk | 0:68c8cf9a1f96 | 46 | /*variable to store value in*/ |
Daanmk | 0:68c8cf9a1f96 | 47 | uint16_t emg_value; |
Daanmk | 0:68c8cf9a1f96 | 48 | /*put raw emg value both in red and in emg_value*/ |
Daanmk | 0:68c8cf9a1f96 | 49 | emg_value = emg0.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V) |
Daanmk | 0:68c8cf9a1f96 | 50 | emg_value_f32 = emg0.read(); |
Daanmk | 4:c8ec56d87a5b | 51 | |
Daanmk | 0:68c8cf9a1f96 | 52 | //process emg |
DominiqueC | 21:c9da9adc6706 | 53 | arm_biquad_cascade_df1_f32(¬ch, &emg_value_f32, &filtered_emg_notch, 1 ); |
DominiqueC | 22:fe4d9195f6dd | 54 | arm_biquad_cascade_df1_f32(&highpass, &filtered_emg_notch, &filtered_emg_notch_highpass, 1 ); |
DominiqueC | 27:2ccef1104c16 | 55 | arm_biquad_cascade_df1_f32(&lowpass, &filtered_emg_notch_highpass, &filtered_emg_notch_highpass_lowpass, 1 ); |
DominiqueC | 29:40a157c53841 | 56 | filtered_emg_eindsignaal_abs = 10*fabs(filtered_emg_notch_highpass_lowpass); //gelijkrichter |
DominiqueC | 31:6b6f60f64cb6 | 57 | emg_biceps []= filtered_emg_eindsignaal_abs; |
DominiqueC | 31:6b6f60f64cb6 | 58 | |
DominiqueC | 28:c40dc343033f | 59 | scope.set(0,emg_value_f32); |
DominiqueC | 22:fe4d9195f6dd | 60 | scope.set(1,filtered_emg_notch); |
DominiqueC | 22:fe4d9195f6dd | 61 | scope.set(2,filtered_emg_notch_highpass); |
DominiqueC | 27:2ccef1104c16 | 62 | scope.set(3,filtered_emg_notch_highpass_lowpass); |
DominiqueC | 28:c40dc343033f | 63 | scope.set(4,filtered_emg_eindsignaal_abs); |
Daanmk | 0:68c8cf9a1f96 | 64 | scope.send(); |
Daanmk | 0:68c8cf9a1f96 | 65 | } |
Daanmk | 4:c8ec56d87a5b | 66 | |
DominiqueC | 31:6b6f60f64cb6 | 67 | // movag berekenen |
DominiqueC | 31:6b6f60f64cb6 | 68 | void movag() { |
DominiqueC | 31:6b6f60f64cb6 | 69 | //Variabelen voor berekenen gemiddelden |
DominiqueC | 31:6b6f60f64cb6 | 70 | float avg1,avg2; |
DominiqueC | 31:6b6f60f64cb6 | 71 | avg1=avg2=0; |
DominiqueC | 31:6b6f60f64cb6 | 72 | |
DominiqueC | 31:6b6f60f64cb6 | 73 | //Inhoud van een buffer (=gefilterd signaal) optellen |
DominiqueC | 31:6b6f60f64cb6 | 74 | for(int x=0; x<windowsamples; x++) { |
DominiqueC | 31:6b6f60f64cb6 | 75 | avg1 = avg1 + (emg_biceps[x]); |
DominiqueC | 31:6b6f60f64cb6 | 76 | // avg2 = avg2 + (filtered_emg1_eindsignaal_abs[x]); |
DominiqueC | 31:6b6f60f64cb6 | 77 | } |
DominiqueC | 31:6b6f60f64cb6 | 78 | |
DominiqueC | 31:6b6f60f64cb6 | 79 | //Gemiddelde berekenen en relativeren tov maximum voluntary contraction |
DominiqueC | 31:6b6f60f64cb6 | 80 | avg1 = avg1/windowsamples/xpos_max; |
DominiqueC | 31:6b6f60f64cb6 | 81 | avg2 = avg2/windowsamples/xneg_max; |
DominiqueC | 31:6b6f60f64cb6 | 82 | } |
DominiqueC | 31:6b6f60f64cb6 | 83 | |
DominiqueC | 31:6b6f60f64cb6 | 84 | //eind |
DominiqueC | 31:6b6f60f64cb6 | 85 | |
Daanmk | 0:68c8cf9a1f96 | 86 | int main() |
Daanmk | 0:68c8cf9a1f96 | 87 | { |
DominiqueC | 31:6b6f60f64cb6 | 88 | //referentiewaarden bepalen (ergo kalibreren) |
DominiqueC | 31:6b6f60f64cb6 | 89 | xpos_max = 0.25; |
DominiqueC | 31:6b6f60f64cb6 | 90 | xpos_min = 0.1/xpos_max; |
DominiqueC | 31:6b6f60f64cb6 | 91 | xneg_max = 0.20; |
DominiqueC | 31:6b6f60f64cb6 | 92 | xneg_min = 0.1/xneg_max; |
DominiqueC | 31:6b6f60f64cb6 | 93 | |
DominiqueC | 31:6b6f60f64cb6 | 94 | // Tickers |
Daanmk | 0:68c8cf9a1f96 | 95 | Ticker log_timer; |
DominiqueC | 31:6b6f60f64cb6 | 96 | Ticker window; |
DominiqueC | 31:6b6f60f64cb6 | 97 | |
DominiqueC | 31:6b6f60f64cb6 | 98 | //set up filters. Use external array for constants |
DominiqueC | 20:3ae43944846a | 99 | arm_biquad_cascade_df1_init_f32(¬ch,1 , notch_const, notch_states); |
DominiqueC | 18:2523eba53f16 | 100 | arm_biquad_cascade_df1_init_f32(&highpass,1 ,highpass_const,highpass_states); |
DominiqueC | 20:3ae43944846a | 101 | arm_biquad_cascade_df1_init_f32(&lowpass,1 ,lowpass_const,lowpass_states); |
Daanmk | 0:68c8cf9a1f96 | 102 | |
DominiqueC | 31:6b6f60f64cb6 | 103 | log_timer.attach(looper, TSAMP); |
DominiqueC | 31:6b6f60f64cb6 | 104 | window.attach(movag, TSAMP); |
Daanmk | 0:68c8cf9a1f96 | 105 | while(1) //Loop |
Daanmk | 0:68c8cf9a1f96 | 106 | { |
Daanmk | 0:68c8cf9a1f96 | 107 | /*Empty!*/ |
Daanmk | 0:68c8cf9a1f96 | 108 | /*Everything is handled by the interrupt routine now!*/ |
Daanmk | 0:68c8cf9a1f96 | 109 | } |
Daanmk | 0:68c8cf9a1f96 | 110 | } |