Werkcollege opgave 23 september BMT K9
Dependencies: Encoder HIDScope MODSERIAL mbed QEI biquadFilter
main.cpp@46:c88e0d2daf15, 2015-10-21 (annotated)
- Committer:
- sigert
- Date:
- Wed Oct 21 13:15:54 2015 +0000
- Revision:
- 46:c88e0d2daf15
- Parent:
- 45:10ba78d97d75
- Child:
- 48:a0cc7ac1856f
- Child:
- 50:c4c9daf7b74c
untested: lights <-> pwm_average (EMG)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bscheltinga | 0:fe3896c6eeb0 | 1 | #include "mbed.h" |
bscheltinga | 12:0a079e86348e | 2 | #include "HIDScope.h" |
bscheltinga | 0:fe3896c6eeb0 | 3 | #include "MODSERIAL.h" |
bscheltinga | 13:04e10692e239 | 4 | #include "biquadFilter.h" //Filter direct form II |
sigert | 42:b5186a54d839 | 5 | #include "QEI.h" |
sigert | 46:c88e0d2daf15 | 6 | const double ledon = 1; |
sigert | 46:c88e0d2daf15 | 7 | const double ledoff = 0; |
sigert | 46:c88e0d2daf15 | 8 | |
sigert | 46:c88e0d2daf15 | 9 | DigitalOut ledgreen1(D0); |
sigert | 46:c88e0d2daf15 | 10 | DigitalOut ledgreen2(D1); |
sigert | 46:c88e0d2daf15 | 11 | DigitalOut ledyellow1(D2); |
sigert | 46:c88e0d2daf15 | 12 | DigitalOut ledyellow2(D3); |
sigert | 46:c88e0d2daf15 | 13 | DigitalOut ledred1(D9); |
sigert | 46:c88e0d2daf15 | 14 | DigitalOut ledred2(D10); |
sigert | 46:c88e0d2daf15 | 15 | |
sigert | 46:c88e0d2daf15 | 16 | void leduit() { ledgreen1 = ledoff; ledgreen2 = ledoff; ledyellow1 = ledoff; ledyellow2 = ledoff; ledred1 = ledoff; ledred2 = ledoff; } |
bscheltinga | 0:fe3896c6eeb0 | 17 | |
bscheltinga | 26:1090acf98efc | 18 | // [DEFINE INPUTS] // |
sigert | 42:b5186a54d839 | 19 | DigitalOut debug_led_red (LED_RED); // Debug LED |
sigert | 37:6c04c15d9bbe | 20 | DigitalOut debug_led_green(LED_GREEN); // Debug LED |
sigert | 42:b5186a54d839 | 21 | DigitalOut debug_led_blue (LED_BLUE); // Debug LED |
ThomasBNL | 36:f29a36683b1a | 22 | |
sigert | 37:6c04c15d9bbe | 23 | const double off=1; // Led off |
sigert | 37:6c04c15d9bbe | 24 | const double on=0; // Led on |
bscheltinga | 21:594915ba2bf9 | 25 | |
sigert | 42:b5186a54d839 | 26 | // MOTORS |
sigert | 42:b5186a54d839 | 27 | QEI motor_turn(D12,D13,NC,32); QEI motor_strike(D9,D10,NC,32); // TURN - STRIKE : Encoder for motor |
sigert | 42:b5186a54d839 | 28 | PwmOut pwm_motor_turn(D5); PwmOut pwm_motor_strike(D6); // TURN - STRIKE : Pwm for motor |
sigert | 42:b5186a54d839 | 29 | DigitalOut motordirection_turn(D4); DigitalOut motordirection_strike(D7); // TURN - STRIKE : Direction of the motor |
sigert | 42:b5186a54d839 | 30 | |
sigert | 42:b5186a54d839 | 31 | |
bscheltinga | 26:1090acf98efc | 32 | // [DEFINE CONSTANTS] // |
ThomasBNL | 27:85e5d36bb6c5 | 33 | HIDScope scope(3); // Aantal HIDScope kanalen |
bscheltinga | 18:68067ffd169e | 34 | MODSERIAL pc(USBTX,USBRX); |
bscheltinga | 20:d5f5c60adc43 | 35 | Ticker control_tick; |
ThomasBNL | 27:85e5d36bb6c5 | 36 | Ticker T1; |
bscheltinga | 15:7870f7912904 | 37 | |
bscheltinga | 26:1090acf98efc | 38 | // [BIQUAD FILTERS] // |
ThomasBNL | 27:85e5d36bb6c5 | 39 | int Fs = 512; // sampling frequency |
ThomasBNL | 36:f29a36683b1a | 40 | const double low_b0 = 0.05892937945281792; |
ThomasBNL | 36:f29a36683b1a | 41 | const double low_b1 = 0.11785875890563584; |
ThomasBNL | 36:f29a36683b1a | 42 | const double low_b2 = 0.05892937945281792; |
ThomasBNL | 36:f29a36683b1a | 43 | const double low_a1 = -1.205716572226748; |
sigert | 37:6c04c15d9bbe | 44 | const double low_a2 = 0.44143409003801976; //Notch 1 LOW: VIA online biquad calculator Lowpass 520-48-0.7071-6 |
bscheltinga | 15:7870f7912904 | 45 | |
ThomasBNL | 36:f29a36683b1a | 46 | const double high_b0 = 0.6389437261127494; |
ThomasBNL | 36:f29a36683b1a | 47 | const double high_b1 = -1.2778874522254988; |
ThomasBNL | 36:f29a36683b1a | 48 | const double high_b2 = 0.6389437261127494; |
ThomasBNL | 36:f29a36683b1a | 49 | const double high_a1 = -1.1429772843080923; |
ThomasBNL | 39:9fa091753592 | 50 | const double high_a2 = 0.41279762014290533; // NOTCH 2 HIGH: VIA online biquad calculator Highpass 520-52-0.7071-6 |
bscheltinga | 22:14abcfdd1554 | 51 | |
ThomasBNL | 39:9fa091753592 | 52 | const double high_b0_HP = 0.84855234544818812; |
ThomasBNL | 39:9fa091753592 | 53 | const double high_b1_HP = -1.6970469089637623; |
ThomasBNL | 39:9fa091753592 | 54 | const double high_b2_HP = 0.8485234544818812; |
ThomasBNL | 39:9fa091753592 | 55 | const double high_a1_HP = -1.6564788473046559; |
ThomasBNL | 39:9fa091753592 | 56 | const double high_a2_HP = 0.7376149706228688; // HIGHPASS : NOG OPZOEKEN!! : >25Hz? sample rate 512Hz |
sigert | 37:6c04c15d9bbe | 57 | |
ThomasBNL | 39:9fa091753592 | 58 | const double low_b0_LP = 0.0013067079602315681; |
ThomasBNL | 39:9fa091753592 | 59 | const double low_b1_LP = 0.0026134159204631363; |
ThomasBNL | 39:9fa091753592 | 60 | const double low_b2_LP = 0.0013067079602315681; |
ThomasBNL | 39:9fa091753592 | 61 | const double low_a1_LP = -1.9238184798980429; |
ThomasBNL | 39:9fa091753592 | 62 | const double low_a2_LP = 0.9290453117389691; // LOWPASS : NOG OPZOEKEN!! <5-10 Hz? sample rate 512Hz |
sigert | 37:6c04c15d9bbe | 63 | // |
ThomasBNL | 36:f29a36683b1a | 64 | //Left bicep |
sigert | 37:6c04c15d9bbe | 65 | biquadFilter highpassfilter_1(high_a1_HP, high_a2_HP, high_b0_HP, high_b1_HP, high_b2_HP);// moeten nog waardes voor gemaakt worden? (>25Hz doorlaten) |
sigert | 37:6c04c15d9bbe | 66 | biquadFilter notchL1(high_a1, high_a2, high_b0, high_b1, high_b2);// moeten nog waardes voor gemaakt worden (>52Hz doorlaten) |
ThomasBNL | 36:f29a36683b1a | 67 | biquadFilter notchL2(low_a1, low_a2, low_b0, low_b1, low_b2);// moeten nog waardes voor gemaakt worden (<48Hz doorlaten) |
sigert | 37:6c04c15d9bbe | 68 | biquadFilter lowpassfilter_1(low_a1_LP, low_a2_LP, low_b0_LP, low_b1_LP, low_b2_LP); |
bscheltinga | 0:fe3896c6eeb0 | 69 | |
ThomasBNL | 36:f29a36683b1a | 70 | // Right bicep |
sigert | 37:6c04c15d9bbe | 71 | biquadFilter highpassfilter_2(high_a1_HP, high_a2_HP, high_b0_HP, high_b1_HP, high_b2_HP);// moeten nog waardes voor gemaakt worden? |
sigert | 37:6c04c15d9bbe | 72 | biquadFilter notchR1(high_a1, high_a2, high_b0, high_b1, high_b2); // moeten nog waardes voor gemaakt worden |
ThomasBNL | 36:f29a36683b1a | 73 | biquadFilter notchR2(low_a1, low_a2, low_b0, low_b1, low_b2); // moeten nog waardes voor gemaakt worden |
sigert | 37:6c04c15d9bbe | 74 | biquadFilter lowpassfilter_2(low_a1_LP, low_a2_LP, low_b0_LP, low_b1_LP, low_b2_LP);// moeten nog waardes voor gemaakt worden? |
bscheltinga | 20:d5f5c60adc43 | 75 | |
ThomasBNL | 27:85e5d36bb6c5 | 76 | |
ThomasBNL | 36:f29a36683b1a | 77 | AnalogIn input1(A0); // EMG: Two AnalogIn EMG inputs |
ThomasBNL | 36:f29a36683b1a | 78 | AnalogIn input2(A1); // EMG: Two AnalogIn EMG inputs |
ThomasBNL | 36:f29a36683b1a | 79 | |
ThomasBNL | 36:f29a36683b1a | 80 | // __________________________ |
ThomasBNL | 36:f29a36683b1a | 81 | // : [EMG variables] : |
ThomasBNL | 36:f29a36683b1a | 82 | // :__________________________: |
ThomasBNL | 36:f29a36683b1a | 83 | // |
ThomasBNL | 36:f29a36683b1a | 84 | |
ThomasBNL | 36:f29a36683b1a | 85 | volatile bool control_go = false; // EMG: |
ThomasBNL | 36:f29a36683b1a | 86 | volatile bool sample = false; |
ThomasBNL | 36:f29a36683b1a | 87 | volatile bool sample_error = false; |
ThomasBNL | 36:f29a36683b1a | 88 | volatile bool sample_error_strike = false; |
ThomasBNL | 36:f29a36683b1a | 89 | |
sigert | 42:b5186a54d839 | 90 | double Threshold_Bicep_Left_1, Threshold_Bicep_Left_2, Threshold_Bicep_Right_1, Threshold_Bicep_Right_2; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is); |
sigert | 42:b5186a54d839 | 91 | |
ThomasBNL | 36:f29a36683b1a | 92 | |
ThomasBNL | 36:f29a36683b1a | 93 | double minimum_L; double maximum_L; |
ThomasBNL | 36:f29a36683b1a | 94 | double EMG_L_min; double EMG_L_max; |
ThomasBNL | 36:f29a36683b1a | 95 | double minimum_R; double maximum_R; |
ThomasBNL | 36:f29a36683b1a | 96 | double EMG_R_min; double EMG_R_max; |
ThomasBNL | 36:f29a36683b1a | 97 | double EMG_left_Bicep; double EMG_Right_Bicep; // input variables |
ThomasBNL | 36:f29a36683b1a | 98 | double EMG_Left_Bicep_filtered_notch_1;double EMG_Right_Bicep_filtered_notch_1; |
ThomasBNL | 36:f29a36683b1a | 99 | double EMG_Left_Bicep_filtered_notch_2;double EMG_Right_Bicep_filtered_notch_2; |
ThomasBNL | 36:f29a36683b1a | 100 | double EMG_Left_Bicep_filtered; double EMG_Right_Bicep_filtered; // output variables |
ThomasBNL | 36:f29a36683b1a | 101 | |
ThomasBNL | 36:f29a36683b1a | 102 | |
ThomasBNL | 36:f29a36683b1a | 103 | double n=0; double c=0; double k=0; double p=0; double er=0; |
ThomasBNL | 36:f29a36683b1a | 104 | |
ThomasBNL | 36:f29a36683b1a | 105 | // FUNCTIONS |
ThomasBNL | 36:f29a36683b1a | 106 | void ControlGo(); |
ThomasBNL | 36:f29a36683b1a | 107 | void take_sample(); |
ThomasBNL | 36:f29a36683b1a | 108 | void Filter(); |
ThomasBNL | 36:f29a36683b1a | 109 | void sample_filter(); |
ThomasBNL | 36:f29a36683b1a | 110 | void countdown_from_5(); |
ThomasBNL | 36:f29a36683b1a | 111 | void calibration(); |
sigert | 37:6c04c15d9bbe | 112 | void red();void blue();void green();void white();void yellow();void cyan();void purple(); void black(); |
sigert | 37:6c04c15d9bbe | 113 | |
sigert | 42:b5186a54d839 | 114 | double RS_0,RS_1,RS_2,RS_3,RS_4,RS_5,RS_6,RS_7,RS_8,RS_9,RS_10, |
sigert | 42:b5186a54d839 | 115 | RS_11,RS_12,RS_13,RS_14,RS_15,RS_16,RS_17,RS_18,RS_19,RS_20, |
sigert | 42:b5186a54d839 | 116 | RS_21,RS_22,RS_23,RS_24,RS_25,RS_26,RS_27,RS_28,RS_29,RS_30,RS_31; |
sigert | 42:b5186a54d839 | 117 | |
sigert | 42:b5186a54d839 | 118 | double LS_0,LS_1,LS_2,LS_3,LS_4,LS_5,LS_6,LS_7,LS_8,LS_9,LS_10, |
sigert | 42:b5186a54d839 | 119 | LS_11,LS_12,LS_13,LS_14,LS_15,LS_16,LS_17,LS_18,LS_19,LS_20, |
sigert | 42:b5186a54d839 | 120 | LS_21,LS_22,LS_23,LS_24,LS_25,LS_26,LS_27,LS_28,LS_29,LS_30,LS_31; |
sigert | 42:b5186a54d839 | 121 | |
sigert | 42:b5186a54d839 | 122 | double moving_average_right, moving_average_left; |
sigert | 42:b5186a54d839 | 123 | |
sigert | 42:b5186a54d839 | 124 | void keep_in_range (double * in, double min, double max); |
sigert | 42:b5186a54d839 | 125 | |
sigert | 42:b5186a54d839 | 126 | double pwm_strike; |
sigert | 45:10ba78d97d75 | 127 | double f; double pwm_average; double smp; |
ThomasBNL | 36:f29a36683b1a | 128 | //==========================// |
ThomasBNL | 27:85e5d36bb6c5 | 129 | // [MAIN FUNCTION] // |
ThomasBNL | 36:f29a36683b1a | 130 | //==========================// |
ThomasBNL | 27:85e5d36bb6c5 | 131 | int main() |
ThomasBNL | 27:85e5d36bb6c5 | 132 | { |
ThomasBNL | 27:85e5d36bb6c5 | 133 | control_tick.attach(&ControlGo, (float)1/Fs); |
sigert | 38:be38b9318849 | 134 | pc.baud(115200); |
sigert | 42:b5186a54d839 | 135 | //double n=0; |
sigert | 42:b5186a54d839 | 136 | calibration(); // calibreer min en max positie |
sigert | 45:10ba78d97d75 | 137 | f=1; |
sigert | 45:10ba78d97d75 | 138 | smp=0; |
ThomasBNL | 27:85e5d36bb6c5 | 139 | while(1) |
ThomasBNL | 27:85e5d36bb6c5 | 140 | { |
sigert | 42:b5186a54d839 | 141 | // if(control_go) |
sigert | 42:b5186a54d839 | 142 | // { |
sigert | 42:b5186a54d839 | 143 | // //n++; |
sigert | 42:b5186a54d839 | 144 | // //pc.printf("n %f",n); |
sigert | 42:b5186a54d839 | 145 | // sample_filter(); |
sigert | 42:b5186a54d839 | 146 | // scope.set(0,EMG_left_Bicep); //left bicep unfiltered |
sigert | 42:b5186a54d839 | 147 | // scope.set(1,EMG_Left_Bicep_filtered); //Filtered signal |
sigert | 42:b5186a54d839 | 148 | // scope.set(2,moving_average_left); // |
sigert | 42:b5186a54d839 | 149 | // scope.send(); |
sigert | 42:b5186a54d839 | 150 | // control_go = 0; |
sigert | 42:b5186a54d839 | 151 | // } |
sigert | 42:b5186a54d839 | 152 | yellow(); |
ThomasBNL | 27:85e5d36bb6c5 | 153 | if(control_go) |
sigert | 45:10ba78d97d75 | 154 | { |
sigert | 45:10ba78d97d75 | 155 | control_go=0; |
sigert | 42:b5186a54d839 | 156 | sample_filter(); |
sigert | 42:b5186a54d839 | 157 | double signal_above_threshold=(moving_average_left); //(moving_average_right-Threshold_Bicep_Right_1)+ |
sigert | 42:b5186a54d839 | 158 | double max_signal=(EMG_L_max); //(EMG_R_max-Threshold_Bicep_Right_1)+ |
sigert | 42:b5186a54d839 | 159 | pwm_strike=signal_above_threshold/max_signal; |
sigert | 43:9dc385093c8e | 160 | keep_in_range(&pwm_strike, 0,1); |
sigert | 43:9dc385093c8e | 161 | pwm_strike=pwm_strike*pwm_strike; |
sigert | 45:10ba78d97d75 | 162 | |
sigert | 45:10ba78d97d75 | 163 | pwm_average=pwm_strike+pwm_average/f; |
sigert | 45:10ba78d97d75 | 164 | f++; |
sigert | 45:10ba78d97d75 | 165 | smp++; |
sigert | 46:c88e0d2daf15 | 166 | pc.printf("Pwm=%f \n\r", pwm_average); |
sigert | 46:c88e0d2daf15 | 167 | |
sigert | 46:c88e0d2daf15 | 168 | if (pwm_average < 0.1) { leduit(); } |
sigert | 46:c88e0d2daf15 | 169 | if (pwm_average > 0.1) { ledgreen1 = ledon; } |
sigert | 46:c88e0d2daf15 | 170 | if (pwm_average > 0.2) { ledgreen2 = ledon; } |
sigert | 46:c88e0d2daf15 | 171 | if (pwm_average > 0.3) { ledyellow1 = ledon; } |
sigert | 46:c88e0d2daf15 | 172 | if (pwm_average > 0.5) { ledyellow2 = ledon; } |
sigert | 46:c88e0d2daf15 | 173 | if (pwm_average > 0.7) { ledred1 = ledon; } |
sigert | 46:c88e0d2daf15 | 174 | if (pwm_average > 0.9) { ledred2 = ledon; } |
sigert | 46:c88e0d2daf15 | 175 | |
sigert | 45:10ba78d97d75 | 176 | if(smp>512) |
sigert | 45:10ba78d97d75 | 177 | { |
sigert | 45:10ba78d97d75 | 178 | pwm_motor_strike=fabs(pwm_strike); |
sigert | 45:10ba78d97d75 | 179 | green(); |
sigert | 45:10ba78d97d75 | 180 | wait(5); |
sigert | 45:10ba78d97d75 | 181 | pwm_motor_strike=0; |
sigert | 45:10ba78d97d75 | 182 | f=1; |
sigert | 45:10ba78d97d75 | 183 | smp=0;} |
sigert | 45:10ba78d97d75 | 184 | } |
ThomasBNL | 27:85e5d36bb6c5 | 185 | } // while end |
ThomasBNL | 27:85e5d36bb6c5 | 186 | } // main end |
ThomasBNL | 36:f29a36683b1a | 187 | |
ThomasBNL | 36:f29a36683b1a | 188 | // -------------------------------------------------------------------------------------------------------- |
ThomasBNL | 36:f29a36683b1a | 189 | // [FUNCTIONS] // |
ThomasBNL | 36:f29a36683b1a | 190 | void ControlGo() //Control flag |
ThomasBNL | 36:f29a36683b1a | 191 | { |
ThomasBNL | 36:f29a36683b1a | 192 | control_go = true; |
ThomasBNL | 36:f29a36683b1a | 193 | } |
ThomasBNL | 36:f29a36683b1a | 194 | |
ThomasBNL | 36:f29a36683b1a | 195 | void sample_filter() |
ThomasBNL | 36:f29a36683b1a | 196 | { |
ThomasBNL | 36:f29a36683b1a | 197 | Filter(); |
ThomasBNL | 36:f29a36683b1a | 198 | take_sample(); |
ThomasBNL | 36:f29a36683b1a | 199 | if(sample) |
ThomasBNL | 36:f29a36683b1a | 200 | { |
ThomasBNL | 36:f29a36683b1a | 201 | sample=false; |
sigert | 42:b5186a54d839 | 202 | LS_0 = EMG_Left_Bicep_filtered; RS_0 = EMG_Right_Bicep_filtered; |
sigert | 42:b5186a54d839 | 203 | |
sigert | 42:b5186a54d839 | 204 | LS_30=LS_29; LS_29=LS_28;LS_28=LS_27; LS_27=LS_26; LS_26=LS_25; LS_25=LS_24; LS_24=LS_23; LS_23=LS_22; LS_22=LS_21; LS_20=LS_19; |
sigert | 42:b5186a54d839 | 205 | LS_19=LS_18; LS_18=LS_17;LS_17=LS_16; LS_16=LS_15; LS_15=LS_14; LS_14=LS_13; LS_13=LS_12; LS_12=LS_11; LS_11=LS_10; LS_10=LS_9; |
sigert | 42:b5186a54d839 | 206 | LS_9=LS_8; LS_8=LS_7;LS_7=LS_6; LS_6=LS_5; LS_5=LS_4; LS_4=LS_3; LS_3=LS_2; LS_2=LS_1; LS_1=LS_0; |
ThomasBNL | 36:f29a36683b1a | 207 | |
sigert | 42:b5186a54d839 | 208 | RS_30=RS_29; RS_29=RS_28;RS_28=RS_27; RS_27=RS_26; RS_26=RS_25; RS_25=RS_24; RS_24=RS_23; RS_23=RS_22; RS_22=RS_21; RS_20=RS_19; |
sigert | 42:b5186a54d839 | 209 | RS_19=RS_18; RS_18=RS_17;RS_17=RS_16; RS_16=RS_15; RS_15=RS_14; RS_14=RS_13; RS_13=RS_12; RS_12=RS_11; RS_11=RS_10; RS_10=RS_9; |
sigert | 42:b5186a54d839 | 210 | RS_9=RS_8; RS_8=RS_7;RS_7=RS_6; RS_6=RS_5; RS_5=RS_4; RS_4=RS_3; RS_3=RS_2; RS_2=RS_1; RS_1=RS_0; |
sigert | 42:b5186a54d839 | 211 | |
ThomasBNL | 36:f29a36683b1a | 212 | } |
sigert | 42:b5186a54d839 | 213 | moving_average_left= (LS_30+LS_29+LS_28+LS_27+LS_26+LS_25+LS_24+LS_23+LS_22+LS_21+LS_20+LS_19+LS_18+LS_17+LS_16+LS_15+LS_14+LS_13+LS_12+LS_11+LS_10+LS_9+LS_8+LS_7+LS_6+LS_5+LS_4+LS_3+LS_2+LS_1+LS_0)/31; |
sigert | 42:b5186a54d839 | 214 | moving_average_right= (RS_30+RS_29+RS_28+RS_27+RS_26+RS_25+RS_24+RS_23+RS_22+RS_21+RS_20+RS_19+RS_18+RS_17+RS_16+RS_15+RS_14+RS_13+RS_12+RS_11+RS_10+RS_9+RS_8+RS_7+RS_6+RS_5+RS_4+RS_3+RS_2+RS_1+RS_0)/31; |
ThomasBNL | 36:f29a36683b1a | 215 | n++; |
ThomasBNL | 36:f29a36683b1a | 216 | } |
ThomasBNL | 36:f29a36683b1a | 217 | |
ThomasBNL | 36:f29a36683b1a | 218 | void take_sample() // Take a sample every 25th sample |
ThomasBNL | 36:f29a36683b1a | 219 | { |
sigert | 42:b5186a54d839 | 220 | if(n==8) |
ThomasBNL | 36:f29a36683b1a | 221 | { |
ThomasBNL | 36:f29a36683b1a | 222 | sample = true; n=0; |
ThomasBNL | 36:f29a36683b1a | 223 | } |
ThomasBNL | 36:f29a36683b1a | 224 | |
ThomasBNL | 36:f29a36683b1a | 225 | if(er==5) |
ThomasBNL | 36:f29a36683b1a | 226 | { |
ThomasBNL | 36:f29a36683b1a | 227 | sample_error = true; er=0; |
ThomasBNL | 36:f29a36683b1a | 228 | } |
ThomasBNL | 36:f29a36683b1a | 229 | |
ThomasBNL | 36:f29a36683b1a | 230 | sample_error_strike = true; |
ThomasBNL | 36:f29a36683b1a | 231 | } |
ThomasBNL | 36:f29a36683b1a | 232 | |
ThomasBNL | 36:f29a36683b1a | 233 | // [FILTER FUNCTIONS] // |
ThomasBNL | 36:f29a36683b1a | 234 | // [EMG] // |
ThomasBNL | 36:f29a36683b1a | 235 | |
ThomasBNL | 36:f29a36683b1a | 236 | void Filter() // Unfiltered EMG (input) -> highpass filter -> rectify -> lowpass filter -> Filtered EMG (output) |
ThomasBNL | 36:f29a36683b1a | 237 | { |
ThomasBNL | 36:f29a36683b1a | 238 | EMG_left_Bicep = input1; EMG_Right_Bicep = input2; |
ThomasBNL | 36:f29a36683b1a | 239 | |
ThomasBNL | 36:f29a36683b1a | 240 | EMG_Left_Bicep_filtered = highpassfilter_1.step(EMG_left_Bicep); EMG_Right_Bicep_filtered = highpassfilter_2.step(EMG_Right_Bicep); |
ThomasBNL | 36:f29a36683b1a | 241 | EMG_Left_Bicep_filtered = fabs(EMG_Left_Bicep_filtered); EMG_Right_Bicep_filtered = fabs(EMG_Right_Bicep_filtered); |
ThomasBNL | 36:f29a36683b1a | 242 | |
sigert | 42:b5186a54d839 | 243 | //EMG_Left_Bicep_filtered_notch_1 = notchL1.step(EMG_Left_Bicep_filtered); EMG_Right_Bicep_filtered_notch_1 = notchR1.step(EMG_Right_Bicep_filtered); |
sigert | 42:b5186a54d839 | 244 | //EMG_Left_Bicep_filtered_notch_2 = notchL2.step(EMG_Left_Bicep_filtered_notch_1); EMG_Right_Bicep_filtered_notch_2 = notchR2.step(EMG_Right_Bicep_filtered_notch_1); |
ThomasBNL | 36:f29a36683b1a | 245 | |
sigert | 42:b5186a54d839 | 246 | //EMG_Left_Bicep_filtered = lowpassfilter_1.step(EMG_Left_Bicep_filtered_notch_2); EMG_Right_Bicep_filtered = lowpassfilter_2.step(EMG_Right_Bicep_filtered_notch_2); |
sigert | 42:b5186a54d839 | 247 | |
sigert | 42:b5186a54d839 | 248 | EMG_Left_Bicep_filtered = lowpassfilter_1.step(EMG_Left_Bicep_filtered); EMG_Right_Bicep_filtered = lowpassfilter_2.step(EMG_Right_Bicep_filtered); |
ThomasBNL | 36:f29a36683b1a | 249 | } |
ThomasBNL | 36:f29a36683b1a | 250 | |
ThomasBNL | 36:f29a36683b1a | 251 | void countdown_from_5() // Countdown from 5 till 0 inside Putty (interface) |
ThomasBNL | 36:f29a36683b1a | 252 | { |
ThomasBNL | 36:f29a36683b1a | 253 | wait(1); pc.printf("5 \n\r"); wait(1); pc.printf("4 \n\r"); wait(1); pc.printf("3 \n\r"); wait(1); pc.printf("2 Ready \n\r"); |
ThomasBNL | 36:f29a36683b1a | 254 | wait(1); pc.printf("1 Set \n\r"); wait(1); pc.printf("Go \n\r"); |
ThomasBNL | 36:f29a36683b1a | 255 | } |
ThomasBNL | 36:f29a36683b1a | 256 | |
ThomasBNL | 36:f29a36683b1a | 257 | void calibration() |
ThomasBNL | 36:f29a36683b1a | 258 | { |
ThomasBNL | 36:f29a36683b1a | 259 | |
ThomasBNL | 36:f29a36683b1a | 260 | // [MINIMUM VALUE BICEPS CALIBRATION] // |
ThomasBNL | 36:f29a36683b1a | 261 | |
ThomasBNL | 36:f29a36683b1a | 262 | pc.printf("Start minimum calibration in 5 seconds \n\r"); |
ThomasBNL | 36:f29a36683b1a | 263 | pc.printf("Keep your biceps as relaxed as possible \n\r"); |
ThomasBNL | 36:f29a36683b1a | 264 | |
ThomasBNL | 36:f29a36683b1a | 265 | countdown_from_5(); |
ThomasBNL | 36:f29a36683b1a | 266 | c=0; |
ThomasBNL | 36:f29a36683b1a | 267 | |
ThomasBNL | 36:f29a36683b1a | 268 | while(c<2560) // 512Hz -> 2560 is equal to five seconds |
ThomasBNL | 36:f29a36683b1a | 269 | { |
ThomasBNL | 36:f29a36683b1a | 270 | Filter(); // Filter EMG signal |
ThomasBNL | 36:f29a36683b1a | 271 | minimum_L=EMG_Left_Bicep_filtered+minimum_L; // Take previous sample EMG_Left_Bicep_filtered and add the new value |
ThomasBNL | 36:f29a36683b1a | 272 | minimum_R=EMG_Right_Bicep_filtered+minimum_R; |
ThomasBNL | 36:f29a36683b1a | 273 | // scope.set(0,EMG_left_Bicep); |
ThomasBNL | 36:f29a36683b1a | 274 | // scope.set(1,EMG_Left_Bicep_filtered); |
ThomasBNL | 36:f29a36683b1a | 275 | // scope.set(2,minimum_L); |
ThomasBNL | 36:f29a36683b1a | 276 | // scope.send(); |
ThomasBNL | 36:f29a36683b1a | 277 | c++; // Every sample c is increased by one until the statement c<2560 is false |
ThomasBNL | 36:f29a36683b1a | 278 | wait(0.001953125); // wait one sample |
ThomasBNL | 36:f29a36683b1a | 279 | } |
ThomasBNL | 36:f29a36683b1a | 280 | |
ThomasBNL | 36:f29a36683b1a | 281 | pc.printf("Finished minimum calibration \n\r"); |
ThomasBNL | 36:f29a36683b1a | 282 | |
ThomasBNL | 36:f29a36683b1a | 283 | EMG_L_min=minimum_L/2560; // Divide the summation by the number of measurements (2560 measurements) to get a mean value over 5 seconds |
ThomasBNL | 36:f29a36683b1a | 284 | EMG_R_min=minimum_R/2560; |
ThomasBNL | 36:f29a36683b1a | 285 | |
ThomasBNL | 36:f29a36683b1a | 286 | pc.printf("EMG_L_min = %f \n\r EMG_R_min = %f \n\r", EMG_L_min, EMG_R_min); |
ThomasBNL | 36:f29a36683b1a | 287 | |
ThomasBNL | 36:f29a36683b1a | 288 | wait (3); //cooldown |
ThomasBNL | 36:f29a36683b1a | 289 | |
ThomasBNL | 36:f29a36683b1a | 290 | |
ThomasBNL | 36:f29a36683b1a | 291 | // [MAXIMUM VALUE BICEPS CALIBRATION] // |
ThomasBNL | 36:f29a36683b1a | 292 | |
ThomasBNL | 36:f29a36683b1a | 293 | |
ThomasBNL | 36:f29a36683b1a | 294 | pc.printf("start maximum calibration in 5 seconds (start contraction at 3) \n\r"); |
ThomasBNL | 36:f29a36683b1a | 295 | |
ThomasBNL | 36:f29a36683b1a | 296 | countdown_from_5(); |
ThomasBNL | 36:f29a36683b1a | 297 | c=0; |
ThomasBNL | 36:f29a36683b1a | 298 | |
ThomasBNL | 36:f29a36683b1a | 299 | while(c<2560) // 512Hz -> 2560 is equal to five seconds |
ThomasBNL | 36:f29a36683b1a | 300 | { |
ThomasBNL | 36:f29a36683b1a | 301 | Filter(); // Filter EMG signal |
ThomasBNL | 36:f29a36683b1a | 302 | maximum_L=EMG_Left_Bicep_filtered+maximum_L; // Take previous sample EMG_Left_Bicep_filtered and add the new value |
ThomasBNL | 36:f29a36683b1a | 303 | maximum_R=EMG_Right_Bicep_filtered+maximum_R; |
ThomasBNL | 36:f29a36683b1a | 304 | c++; // Every sample c is increased by one until the statement c<2560 is false |
ThomasBNL | 36:f29a36683b1a | 305 | wait(0.001953125); |
ThomasBNL | 36:f29a36683b1a | 306 | } |
ThomasBNL | 36:f29a36683b1a | 307 | |
ThomasBNL | 36:f29a36683b1a | 308 | pc.printf("Finished minimum calibration \n\r"); |
ThomasBNL | 36:f29a36683b1a | 309 | |
ThomasBNL | 36:f29a36683b1a | 310 | EMG_L_max=maximum_L/2560; // Divide the summation by the number of measurements (2560 measurements) to get a mean value over 5 seconds |
ThomasBNL | 36:f29a36683b1a | 311 | EMG_R_max=maximum_R/2560; |
ThomasBNL | 36:f29a36683b1a | 312 | |
ThomasBNL | 36:f29a36683b1a | 313 | pc.printf("EMG_L_max = %f \n\r EMG_R_max = %f \n\r", EMG_L_max, EMG_R_max); |
ThomasBNL | 36:f29a36683b1a | 314 | |
ThomasBNL | 36:f29a36683b1a | 315 | wait (3); //cooldown |
ThomasBNL | 36:f29a36683b1a | 316 | |
ThomasBNL | 36:f29a36683b1a | 317 | |
ThomasBNL | 36:f29a36683b1a | 318 | // [MAXIMUM VALUE BICEPS CALIBRATION] // |
ThomasBNL | 36:f29a36683b1a | 319 | // Calculate threshold percentages // |
ThomasBNL | 36:f29a36683b1a | 320 | |
sigert | 42:b5186a54d839 | 321 | Threshold_Bicep_Left_1=((EMG_L_max-EMG_L_min)*0.2); //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // LEFT |
sigert | 42:b5186a54d839 | 322 | Threshold_Bicep_Left_2=((EMG_L_max-EMG_L_min)*0.6); //(waarde waarop het gemeten EMG signaal 60% van max het maximale is); |
sigert | 42:b5186a54d839 | 323 | Threshold_Bicep_Right_1=((EMG_R_max-EMG_R_min)*0.2); //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // RIGHT |
sigert | 42:b5186a54d839 | 324 | Threshold_Bicep_Right_2=((EMG_R_max-EMG_R_min)*0.6); //(waarde waarop het gemeten EMG signaal 60% van max het maximale is); |
ThomasBNL | 36:f29a36683b1a | 325 | |
ThomasBNL | 36:f29a36683b1a | 326 | pc.printf("left 1: %f left 2: %f right 1: %f right 2: %f \n\r", Threshold_Bicep_Left_1, Threshold_Bicep_Left_2, Threshold_Bicep_Right_1, Threshold_Bicep_Right_2); |
ThomasBNL | 36:f29a36683b1a | 327 | |
ThomasBNL | 36:f29a36683b1a | 328 | } |
ThomasBNL | 36:f29a36683b1a | 329 | |
sigert | 37:6c04c15d9bbe | 330 | void red() { debug_led_red=on; debug_led_blue=off; debug_led_green=off; } |
sigert | 37:6c04c15d9bbe | 331 | void blue() { debug_led_red=off; debug_led_blue=on; debug_led_green=off; } |
sigert | 37:6c04c15d9bbe | 332 | void green() { debug_led_red=off; debug_led_blue=off; debug_led_green=on; } |
sigert | 37:6c04c15d9bbe | 333 | void white() { debug_led_red=on; debug_led_blue=on; debug_led_green=on; } |
sigert | 37:6c04c15d9bbe | 334 | void yellow() { debug_led_red=on; debug_led_blue=off; debug_led_green=on; } |
sigert | 37:6c04c15d9bbe | 335 | void cyan() { debug_led_red=off; debug_led_blue=on; debug_led_green=on; } |
sigert | 37:6c04c15d9bbe | 336 | void purple() { debug_led_red=on; debug_led_blue=on; debug_led_green=off; } |
sigert | 37:6c04c15d9bbe | 337 | void black() { debug_led_red=off; debug_led_blue=off; debug_led_green=off; } |
ThomasBNL | 36:f29a36683b1a | 338 | |
sigert | 42:b5186a54d839 | 339 | void keep_in_range(double * in, double min, double max) // Put in certain min and max values that the input needs to stay within |
sigert | 42:b5186a54d839 | 340 | { |
sigert | 42:b5186a54d839 | 341 | *in > min ? *in < max? : *in = max: *in = min; |
sigert | 42:b5186a54d839 | 342 | } |