The final program for the #include AIR robot
Dependencies: Biquad HIDScope QEI angleandposition controlandadjust mbed
Fork of includeair by
main.cpp@16:63320b8f79c2, 2015-10-19 (annotated)
- Committer:
- Gerth
- Date:
- Mon Oct 19 13:26:14 2015 +0000
- Revision:
- 16:63320b8f79c2
- Parent:
- 15:17de575b7385
- Child:
- 17:72d3522165ac
added buttoncontrolmode, but desiredpositoin moves too much per second;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Gerth | 0:dd66fff537d7 | 1 | #include "mbed.h" |
Gerth | 1:917c07a4f3ec | 2 | #include "QEI.h" |
Gerth | 1:917c07a4f3ec | 3 | #include "HIDScope.h" |
Gerth | 1:917c07a4f3ec | 4 | #include "Biquad.h" |
Gerth | 1:917c07a4f3ec | 5 | #include "controlandadjust.h" |
Gerth | 16:63320b8f79c2 | 6 | #include "angleandposition.h" |
Gerth | 1:917c07a4f3ec | 7 | |
Gerth | 1:917c07a4f3ec | 8 | //info out |
Gerth | 10:9e96d14d7034 | 9 | HIDScope scope(6); |
Gerth | 1:917c07a4f3ec | 10 | Ticker scope_ticker; |
Gerth | 3:48438eea184e | 11 | const double scope_frequency=500; |
Gerth | 1:917c07a4f3ec | 12 | Serial pc(USBTX,USBRX); |
Gerth | 1:917c07a4f3ec | 13 | |
Gerth | 16:63320b8f79c2 | 14 | DigitalOut ledred(LED_RED); |
Gerth | 16:63320b8f79c2 | 15 | DigitalOut ledgreen(LED_GREEN); |
Gerth | 16:63320b8f79c2 | 16 | DigitalOut ledblue(LED_BLUE); |
Gerth | 14:4c4f45a1dd23 | 17 | |
Gerth | 1:917c07a4f3ec | 18 | ////////////////ENCODERS |
Gerth | 9:4ee354663560 | 19 | const float cpr_sensor=32; |
Gerth | 9:4ee354663560 | 20 | const float cpr_shaft=cpr_sensor*131; |
Gerth | 9:4ee354663560 | 21 | QEI encoder1(D12,D13,NC,cpr_sensor);/// maybe use Encoder in stead of QEI, because Encoder had setposition |
Gerth | 9:4ee354663560 | 22 | QEI encoder2(D10,D11,NC,cpr_sensor); |
Gerth | 9:4ee354663560 | 23 | const double PIE=3.14159265359; |
Gerth | 9:4ee354663560 | 24 | const float counttorad=((2*PIE)/cpr_shaft); |
Gerth | 1:917c07a4f3ec | 25 | |
Gerth | 6:37c94a5e205f | 26 | /////////////////////////////////CALIBRATION (MODE) |
Gerth | 8:54a7da09ccad | 27 | int modecounter=1; |
Gerth | 6:37c94a5e205f | 28 | DigitalIn changemodebutton(PTA4); |
Gerth | 6:37c94a5e205f | 29 | |
Gerth | 7:7fbb2c028778 | 30 | Ticker readbuttoncalibrate_ticker; |
Gerth | 7:7fbb2c028778 | 31 | const double readbuttoncalibrate_frequency=10; |
Gerth | 7:7fbb2c028778 | 32 | |
Gerth | 14:4c4f45a1dd23 | 33 | Ticker ledblink_ticker; |
Gerth | 14:4c4f45a1dd23 | 34 | const double ledblink_frequency=4; |
Gerth | 14:4c4f45a1dd23 | 35 | |
Gerth | 9:4ee354663560 | 36 | const double radpersec_calibrate=0.1*PIE; |
Gerth | 8:54a7da09ccad | 37 | |
Gerth | 8:54a7da09ccad | 38 | DigitalIn buttonR(D2); |
Gerth | 8:54a7da09ccad | 39 | DigitalIn buttonL(D3); |
Gerth | 8:54a7da09ccad | 40 | |
Gerth | 14:4c4f45a1dd23 | 41 | |
Gerth | 14:4c4f45a1dd23 | 42 | |
Gerth | 1:917c07a4f3ec | 43 | //////////////////////////////////CONTROLLER |
Gerth | 1:917c07a4f3ec | 44 | controlandadjust mycontroller; // make a controller |
Gerth | 1:917c07a4f3ec | 45 | //controller constants |
Gerth | 1:917c07a4f3ec | 46 | float Kp=0.5; |
Gerth | 9:4ee354663560 | 47 | float Ki=0.01; |
Gerth | 1:917c07a4f3ec | 48 | float Kd=0.001; |
Gerth | 1:917c07a4f3ec | 49 | Ticker control_ticker; |
Gerth | 9:4ee354663560 | 50 | const double control_frequency=25; |
Gerth | 7:7fbb2c028778 | 51 | const double Ts_control=1.0/control_frequency; |
Gerth | 1:917c07a4f3ec | 52 | |
Gerth | 7:7fbb2c028778 | 53 | float error1_int=0;// storage variables for the errors |
Gerth | 7:7fbb2c028778 | 54 | float error2_int=0; |
Gerth | 7:7fbb2c028778 | 55 | float error1_prev=0; |
Gerth | 7:7fbb2c028778 | 56 | float error2_prev=0; |
Gerth | 1:917c07a4f3ec | 57 | |
Gerth | 1:917c07a4f3ec | 58 | InterruptIn valuechangebutton(PTC6);//button to change controller constants |
Gerth | 1:917c07a4f3ec | 59 | |
Gerth | 1:917c07a4f3ec | 60 | //safetyandthreshold |
Gerth | 3:48438eea184e | 61 | AnalogIn safety_pot(A3);//pot 2, used for the safety cutoff value for the pwm |
Gerth | 3:48438eea184e | 62 | AnalogIn threshold_pot(A2);//pot1, used to adjust threshold if signal differs per person |
Gerth | 1:917c07a4f3ec | 63 | |
Gerth | 1:917c07a4f3ec | 64 | Ticker safetyandthreshold_ticker; // ticker to read potmeters |
Gerth | 3:48438eea184e | 65 | const double safetyandthreshold_frequency=1; // frequency for the ticker |
Gerth | 3:48438eea184e | 66 | |
Gerth | 4:bf7765b0f612 | 67 | float threshold_value=1;//initial threshold value |
Gerth | 1:917c07a4f3ec | 68 | |
Gerth | 1:917c07a4f3ec | 69 | ////////////////////////////////FILTER |
Gerth | 1:917c07a4f3ec | 70 | #include "filtervalues.h" |
Gerth | 1:917c07a4f3ec | 71 | Ticker filter_ticker; |
Gerth | 3:48438eea184e | 72 | const double filter_frequency=500; |
Gerth | 1:917c07a4f3ec | 73 | Biquad myfilter1; |
Gerth | 1:917c07a4f3ec | 74 | Biquad myfilter2; |
Gerth | 1:917c07a4f3ec | 75 | |
Gerth | 1:917c07a4f3ec | 76 | AnalogIn emg1_input(A0); |
Gerth | 1:917c07a4f3ec | 77 | AnalogIn emg2_input(A1); |
Gerth | 1:917c07a4f3ec | 78 | |
Gerth | 1:917c07a4f3ec | 79 | double filteredsignal1=0; |
Gerth | 1:917c07a4f3ec | 80 | double filteredsignal2=0; |
Gerth | 5:8ac5d0651e4d | 81 | float filter_extragain=1; |
Gerth | 1:917c07a4f3ec | 82 | |
Gerth | 3:48438eea184e | 83 | /////////////////READSIGNAL |
Gerth | 3:48438eea184e | 84 | Ticker readsignal_ticker; |
Gerth | 3:48438eea184e | 85 | const double readsignal_frequency=25; |
Gerth | 3:48438eea184e | 86 | |
Gerth | 11:c10651055871 | 87 | DigitalOut led1(PTC12); |
Gerth | 4:bf7765b0f612 | 88 | DigitalOut led2(D9); |
Gerth | 4:bf7765b0f612 | 89 | |
Gerth | 4:bf7765b0f612 | 90 | //////////////////////////////// POSITION AND ANGLE SHIZZLE |
Gerth | 4:bf7765b0f612 | 91 | float desired_position=0; |
Gerth | 4:bf7765b0f612 | 92 | float desired_angle[]= {0,0}; |
Gerth | 16:63320b8f79c2 | 93 | float mm_per_sec_emg=0.1;// move the pod 50 mm per sec if muscle is flexed |
Gerth | 11:c10651055871 | 94 | float fieldwidth=473; |
Gerth | 11:c10651055871 | 95 | float safetymarginfield=75; //adjustable, tweak for maximum but safe range |
Gerth | 11:c10651055871 | 96 | float maxdisplacement=((fieldwidth/2)-safetymarginfield); //so the pod doesn't hit the edges of the playfield |
Gerth | 11:c10651055871 | 97 | float rad_per_sec_emg=0.25*PIE;// THIS ONE IS NOT NESSECARY FOR ACTUAL PROGRAM |
Gerth | 3:48438eea184e | 98 | |
Gerth | 16:63320b8f79c2 | 99 | angleandposition anglepos; |
Gerth | 16:63320b8f79c2 | 100 | float y_start=255; |
Gerth | 16:63320b8f79c2 | 101 | float y_punch=473; |
Gerth | 1:917c07a4f3ec | 102 | //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS |
Gerth | 1:917c07a4f3ec | 103 | volatile bool scopedata_go=false, |
Gerth | 1:917c07a4f3ec | 104 | control_go=false, |
Gerth | 1:917c07a4f3ec | 105 | filter_go=false, |
Gerth | 3:48438eea184e | 106 | safetyandthreshold_go=false, |
Gerth | 6:37c94a5e205f | 107 | readsignal_go=false, |
Gerth | 8:54a7da09ccad | 108 | switchedmode=true, |
Gerth | 14:4c4f45a1dd23 | 109 | readbuttoncalibrate_go=false, |
Gerth | 14:4c4f45a1dd23 | 110 | ledblink_go=false; |
Gerth | 1:917c07a4f3ec | 111 | |
Gerth | 1:917c07a4f3ec | 112 | void scopedata_activate() |
Gerth | 1:917c07a4f3ec | 113 | { |
Gerth | 1:917c07a4f3ec | 114 | scopedata_go=true; |
Gerth | 1:917c07a4f3ec | 115 | } |
Gerth | 1:917c07a4f3ec | 116 | void control_activate() |
Gerth | 1:917c07a4f3ec | 117 | { |
Gerth | 1:917c07a4f3ec | 118 | control_go=true; |
Gerth | 1:917c07a4f3ec | 119 | } |
Gerth | 1:917c07a4f3ec | 120 | void filter_activate() |
Gerth | 1:917c07a4f3ec | 121 | { |
Gerth | 1:917c07a4f3ec | 122 | filter_go=true; |
Gerth | 1:917c07a4f3ec | 123 | } |
Gerth | 1:917c07a4f3ec | 124 | void safetyandthreshold_activate() |
Gerth | 1:917c07a4f3ec | 125 | { |
Gerth | 1:917c07a4f3ec | 126 | safetyandthreshold_go=true; |
Gerth | 1:917c07a4f3ec | 127 | } |
Gerth | 3:48438eea184e | 128 | void readsignal_activate() |
Gerth | 3:48438eea184e | 129 | { |
Gerth | 3:48438eea184e | 130 | readsignal_go=true; |
Gerth | 3:48438eea184e | 131 | } |
Gerth | 7:7fbb2c028778 | 132 | void readbuttoncalibrate_activate() |
Gerth | 7:7fbb2c028778 | 133 | { |
Gerth | 7:7fbb2c028778 | 134 | readbuttoncalibrate_go=true; |
Gerth | 7:7fbb2c028778 | 135 | } |
Gerth | 14:4c4f45a1dd23 | 136 | void ledblink_activate() |
Gerth | 14:4c4f45a1dd23 | 137 | { |
Gerth | 14:4c4f45a1dd23 | 138 | ledblink_go=true; |
Gerth | 14:4c4f45a1dd23 | 139 | } |
Gerth | 1:917c07a4f3ec | 140 | |
Gerth | 1:917c07a4f3ec | 141 | ////////////////////////FUNCTIONS |
Gerth | 1:917c07a4f3ec | 142 | //gather data and send to scope |
Gerth | 1:917c07a4f3ec | 143 | void scopedata() |
Gerth | 1:917c07a4f3ec | 144 | { |
Gerth | 10:9e96d14d7034 | 145 | scope.set(0,desired_angle[0]); |
Gerth | 10:9e96d14d7034 | 146 | scope.set(1,counttorad*encoder1.getPulses()); |
Gerth | 10:9e96d14d7034 | 147 | scope.set(2,mycontroller.motor1pwm()); |
Gerth | 10:9e96d14d7034 | 148 | scope.set(3,desired_angle[1]); |
Gerth | 10:9e96d14d7034 | 149 | scope.set(4,counttorad*encoder2.getPulses()); |
Gerth | 16:63320b8f79c2 | 150 | scope.set(5,desired_position); |
Gerth | 3:48438eea184e | 151 | scope.send(); |
Gerth | 4:bf7765b0f612 | 152 | } |
Gerth | 1:917c07a4f3ec | 153 | //read potmeters and adjust the safetyfactor and threshold |
Gerth | 1:917c07a4f3ec | 154 | void safetyandthreshold() |
Gerth | 1:917c07a4f3ec | 155 | { |
Gerth | 3:48438eea184e | 156 | mycontroller.cutoff((ceil (10*safety_pot.read()) )/10); // adjust the safetyfactor value between 0 and 1 rounded to 1 decimal |
Gerth | 3:48438eea184e | 157 | threshold_value=((ceil (10*threshold_pot.read()) )/10); // adjust the threshold value between 0 and 1 rounded to 1 decimal |
Gerth | 1:917c07a4f3ec | 158 | } |
Gerth | 1:917c07a4f3ec | 159 | /////filter |
Gerth | 1:917c07a4f3ec | 160 | void filtereverything() |
Gerth | 1:917c07a4f3ec | 161 | { |
Gerth | 1:917c07a4f3ec | 162 | //filter_timer.reset(); |
Gerth | 1:917c07a4f3ec | 163 | // filter_timer.start(); |
Gerth | 1:917c07a4f3ec | 164 | //pass1 so f1 |
Gerth | 2:c7707856d137 | 165 | double pass1_emg1 = myfilter1.filter(emg1_input.read(), v1_f1_emg1 , v2_f1_emg1 , a1_f1 , a2_f1 , b0_f1 , b1_f1 , b2_f1); |
Gerth | 2:c7707856d137 | 166 | double pass1_emg2 = myfilter2.filter(emg2_input.read(), v1_f1_emg2 , v2_f1_emg2 , a1_f1 , a2_f1 , b0_f1 , b1_f1 , b2_f1); |
Gerth | 1:917c07a4f3ec | 167 | |
Gerth | 1:917c07a4f3ec | 168 | //pass2 so f2 |
Gerth | 2:c7707856d137 | 169 | double pass2_emg1 = myfilter1.filter(pass1_emg1, v1_f2_emg1 , v2_f2_emg1 , a1_f2 , a2_f2 , b0_f2 , b1_f2 , b2_f2); |
Gerth | 2:c7707856d137 | 170 | double pass2_emg2 = myfilter2.filter(pass1_emg2, v1_f2_emg2 , v2_f2_emg2 , a1_f2 , a2_f2 , b0_f2 , b1_f2 , b2_f2); |
Gerth | 1:917c07a4f3ec | 171 | |
Gerth | 1:917c07a4f3ec | 172 | //pass3 so f3 |
Gerth | 2:c7707856d137 | 173 | double pass3_emg1 = myfilter1.filter(pass2_emg1, v1_f3_emg1 , v2_f3_emg1 , a1_f3 , a2_f3 , b0_f3 , b1_f3 , b2_f3); |
Gerth | 2:c7707856d137 | 174 | double pass3_emg2 = myfilter2.filter(pass2_emg2, v1_f3_emg2 , v2_f3_emg2 , a1_f3 , a2_f3 , b0_f3 , b1_f3 , b2_f3); |
Gerth | 1:917c07a4f3ec | 175 | |
Gerth | 1:917c07a4f3ec | 176 | //pass4 so f4 |
Gerth | 2:c7707856d137 | 177 | double pass4_emg1 = myfilter1.filter(pass3_emg1, v1_f4_emg1 , v2_f4_emg1 , a1_f4 , a2_f4 , b0_f4 , b1_f4 , b2_f4); |
Gerth | 2:c7707856d137 | 178 | double pass4_emg2 = myfilter2.filter(pass3_emg2, v1_f4_emg2 , v2_f4_emg2 , a1_f4 , a2_f4 , b0_f4 , b1_f4 , b2_f4); |
Gerth | 1:917c07a4f3ec | 179 | |
Gerth | 1:917c07a4f3ec | 180 | //pass5 so f5 |
Gerth | 2:c7707856d137 | 181 | double pass5_emg1 = myfilter1.filter(pass4_emg1, v1_f5_emg1 , v2_f5_emg1 , a1_f5 , a2_f5 , b0_f5 , b1_f5 , b2_f5); |
Gerth | 2:c7707856d137 | 182 | double pass5_emg2 = myfilter2.filter(pass4_emg2, v1_f5_emg2 , v2_f5_emg2 , a1_f5 , a2_f5 , b0_f5 , b1_f5 , b2_f5); |
Gerth | 1:917c07a4f3ec | 183 | |
Gerth | 1:917c07a4f3ec | 184 | ///// take absolute value |
Gerth | 2:c7707856d137 | 185 | double pass5_emg1_abs=(fabs(pass5_emg1)); |
Gerth | 2:c7707856d137 | 186 | double pass5_emg2_abs=(fabs(pass5_emg2)); |
Gerth | 1:917c07a4f3ec | 187 | |
Gerth | 1:917c07a4f3ec | 188 | //pass6 so f6 |
Gerth | 2:c7707856d137 | 189 | double pass6_emg1 = myfilter1.filter(pass5_emg1_abs, v1_f6_emg1 , v2_f6_emg1 , a1_f6 , a2_f6 , b0_f6 , b1_f6 , b2_f6); |
Gerth | 2:c7707856d137 | 190 | double pass6_emg2 = myfilter2.filter(pass5_emg2_abs, v1_f6_emg2 , v2_f6_emg2 , a1_f6 , a2_f6 , b0_f6 , b1_f6 , b2_f6); |
Gerth | 1:917c07a4f3ec | 191 | |
Gerth | 1:917c07a4f3ec | 192 | |
Gerth | 1:917c07a4f3ec | 193 | //pass7 so f7 |
Gerth | 2:c7707856d137 | 194 | double pass7_emg1 = myfilter1.filter(pass6_emg1, v1_f7_emg1 , v2_f7_emg1 , a1_f7 , a2_f7 , b0_f7 , b1_f7 , b2_f7); |
Gerth | 2:c7707856d137 | 195 | double pass7_emg2 = myfilter2.filter(pass6_emg2, v1_f7_emg2 , v2_f7_emg2 , a1_f7 , a2_f7 , b0_f7 , b1_f7 , b2_f7); |
Gerth | 1:917c07a4f3ec | 196 | |
Gerth | 2:c7707856d137 | 197 | filteredsignal1=(pass7_emg1*9e11*filter_extragain); |
Gerth | 2:c7707856d137 | 198 | filteredsignal2=(pass7_emg2*9e11*filter_extragain); |
Gerth | 1:917c07a4f3ec | 199 | |
Gerth | 1:917c07a4f3ec | 200 | //filter_timer.stop(); |
Gerth | 1:917c07a4f3ec | 201 | } |
Gerth | 1:917c07a4f3ec | 202 | |
Gerth | 7:7fbb2c028778 | 203 | |
Gerth | 1:917c07a4f3ec | 204 | |
Gerth | 1:917c07a4f3ec | 205 | //adjust controller values when sw2 is pressed |
Gerth | 1:917c07a4f3ec | 206 | void valuechange() |
Gerth | 1:917c07a4f3ec | 207 | { |
Gerth | 3:48438eea184e | 208 | mycontroller.STOP(); |
Gerth | 5:8ac5d0651e4d | 209 | pc.printf("KP is now %f, enter new value\n",Kp); |
Gerth | 1:917c07a4f3ec | 210 | pc.scanf("%f", &Kp); |
Gerth | 1:917c07a4f3ec | 211 | |
Gerth | 1:917c07a4f3ec | 212 | pc.printf("KI is now %f, enter new value\n",Ki); |
Gerth | 1:917c07a4f3ec | 213 | pc.scanf("%f", &Ki); |
Gerth | 1:917c07a4f3ec | 214 | |
Gerth | 1:917c07a4f3ec | 215 | pc.printf("KD is now %f, enter new value\n",Kd); |
Gerth | 5:8ac5d0651e4d | 216 | pc.scanf("%f", &Kd); |
Gerth | 3:48438eea184e | 217 | |
Gerth | 3:48438eea184e | 218 | pc.printf("Extra gain is now %f, enter new value\n",filter_extragain); |
Gerth | 5:8ac5d0651e4d | 219 | pc.scanf("%f", &filter_extragain); |
Gerth | 1:917c07a4f3ec | 220 | } |
Gerth | 11:c10651055871 | 221 | |
Gerth | 11:c10651055871 | 222 | |
Gerth | 11:c10651055871 | 223 | |
Gerth | 11:c10651055871 | 224 | const float schiethoek=0.35*PIE; |
Gerth | 11:c10651055871 | 225 | const float schiettijd=0.5; |
Gerth | 11:c10651055871 | 226 | void shoot() // THIS NEEDS ADJUSTMEND |
Gerth | 14:4c4f45a1dd23 | 227 | { |
Gerth | 11:c10651055871 | 228 | pc.printf("SHOOT\n"); |
Gerth | 11:c10651055871 | 229 | //hoeken groter maken |
Gerth | 11:c10651055871 | 230 | desired_angle[0]-=schiethoek; |
Gerth | 11:c10651055871 | 231 | desired_angle[1]+=schiethoek; |
Gerth | 11:c10651055871 | 232 | |
Gerth | 11:c10651055871 | 233 | Timer schiettimer; |
Gerth | 11:c10651055871 | 234 | schiettimer.reset(); |
Gerth | 11:c10651055871 | 235 | schiettimer.start(); |
Gerth | 11:c10651055871 | 236 | float pass=0; |
Gerth | 11:c10651055871 | 237 | while(schiettimer.read()<=schiettijd) { |
Gerth | 11:c10651055871 | 238 | // errors berekenen en naar de controller passen |
Gerth | 14:4c4f45a1dd23 | 239 | float error1=(desired_angle[0]-counttorad*encoder1.getPulses()); |
Gerth | 14:4c4f45a1dd23 | 240 | float error2=(desired_angle[1]-counttorad*encoder2.getPulses()); |
Gerth | 11:c10651055871 | 241 | mycontroller.PI(error1, error2, Kp, Ki, Ts_control, error1_int, error2_int); |
Gerth | 11:c10651055871 | 242 | scopedata(); |
Gerth | 11:c10651055871 | 243 | wait (Ts_control-(schiettimer.read()-Ts_control*pass)); // even wachten anders wordt de while loop te snel doorlopen en gaan de motoren wak |
Gerth | 14:4c4f45a1dd23 | 244 | ledblue=!ledblue; |
Gerth | 11:c10651055871 | 245 | pass++; |
Gerth | 11:c10651055871 | 246 | } |
Gerth | 11:c10651055871 | 247 | schiettimer.stop(); |
Gerth | 16:63320b8f79c2 | 248 | ledblue=1; |
Gerth | 11:c10651055871 | 249 | //terug na schieten |
Gerth | 11:c10651055871 | 250 | desired_angle[0]+=schiethoek; |
Gerth | 11:c10651055871 | 251 | desired_angle[1]-=schiethoek; |
Gerth | 11:c10651055871 | 252 | } |
Gerth | 11:c10651055871 | 253 | |
Gerth | 16:63320b8f79c2 | 254 | ////////////////////////////////////////////////////READ EMG AND MOVE DESIRED POSITION |
Gerth | 3:48438eea184e | 255 | void readsignal() |
Gerth | 3:48438eea184e | 256 | { |
Gerth | 11:c10651055871 | 257 | //check if pod has to shoot |
Gerth | 4:bf7765b0f612 | 258 | if (filteredsignal1>=threshold_value && filteredsignal2>=threshold_value) { |
Gerth | 4:bf7765b0f612 | 259 | led1=led2=1; |
Gerth | 11:c10651055871 | 260 | shoot(); |
Gerth | 14:4c4f45a1dd23 | 261 | // check if pod has to move to the right |
Gerth | 4:bf7765b0f612 | 262 | } else if (filteredsignal1>=threshold_value && filteredsignal2<=threshold_value) { |
Gerth | 4:bf7765b0f612 | 263 | led1=1; |
Gerth | 4:bf7765b0f612 | 264 | led2=0; |
Gerth | 16:63320b8f79c2 | 265 | desired_position += (mm_per_sec_emg/readsignal_frequency);// move desiredposition right |
Gerth | 11:c10651055871 | 266 | if (desired_position>=maxdisplacement) {//check if the pod doesnt move too far and hit the edge |
Gerth | 11:c10651055871 | 267 | desired_position=maxdisplacement; |
Gerth | 11:c10651055871 | 268 | } else { |
Gerth | 11:c10651055871 | 269 | desired_position=desired_position; |
Gerth | 11:c10651055871 | 270 | } |
Gerth | 14:4c4f45a1dd23 | 271 | // check if pod has to move to the left |
Gerth | 4:bf7765b0f612 | 272 | } else if (filteredsignal1<=threshold_value && filteredsignal2>=threshold_value) { |
Gerth | 4:bf7765b0f612 | 273 | led1=0; |
Gerth | 4:bf7765b0f612 | 274 | led2=1; |
Gerth | 16:63320b8f79c2 | 275 | desired_position -= (mm_per_sec_emg/readsignal_frequency);//move desiredposition left |
Gerth | 11:c10651055871 | 276 | if (desired_position<=(-1*maxdisplacement)) {//check if the pod doesnt move too far and hit the edge |
Gerth | 14:4c4f45a1dd23 | 277 | desired_position=(-1*maxdisplacement); |
Gerth | 11:c10651055871 | 278 | } else { |
Gerth | 11:c10651055871 | 279 | desired_position=desired_position; |
Gerth | 11:c10651055871 | 280 | } |
Gerth | 3:48438eea184e | 281 | } else { |
Gerth | 4:bf7765b0f612 | 282 | led1=led2=0; |
Gerth | 3:48438eea184e | 283 | } |
Gerth | 16:63320b8f79c2 | 284 | } |
Gerth | 16:63320b8f79c2 | 285 | ///////////////////////////////////////////////READ BUTTON AND MOVE DESIRED POSITION |
Gerth | 16:63320b8f79c2 | 286 | void readbutton() |
Gerth | 16:63320b8f79c2 | 287 | { |
Gerth | 16:63320b8f79c2 | 288 | volatile bool go=true; |
Gerth | 16:63320b8f79c2 | 289 | //check if pod has to shoot |
Gerth | 16:63320b8f79c2 | 290 | if (buttonR.read()==0 && buttonL.read()==0 &&go==true) { |
Gerth | 16:63320b8f79c2 | 291 | led1=led2=1; |
Gerth | 16:63320b8f79c2 | 292 | shoot(); |
Gerth | 16:63320b8f79c2 | 293 | // check if pod has to move to the right |
Gerth | 16:63320b8f79c2 | 294 | go=false; |
Gerth | 16:63320b8f79c2 | 295 | } else if (buttonR.read()==0 && buttonL.read()==1 && go==true ) { |
Gerth | 16:63320b8f79c2 | 296 | led1=1; |
Gerth | 16:63320b8f79c2 | 297 | led2=0; |
Gerth | 16:63320b8f79c2 | 298 | desired_position += (mm_per_sec_emg/readsignal_frequency);// move desiredposition right |
Gerth | 16:63320b8f79c2 | 299 | if (desired_position>=maxdisplacement && go==true) {//check if the pod doesnt move too far and hit the edge |
Gerth | 16:63320b8f79c2 | 300 | desired_position=maxdisplacement; |
Gerth | 16:63320b8f79c2 | 301 | go=false; |
Gerth | 16:63320b8f79c2 | 302 | } else { |
Gerth | 16:63320b8f79c2 | 303 | desired_position=desired_position; |
Gerth | 16:63320b8f79c2 | 304 | go=false; |
Gerth | 16:63320b8f79c2 | 305 | } |
Gerth | 16:63320b8f79c2 | 306 | go=false; |
Gerth | 16:63320b8f79c2 | 307 | } else if (buttonR.read()==1 && buttonL.read()==0&&go==true) { |
Gerth | 16:63320b8f79c2 | 308 | led1=0; |
Gerth | 16:63320b8f79c2 | 309 | led2=1; |
Gerth | 16:63320b8f79c2 | 310 | desired_position -= (mm_per_sec_emg/readsignal_frequency);//move desiredposition left |
Gerth | 16:63320b8f79c2 | 311 | if (desired_position<=(-1*maxdisplacement) && go==true) {//check if the pod doesnt move too far and hit the edge |
Gerth | 16:63320b8f79c2 | 312 | desired_position=(-1*maxdisplacement); |
Gerth | 16:63320b8f79c2 | 313 | go=false; |
Gerth | 16:63320b8f79c2 | 314 | } else { |
Gerth | 16:63320b8f79c2 | 315 | desired_position=desired_position; |
Gerth | 16:63320b8f79c2 | 316 | go=false; |
Gerth | 16:63320b8f79c2 | 317 | } |
Gerth | 16:63320b8f79c2 | 318 | go=false; |
Gerth | 16:63320b8f79c2 | 319 | } else { |
Gerth | 16:63320b8f79c2 | 320 | led1=led2=0; |
Gerth | 16:63320b8f79c2 | 321 | } |
Gerth | 3:48438eea184e | 322 | } |
Gerth | 3:48438eea184e | 323 | |
Gerth | 6:37c94a5e205f | 324 | void changemode() |
Gerth | 6:37c94a5e205f | 325 | { |
Gerth | 10:9e96d14d7034 | 326 | mycontroller.STOP(); |
Gerth | 6:37c94a5e205f | 327 | switchedmode=true; |
Gerth | 6:37c94a5e205f | 328 | modecounter++; |
Gerth | 15:17de575b7385 | 329 | if (modecounter==4) { |
Gerth | 6:37c94a5e205f | 330 | modecounter=0; |
Gerth | 6:37c94a5e205f | 331 | } else { |
Gerth | 6:37c94a5e205f | 332 | modecounter=modecounter; |
Gerth | 6:37c94a5e205f | 333 | } |
Gerth | 6:37c94a5e205f | 334 | wait(1); |
Gerth | 6:37c94a5e205f | 335 | } |
Gerth | 0:dd66fff537d7 | 336 | |
Gerth | 16:63320b8f79c2 | 337 | ///////////////////////////////////////////////////MAIN |
Gerth | 7:7fbb2c028778 | 338 | |
Gerth | 0:dd66fff537d7 | 339 | int main() |
Gerth | 0:dd66fff537d7 | 340 | { |
Gerth | 1:917c07a4f3ec | 341 | //tickers |
Gerth | 1:917c07a4f3ec | 342 | safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency); |
Gerth | 1:917c07a4f3ec | 343 | filter_ticker.attach(&filter_activate,1.0/filter_frequency); |
Gerth | 1:917c07a4f3ec | 344 | control_ticker.attach(&control_activate,1.0/control_frequency); |
Gerth | 1:917c07a4f3ec | 345 | scope_ticker.attach(&scopedata_activate,1.0/scope_frequency); |
Gerth | 3:48438eea184e | 346 | readsignal_ticker.attach(&readsignal_activate, 1.0/readsignal_frequency); |
Gerth | 7:7fbb2c028778 | 347 | readbuttoncalibrate_ticker.attach(&readbuttoncalibrate_activate, 1.0/readbuttoncalibrate_frequency); |
Gerth | 14:4c4f45a1dd23 | 348 | ledblink_ticker.attach(&ledblink_activate, 1.0/ledblink_frequency); |
Gerth | 14:4c4f45a1dd23 | 349 | |
Gerth | 1:917c07a4f3ec | 350 | while(1) { |
Gerth | 6:37c94a5e205f | 351 | if (changemodebutton==0) { |
Gerth | 6:37c94a5e205f | 352 | changemode(); |
Gerth | 1:917c07a4f3ec | 353 | } |
Gerth | 9:4ee354663560 | 354 | if (scopedata_go==true) { |
Gerth | 9:4ee354663560 | 355 | scopedata(); |
Gerth | 9:4ee354663560 | 356 | scopedata_go=false; |
Gerth | 9:4ee354663560 | 357 | } |
Gerth | 9:4ee354663560 | 358 | if (safetyandthreshold_go==true) { |
Gerth | 9:4ee354663560 | 359 | safetyandthreshold(); |
Gerth | 9:4ee354663560 | 360 | safetyandthreshold_go=false; |
Gerth | 9:4ee354663560 | 361 | } |
Gerth | 7:7fbb2c028778 | 362 | ///////////////////////////////////////////NORMAL RUNNING MODE |
Gerth | 7:7fbb2c028778 | 363 | if(modecounter==0) { |
Gerth | 6:37c94a5e205f | 364 | if (switchedmode==true) { |
Gerth | 6:37c94a5e205f | 365 | encoder1.reset(); |
Gerth | 6:37c94a5e205f | 366 | encoder2.reset(); |
Gerth | 6:37c94a5e205f | 367 | pc.printf("Program running\n");// |
Gerth | 16:63320b8f79c2 | 368 | ledgreen=0; |
Gerth | 16:63320b8f79c2 | 369 | led1=led2=ledred=ledblue=1; |
Gerth | 6:37c94a5e205f | 370 | switchedmode=false; |
Gerth | 6:37c94a5e205f | 371 | } |
Gerth | 9:4ee354663560 | 372 | |
Gerth | 6:37c94a5e205f | 373 | if (filter_go==true) { |
Gerth | 6:37c94a5e205f | 374 | filtereverything(); |
Gerth | 6:37c94a5e205f | 375 | filter_go=false; |
Gerth | 6:37c94a5e205f | 376 | } |
Gerth | 6:37c94a5e205f | 377 | if (control_go==true) { |
Gerth | 16:63320b8f79c2 | 378 | desired_angle[0]=anglepos.positiontoangle1(desired_position,y_start); |
Gerth | 16:63320b8f79c2 | 379 | desired_angle[1]=anglepos.positiontoangle2(desired_position,y_start); |
Gerth | 16:63320b8f79c2 | 380 | |
Gerth | 11:c10651055871 | 381 | float error1=(desired_angle[0]-counttorad*encoder1.getPulses()); |
Gerth | 11:c10651055871 | 382 | float error2=(desired_angle[1]-counttorad*encoder2.getPulses()); |
Gerth | 7:7fbb2c028778 | 383 | mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int); |
Gerth | 6:37c94a5e205f | 384 | control_go=false; |
Gerth | 6:37c94a5e205f | 385 | } |
Gerth | 6:37c94a5e205f | 386 | if (readsignal_go==true) { |
Gerth | 6:37c94a5e205f | 387 | readsignal(); |
Gerth | 6:37c94a5e205f | 388 | readsignal_go=false; |
Gerth | 6:37c94a5e205f | 389 | } |
Gerth | 6:37c94a5e205f | 390 | valuechangebutton.fall(&valuechange); |
Gerth | 1:917c07a4f3ec | 391 | } |
Gerth | 7:7fbb2c028778 | 392 | ////////////////////////////////////////////////////CALIBRATE RIGHT ARM |
Gerth | 6:37c94a5e205f | 393 | if (modecounter==1) { |
Gerth | 6:37c94a5e205f | 394 | if(switchedmode==true) { |
Gerth | 6:37c94a5e205f | 395 | pc.printf("Calibration mode! Use buttons to move rigth arm to 0 degrees\n"); |
Gerth | 16:63320b8f79c2 | 396 | led1=led2=ledred=0; |
Gerth | 16:63320b8f79c2 | 397 | ledgreen=ledblue=1; |
Gerth | 6:37c94a5e205f | 398 | switchedmode=false; |
Gerth | 14:4c4f45a1dd23 | 399 | } |
Gerth | 14:4c4f45a1dd23 | 400 | if (ledblink_go==true) { |
Gerth | 14:4c4f45a1dd23 | 401 | led1=!led1; |
Gerth | 16:63320b8f79c2 | 402 | ledblink_go=false; |
Gerth | 6:37c94a5e205f | 403 | } |
Gerth | 7:7fbb2c028778 | 404 | if (control_go==true) { |
Gerth | 11:c10651055871 | 405 | float error1=(desired_angle[0]-counttorad*encoder1.getPulses()); |
Gerth | 9:4ee354663560 | 406 | float error2=0;// this is the error you want to use |
Gerth | 7:7fbb2c028778 | 407 | mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int); |
Gerth | 7:7fbb2c028778 | 408 | control_go=false; |
Gerth | 7:7fbb2c028778 | 409 | } |
Gerth | 9:4ee354663560 | 410 | if (readbuttoncalibrate_go==true) { |
Gerth | 9:4ee354663560 | 411 | if (buttonR.read()==0 && buttonL.read()==1) { |
Gerth | 9:4ee354663560 | 412 | desired_angle[0] += (radpersec_calibrate/readbuttoncalibrate_frequency); |
Gerth | 9:4ee354663560 | 413 | readbuttoncalibrate_go=false; |
Gerth | 9:4ee354663560 | 414 | } |
Gerth | 9:4ee354663560 | 415 | if (buttonR.read()==1 && buttonL.read()==0) { |
Gerth | 9:4ee354663560 | 416 | desired_angle[0] -= (radpersec_calibrate/readbuttoncalibrate_frequency); |
Gerth | 9:4ee354663560 | 417 | readbuttoncalibrate_go=false; |
Gerth | 9:4ee354663560 | 418 | } |
Gerth | 9:4ee354663560 | 419 | } |
Gerth | 8:54a7da09ccad | 420 | } |
Gerth | 8:54a7da09ccad | 421 | ////////////////////////////////////////////CALIBRATE LEFT ARM |
Gerth | 8:54a7da09ccad | 422 | if (modecounter==2) { |
Gerth | 8:54a7da09ccad | 423 | if(switchedmode==true) { |
Gerth | 8:54a7da09ccad | 424 | pc.printf("Calibration mode! Use buttons to move left arm to 0 degrees\n"); |
Gerth | 16:63320b8f79c2 | 425 | led1=led2=ledred=0; |
Gerth | 16:63320b8f79c2 | 426 | ledgreen=ledblue=1; |
Gerth | 8:54a7da09ccad | 427 | switchedmode=false; |
Gerth | 8:54a7da09ccad | 428 | } |
Gerth | 14:4c4f45a1dd23 | 429 | if (ledblink_go==true) { |
Gerth | 14:4c4f45a1dd23 | 430 | led2=!led2; |
Gerth | 16:63320b8f79c2 | 431 | ledblink_go=false; |
Gerth | 14:4c4f45a1dd23 | 432 | } |
Gerth | 8:54a7da09ccad | 433 | if (control_go==true) { |
Gerth | 8:54a7da09ccad | 434 | float error1=0; |
Gerth | 8:54a7da09ccad | 435 | float error2=(desired_angle[1]-counttorad*encoder2.getPulses());// this is the error you want to use |
Gerth | 8:54a7da09ccad | 436 | mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int); |
Gerth | 8:54a7da09ccad | 437 | control_go=false; |
Gerth | 8:54a7da09ccad | 438 | } |
Gerth | 8:54a7da09ccad | 439 | if (readbuttoncalibrate_go==true) { |
Gerth | 8:54a7da09ccad | 440 | if (buttonR.read()==0 && buttonL.read()==1) { |
Gerth | 8:54a7da09ccad | 441 | desired_angle[1] += (radpersec_calibrate/readbuttoncalibrate_frequency); |
Gerth | 8:54a7da09ccad | 442 | readbuttoncalibrate_go=false; |
Gerth | 7:7fbb2c028778 | 443 | } |
Gerth | 8:54a7da09ccad | 444 | if (buttonR.read()==1 && buttonL.read()==0) { |
Gerth | 8:54a7da09ccad | 445 | desired_angle[1] -= (radpersec_calibrate/readbuttoncalibrate_frequency); |
Gerth | 8:54a7da09ccad | 446 | readbuttoncalibrate_go=false; |
Gerth | 7:7fbb2c028778 | 447 | } |
Gerth | 6:37c94a5e205f | 448 | } |
Gerth | 3:48438eea184e | 449 | } |
Gerth | 16:63320b8f79c2 | 450 | ///////////////////////////////BUTTONCONTROLMODE |
Gerth | 15:17de575b7385 | 451 | if (modecounter==3) { |
Gerth | 16:63320b8f79c2 | 452 | if (switchedmode==true) { |
Gerth | 16:63320b8f79c2 | 453 | led1=led2=0; |
Gerth | 16:63320b8f79c2 | 454 | ledred=ledblue=1; |
Gerth | 16:63320b8f79c2 | 455 | switchedmode=false; |
Gerth | 16:63320b8f79c2 | 456 | } |
Gerth | 16:63320b8f79c2 | 457 | if (ledblink_go==true) { |
Gerth | 15:17de575b7385 | 458 | ledgreen=!ledgreen; |
Gerth | 16:63320b8f79c2 | 459 | ledblink_go=false; |
Gerth | 15:17de575b7385 | 460 | } |
Gerth | 16:63320b8f79c2 | 461 | if (readsignal_go==true) { |
Gerth | 16:63320b8f79c2 | 462 | readbutton(); |
Gerth | 16:63320b8f79c2 | 463 | readsignal_go==false; |
Gerth | 16:63320b8f79c2 | 464 | } |
Gerth | 16:63320b8f79c2 | 465 | } |
Gerth | 15:17de575b7385 | 466 | |
Gerth | 0:dd66fff537d7 | 467 | } |
Gerth | 8:54a7da09ccad | 468 | } |