pid gecomment
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of a_pid_kal_end_def by
main.cpp@45:08bddea67bd8, 2016-11-02 (annotated)
- Committer:
- daniQQue
- Date:
- Wed Nov 02 16:18:25 2016 +0000
- Revision:
- 45:08bddea67bd8
- Parent:
- 44:c79e5944ac91
- Child:
- 46:4a8889f9dc9f
versie die werkt om 17:17 lege paal!, motoren aangestuurd met emg! geen kalibratie! geen encoder! alleen aansturing!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daniQQue | 45:08bddea67bd8 | 1 | //========================================== |
daniQQue | 0:34c739fcc3e0 | 2 | //libraries |
daniQQue | 0:34c739fcc3e0 | 3 | #include "mbed.h" |
daniQQue | 0:34c739fcc3e0 | 4 | #include "HIDScope.h" |
daniQQue | 14:5b17697cf775 | 5 | #include "BiQuad.h" |
daniQQue | 40:187ef29de53d | 6 | #include "MODSERIAL.h" |
daniQQue | 0:34c739fcc3e0 | 7 | |
daniQQue | 45:08bddea67bd8 | 8 | //========================================= |
daniQQue | 0:34c739fcc3e0 | 9 | //Define objects |
daniQQue | 40:187ef29de53d | 10 | AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG biceps (r) in to c++ |
daniQQue | 40:187ef29de53d | 11 | AnalogIn emg_triceps_right_in(A1); //analog in to get EMG triceps (r) in to c++ |
daniQQue | 40:187ef29de53d | 12 | AnalogIn emg_biceps_left_in (A2); //analog in to get EMG biceps (l) in to c++ |
daniQQue | 0:34c739fcc3e0 | 13 | Ticker sample_timer; //ticker |
daniQQue | 45:08bddea67bd8 | 14 | Ticker ticker_switch; //ticker |
daniQQue | 40:187ef29de53d | 15 | HIDScope scope(5); //open 3 channels in hidscope |
daniQQue | 40:187ef29de53d | 16 | MODSERIAL pc(USBTX, USBRX); //pc connection |
daniQQue | 40:187ef29de53d | 17 | DigitalOut red(LED_RED); |
daniQQue | 40:187ef29de53d | 18 | DigitalOut green(LED_GREEN); |
daniQQue | 40:187ef29de53d | 19 | |
daniQQue | 40:187ef29de53d | 20 | //motors |
daniQQue | 40:187ef29de53d | 21 | DigitalOut richting_motor1(D4); |
daniQQue | 40:187ef29de53d | 22 | PwmOut pwm_motor1(D5); |
daniQQue | 40:187ef29de53d | 23 | DigitalOut richting_motor2(D7); |
daniQQue | 40:187ef29de53d | 24 | PwmOut pwm_motor2(D6); |
daniQQue | 0:34c739fcc3e0 | 25 | |
daniQQue | 0:34c739fcc3e0 | 26 | //define variables |
daniQQue | 40:187ef29de53d | 27 | //other |
daniQQue | 40:187ef29de53d | 28 | int onoffsignal_rightarm=0; // on/off signal: 1; biceps activation, 0: nothing, -1, triceps activation |
daniQQue | 40:187ef29de53d | 29 | int switch_signal_leftarm=0; // switching between motors. |
daniQQue | 45:08bddea67bd8 | 30 | double cut_off_value_biceps_right =0.04; //gespecificeerd door floortje |
daniQQue | 45:08bddea67bd8 | 31 | double cut_off_value_biceps_left=-0.04; |
daniQQue | 45:08bddea67bd8 | 32 | double cut_off_value_triceps=-0.04; |
daniQQue | 45:08bddea67bd8 | 33 | int switch_signal = 0; //start van de teller wordt op nul gesteld |
daniQQue | 45:08bddea67bd8 | 34 | |
daniQQue | 45:08bddea67bd8 | 35 | int onoffsignal_biceps; |
daniQQue | 45:08bddea67bd8 | 36 | int switch_signal_triceps; |
daniQQue | 40:187ef29de53d | 37 | |
daniQQue | 44:c79e5944ac91 | 38 | //======================================= |
daniQQue | 44:c79e5944ac91 | 39 | //filter coefficients |
daniQQue | 40:187ef29de53d | 40 | |
daniQQue | 40:187ef29de53d | 41 | //b1 = biceps right arm |
daniQQue | 40:187ef29de53d | 42 | BiQuad filterhigh_b1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 40:187ef29de53d | 43 | BiQuad filternotch1_b1 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01); |
daniQQue | 40:187ef29de53d | 44 | |
daniQQue | 40:187ef29de53d | 45 | //t1= triceps right arm |
daniQQue | 40:187ef29de53d | 46 | BiQuad filterhigh_t1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 40:187ef29de53d | 47 | BiQuad filternotch1_t1 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01); |
daniQQue | 5:688b1b5530d8 | 48 | |
daniQQue | 40:187ef29de53d | 49 | //b2= biceps left arm |
daniQQue | 40:187ef29de53d | 50 | BiQuad filterhigh_b2(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01); |
daniQQue | 40:187ef29de53d | 51 | BiQuad filternotch1_b2 (9.9376e-01 , -1.8902e-00, 9.9376e-01 , -1.8902e-00 , 9.875e-01); |
daniQQue | 40:187ef29de53d | 52 | |
daniQQue | 40:187ef29de53d | 53 | //after abs filtering |
daniQQue | 40:187ef29de53d | 54 | BiQuad filterlow_b1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 40:187ef29de53d | 55 | BiQuad filterlow_t1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 40:187ef29de53d | 56 | BiQuad filterlow_b2 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01); |
daniQQue | 10:7255b59224cc | 57 | |
daniQQue | 45:08bddea67bd8 | 58 | //====================================================================== |
daniQQue | 45:08bddea67bd8 | 59 | //voids |
daniQQue | 45:08bddea67bd8 | 60 | //====================================================================== |
daniQQue | 45:08bddea67bd8 | 61 | |
daniQQue | 40:187ef29de53d | 62 | //function teller |
daniQQue | 45:08bddea67bd8 | 63 | void switch_function() { // maakt simpele functie die 1 bij n optelt |
daniQQue | 45:08bddea67bd8 | 64 | if(switch_signal_triceps==1) |
daniQQue | 40:187ef29de53d | 65 | { |
daniQQue | 45:08bddea67bd8 | 66 | switch_signal++; |
daniQQue | 40:187ef29de53d | 67 | green=!green; |
daniQQue | 40:187ef29de53d | 68 | red=!red; |
daniQQue | 45:08bddea67bd8 | 69 | if (switch_signal%2==0) |
daniQQue | 40:187ef29de53d | 70 | {pc.printf("If you contract the biceps, the robot will go up \r\n"); |
daniQQue | 40:187ef29de53d | 71 | pc.printf("If you contract the triceps, the robot will go down \r\n"); |
daniQQue | 40:187ef29de53d | 72 | } |
daniQQue | 40:187ef29de53d | 73 | |
daniQQue | 40:187ef29de53d | 74 | else |
daniQQue | 40:187ef29de53d | 75 | {pc.printf("If you contract the biceps, the robot will go right \r\n"); |
daniQQue | 40:187ef29de53d | 76 | pc.printf("If you contract the triceps, the robot will go left \r\n"); |
daniQQue | 40:187ef29de53d | 77 | } |
daniQQue | 40:187ef29de53d | 78 | |
daniQQue | 40:187ef29de53d | 79 | } |
daniQQue | 40:187ef29de53d | 80 | } |
daniQQue | 40:187ef29de53d | 81 | |
daniQQue | 40:187ef29de53d | 82 | //functions which are called in ticker to sample the analog signal |
daniQQue | 40:187ef29de53d | 83 | |
daniQQue | 0:34c739fcc3e0 | 84 | void filter(){ |
daniQQue | 40:187ef29de53d | 85 | //biceps right arm read+filtering |
daniQQue | 44:c79e5944ac91 | 86 | double emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes |
daniQQue | 44:c79e5944ac91 | 87 | double emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right); |
daniQQue | 44:c79e5944ac91 | 88 | double emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right); |
daniQQue | 44:c79e5944ac91 | 89 | double emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_biceps_right); //fabs because float |
daniQQue | 44:c79e5944ac91 | 90 | double emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right); |
daniQQue | 40:187ef29de53d | 91 | |
daniQQue | 40:187ef29de53d | 92 | //triceps right arm read+filtering |
daniQQue | 44:c79e5944ac91 | 93 | double emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes |
daniQQue | 44:c79e5944ac91 | 94 | double emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right); |
daniQQue | 44:c79e5944ac91 | 95 | double emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right); |
daniQQue | 44:c79e5944ac91 | 96 | double emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_triceps_right); //fabs because float |
daniQQue | 44:c79e5944ac91 | 97 | double emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right); |
daniQQue | 7:42d0e38196f1 | 98 | |
daniQQue | 40:187ef29de53d | 99 | //biceps left arm read+filtering |
daniQQue | 44:c79e5944ac91 | 100 | double emg_biceps_left=emg_biceps_left_in.read(); //read the emg value from the elektrodes |
daniQQue | 44:c79e5944ac91 | 101 | double emg_filtered_high_biceps_left= filterhigh_b2.step(emg_biceps_left); |
daniQQue | 44:c79e5944ac91 | 102 | double emg_filtered_high_notch_1_biceps_left=filternotch1_b2.step(emg_filtered_high_biceps_left); |
daniQQue | 44:c79e5944ac91 | 103 | double emg_abs_biceps_left=fabs(emg_filtered_high_notch_1_biceps_left); //fabs because float |
daniQQue | 44:c79e5944ac91 | 104 | double emg_filtered_biceps_left=filterlow_b2.step(emg_abs_biceps_left); |
daniQQue | 40:187ef29de53d | 105 | |
daniQQue | 40:187ef29de53d | 106 | //creating of on/off signal with the created on/off signals, with if statement for right arm! |
daniQQue | 44:c79e5944ac91 | 107 | //signal substraction of filter biceps and triceps. right Biceps + left biceps - |
daniQQue | 44:c79e5944ac91 | 108 | double signal_biceps_sum=emg_filtered_biceps_right-emg_filtered_biceps_left; |
daniQQue | 44:c79e5944ac91 | 109 | double bicepstriceps_rightarm=emg_filtered_biceps_right-emg_filtered_triceps_right; |
daniQQue | 45:08bddea67bd8 | 110 | |
daniQQue | 44:c79e5944ac91 | 111 | //creating of on/off signal with the created on/off signals, with if statement for right arm! |
daniQQue | 44:c79e5944ac91 | 112 | if (signal_biceps_sum>cut_off_value_biceps_right) |
daniQQue | 44:c79e5944ac91 | 113 | {onoffsignal_biceps=1;} |
daniQQue | 7:42d0e38196f1 | 114 | |
daniQQue | 44:c79e5944ac91 | 115 | else if (signal_biceps_sum<cut_off_value_biceps_left) |
daniQQue | 40:187ef29de53d | 116 | { |
daniQQue | 44:c79e5944ac91 | 117 | onoffsignal_biceps=-1; |
daniQQue | 40:187ef29de53d | 118 | } |
daniQQue | 40:187ef29de53d | 119 | |
daniQQue | 40:187ef29de53d | 120 | else |
daniQQue | 44:c79e5944ac91 | 121 | {onoffsignal_biceps=0;} |
daniQQue | 7:42d0e38196f1 | 122 | |
daniQQue | 40:187ef29de53d | 123 | //creating on/off signal for switch (left arm) |
daniQQue | 40:187ef29de53d | 124 | |
daniQQue | 44:c79e5944ac91 | 125 | if (bicepstriceps_rightarm<cut_off_value_triceps) |
daniQQue | 40:187ef29de53d | 126 | { |
daniQQue | 44:c79e5944ac91 | 127 | switch_signal_triceps=1; |
daniQQue | 40:187ef29de53d | 128 | } |
daniQQue | 40:187ef29de53d | 129 | |
daniQQue | 40:187ef29de53d | 130 | else |
daniQQue | 40:187ef29de53d | 131 | { |
daniQQue | 44:c79e5944ac91 | 132 | switch_signal_triceps=0; |
daniQQue | 40:187ef29de53d | 133 | } |
daniQQue | 0:34c739fcc3e0 | 134 | //send signals to scope |
daniQQue | 40:187ef29de53d | 135 | scope.set(0, emg_filtered_biceps_right); //set emg signal to scope in channel 0 |
daniQQue | 40:187ef29de53d | 136 | scope.set(1, emg_filtered_triceps_right); // set emg signal to scope in channel 1 |
daniQQue | 40:187ef29de53d | 137 | scope.set(2, emg_filtered_biceps_left); // set emg signal to scope in channel 2 |
daniQQue | 45:08bddea67bd8 | 138 | scope.set(3, onoffsignal_biceps); // set emg signal to scope in channel 3 |
daniQQue | 45:08bddea67bd8 | 139 | scope.set(4, switch_signal_triceps); |
daniQQue | 40:187ef29de53d | 140 | |
daniQQue | 40:187ef29de53d | 141 | scope.send(); //send all the signals to the scope |
daniQQue | 0:34c739fcc3e0 | 142 | } |
daniQQue | 0:34c739fcc3e0 | 143 | |
daniQQue | 0:34c739fcc3e0 | 144 | //program |
daniQQue | 0:34c739fcc3e0 | 145 | |
daniQQue | 0:34c739fcc3e0 | 146 | int main() |
daniQQue | 0:34c739fcc3e0 | 147 | { |
daniQQue | 40:187ef29de53d | 148 | pc.baud(115200); |
daniQQue | 40:187ef29de53d | 149 | green=0; |
daniQQue | 40:187ef29de53d | 150 | red=1; |
daniQQue | 40:187ef29de53d | 151 | |
daniQQue | 0:34c739fcc3e0 | 152 | sample_timer.attach(&filter, 0.001); //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds |
daniQQue | 45:08bddea67bd8 | 153 | ticker_switch.attach(&switch_function,1.0); |
daniQQue | 44:c79e5944ac91 | 154 | |
daniQQue | 45:08bddea67bd8 | 155 | if (switch_signal%2==0) |
daniQQue | 45:08bddea67bd8 | 156 | {pc.printf("If you contract the biceps, the robot will go up \r\n"); |
daniQQue | 45:08bddea67bd8 | 157 | pc.printf("If you contract the triceps, the robot will go down \r\n"); |
daniQQue | 45:08bddea67bd8 | 158 | } |
daniQQue | 45:08bddea67bd8 | 159 | |
daniQQue | 45:08bddea67bd8 | 160 | else |
daniQQue | 45:08bddea67bd8 | 161 | {pc.printf("If you contract the biceps, the robot will go right \r\n"); |
daniQQue | 45:08bddea67bd8 | 162 | pc.printf("If you contract the triceps, the robot will go left \r\n"); |
daniQQue | 45:08bddea67bd8 | 163 | } |
daniQQue | 45:08bddea67bd8 | 164 | |
daniQQue | 44:c79e5944ac91 | 165 | //============================================================================================== |
daniQQue | 0:34c739fcc3e0 | 166 | //endless loop |
daniQQue | 0:34c739fcc3e0 | 167 | |
daniQQue | 40:187ef29de53d | 168 | |
daniQQue | 40:187ef29de53d | 169 | while (true) { // zorgt er voor dat de code oneindig doorgelopen wordt |
daniQQue | 40:187ef29de53d | 170 | |
daniQQue | 8:cd0cb71b69f2 | 171 | |
daniQQue | 45:08bddea67bd8 | 172 | if (onoffsignal_biceps==-1) //left biceps contracted |
daniQQue | 40:187ef29de53d | 173 | { |
daniQQue | 45:08bddea67bd8 | 174 | if (switch_signal%2==0) |
daniQQue | 40:187ef29de53d | 175 | { |
daniQQue | 45:08bddea67bd8 | 176 | richting_motor1 = 1; //motor 1, left |
daniQQue | 40:187ef29de53d | 177 | pwm_motor1 = 1; |
daniQQue | 40:187ef29de53d | 178 | |
daniQQue | 40:187ef29de53d | 179 | } |
daniQQue | 40:187ef29de53d | 180 | |
daniQQue | 45:08bddea67bd8 | 181 | else |
daniQQue | 40:187ef29de53d | 182 | { |
daniQQue | 45:08bddea67bd8 | 183 | richting_motor2 = 1; //motor 2, up |
daniQQue | 40:187ef29de53d | 184 | pwm_motor2 = 1; |
daniQQue | 40:187ef29de53d | 185 | |
daniQQue | 40:187ef29de53d | 186 | } |
daniQQue | 40:187ef29de53d | 187 | |
daniQQue | 8:cd0cb71b69f2 | 188 | } |
daniQQue | 45:08bddea67bd8 | 189 | else if (onoffsignal_biceps==1) // als d ingedrukt wordt gebeurd het volgende |
daniQQue | 40:187ef29de53d | 190 | { |
daniQQue | 45:08bddea67bd8 | 191 | if (switch_signal%2==0) // als d is ingedrukt en n is even dan gebeurd het volgende |
daniQQue | 40:187ef29de53d | 192 | { |
daniQQue | 45:08bddea67bd8 | 193 | richting_motor1 = 0; //motor 1, right |
daniQQue | 40:187ef29de53d | 194 | pwm_motor1 = 1; |
daniQQue | 40:187ef29de53d | 195 | |
daniQQue | 40:187ef29de53d | 196 | } |
daniQQue | 40:187ef29de53d | 197 | else // als d is ingedrukt maar het getal is niet even (dus oneven) gebeurt het onderstaande |
daniQQue | 40:187ef29de53d | 198 | { |
daniQQue | 45:08bddea67bd8 | 199 | richting_motor2 = 0; //motor 2. down |
daniQQue | 40:187ef29de53d | 200 | pwm_motor2 = 1; |
daniQQue | 40:187ef29de53d | 201 | |
daniQQue | 40:187ef29de53d | 202 | } |
daniQQue | 40:187ef29de53d | 203 | } |
daniQQue | 40:187ef29de53d | 204 | else{ |
daniQQue | 40:187ef29de53d | 205 | |
daniQQue | 40:187ef29de53d | 206 | pwm_motor2=0; |
daniQQue | 40:187ef29de53d | 207 | pwm_motor1=0; |
daniQQue | 40:187ef29de53d | 208 | } |
daniQQue | 40:187ef29de53d | 209 | |
daniQQue | 40:187ef29de53d | 210 | } |
daniQQue | 0:34c739fcc3e0 | 211 | |
daniQQue | 0:34c739fcc3e0 | 212 | } |