Bandpass, notch, abs en laagdoorlaat 3H
Dependencies: HIDScope MODSERIAL TextLCD mbed-dsp mbed
Fork of EMGmeten by
EMGmeten.cpp@6:740f08fad2c3, 2014-10-30 (annotated)
- Committer:
- lauradeheus
- Date:
- Thu Oct 30 17:49:08 2014 +0000
- Revision:
- 6:740f08fad2c3
- Parent:
- 5:f93b7068c202
- Child:
- 7:0e76120eb7ad
EMGmeten en state aangeven
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lauradeheus | 0:b68a6d0451b8 | 1 | #include "mbed.h" |
lauradeheus | 0:b68a6d0451b8 | 2 | #include "HIDScope.h" |
lauradeheus | 2:5d64e4c95f5c | 3 | #include "arm_math.h" |
lauradeheus | 2:5d64e4c95f5c | 4 | #include "MODSERIAL.h" |
lauradeheus | 4:7150ed6ce984 | 5 | #include "TextLCD.h" |
lauradeheus | 4:7150ed6ce984 | 6 | |
lauradeheus | 4:7150ed6ce984 | 7 | #define EMG_treshhold 0,05 |
lauradeheus | 0:b68a6d0451b8 | 8 | |
lauradeheus | 2:5d64e4c95f5c | 9 | //MODSERIAL pc(USBTX,USBRX); |
lauradeheus | 0:b68a6d0451b8 | 10 | AnalogIn emg(PTB1); //Analog input |
lauradeheus | 4:7150ed6ce984 | 11 | HIDScope scope(2); // Twee kanalen op de HIDScope |
lauradeheus | 4:7150ed6ce984 | 12 | DigitalIn knop(PTD4); /*Digital input pin (knop) definieren*/ |
lauradeheus | 4:7150ed6ce984 | 13 | TextLCD lcd(PTD2, PTA12, PTB2, PTB3, PTC2, PTA13, TextLCD::LCD16x2); |
lauradeheus | 0:b68a6d0451b8 | 14 | |
lauradeheus | 4:7150ed6ce984 | 15 | arm_biquad_casd_df1_inst_f32 lowpass_1; //2e orde lowpass biquad butterworthfilter 99Hz |
lauradeheus | 4:7150ed6ce984 | 16 | arm_biquad_casd_df1_inst_f32 lowpass_2; //2e orde lowpass biquad butterworthfilter 3Hz |
lauradeheus | 4:7150ed6ce984 | 17 | arm_biquad_casd_df1_inst_f32 highpass; //2e orde highpass biquad butterworthfilter 20Hz |
lauradeheus | 4:7150ed6ce984 | 18 | arm_biquad_casd_df1_inst_f32 notch; //2e orde lowpass biquad butterworthfilter 50Hz |
lauradeheus | 1:b7a9fcf50776 | 19 | float lowpass_1_const[] = {0.978030479206560 , 1.956060958413119 , 0.978030479206560 , -1.955578240315036 , -0.956543676511203}; |
lauradeheus | 1:b7a9fcf50776 | 20 | float lowpass_1_states[4]; |
lauradeheus | 4:7150ed6ce984 | 21 | float lowpass_2_const[] = {0.002080567135492 , 0.004161134270985 , 0.002080567135492 , 1.866892279711715 , -0.875214548253684}; //3Hz |
lauradeheus | 4:7150ed6ce984 | 22 | //{0.0009446914586925257 , 0.0018893829173850514 , 0.0009446914586925257 , 1.911196288237583 , -0.914975054072353};//2Hz |
lauradeheus | 4:7150ed6ce984 | 23 | //{0.00024135899874854145 , 0.0004827179974970829 , 0.00024135899874854145 , 1.9555778328194147 , -0.9565432688144089}; 1Hz |
lauradeheus | 1:b7a9fcf50776 | 24 | float lowpass_2_states[4]; |
lauradeheus | 1:b7a9fcf50776 | 25 | float highpass_const[] = {0.638945525159022 , -1.277891050318045 , 0.638945525159022 , 1.142980502539901 , -0.412801598096189}; |
lauradeheus | 0:b68a6d0451b8 | 26 | float highpass_states[4]; |
lauradeheus | 2:5d64e4c95f5c | 27 | float notch_const[] = {0.978048948305681 , 0.000000000000000 , 0.978048948305681 , 0.000000000000000 , -0.956097896611362}; |
lauradeheus | 2:5d64e4c95f5c | 28 | float notch_states[4]; |
lauradeheus | 4:7150ed6ce984 | 29 | float filtered_emg; |
lauradeheus | 6:740f08fad2c3 | 30 | float EMG_max=0.15; |
lauradeheus | 4:7150ed6ce984 | 31 | float EMG_treshhold_laag = 0.3*EMG_max; |
lauradeheus | 4:7150ed6ce984 | 32 | float EMG_treshhold_hoog = 0.7*EMG_max; |
lauradeheus | 4:7150ed6ce984 | 33 | |
lauradeheus | 4:7150ed6ce984 | 34 | bool aanspan; |
lauradeheus | 4:7150ed6ce984 | 35 | int aantal_pieken; |
lauradeheus | 5:f93b7068c202 | 36 | int doel; |
lauradeheus | 4:7150ed6ce984 | 37 | |
lauradeheus | 4:7150ed6ce984 | 38 | void looper(); |
lauradeheus | 4:7150ed6ce984 | 39 | void pieken_tellen(); |
lauradeheus | 6:740f08fad2c3 | 40 | void EMG_max_meting(); |
lauradeheus | 4:7150ed6ce984 | 41 | //char *lcd_r1 = new char[16]; |
lauradeheus | 4:7150ed6ce984 | 42 | //char *lcd_r2 = new char[16]; |
lauradeheus | 0:b68a6d0451b8 | 43 | |
lauradeheus | 0:b68a6d0451b8 | 44 | void looper() |
lauradeheus | 0:b68a6d0451b8 | 45 | { |
lauradeheus | 0:b68a6d0451b8 | 46 | uint16_t emg_value; |
lauradeheus | 0:b68a6d0451b8 | 47 | float emg_value_f32; |
lauradeheus | 0:b68a6d0451b8 | 48 | emg_value = emg.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V) |
lauradeheus | 0:b68a6d0451b8 | 49 | emg_value_f32 = emg.read(); |
lauradeheus | 0:b68a6d0451b8 | 50 | |
lauradeheus | 0:b68a6d0451b8 | 51 | arm_biquad_cascade_df1_f32(&highpass, &emg_value_f32, &filtered_emg, 1 ); |
lauradeheus | 1:b7a9fcf50776 | 52 | arm_biquad_cascade_df1_f32(&lowpass_1, &filtered_emg, &filtered_emg, 1 ); |
lauradeheus | 2:5d64e4c95f5c | 53 | arm_biquad_cascade_df1_f32(¬ch, &filtered_emg, &filtered_emg, 1); |
lauradeheus | 1:b7a9fcf50776 | 54 | filtered_emg = fabs(filtered_emg); |
lauradeheus | 2:5d64e4c95f5c | 55 | //emg_value_f32 = fabs(emg_value_f32); |
lauradeheus | 3:c82170d8b6c8 | 56 | arm_biquad_cascade_df1_f32(&lowpass_2, &filtered_emg, &filtered_emg, 1 ); |
lauradeheus | 0:b68a6d0451b8 | 57 | |
lauradeheus | 6:740f08fad2c3 | 58 | //EMG_max_meting(); |
lauradeheus | 6:740f08fad2c3 | 59 | pieken_tellen(); |
lauradeheus | 6:740f08fad2c3 | 60 | doel = aantal_pieken-((aantal_pieken/3)*3)+1; |
lauradeheus | 6:740f08fad2c3 | 61 | |
lauradeheus | 0:b68a6d0451b8 | 62 | scope.set(0,emg_value); //uint value |
lauradeheus | 0:b68a6d0451b8 | 63 | scope.set(1,filtered_emg); //processed float |
lauradeheus | 0:b68a6d0451b8 | 64 | scope.send(); |
lauradeheus | 0:b68a6d0451b8 | 65 | } |
lauradeheus | 0:b68a6d0451b8 | 66 | |
lauradeheus | 6:740f08fad2c3 | 67 | void EMG_max_meting() |
lauradeheus | 6:740f08fad2c3 | 68 | { |
lauradeheus | 6:740f08fad2c3 | 69 | int i=0; // tijdspad |
lauradeheus | 6:740f08fad2c3 | 70 | lcd.cls(); |
lauradeheus | 6:740f08fad2c3 | 71 | lcd.printf("Maximale\nEMGmeting"); |
lauradeheus | 6:740f08fad2c3 | 72 | for(i=0; i<300; i++) |
lauradeheus | 6:740f08fad2c3 | 73 | { |
lauradeheus | 6:740f08fad2c3 | 74 | wait(0.1); |
lauradeheus | 6:740f08fad2c3 | 75 | if (filtered_emg>=EMG_max) |
lauradeheus | 6:740f08fad2c3 | 76 | { |
lauradeheus | 6:740f08fad2c3 | 77 | EMG_max=filtered_emg; |
lauradeheus | 6:740f08fad2c3 | 78 | } |
lauradeheus | 6:740f08fad2c3 | 79 | } |
lauradeheus | 6:740f08fad2c3 | 80 | lcd.cls(); |
lauradeheus | 6:740f08fad2c3 | 81 | lcd.printf("Maximale EMG\nbepaald"); |
lauradeheus | 6:740f08fad2c3 | 82 | wait(3); |
lauradeheus | 6:740f08fad2c3 | 83 | EMG_treshhold_laag = 0.3*EMG_max; |
lauradeheus | 6:740f08fad2c3 | 84 | EMG_treshhold_hoog = 0.7*EMG_max; |
lauradeheus | 6:740f08fad2c3 | 85 | } |
lauradeheus | 6:740f08fad2c3 | 86 | |
lauradeheus | 6:740f08fad2c3 | 87 | void pieken_tellen() |
lauradeheus | 6:740f08fad2c3 | 88 | { |
lauradeheus | 6:740f08fad2c3 | 89 | if (filtered_emg>=EMG_treshhold_hoog) |
lauradeheus | 6:740f08fad2c3 | 90 | { |
lauradeheus | 6:740f08fad2c3 | 91 | aanspan=true; //maak een variabele waarin je opslaat dat het signaal hoog is. |
lauradeheus | 6:740f08fad2c3 | 92 | } |
lauradeheus | 6:740f08fad2c3 | 93 | if (aanspan==true && filtered_emg<=EMG_treshhold_laag)//== ipv =, anders wordt aanspan true gemaakt |
lauradeheus | 6:740f08fad2c3 | 94 | { |
lauradeheus | 6:740f08fad2c3 | 95 | aanspan=false; |
lauradeheus | 6:740f08fad2c3 | 96 | aantal_pieken++; |
lauradeheus | 6:740f08fad2c3 | 97 | } |
lauradeheus | 6:740f08fad2c3 | 98 | } |
lauradeheus | 6:740f08fad2c3 | 99 | |
lauradeheus | 0:b68a6d0451b8 | 100 | int main() |
lauradeheus | 0:b68a6d0451b8 | 101 | { |
lauradeheus | 0:b68a6d0451b8 | 102 | Ticker log_timer; |
lauradeheus | 0:b68a6d0451b8 | 103 | |
lauradeheus | 1:b7a9fcf50776 | 104 | arm_biquad_cascade_df1_init_f32(&lowpass_1,1 , lowpass_1_const, lowpass_1_states); |
lauradeheus | 0:b68a6d0451b8 | 105 | arm_biquad_cascade_df1_init_f32(&highpass,1 , highpass_const, highpass_states); |
lauradeheus | 2:5d64e4c95f5c | 106 | arm_biquad_cascade_df1_init_f32(¬ch,1 , notch_const, notch_states); |
lauradeheus | 3:c82170d8b6c8 | 107 | arm_biquad_cascade_df1_init_f32(&lowpass_2,1 , lowpass_2_const, lowpass_2_states); |
lauradeheus | 0:b68a6d0451b8 | 108 | |
lauradeheus | 0:b68a6d0451b8 | 109 | log_timer.attach(looper, 0.005); |
lauradeheus | 0:b68a6d0451b8 | 110 | |
lauradeheus | 0:b68a6d0451b8 | 111 | while(1) //Loop |
lauradeheus | 0:b68a6d0451b8 | 112 | { |
lauradeheus | 4:7150ed6ce984 | 113 | lcd.cls(); |
lauradeheus | 5:f93b7068c202 | 114 | lcd.printf("Te raken doel =\n%d", doel); |
lauradeheus | 4:7150ed6ce984 | 115 | wait(0.02); |
lauradeheus | 0:b68a6d0451b8 | 116 | /*Empty!*/ |
lauradeheus | 0:b68a6d0451b8 | 117 | /*Everything is handled by the interrupt routine now!*/ |
lauradeheus | 2:5d64e4c95f5c | 118 | //pc.baud(9600); |
lauradeheus | 0:b68a6d0451b8 | 119 | } |
lauradeheus | 6:740f08fad2c3 | 120 | } |