The final program for the #include AIR robot
Dependencies: Biquad HIDScope QEI angleandposition controlandadjust mbed
Fork of includeair by
main.cpp@2:c7707856d137, 2015-10-13 (annotated)
- Committer:
- Gerth
- Date:
- Tue Oct 13 12:41:12 2015 +0000
- Revision:
- 2:c7707856d137
- Parent:
- 1:917c07a4f3ec
- Child:
- 3:48438eea184e
Added option to adjust the gain of the filtered signal via Serial, called with sw2
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 | 1:917c07a4f3ec | 10 | const float scope_frequency=500; |
Gerth | 1:917c07a4f3ec | 11 | Serial pc(USBTX,USBRX); |
Gerth | 1:917c07a4f3ec | 12 | |
Gerth | 1:917c07a4f3ec | 13 | ////////////////ENCODERS |
Gerth | 1:917c07a4f3ec | 14 | QEI encoder1(D12,D13,NC,32); |
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 | 1:917c07a4f3ec | 24 | const float 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 | 1:917c07a4f3ec | 34 | AnalogIn safety(A3);//pot 2, used for the safety cutoff value for the pwm |
Gerth | 1:917c07a4f3ec | 35 | AnalogIn threshold(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 | 1:917c07a4f3ec | 38 | const float safetyandthreshold_frequency=1; // frequency for the ticker |
Gerth | 1:917c07a4f3ec | 39 | |
Gerth | 1:917c07a4f3ec | 40 | ////////////////////////////////FILTER |
Gerth | 1:917c07a4f3ec | 41 | #include "filtervalues.h" |
Gerth | 1:917c07a4f3ec | 42 | Ticker filter_ticker; |
Gerth | 1:917c07a4f3ec | 43 | const float filter_frequency=500; |
Gerth | 1:917c07a4f3ec | 44 | Biquad myfilter1; |
Gerth | 1:917c07a4f3ec | 45 | Biquad myfilter2; |
Gerth | 1:917c07a4f3ec | 46 | |
Gerth | 1:917c07a4f3ec | 47 | AnalogIn emg1_input(A0); |
Gerth | 1:917c07a4f3ec | 48 | AnalogIn emg2_input(A1); |
Gerth | 1:917c07a4f3ec | 49 | |
Gerth | 1:917c07a4f3ec | 50 | double filteredsignal1=0; |
Gerth | 1:917c07a4f3ec | 51 | double filteredsignal2=0; |
Gerth | 2:c7707856d137 | 52 | double filter_extragain=1; |
Gerth | 1:917c07a4f3ec | 53 | |
Gerth | 1:917c07a4f3ec | 54 | //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS |
Gerth | 1:917c07a4f3ec | 55 | volatile bool scopedata_go=false, |
Gerth | 1:917c07a4f3ec | 56 | control_go=false, |
Gerth | 1:917c07a4f3ec | 57 | filter_go=false, |
Gerth | 1:917c07a4f3ec | 58 | safetyandthreshold_go=false; |
Gerth | 1:917c07a4f3ec | 59 | |
Gerth | 1:917c07a4f3ec | 60 | void scopedata_activate() |
Gerth | 1:917c07a4f3ec | 61 | { |
Gerth | 1:917c07a4f3ec | 62 | scopedata_go=true; |
Gerth | 1:917c07a4f3ec | 63 | } |
Gerth | 1:917c07a4f3ec | 64 | void control_activate() |
Gerth | 1:917c07a4f3ec | 65 | { |
Gerth | 1:917c07a4f3ec | 66 | control_go=true; |
Gerth | 1:917c07a4f3ec | 67 | } |
Gerth | 1:917c07a4f3ec | 68 | void filter_activate() |
Gerth | 1:917c07a4f3ec | 69 | { |
Gerth | 1:917c07a4f3ec | 70 | filter_go=true; |
Gerth | 1:917c07a4f3ec | 71 | } |
Gerth | 1:917c07a4f3ec | 72 | void safetyandthreshold_activate() |
Gerth | 1:917c07a4f3ec | 73 | { |
Gerth | 1:917c07a4f3ec | 74 | safetyandthreshold_go=true; |
Gerth | 1:917c07a4f3ec | 75 | } |
Gerth | 0:dd66fff537d7 | 76 | |
Gerth | 1:917c07a4f3ec | 77 | |
Gerth | 1:917c07a4f3ec | 78 | ////////////////////////FUNCTIONS |
Gerth | 1:917c07a4f3ec | 79 | //gather data and send to scope |
Gerth | 1:917c07a4f3ec | 80 | void scopedata() |
Gerth | 1:917c07a4f3ec | 81 | { |
Gerth | 1:917c07a4f3ec | 82 | scope.set(0,encoder1.getPulses()); |
Gerth | 1:917c07a4f3ec | 83 | scope.set(1,encoder2.getPulses()); |
Gerth | 1:917c07a4f3ec | 84 | } |
Gerth | 1:917c07a4f3ec | 85 | //read potmeters and adjust the safetyfactor and threshold |
Gerth | 1:917c07a4f3ec | 86 | void safetyandthreshold() |
Gerth | 1:917c07a4f3ec | 87 | { |
Gerth | 1:917c07a4f3ec | 88 | mycontroller.cutoff((ceil (10*safety.read()) )/10); // adjust the safetyfactor rounded to 1 decimal |
Gerth | 1:917c07a4f3ec | 89 | ///////////////////////NEED AN IDEA FOR THE THRESHOLD!!!!!!!!!!!!!!!!!!!! |
Gerth | 1:917c07a4f3ec | 90 | } |
Gerth | 1:917c07a4f3ec | 91 | /////filter |
Gerth | 1:917c07a4f3ec | 92 | void filtereverything() |
Gerth | 1:917c07a4f3ec | 93 | { |
Gerth | 1:917c07a4f3ec | 94 | //filter_timer.reset(); |
Gerth | 1:917c07a4f3ec | 95 | // filter_timer.start(); |
Gerth | 1:917c07a4f3ec | 96 | //pass1 so f1 |
Gerth | 2:c7707856d137 | 97 | 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 | 98 | 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 | 99 | |
Gerth | 1:917c07a4f3ec | 100 | //pass2 so f2 |
Gerth | 2:c7707856d137 | 101 | 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 | 102 | 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 | 103 | |
Gerth | 1:917c07a4f3ec | 104 | //pass3 so f3 |
Gerth | 2:c7707856d137 | 105 | 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 | 106 | 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 | 107 | |
Gerth | 1:917c07a4f3ec | 108 | //pass4 so f4 |
Gerth | 2:c7707856d137 | 109 | 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 | 110 | 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 | 111 | |
Gerth | 1:917c07a4f3ec | 112 | //pass5 so f5 |
Gerth | 2:c7707856d137 | 113 | 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 | 114 | 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 | 115 | |
Gerth | 1:917c07a4f3ec | 116 | ///// take absolute value |
Gerth | 2:c7707856d137 | 117 | double pass5_emg1_abs=(fabs(pass5_emg1)); |
Gerth | 2:c7707856d137 | 118 | double pass5_emg2_abs=(fabs(pass5_emg2)); |
Gerth | 1:917c07a4f3ec | 119 | |
Gerth | 1:917c07a4f3ec | 120 | //pass6 so f6 |
Gerth | 2:c7707856d137 | 121 | 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 | 122 | 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 | 123 | |
Gerth | 1:917c07a4f3ec | 124 | |
Gerth | 1:917c07a4f3ec | 125 | //pass7 so f7 |
Gerth | 2:c7707856d137 | 126 | 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 | 127 | 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 | 128 | |
Gerth | 2:c7707856d137 | 129 | filteredsignal1=(pass7_emg1*9e11*filter_extragain); |
Gerth | 2:c7707856d137 | 130 | filteredsignal2=(pass7_emg2*9e11*filter_extragain); |
Gerth | 1:917c07a4f3ec | 131 | |
Gerth | 1:917c07a4f3ec | 132 | //filter_timer.stop(); |
Gerth | 1:917c07a4f3ec | 133 | } |
Gerth | 1:917c07a4f3ec | 134 | |
Gerth | 1:917c07a4f3ec | 135 | void control(){ |
Gerth | 1:917c07a4f3ec | 136 | ///call desired controller here |
Gerth | 1:917c07a4f3ec | 137 | } |
Gerth | 1:917c07a4f3ec | 138 | |
Gerth | 1:917c07a4f3ec | 139 | //adjust controller values when sw2 is pressed |
Gerth | 1:917c07a4f3ec | 140 | void valuechange() |
Gerth | 1:917c07a4f3ec | 141 | { |
Gerth | 2:c7707856d137 | 142 | pc.printf("Extra gain is now %f, enter new value\n",filter_extragain); |
Gerth | 2:c7707856d137 | 143 | pc.scanf("%f",&filter_extragain); |
Gerth | 2:c7707856d137 | 144 | |
Gerth | 2:c7707856d137 | 145 | /*mycontroller.STOP(); |
Gerth | 1:917c07a4f3ec | 146 | pc.printf("KP is now %f, enter new value\n",Kp); |
Gerth | 1:917c07a4f3ec | 147 | pc.scanf("%f", &Kp); |
Gerth | 1:917c07a4f3ec | 148 | |
Gerth | 1:917c07a4f3ec | 149 | pc.printf("KI is now %f, enter new value\n",Ki); |
Gerth | 1:917c07a4f3ec | 150 | pc.scanf("%f", &Ki); |
Gerth | 1:917c07a4f3ec | 151 | |
Gerth | 1:917c07a4f3ec | 152 | pc.printf("KD is now %f, enter new value\n",Kd); |
Gerth | 2:c7707856d137 | 153 | pc.scanf("%f", &Kd);*/ |
Gerth | 1:917c07a4f3ec | 154 | } |
Gerth | 0:dd66fff537d7 | 155 | |
Gerth | 0:dd66fff537d7 | 156 | int main() |
Gerth | 0:dd66fff537d7 | 157 | { |
Gerth | 1:917c07a4f3ec | 158 | //tickers |
Gerth | 1:917c07a4f3ec | 159 | safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency); |
Gerth | 1:917c07a4f3ec | 160 | filter_ticker.attach(&filter_activate,1.0/filter_frequency); |
Gerth | 1:917c07a4f3ec | 161 | control_ticker.attach(&control_activate,1.0/control_frequency); |
Gerth | 1:917c07a4f3ec | 162 | scope_ticker.attach(&scopedata_activate,1.0/scope_frequency); |
Gerth | 1:917c07a4f3ec | 163 | |
Gerth | 1:917c07a4f3ec | 164 | while(1) { |
Gerth | 1:917c07a4f3ec | 165 | if (scopedata_go==true) { |
Gerth | 1:917c07a4f3ec | 166 | scopedata(); |
Gerth | 1:917c07a4f3ec | 167 | scopedata_go=false; |
Gerth | 1:917c07a4f3ec | 168 | } |
Gerth | 1:917c07a4f3ec | 169 | if (filter_go==true) { |
Gerth | 1:917c07a4f3ec | 170 | filtereverything(); |
Gerth | 1:917c07a4f3ec | 171 | filter_go=false; |
Gerth | 1:917c07a4f3ec | 172 | } |
Gerth | 1:917c07a4f3ec | 173 | if (safetyandthreshold_go==true) { |
Gerth | 1:917c07a4f3ec | 174 | safetyandthreshold(); |
Gerth | 1:917c07a4f3ec | 175 | safetyandthreshold_go=false; |
Gerth | 1:917c07a4f3ec | 176 | } |
Gerth | 1:917c07a4f3ec | 177 | if (control_go==true) { |
Gerth | 1:917c07a4f3ec | 178 | control(); |
Gerth | 1:917c07a4f3ec | 179 | control_go=false; |
Gerth | 1:917c07a4f3ec | 180 | } |
Gerth | 2:c7707856d137 | 181 | valuechangebutton.fall(&valuechange); |
Gerth | 0:dd66fff537d7 | 182 | } |
Gerth | 0:dd66fff537d7 | 183 | } |