![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The final program for the #include AIR robot
Dependencies: Biquad HIDScope QEI angleandposition controlandadjust mbed
Fork of includeair by
main.cpp@1:917c07a4f3ec, 2015-10-12 (annotated)
- Committer:
- Gerth
- Date:
- Mon Oct 12 14:27:42 2015 +0000
- Revision:
- 1:917c07a4f3ec
- Parent:
- 0:dd66fff537d7
- Child:
- 2:c7707856d137
Added the biquad filter;
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 | 1:917c07a4f3ec | 52 | |
Gerth | 1:917c07a4f3ec | 53 | //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS |
Gerth | 1:917c07a4f3ec | 54 | volatile bool scopedata_go=false, |
Gerth | 1:917c07a4f3ec | 55 | control_go=false, |
Gerth | 1:917c07a4f3ec | 56 | filter_go=false, |
Gerth | 1:917c07a4f3ec | 57 | safetyandthreshold_go=false; |
Gerth | 1:917c07a4f3ec | 58 | |
Gerth | 1:917c07a4f3ec | 59 | void scopedata_activate() |
Gerth | 1:917c07a4f3ec | 60 | { |
Gerth | 1:917c07a4f3ec | 61 | scopedata_go=true; |
Gerth | 1:917c07a4f3ec | 62 | } |
Gerth | 1:917c07a4f3ec | 63 | void control_activate() |
Gerth | 1:917c07a4f3ec | 64 | { |
Gerth | 1:917c07a4f3ec | 65 | control_go=true; |
Gerth | 1:917c07a4f3ec | 66 | } |
Gerth | 1:917c07a4f3ec | 67 | void filter_activate() |
Gerth | 1:917c07a4f3ec | 68 | { |
Gerth | 1:917c07a4f3ec | 69 | filter_go=true; |
Gerth | 1:917c07a4f3ec | 70 | } |
Gerth | 1:917c07a4f3ec | 71 | void safetyandthreshold_activate() |
Gerth | 1:917c07a4f3ec | 72 | { |
Gerth | 1:917c07a4f3ec | 73 | safetyandthreshold_go=true; |
Gerth | 1:917c07a4f3ec | 74 | } |
Gerth | 0:dd66fff537d7 | 75 | |
Gerth | 1:917c07a4f3ec | 76 | |
Gerth | 1:917c07a4f3ec | 77 | ////////////////////////FUNCTIONS |
Gerth | 1:917c07a4f3ec | 78 | //gather data and send to scope |
Gerth | 1:917c07a4f3ec | 79 | void scopedata() |
Gerth | 1:917c07a4f3ec | 80 | { |
Gerth | 1:917c07a4f3ec | 81 | scope.set(0,encoder1.getPulses()); |
Gerth | 1:917c07a4f3ec | 82 | scope.set(1,encoder2.getPulses()); |
Gerth | 1:917c07a4f3ec | 83 | } |
Gerth | 1:917c07a4f3ec | 84 | //read potmeters and adjust the safetyfactor and threshold |
Gerth | 1:917c07a4f3ec | 85 | void safetyandthreshold() |
Gerth | 1:917c07a4f3ec | 86 | { |
Gerth | 1:917c07a4f3ec | 87 | mycontroller.cutoff((ceil (10*safety.read()) )/10); // adjust the safetyfactor rounded to 1 decimal |
Gerth | 1:917c07a4f3ec | 88 | ///////////////////////NEED AN IDEA FOR THE THRESHOLD!!!!!!!!!!!!!!!!!!!! |
Gerth | 1:917c07a4f3ec | 89 | } |
Gerth | 1:917c07a4f3ec | 90 | /////filter |
Gerth | 1:917c07a4f3ec | 91 | void filtereverything() |
Gerth | 1:917c07a4f3ec | 92 | { |
Gerth | 1:917c07a4f3ec | 93 | //filter_timer.reset(); |
Gerth | 1:917c07a4f3ec | 94 | // filter_timer.start(); |
Gerth | 1:917c07a4f3ec | 95 | //pass1 so f1 |
Gerth | 1:917c07a4f3ec | 96 | float pass1_emg1 = myfilter1.filter(emg1_input.read(), v1_f1_emg1 , v2_f1_emg1 , a1_f1 , a2_f1 , b0_f1 , b1_f1 , b2_f1); |
Gerth | 1:917c07a4f3ec | 97 | float 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 | 98 | |
Gerth | 1:917c07a4f3ec | 99 | //pass2 so f2 |
Gerth | 1:917c07a4f3ec | 100 | float pass2_emg1 = myfilter1.filter(pass1_emg1, v1_f2_emg1 , v2_f2_emg1 , a1_f2 , a2_f2 , b0_f2 , b1_f2 , b2_f2); |
Gerth | 1:917c07a4f3ec | 101 | float 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 | 102 | |
Gerth | 1:917c07a4f3ec | 103 | //pass3 so f3 |
Gerth | 1:917c07a4f3ec | 104 | float pass3_emg1 = myfilter1.filter(pass2_emg1, v1_f3_emg1 , v2_f3_emg1 , a1_f3 , a2_f3 , b0_f3 , b1_f3 , b2_f3); |
Gerth | 1:917c07a4f3ec | 105 | float 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 | 106 | |
Gerth | 1:917c07a4f3ec | 107 | //pass4 so f4 |
Gerth | 1:917c07a4f3ec | 108 | float pass4_emg1 = myfilter1.filter(pass3_emg1, v1_f4_emg1 , v2_f4_emg1 , a1_f4 , a2_f4 , b0_f4 , b1_f4 , b2_f4); |
Gerth | 1:917c07a4f3ec | 109 | float 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 | 110 | |
Gerth | 1:917c07a4f3ec | 111 | //pass5 so f5 |
Gerth | 1:917c07a4f3ec | 112 | float pass5_emg1 = myfilter1.filter(pass4_emg1, v1_f5_emg1 , v2_f5_emg1 , a1_f5 , a2_f5 , b0_f5 , b1_f5 , b2_f5); |
Gerth | 1:917c07a4f3ec | 113 | float 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 | 114 | |
Gerth | 1:917c07a4f3ec | 115 | ///// take absolute value |
Gerth | 1:917c07a4f3ec | 116 | float pass5_emg1_abs=(fabs(pass5_emg1)); |
Gerth | 1:917c07a4f3ec | 117 | float pass5_emg2_abs=(fabs(pass5_emg2)); |
Gerth | 1:917c07a4f3ec | 118 | |
Gerth | 1:917c07a4f3ec | 119 | //pass6 so f6 |
Gerth | 1:917c07a4f3ec | 120 | float pass6_emg1 = myfilter1.filter(pass5_emg1_abs, v1_f6_emg1 , v2_f6_emg1 , a1_f6 , a2_f6 , b0_f6 , b1_f6 , b2_f6); |
Gerth | 1:917c07a4f3ec | 121 | float 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 | 122 | |
Gerth | 1:917c07a4f3ec | 123 | |
Gerth | 1:917c07a4f3ec | 124 | //pass7 so f7 |
Gerth | 1:917c07a4f3ec | 125 | float pass7_emg1 = myfilter1.filter(pass6_emg1, v1_f7_emg1 , v2_f7_emg1 , a1_f7 , a2_f7 , b0_f7 , b1_f7 , b2_f7); |
Gerth | 1:917c07a4f3ec | 126 | float 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 | 127 | |
Gerth | 1:917c07a4f3ec | 128 | filteredsignal1=(pass7_emg1*10000000); |
Gerth | 1:917c07a4f3ec | 129 | filteredsignal2=(pass7_emg2*10000000); |
Gerth | 1:917c07a4f3ec | 130 | |
Gerth | 1:917c07a4f3ec | 131 | //filter_timer.stop(); |
Gerth | 1:917c07a4f3ec | 132 | } |
Gerth | 1:917c07a4f3ec | 133 | |
Gerth | 1:917c07a4f3ec | 134 | void control(){ |
Gerth | 1:917c07a4f3ec | 135 | ///call desired controller here |
Gerth | 1:917c07a4f3ec | 136 | } |
Gerth | 1:917c07a4f3ec | 137 | |
Gerth | 1:917c07a4f3ec | 138 | //adjust controller values when sw2 is pressed |
Gerth | 1:917c07a4f3ec | 139 | void valuechange() |
Gerth | 1:917c07a4f3ec | 140 | { |
Gerth | 1:917c07a4f3ec | 141 | mycontroller.STOP(); |
Gerth | 1:917c07a4f3ec | 142 | pc.printf("KP is now %f, enter new value\n",Kp); |
Gerth | 1:917c07a4f3ec | 143 | pc.scanf("%f", &Kp); |
Gerth | 1:917c07a4f3ec | 144 | |
Gerth | 1:917c07a4f3ec | 145 | pc.printf("KI is now %f, enter new value\n",Ki); |
Gerth | 1:917c07a4f3ec | 146 | pc.scanf("%f", &Ki); |
Gerth | 1:917c07a4f3ec | 147 | |
Gerth | 1:917c07a4f3ec | 148 | pc.printf("KD is now %f, enter new value\n",Kd); |
Gerth | 1:917c07a4f3ec | 149 | pc.scanf("%f", &Kd); |
Gerth | 1:917c07a4f3ec | 150 | } |
Gerth | 0:dd66fff537d7 | 151 | |
Gerth | 0:dd66fff537d7 | 152 | int main() |
Gerth | 0:dd66fff537d7 | 153 | { |
Gerth | 1:917c07a4f3ec | 154 | //tickers |
Gerth | 1:917c07a4f3ec | 155 | safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency); |
Gerth | 1:917c07a4f3ec | 156 | filter_ticker.attach(&filter_activate,1.0/filter_frequency); |
Gerth | 1:917c07a4f3ec | 157 | control_ticker.attach(&control_activate,1.0/control_frequency); |
Gerth | 1:917c07a4f3ec | 158 | scope_ticker.attach(&scopedata_activate,1.0/scope_frequency); |
Gerth | 1:917c07a4f3ec | 159 | |
Gerth | 1:917c07a4f3ec | 160 | while(1) { |
Gerth | 1:917c07a4f3ec | 161 | if (scopedata_go==true) { |
Gerth | 1:917c07a4f3ec | 162 | scopedata(); |
Gerth | 1:917c07a4f3ec | 163 | scopedata_go=false; |
Gerth | 1:917c07a4f3ec | 164 | } |
Gerth | 1:917c07a4f3ec | 165 | if (filter_go==true) { |
Gerth | 1:917c07a4f3ec | 166 | filtereverything(); |
Gerth | 1:917c07a4f3ec | 167 | filter_go=false; |
Gerth | 1:917c07a4f3ec | 168 | } |
Gerth | 1:917c07a4f3ec | 169 | if (safetyandthreshold_go==true) { |
Gerth | 1:917c07a4f3ec | 170 | safetyandthreshold(); |
Gerth | 1:917c07a4f3ec | 171 | safetyandthreshold_go=false; |
Gerth | 1:917c07a4f3ec | 172 | } |
Gerth | 1:917c07a4f3ec | 173 | if (control_go==true) { |
Gerth | 1:917c07a4f3ec | 174 | control(); |
Gerth | 1:917c07a4f3ec | 175 | control_go=false; |
Gerth | 1:917c07a4f3ec | 176 | } |
Gerth | 1:917c07a4f3ec | 177 | if (valuechangebutton==0) { |
Gerth | 1:917c07a4f3ec | 178 | valuechange(); |
Gerth | 1:917c07a4f3ec | 179 | } |
Gerth | 0:dd66fff537d7 | 180 | } |
Gerth | 0:dd66fff537d7 | 181 | } |