The final program for the #include AIR robot
Dependencies: Biquad HIDScope QEI angleandposition controlandadjust mbed
Fork of includeair by
main.cpp@9:4ee354663560, 2015-10-14 (annotated)
- Committer:
- Gerth
- Date:
- Wed Oct 14 15:09:23 2015 +0000
- Revision:
- 9:4ee354663560
- Parent:
- 8:54a7da09ccad
- Child:
- 10:9e96d14d7034
calibration now working!;
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 | 1:917c07a4f3ec | 6 | |
Gerth | 1:917c07a4f3ec | 7 | //info out |
Gerth | 1:917c07a4f3ec | 8 | HIDScope scope(4); |
Gerth | 1:917c07a4f3ec | 9 | Ticker scope_ticker; |
Gerth | 3:48438eea184e | 10 | const double scope_frequency=500; |
Gerth | 1:917c07a4f3ec | 11 | Serial pc(USBTX,USBRX); |
Gerth | 1:917c07a4f3ec | 12 | |
Gerth | 1:917c07a4f3ec | 13 | ////////////////ENCODERS |
Gerth | 9:4ee354663560 | 14 | const float cpr_sensor=32; |
Gerth | 9:4ee354663560 | 15 | const float cpr_shaft=cpr_sensor*131; |
Gerth | 9:4ee354663560 | 16 | QEI encoder1(D12,D13,NC,cpr_sensor);/// maybe use Encoder in stead of QEI, because Encoder had setposition |
Gerth | 9:4ee354663560 | 17 | QEI encoder2(D10,D11,NC,cpr_sensor); |
Gerth | 9:4ee354663560 | 18 | const double PIE=3.14159265359; |
Gerth | 9:4ee354663560 | 19 | const float counttorad=((2*PIE)/cpr_shaft); |
Gerth | 1:917c07a4f3ec | 20 | |
Gerth | 6:37c94a5e205f | 21 | /////////////////////////////////CALIBRATION (MODE) |
Gerth | 8:54a7da09ccad | 22 | int modecounter=1; |
Gerth | 6:37c94a5e205f | 23 | DigitalIn changemodebutton(PTA4); |
Gerth | 6:37c94a5e205f | 24 | |
Gerth | 7:7fbb2c028778 | 25 | Ticker readbuttoncalibrate_ticker; |
Gerth | 7:7fbb2c028778 | 26 | const double readbuttoncalibrate_frequency=10; |
Gerth | 7:7fbb2c028778 | 27 | |
Gerth | 9:4ee354663560 | 28 | const double radpersec_calibrate=0.1*PIE; |
Gerth | 8:54a7da09ccad | 29 | |
Gerth | 8:54a7da09ccad | 30 | DigitalIn buttonR(D2); |
Gerth | 8:54a7da09ccad | 31 | DigitalIn buttonL(D3); |
Gerth | 8:54a7da09ccad | 32 | |
Gerth | 1:917c07a4f3ec | 33 | //////////////////////////////////CONTROLLER |
Gerth | 1:917c07a4f3ec | 34 | controlandadjust mycontroller; // make a controller |
Gerth | 1:917c07a4f3ec | 35 | //controller constants |
Gerth | 1:917c07a4f3ec | 36 | float Kp=0.5; |
Gerth | 9:4ee354663560 | 37 | float Ki=0.01; |
Gerth | 1:917c07a4f3ec | 38 | float Kd=0.001; |
Gerth | 1:917c07a4f3ec | 39 | Ticker control_ticker; |
Gerth | 9:4ee354663560 | 40 | const double control_frequency=25; |
Gerth | 7:7fbb2c028778 | 41 | const double Ts_control=1.0/control_frequency; |
Gerth | 1:917c07a4f3ec | 42 | |
Gerth | 7:7fbb2c028778 | 43 | float error1_int=0;// storage variables for the errors |
Gerth | 7:7fbb2c028778 | 44 | float error2_int=0; |
Gerth | 7:7fbb2c028778 | 45 | float error1_prev=0; |
Gerth | 7:7fbb2c028778 | 46 | float error2_prev=0; |
Gerth | 1:917c07a4f3ec | 47 | |
Gerth | 1:917c07a4f3ec | 48 | InterruptIn valuechangebutton(PTC6);//button to change controller constants |
Gerth | 1:917c07a4f3ec | 49 | |
Gerth | 1:917c07a4f3ec | 50 | //safetyandthreshold |
Gerth | 3:48438eea184e | 51 | AnalogIn safety_pot(A3);//pot 2, used for the safety cutoff value for the pwm |
Gerth | 3:48438eea184e | 52 | AnalogIn threshold_pot(A2);//pot1, used to adjust threshold if signal differs per person |
Gerth | 1:917c07a4f3ec | 53 | |
Gerth | 1:917c07a4f3ec | 54 | Ticker safetyandthreshold_ticker; // ticker to read potmeters |
Gerth | 3:48438eea184e | 55 | const double safetyandthreshold_frequency=1; // frequency for the ticker |
Gerth | 3:48438eea184e | 56 | |
Gerth | 4:bf7765b0f612 | 57 | float threshold_value=1;//initial threshold value |
Gerth | 1:917c07a4f3ec | 58 | |
Gerth | 1:917c07a4f3ec | 59 | ////////////////////////////////FILTER |
Gerth | 1:917c07a4f3ec | 60 | #include "filtervalues.h" |
Gerth | 1:917c07a4f3ec | 61 | Ticker filter_ticker; |
Gerth | 3:48438eea184e | 62 | const double filter_frequency=500; |
Gerth | 1:917c07a4f3ec | 63 | Biquad myfilter1; |
Gerth | 1:917c07a4f3ec | 64 | Biquad myfilter2; |
Gerth | 1:917c07a4f3ec | 65 | |
Gerth | 1:917c07a4f3ec | 66 | AnalogIn emg1_input(A0); |
Gerth | 1:917c07a4f3ec | 67 | AnalogIn emg2_input(A1); |
Gerth | 1:917c07a4f3ec | 68 | |
Gerth | 1:917c07a4f3ec | 69 | double filteredsignal1=0; |
Gerth | 1:917c07a4f3ec | 70 | double filteredsignal2=0; |
Gerth | 5:8ac5d0651e4d | 71 | float filter_extragain=1; |
Gerth | 1:917c07a4f3ec | 72 | |
Gerth | 3:48438eea184e | 73 | /////////////////READSIGNAL |
Gerth | 3:48438eea184e | 74 | Ticker readsignal_ticker; |
Gerth | 3:48438eea184e | 75 | const double readsignal_frequency=25; |
Gerth | 3:48438eea184e | 76 | |
Gerth | 4:bf7765b0f612 | 77 | DigitalOut led1(D8); |
Gerth | 4:bf7765b0f612 | 78 | DigitalOut led2(D9); |
Gerth | 4:bf7765b0f612 | 79 | |
Gerth | 4:bf7765b0f612 | 80 | //////////////////////////////// POSITION AND ANGLE SHIZZLE |
Gerth | 4:bf7765b0f612 | 81 | float desired_position=0; |
Gerth | 4:bf7765b0f612 | 82 | float desired_angle[]= {0,0}; |
Gerth | 3:48438eea184e | 83 | |
Gerth | 1:917c07a4f3ec | 84 | //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS |
Gerth | 1:917c07a4f3ec | 85 | volatile bool scopedata_go=false, |
Gerth | 1:917c07a4f3ec | 86 | control_go=false, |
Gerth | 1:917c07a4f3ec | 87 | filter_go=false, |
Gerth | 3:48438eea184e | 88 | safetyandthreshold_go=false, |
Gerth | 6:37c94a5e205f | 89 | readsignal_go=false, |
Gerth | 8:54a7da09ccad | 90 | switchedmode=true, |
Gerth | 7:7fbb2c028778 | 91 | readbuttoncalibrate_go=false; |
Gerth | 1:917c07a4f3ec | 92 | |
Gerth | 1:917c07a4f3ec | 93 | void scopedata_activate() |
Gerth | 1:917c07a4f3ec | 94 | { |
Gerth | 1:917c07a4f3ec | 95 | scopedata_go=true; |
Gerth | 1:917c07a4f3ec | 96 | } |
Gerth | 1:917c07a4f3ec | 97 | void control_activate() |
Gerth | 1:917c07a4f3ec | 98 | { |
Gerth | 1:917c07a4f3ec | 99 | control_go=true; |
Gerth | 1:917c07a4f3ec | 100 | } |
Gerth | 1:917c07a4f3ec | 101 | void filter_activate() |
Gerth | 1:917c07a4f3ec | 102 | { |
Gerth | 1:917c07a4f3ec | 103 | filter_go=true; |
Gerth | 1:917c07a4f3ec | 104 | } |
Gerth | 1:917c07a4f3ec | 105 | void safetyandthreshold_activate() |
Gerth | 1:917c07a4f3ec | 106 | { |
Gerth | 1:917c07a4f3ec | 107 | safetyandthreshold_go=true; |
Gerth | 1:917c07a4f3ec | 108 | } |
Gerth | 3:48438eea184e | 109 | void readsignal_activate() |
Gerth | 3:48438eea184e | 110 | { |
Gerth | 3:48438eea184e | 111 | readsignal_go=true; |
Gerth | 3:48438eea184e | 112 | } |
Gerth | 7:7fbb2c028778 | 113 | void readbuttoncalibrate_activate() |
Gerth | 7:7fbb2c028778 | 114 | { |
Gerth | 7:7fbb2c028778 | 115 | readbuttoncalibrate_go=true; |
Gerth | 7:7fbb2c028778 | 116 | } |
Gerth | 1:917c07a4f3ec | 117 | |
Gerth | 1:917c07a4f3ec | 118 | ////////////////////////FUNCTIONS |
Gerth | 1:917c07a4f3ec | 119 | //gather data and send to scope |
Gerth | 1:917c07a4f3ec | 120 | void scopedata() |
Gerth | 1:917c07a4f3ec | 121 | { |
Gerth | 8:54a7da09ccad | 122 | scope.set(0,desired_angle[1]); |
Gerth | 8:54a7da09ccad | 123 | scope.set(1,counttorad*encoder2.getPulses()); |
Gerth | 8:54a7da09ccad | 124 | scope.set(2,mycontroller.motor2pwm()); |
Gerth | 3:48438eea184e | 125 | scope.send(); |
Gerth | 4:bf7765b0f612 | 126 | } |
Gerth | 1:917c07a4f3ec | 127 | //read potmeters and adjust the safetyfactor and threshold |
Gerth | 1:917c07a4f3ec | 128 | void safetyandthreshold() |
Gerth | 1:917c07a4f3ec | 129 | { |
Gerth | 3:48438eea184e | 130 | mycontroller.cutoff((ceil (10*safety_pot.read()) )/10); // adjust the safetyfactor value between 0 and 1 rounded to 1 decimal |
Gerth | 3:48438eea184e | 131 | threshold_value=((ceil (10*threshold_pot.read()) )/10); // adjust the threshold value between 0 and 1 rounded to 1 decimal |
Gerth | 1:917c07a4f3ec | 132 | } |
Gerth | 1:917c07a4f3ec | 133 | /////filter |
Gerth | 1:917c07a4f3ec | 134 | void filtereverything() |
Gerth | 1:917c07a4f3ec | 135 | { |
Gerth | 1:917c07a4f3ec | 136 | //filter_timer.reset(); |
Gerth | 1:917c07a4f3ec | 137 | // filter_timer.start(); |
Gerth | 1:917c07a4f3ec | 138 | //pass1 so f1 |
Gerth | 2:c7707856d137 | 139 | 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 | 140 | 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 | 141 | |
Gerth | 1:917c07a4f3ec | 142 | //pass2 so f2 |
Gerth | 2:c7707856d137 | 143 | 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 | 144 | 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 | 145 | |
Gerth | 1:917c07a4f3ec | 146 | //pass3 so f3 |
Gerth | 2:c7707856d137 | 147 | 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 | 148 | 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 | 149 | |
Gerth | 1:917c07a4f3ec | 150 | //pass4 so f4 |
Gerth | 2:c7707856d137 | 151 | 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 | 152 | 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 | 153 | |
Gerth | 1:917c07a4f3ec | 154 | //pass5 so f5 |
Gerth | 2:c7707856d137 | 155 | 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 | 156 | 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 | 157 | |
Gerth | 1:917c07a4f3ec | 158 | ///// take absolute value |
Gerth | 2:c7707856d137 | 159 | double pass5_emg1_abs=(fabs(pass5_emg1)); |
Gerth | 2:c7707856d137 | 160 | double pass5_emg2_abs=(fabs(pass5_emg2)); |
Gerth | 1:917c07a4f3ec | 161 | |
Gerth | 1:917c07a4f3ec | 162 | //pass6 so f6 |
Gerth | 2:c7707856d137 | 163 | 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 | 164 | 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 | 165 | |
Gerth | 1:917c07a4f3ec | 166 | |
Gerth | 1:917c07a4f3ec | 167 | //pass7 so f7 |
Gerth | 2:c7707856d137 | 168 | 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 | 169 | 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 | 170 | |
Gerth | 2:c7707856d137 | 171 | filteredsignal1=(pass7_emg1*9e11*filter_extragain); |
Gerth | 2:c7707856d137 | 172 | filteredsignal2=(pass7_emg2*9e11*filter_extragain); |
Gerth | 1:917c07a4f3ec | 173 | |
Gerth | 1:917c07a4f3ec | 174 | //filter_timer.stop(); |
Gerth | 1:917c07a4f3ec | 175 | } |
Gerth | 1:917c07a4f3ec | 176 | |
Gerth | 7:7fbb2c028778 | 177 | |
Gerth | 1:917c07a4f3ec | 178 | |
Gerth | 1:917c07a4f3ec | 179 | //adjust controller values when sw2 is pressed |
Gerth | 1:917c07a4f3ec | 180 | void valuechange() |
Gerth | 1:917c07a4f3ec | 181 | { |
Gerth | 3:48438eea184e | 182 | mycontroller.STOP(); |
Gerth | 5:8ac5d0651e4d | 183 | pc.printf("KP is now %f, enter new value\n",Kp); |
Gerth | 1:917c07a4f3ec | 184 | pc.scanf("%f", &Kp); |
Gerth | 1:917c07a4f3ec | 185 | |
Gerth | 1:917c07a4f3ec | 186 | pc.printf("KI is now %f, enter new value\n",Ki); |
Gerth | 1:917c07a4f3ec | 187 | pc.scanf("%f", &Ki); |
Gerth | 1:917c07a4f3ec | 188 | |
Gerth | 1:917c07a4f3ec | 189 | pc.printf("KD is now %f, enter new value\n",Kd); |
Gerth | 5:8ac5d0651e4d | 190 | pc.scanf("%f", &Kd); |
Gerth | 3:48438eea184e | 191 | |
Gerth | 3:48438eea184e | 192 | pc.printf("Extra gain is now %f, enter new value\n",filter_extragain); |
Gerth | 5:8ac5d0651e4d | 193 | pc.scanf("%f", &filter_extragain); |
Gerth | 1:917c07a4f3ec | 194 | } |
Gerth | 3:48438eea184e | 195 | void readsignal() |
Gerth | 3:48438eea184e | 196 | { |
Gerth | 4:bf7765b0f612 | 197 | if (filteredsignal1>=threshold_value && filteredsignal2>=threshold_value) { |
Gerth | 4:bf7765b0f612 | 198 | led1=led2=1; |
Gerth | 4:bf7765b0f612 | 199 | } else if (filteredsignal1>=threshold_value && filteredsignal2<=threshold_value) { |
Gerth | 4:bf7765b0f612 | 200 | led1=1; |
Gerth | 4:bf7765b0f612 | 201 | led2=0; |
Gerth | 4:bf7765b0f612 | 202 | } else if (filteredsignal1<=threshold_value && filteredsignal2>=threshold_value) { |
Gerth | 4:bf7765b0f612 | 203 | led1=0; |
Gerth | 4:bf7765b0f612 | 204 | led2=1; |
Gerth | 3:48438eea184e | 205 | } else { |
Gerth | 4:bf7765b0f612 | 206 | led1=led2=0; |
Gerth | 3:48438eea184e | 207 | } |
Gerth | 4:bf7765b0f612 | 208 | |
Gerth | 3:48438eea184e | 209 | } |
Gerth | 3:48438eea184e | 210 | |
Gerth | 6:37c94a5e205f | 211 | void changemode() |
Gerth | 6:37c94a5e205f | 212 | { |
Gerth | 6:37c94a5e205f | 213 | switchedmode=true; |
Gerth | 6:37c94a5e205f | 214 | modecounter++; |
Gerth | 6:37c94a5e205f | 215 | if (modecounter==3) { |
Gerth | 6:37c94a5e205f | 216 | modecounter=0; |
Gerth | 6:37c94a5e205f | 217 | } else { |
Gerth | 6:37c94a5e205f | 218 | modecounter=modecounter; |
Gerth | 6:37c94a5e205f | 219 | } |
Gerth | 6:37c94a5e205f | 220 | wait(1); |
Gerth | 6:37c94a5e205f | 221 | } |
Gerth | 0:dd66fff537d7 | 222 | |
Gerth | 7:7fbb2c028778 | 223 | |
Gerth | 7:7fbb2c028778 | 224 | |
Gerth | 0:dd66fff537d7 | 225 | int main() |
Gerth | 0:dd66fff537d7 | 226 | { |
Gerth | 1:917c07a4f3ec | 227 | //tickers |
Gerth | 1:917c07a4f3ec | 228 | safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency); |
Gerth | 1:917c07a4f3ec | 229 | filter_ticker.attach(&filter_activate,1.0/filter_frequency); |
Gerth | 1:917c07a4f3ec | 230 | control_ticker.attach(&control_activate,1.0/control_frequency); |
Gerth | 1:917c07a4f3ec | 231 | scope_ticker.attach(&scopedata_activate,1.0/scope_frequency); |
Gerth | 3:48438eea184e | 232 | readsignal_ticker.attach(&readsignal_activate, 1.0/readsignal_frequency); |
Gerth | 7:7fbb2c028778 | 233 | readbuttoncalibrate_ticker.attach(&readbuttoncalibrate_activate, 1.0/readbuttoncalibrate_frequency); |
Gerth | 1:917c07a4f3ec | 234 | |
Gerth | 1:917c07a4f3ec | 235 | while(1) { |
Gerth | 6:37c94a5e205f | 236 | if (changemodebutton==0) { |
Gerth | 6:37c94a5e205f | 237 | changemode(); |
Gerth | 1:917c07a4f3ec | 238 | } |
Gerth | 9:4ee354663560 | 239 | if (scopedata_go==true) { |
Gerth | 9:4ee354663560 | 240 | scopedata(); |
Gerth | 9:4ee354663560 | 241 | scopedata_go=false; |
Gerth | 9:4ee354663560 | 242 | } |
Gerth | 9:4ee354663560 | 243 | if (safetyandthreshold_go==true) { |
Gerth | 9:4ee354663560 | 244 | safetyandthreshold(); |
Gerth | 9:4ee354663560 | 245 | safetyandthreshold_go=false; |
Gerth | 9:4ee354663560 | 246 | } |
Gerth | 7:7fbb2c028778 | 247 | ///////////////////////////////////////////NORMAL RUNNING MODE |
Gerth | 7:7fbb2c028778 | 248 | if(modecounter==0) { |
Gerth | 6:37c94a5e205f | 249 | if (switchedmode==true) { |
Gerth | 6:37c94a5e205f | 250 | encoder1.reset(); |
Gerth | 6:37c94a5e205f | 251 | encoder2.reset(); |
Gerth | 6:37c94a5e205f | 252 | pc.printf("Program running\n");// |
Gerth | 6:37c94a5e205f | 253 | switchedmode=false; |
Gerth | 6:37c94a5e205f | 254 | } |
Gerth | 9:4ee354663560 | 255 | |
Gerth | 6:37c94a5e205f | 256 | if (filter_go==true) { |
Gerth | 6:37c94a5e205f | 257 | filtereverything(); |
Gerth | 6:37c94a5e205f | 258 | filter_go=false; |
Gerth | 6:37c94a5e205f | 259 | } |
Gerth | 6:37c94a5e205f | 260 | if (control_go==true) { |
Gerth | 7:7fbb2c028778 | 261 | float error1=0; |
Gerth | 7:7fbb2c028778 | 262 | float error2=0; |
Gerth | 7:7fbb2c028778 | 263 | mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int); |
Gerth | 6:37c94a5e205f | 264 | control_go=false; |
Gerth | 6:37c94a5e205f | 265 | } |
Gerth | 6:37c94a5e205f | 266 | if (readsignal_go==true) { |
Gerth | 6:37c94a5e205f | 267 | readsignal(); |
Gerth | 6:37c94a5e205f | 268 | readsignal_go=false; |
Gerth | 6:37c94a5e205f | 269 | } |
Gerth | 6:37c94a5e205f | 270 | valuechangebutton.fall(&valuechange); |
Gerth | 1:917c07a4f3ec | 271 | } |
Gerth | 7:7fbb2c028778 | 272 | ////////////////////////////////////////////////////CALIBRATE RIGHT ARM |
Gerth | 6:37c94a5e205f | 273 | if (modecounter==1) { |
Gerth | 6:37c94a5e205f | 274 | if(switchedmode==true) { |
Gerth | 6:37c94a5e205f | 275 | pc.printf("Calibration mode! Use buttons to move rigth arm to 0 degrees\n"); |
Gerth | 6:37c94a5e205f | 276 | switchedmode=false; |
Gerth | 6:37c94a5e205f | 277 | } |
Gerth | 7:7fbb2c028778 | 278 | if (control_go==true) { |
Gerth | 9:4ee354663560 | 279 | float error1=(desired_angle[0]-counttorad*encoder1.getPulses());; |
Gerth | 9:4ee354663560 | 280 | float error2=0;// this is the error you want to use |
Gerth | 7:7fbb2c028778 | 281 | mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int); |
Gerth | 7:7fbb2c028778 | 282 | control_go=false; |
Gerth | 7:7fbb2c028778 | 283 | } |
Gerth | 9:4ee354663560 | 284 | if (readbuttoncalibrate_go==true) { |
Gerth | 9:4ee354663560 | 285 | if (buttonR.read()==0 && buttonL.read()==1) { |
Gerth | 9:4ee354663560 | 286 | desired_angle[0] += (radpersec_calibrate/readbuttoncalibrate_frequency); |
Gerth | 9:4ee354663560 | 287 | readbuttoncalibrate_go=false; |
Gerth | 9:4ee354663560 | 288 | } |
Gerth | 9:4ee354663560 | 289 | if (buttonR.read()==1 && buttonL.read()==0) { |
Gerth | 9:4ee354663560 | 290 | desired_angle[0] -= (radpersec_calibrate/readbuttoncalibrate_frequency); |
Gerth | 9:4ee354663560 | 291 | readbuttoncalibrate_go=false; |
Gerth | 9:4ee354663560 | 292 | } |
Gerth | 9:4ee354663560 | 293 | } |
Gerth | 8:54a7da09ccad | 294 | } |
Gerth | 8:54a7da09ccad | 295 | ////////////////////////////////////////////CALIBRATE LEFT ARM |
Gerth | 8:54a7da09ccad | 296 | if (modecounter==2) { |
Gerth | 8:54a7da09ccad | 297 | if(switchedmode==true) { |
Gerth | 8:54a7da09ccad | 298 | pc.printf("Calibration mode! Use buttons to move left arm to 0 degrees\n"); |
Gerth | 8:54a7da09ccad | 299 | switchedmode=false; |
Gerth | 8:54a7da09ccad | 300 | } |
Gerth | 8:54a7da09ccad | 301 | if (control_go==true) { |
Gerth | 8:54a7da09ccad | 302 | float error1=0; |
Gerth | 8:54a7da09ccad | 303 | float error2=(desired_angle[1]-counttorad*encoder2.getPulses());// this is the error you want to use |
Gerth | 8:54a7da09ccad | 304 | mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int); |
Gerth | 8:54a7da09ccad | 305 | control_go=false; |
Gerth | 8:54a7da09ccad | 306 | } |
Gerth | 8:54a7da09ccad | 307 | if (readbuttoncalibrate_go==true) { |
Gerth | 8:54a7da09ccad | 308 | if (buttonR.read()==0 && buttonL.read()==1) { |
Gerth | 8:54a7da09ccad | 309 | desired_angle[1] += (radpersec_calibrate/readbuttoncalibrate_frequency); |
Gerth | 8:54a7da09ccad | 310 | readbuttoncalibrate_go=false; |
Gerth | 7:7fbb2c028778 | 311 | } |
Gerth | 8:54a7da09ccad | 312 | if (buttonR.read()==1 && buttonL.read()==0) { |
Gerth | 8:54a7da09ccad | 313 | desired_angle[1] -= (radpersec_calibrate/readbuttoncalibrate_frequency); |
Gerth | 8:54a7da09ccad | 314 | readbuttoncalibrate_go=false; |
Gerth | 7:7fbb2c028778 | 315 | } |
Gerth | 6:37c94a5e205f | 316 | } |
Gerth | 3:48438eea184e | 317 | } |
Gerth | 0:dd66fff537d7 | 318 | } |
Gerth | 8:54a7da09ccad | 319 | } |