The final program for the #include AIR robot
Dependencies: Biquad HIDScope QEI angleandposition controlandadjust mbed
Fork of includeair by
main.cpp@5:8ac5d0651e4d, 2015-10-14 (annotated)
- Committer:
- Gerth
- Date:
- Wed Oct 14 12:29:27 2015 +0000
- Revision:
- 5:8ac5d0651e4d
- Parent:
- 4:bf7765b0f612
- Child:
- 6:37c94a5e205f
valuechange working make sure you specify the format correct when using scanf (floats and doubles are different);
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 | 4:bf7765b0f612 | 14 | QEI encoder1(D12,D13,NC,32);/// maybe use Encoder in stead of QEI, because Encoder had setposition |
Gerth | 1:917c07a4f3ec | 15 | QEI encoder2(D10,D11,NC,32); |
Gerth | 1:917c07a4f3ec | 16 | |
Gerth | 1:917c07a4f3ec | 17 | //////////////////////////////////CONTROLLER |
Gerth | 1:917c07a4f3ec | 18 | controlandadjust mycontroller; // make a controller |
Gerth | 1:917c07a4f3ec | 19 | //controller constants |
Gerth | 1:917c07a4f3ec | 20 | float Kp=0.5; |
Gerth | 1:917c07a4f3ec | 21 | float Ki=0.01; |
Gerth | 1:917c07a4f3ec | 22 | float Kd=0.001; |
Gerth | 1:917c07a4f3ec | 23 | Ticker control_ticker; |
Gerth | 3:48438eea184e | 24 | const double control_frequency=25; |
Gerth | 1:917c07a4f3ec | 25 | |
Gerth | 1:917c07a4f3ec | 26 | double error1_int=0;// storage variables for the errors |
Gerth | 1:917c07a4f3ec | 27 | double error2_int=0; |
Gerth | 1:917c07a4f3ec | 28 | double error1_prev=0; |
Gerth | 1:917c07a4f3ec | 29 | double error2_prev=0; |
Gerth | 1:917c07a4f3ec | 30 | |
Gerth | 1:917c07a4f3ec | 31 | InterruptIn valuechangebutton(PTC6);//button to change controller constants |
Gerth | 1:917c07a4f3ec | 32 | |
Gerth | 1:917c07a4f3ec | 33 | //safetyandthreshold |
Gerth | 3:48438eea184e | 34 | AnalogIn safety_pot(A3);//pot 2, used for the safety cutoff value for the pwm |
Gerth | 3:48438eea184e | 35 | AnalogIn threshold_pot(A2);//pot1, used to adjust threshold if signal differs per person |
Gerth | 1:917c07a4f3ec | 36 | |
Gerth | 1:917c07a4f3ec | 37 | Ticker safetyandthreshold_ticker; // ticker to read potmeters |
Gerth | 3:48438eea184e | 38 | const double safetyandthreshold_frequency=1; // frequency for the ticker |
Gerth | 3:48438eea184e | 39 | |
Gerth | 4:bf7765b0f612 | 40 | float threshold_value=1;//initial threshold value |
Gerth | 1:917c07a4f3ec | 41 | |
Gerth | 1:917c07a4f3ec | 42 | ////////////////////////////////FILTER |
Gerth | 1:917c07a4f3ec | 43 | #include "filtervalues.h" |
Gerth | 1:917c07a4f3ec | 44 | Ticker filter_ticker; |
Gerth | 3:48438eea184e | 45 | const double filter_frequency=500; |
Gerth | 1:917c07a4f3ec | 46 | Biquad myfilter1; |
Gerth | 1:917c07a4f3ec | 47 | Biquad myfilter2; |
Gerth | 1:917c07a4f3ec | 48 | |
Gerth | 1:917c07a4f3ec | 49 | AnalogIn emg1_input(A0); |
Gerth | 1:917c07a4f3ec | 50 | AnalogIn emg2_input(A1); |
Gerth | 1:917c07a4f3ec | 51 | |
Gerth | 1:917c07a4f3ec | 52 | double filteredsignal1=0; |
Gerth | 1:917c07a4f3ec | 53 | double filteredsignal2=0; |
Gerth | 5:8ac5d0651e4d | 54 | float filter_extragain=1; |
Gerth | 1:917c07a4f3ec | 55 | |
Gerth | 3:48438eea184e | 56 | /////////////////READSIGNAL |
Gerth | 3:48438eea184e | 57 | Ticker readsignal_ticker; |
Gerth | 3:48438eea184e | 58 | const double readsignal_frequency=25; |
Gerth | 3:48438eea184e | 59 | |
Gerth | 4:bf7765b0f612 | 60 | DigitalOut led1(D8); |
Gerth | 4:bf7765b0f612 | 61 | DigitalOut led2(D9); |
Gerth | 4:bf7765b0f612 | 62 | |
Gerth | 4:bf7765b0f612 | 63 | //////////////////////////////// POSITION AND ANGLE SHIZZLE |
Gerth | 4:bf7765b0f612 | 64 | float desired_position=0; |
Gerth | 4:bf7765b0f612 | 65 | float desired_angle[]= {0,0}; |
Gerth | 3:48438eea184e | 66 | |
Gerth | 1:917c07a4f3ec | 67 | //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS |
Gerth | 1:917c07a4f3ec | 68 | volatile bool scopedata_go=false, |
Gerth | 1:917c07a4f3ec | 69 | control_go=false, |
Gerth | 1:917c07a4f3ec | 70 | filter_go=false, |
Gerth | 3:48438eea184e | 71 | safetyandthreshold_go=false, |
Gerth | 3:48438eea184e | 72 | readsignal_go=false; |
Gerth | 1:917c07a4f3ec | 73 | |
Gerth | 1:917c07a4f3ec | 74 | void scopedata_activate() |
Gerth | 1:917c07a4f3ec | 75 | { |
Gerth | 1:917c07a4f3ec | 76 | scopedata_go=true; |
Gerth | 1:917c07a4f3ec | 77 | } |
Gerth | 1:917c07a4f3ec | 78 | void control_activate() |
Gerth | 1:917c07a4f3ec | 79 | { |
Gerth | 1:917c07a4f3ec | 80 | control_go=true; |
Gerth | 1:917c07a4f3ec | 81 | } |
Gerth | 1:917c07a4f3ec | 82 | void filter_activate() |
Gerth | 1:917c07a4f3ec | 83 | { |
Gerth | 1:917c07a4f3ec | 84 | filter_go=true; |
Gerth | 1:917c07a4f3ec | 85 | } |
Gerth | 1:917c07a4f3ec | 86 | void safetyandthreshold_activate() |
Gerth | 1:917c07a4f3ec | 87 | { |
Gerth | 1:917c07a4f3ec | 88 | safetyandthreshold_go=true; |
Gerth | 1:917c07a4f3ec | 89 | } |
Gerth | 3:48438eea184e | 90 | void readsignal_activate() |
Gerth | 3:48438eea184e | 91 | { |
Gerth | 3:48438eea184e | 92 | readsignal_go=true; |
Gerth | 3:48438eea184e | 93 | } |
Gerth | 0:dd66fff537d7 | 94 | |
Gerth | 1:917c07a4f3ec | 95 | |
Gerth | 1:917c07a4f3ec | 96 | ////////////////////////FUNCTIONS |
Gerth | 1:917c07a4f3ec | 97 | //gather data and send to scope |
Gerth | 1:917c07a4f3ec | 98 | void scopedata() |
Gerth | 1:917c07a4f3ec | 99 | { |
Gerth | 3:48438eea184e | 100 | scope.set(0,emg1_input.read()); |
Gerth | 3:48438eea184e | 101 | scope.set(1,filteredsignal1); |
Gerth | 3:48438eea184e | 102 | scope.send(); |
Gerth | 4:bf7765b0f612 | 103 | } |
Gerth | 1:917c07a4f3ec | 104 | //read potmeters and adjust the safetyfactor and threshold |
Gerth | 1:917c07a4f3ec | 105 | void safetyandthreshold() |
Gerth | 1:917c07a4f3ec | 106 | { |
Gerth | 3:48438eea184e | 107 | mycontroller.cutoff((ceil (10*safety_pot.read()) )/10); // adjust the safetyfactor value between 0 and 1 rounded to 1 decimal |
Gerth | 3:48438eea184e | 108 | threshold_value=((ceil (10*threshold_pot.read()) )/10); // adjust the threshold value between 0 and 1 rounded to 1 decimal |
Gerth | 1:917c07a4f3ec | 109 | } |
Gerth | 1:917c07a4f3ec | 110 | /////filter |
Gerth | 1:917c07a4f3ec | 111 | void filtereverything() |
Gerth | 1:917c07a4f3ec | 112 | { |
Gerth | 1:917c07a4f3ec | 113 | //filter_timer.reset(); |
Gerth | 1:917c07a4f3ec | 114 | // filter_timer.start(); |
Gerth | 1:917c07a4f3ec | 115 | //pass1 so f1 |
Gerth | 2:c7707856d137 | 116 | 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 | 117 | 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 | 118 | |
Gerth | 1:917c07a4f3ec | 119 | //pass2 so f2 |
Gerth | 2:c7707856d137 | 120 | 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 | 121 | 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 | 122 | |
Gerth | 1:917c07a4f3ec | 123 | //pass3 so f3 |
Gerth | 2:c7707856d137 | 124 | 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 | 125 | 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 | 126 | |
Gerth | 1:917c07a4f3ec | 127 | //pass4 so f4 |
Gerth | 2:c7707856d137 | 128 | 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 | 129 | 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 | 130 | |
Gerth | 1:917c07a4f3ec | 131 | //pass5 so f5 |
Gerth | 2:c7707856d137 | 132 | 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 | 133 | 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 | 134 | |
Gerth | 1:917c07a4f3ec | 135 | ///// take absolute value |
Gerth | 2:c7707856d137 | 136 | double pass5_emg1_abs=(fabs(pass5_emg1)); |
Gerth | 2:c7707856d137 | 137 | double pass5_emg2_abs=(fabs(pass5_emg2)); |
Gerth | 1:917c07a4f3ec | 138 | |
Gerth | 1:917c07a4f3ec | 139 | //pass6 so f6 |
Gerth | 2:c7707856d137 | 140 | 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 | 141 | 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 | 142 | |
Gerth | 1:917c07a4f3ec | 143 | |
Gerth | 1:917c07a4f3ec | 144 | //pass7 so f7 |
Gerth | 2:c7707856d137 | 145 | 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 | 146 | 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 | 147 | |
Gerth | 2:c7707856d137 | 148 | filteredsignal1=(pass7_emg1*9e11*filter_extragain); |
Gerth | 2:c7707856d137 | 149 | filteredsignal2=(pass7_emg2*9e11*filter_extragain); |
Gerth | 1:917c07a4f3ec | 150 | |
Gerth | 1:917c07a4f3ec | 151 | //filter_timer.stop(); |
Gerth | 1:917c07a4f3ec | 152 | } |
Gerth | 1:917c07a4f3ec | 153 | |
Gerth | 3:48438eea184e | 154 | void control() |
Gerth | 3:48438eea184e | 155 | { |
Gerth | 1:917c07a4f3ec | 156 | ///call desired controller here |
Gerth | 3:48438eea184e | 157 | } |
Gerth | 1:917c07a4f3ec | 158 | |
Gerth | 1:917c07a4f3ec | 159 | //adjust controller values when sw2 is pressed |
Gerth | 1:917c07a4f3ec | 160 | void valuechange() |
Gerth | 1:917c07a4f3ec | 161 | { |
Gerth | 3:48438eea184e | 162 | mycontroller.STOP(); |
Gerth | 5:8ac5d0651e4d | 163 | pc.printf("KP is now %f, enter new value\n",Kp); |
Gerth | 1:917c07a4f3ec | 164 | pc.scanf("%f", &Kp); |
Gerth | 1:917c07a4f3ec | 165 | |
Gerth | 1:917c07a4f3ec | 166 | pc.printf("KI is now %f, enter new value\n",Ki); |
Gerth | 1:917c07a4f3ec | 167 | pc.scanf("%f", &Ki); |
Gerth | 1:917c07a4f3ec | 168 | |
Gerth | 1:917c07a4f3ec | 169 | pc.printf("KD is now %f, enter new value\n",Kd); |
Gerth | 5:8ac5d0651e4d | 170 | pc.scanf("%f", &Kd); |
Gerth | 3:48438eea184e | 171 | |
Gerth | 3:48438eea184e | 172 | pc.printf("Extra gain is now %f, enter new value\n",filter_extragain); |
Gerth | 5:8ac5d0651e4d | 173 | pc.scanf("%f", &filter_extragain); |
Gerth | 1:917c07a4f3ec | 174 | } |
Gerth | 3:48438eea184e | 175 | void readsignal() |
Gerth | 3:48438eea184e | 176 | { |
Gerth | 4:bf7765b0f612 | 177 | if (filteredsignal1>=threshold_value && filteredsignal2>=threshold_value) { |
Gerth | 4:bf7765b0f612 | 178 | led1=led2=1; |
Gerth | 4:bf7765b0f612 | 179 | } else if (filteredsignal1>=threshold_value && filteredsignal2<=threshold_value) { |
Gerth | 4:bf7765b0f612 | 180 | led1=1; |
Gerth | 4:bf7765b0f612 | 181 | led2=0; |
Gerth | 4:bf7765b0f612 | 182 | } else if (filteredsignal1<=threshold_value && filteredsignal2>=threshold_value) { |
Gerth | 4:bf7765b0f612 | 183 | led1=0; |
Gerth | 4:bf7765b0f612 | 184 | led2=1; |
Gerth | 3:48438eea184e | 185 | } else { |
Gerth | 4:bf7765b0f612 | 186 | led1=led2=0; |
Gerth | 3:48438eea184e | 187 | } |
Gerth | 4:bf7765b0f612 | 188 | |
Gerth | 3:48438eea184e | 189 | } |
Gerth | 3:48438eea184e | 190 | |
Gerth | 0:dd66fff537d7 | 191 | |
Gerth | 0:dd66fff537d7 | 192 | int main() |
Gerth | 0:dd66fff537d7 | 193 | { |
Gerth | 1:917c07a4f3ec | 194 | //tickers |
Gerth | 1:917c07a4f3ec | 195 | safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency); |
Gerth | 1:917c07a4f3ec | 196 | filter_ticker.attach(&filter_activate,1.0/filter_frequency); |
Gerth | 1:917c07a4f3ec | 197 | control_ticker.attach(&control_activate,1.0/control_frequency); |
Gerth | 1:917c07a4f3ec | 198 | scope_ticker.attach(&scopedata_activate,1.0/scope_frequency); |
Gerth | 3:48438eea184e | 199 | readsignal_ticker.attach(&readsignal_activate, 1.0/readsignal_frequency); |
Gerth | 1:917c07a4f3ec | 200 | |
Gerth | 1:917c07a4f3ec | 201 | while(1) { |
Gerth | 1:917c07a4f3ec | 202 | if (scopedata_go==true) { |
Gerth | 1:917c07a4f3ec | 203 | scopedata(); |
Gerth | 1:917c07a4f3ec | 204 | scopedata_go=false; |
Gerth | 1:917c07a4f3ec | 205 | } |
Gerth | 1:917c07a4f3ec | 206 | if (filter_go==true) { |
Gerth | 1:917c07a4f3ec | 207 | filtereverything(); |
Gerth | 1:917c07a4f3ec | 208 | filter_go=false; |
Gerth | 1:917c07a4f3ec | 209 | } |
Gerth | 1:917c07a4f3ec | 210 | if (safetyandthreshold_go==true) { |
Gerth | 1:917c07a4f3ec | 211 | safetyandthreshold(); |
Gerth | 1:917c07a4f3ec | 212 | safetyandthreshold_go=false; |
Gerth | 1:917c07a4f3ec | 213 | } |
Gerth | 1:917c07a4f3ec | 214 | if (control_go==true) { |
Gerth | 1:917c07a4f3ec | 215 | control(); |
Gerth | 1:917c07a4f3ec | 216 | control_go=false; |
Gerth | 1:917c07a4f3ec | 217 | } |
Gerth | 3:48438eea184e | 218 | if (readsignal_go==true) { |
Gerth | 3:48438eea184e | 219 | readsignal(); |
Gerth | 3:48438eea184e | 220 | readsignal_go=false; |
Gerth | 3:48438eea184e | 221 | } |
Gerth | 3:48438eea184e | 222 | valuechangebutton.fall(&valuechange); |
Gerth | 0:dd66fff537d7 | 223 | } |
Gerth | 0:dd66fff537d7 | 224 | } |