cafads
Dependencies: mbed
Fork of Speed by
main.cpp@2:12c55b0c9cc2, 2016-05-28 (annotated)
- Committer:
- roger5641
- Date:
- Sat May 28 15:54:48 2016 +0000
- Revision:
- 2:12c55b0c9cc2
- Parent:
- 1:571a7ee6023d
- Child:
- 3:f30bd4a0d761
servo;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
smilestone520 | 0:51ebb68301ec | 1 | /*DCMotor*/ |
smilestone520 | 0:51ebb68301ec | 2 | // transfer function 10400/s+21.28 |
smilestone520 | 0:51ebb68301ec | 3 | // motor direction v1(vR) < 0 |
smilestone520 | 0:51ebb68301ec | 4 | // motor direction v2(vL) > 0 |
smilestone520 | 0:51ebb68301ec | 5 | // speed limitatino 300 rpm |
smilestone520 | 0:51ebb68301ec | 6 | |
smilestone520 | 0:51ebb68301ec | 7 | #include "mbed.h" |
smilestone520 | 0:51ebb68301ec | 8 | |
smilestone520 | 0:51ebb68301ec | 9 | //The number will be compiled as type "double" in default |
smilestone520 | 0:51ebb68301ec | 10 | //Add a "f" after the number can make it compiled as type "float" |
smilestone520 | 0:51ebb68301ec | 11 | #define Ts 0.02f //period of timer1 (s) |
smilestone520 | 0:51ebb68301ec | 12 | #define Kp1 0.0048f //0.0048f |
smilestone520 | 0:51ebb68301ec | 13 | #define Kp2 0.0048f //0.0048f |
smilestone520 | 0:51ebb68301ec | 14 | #define Ki 0.002754f //0.1023f |
smilestone520 | 0:51ebb68301ec | 15 | |
smilestone520 | 0:51ebb68301ec | 16 | Serial bluetooth(D10,D2); //宣告藍牙腳位 |
smilestone520 | 1:571a7ee6023d | 17 | //Serial pc(D1, D0); |
smilestone520 | 1:571a7ee6023d | 18 | PwmOut servo(A0); |
smilestone520 | 0:51ebb68301ec | 19 | PwmOut pwm1(D7); |
smilestone520 | 0:51ebb68301ec | 20 | PwmOut pwm1n(D11); |
smilestone520 | 0:51ebb68301ec | 21 | PwmOut pwm2(D8); |
smilestone520 | 0:51ebb68301ec | 22 | PwmOut pwm2n(A3); |
smilestone520 | 0:51ebb68301ec | 23 | |
smilestone520 | 0:51ebb68301ec | 24 | DigitalOut led1(A4); |
smilestone520 | 0:51ebb68301ec | 25 | DigitalOut led2(A5); |
smilestone520 | 0:51ebb68301ec | 26 | |
smilestone520 | 0:51ebb68301ec | 27 | //Motor1 sensor |
smilestone520 | 0:51ebb68301ec | 28 | InterruptIn HallA_1(A1); |
smilestone520 | 0:51ebb68301ec | 29 | InterruptIn HallB_1(A2); |
smilestone520 | 0:51ebb68301ec | 30 | //Motor2 sensor |
smilestone520 | 0:51ebb68301ec | 31 | InterruptIn HallA_2(D13); |
smilestone520 | 0:51ebb68301ec | 32 | InterruptIn HallB_2(D12); |
smilestone520 | 0:51ebb68301ec | 33 | |
smilestone520 | 0:51ebb68301ec | 34 | |
smilestone520 | 0:51ebb68301ec | 35 | Ticker timer1; |
smilestone520 | 0:51ebb68301ec | 36 | void timer1_interrupt(void); |
smilestone520 | 0:51ebb68301ec | 37 | int timer1_counter; |
smilestone520 | 0:51ebb68301ec | 38 | |
smilestone520 | 0:51ebb68301ec | 39 | void CN_interrupt(void); |
smilestone520 | 0:51ebb68301ec | 40 | |
smilestone520 | 0:51ebb68301ec | 41 | void init_TIMER(void); |
smilestone520 | 0:51ebb68301ec | 42 | void init_PWM(void); |
smilestone520 | 0:51ebb68301ec | 43 | void init_CN(void); |
smilestone520 | 0:51ebb68301ec | 44 | void init_BLUETOOTH(void); |
smilestone520 | 0:51ebb68301ec | 45 | |
smilestone520 | 0:51ebb68301ec | 46 | int8_t stateA_1=0, stateB_1=0, stateA_2=0, stateB_2=0; |
smilestone520 | 0:51ebb68301ec | 47 | int8_t state_1 = 0, state_1_old = 0, state_2 = 0, state_2_old = 0; |
smilestone520 | 0:51ebb68301ec | 48 | |
smilestone520 | 0:51ebb68301ec | 49 | int v1Count = 0; |
smilestone520 | 0:51ebb68301ec | 50 | int v2Count = 0; |
smilestone520 | 0:51ebb68301ec | 51 | |
smilestone520 | 0:51ebb68301ec | 52 | float v1 = 0.0, v1_ref = 0.0; |
smilestone520 | 0:51ebb68301ec | 53 | float v1_err = 0.0, v1_err_old = 0.0, PIout_1 = 0.0; |
smilestone520 | 0:51ebb68301ec | 54 | float v1_old[10] = {}, v1_avg = 0.0; |
smilestone520 | 0:51ebb68301ec | 55 | float v2 = 0.0, v2_ref = 0.0; |
smilestone520 | 0:51ebb68301ec | 56 | float v2_err = 0.0, v2_err_old = 0.0, PIout_2 = 0.0; |
smilestone520 | 0:51ebb68301ec | 57 | float v2_old[10] = {}, v2_avg = 0.0; |
smilestone520 | 1:571a7ee6023d | 58 | |
smilestone520 | 1:571a7ee6023d | 59 | int servo_angle = 87; |
smilestone520 | 1:571a7ee6023d | 60 | float servo_duty = 0.079;//0.079 +(0.084/180)*angle, -90<angle<90 |
smilestone520 | 1:571a7ee6023d | 61 | |
smilestone520 | 1:571a7ee6023d | 62 | char Receive_Data[8] = {}; |
smilestone520 | 1:571a7ee6023d | 63 | |
smilestone520 | 0:51ebb68301ec | 64 | int main() { |
roger5641 | 2:12c55b0c9cc2 | 65 | |
smilestone520 | 0:51ebb68301ec | 66 | init_BLUETOOTH(); |
smilestone520 | 0:51ebb68301ec | 67 | init_TIMER(); |
smilestone520 | 0:51ebb68301ec | 68 | init_PWM(); |
smilestone520 | 0:51ebb68301ec | 69 | init_CN(); |
smilestone520 | 0:51ebb68301ec | 70 | |
smilestone520 | 0:51ebb68301ec | 71 | v1_ref = 0.0; |
smilestone520 | 1:571a7ee6023d | 72 | v2_ref = 0.0; |
smilestone520 | 1:571a7ee6023d | 73 | |
smilestone520 | 1:571a7ee6023d | 74 | //bluetooth.baud(115200); //設定鮑率 |
smilestone520 | 1:571a7ee6023d | 75 | //pc.baud(57600); |
smilestone520 | 0:51ebb68301ec | 76 | |
smilestone520 | 0:51ebb68301ec | 77 | while(1) |
smilestone520 | 0:51ebb68301ec | 78 | { |
smilestone520 | 1:571a7ee6023d | 79 | if(bluetooth.readable()) |
smilestone520 | 0:51ebb68301ec | 80 | { |
roger5641 | 2:12c55b0c9cc2 | 81 | bluetooth.scanf("%f%f%d",&v1_ref,&v2_ref,&servo_angle); |
smilestone520 | 1:571a7ee6023d | 82 | v1_ref = v1_ref - 300.0f; |
smilestone520 | 0:51ebb68301ec | 83 | v2_ref = v2_ref - 300.0f; |
roger5641 | 2:12c55b0c9cc2 | 84 | servo_angle = servo_angle - 20; |
smilestone520 | 0:51ebb68301ec | 85 | } |
smilestone520 | 1:571a7ee6023d | 86 | /*if(pc.readable()) |
smilestone520 | 1:571a7ee6023d | 87 | { |
smilestone520 | 1:571a7ee6023d | 88 | bluetooth.putc(pc.getc()); |
smilestone520 | 1:571a7ee6023d | 89 | } |
smilestone520 | 1:571a7ee6023d | 90 | if(bluetooth.readable()) |
smilestone520 | 1:571a7ee6023d | 91 | { |
smilestone520 | 1:571a7ee6023d | 92 | pc.putc(bluetooth.getc()); |
smilestone520 | 1:571a7ee6023d | 93 | }*/ |
smilestone520 | 0:51ebb68301ec | 94 | } |
smilestone520 | 0:51ebb68301ec | 95 | } |
smilestone520 | 0:51ebb68301ec | 96 | |
smilestone520 | 0:51ebb68301ec | 97 | void timer1_interrupt(void) |
smilestone520 | 0:51ebb68301ec | 98 | { |
smilestone520 | 0:51ebb68301ec | 99 | //Motor 1 |
smilestone520 | 0:51ebb68301ec | 100 | v1 = (float)v1Count * 50.0f / 12.0f * 60.0f / 29.0f; //unit: rpm |
smilestone520 | 0:51ebb68301ec | 101 | v1_avg = v1_avg + ( v1 - v1_old[9])/10.0f; |
smilestone520 | 0:51ebb68301ec | 102 | for(int i = 9; i > 0 ; i--) |
smilestone520 | 0:51ebb68301ec | 103 | { |
smilestone520 | 0:51ebb68301ec | 104 | v1_old[i] = v1_old[i-1]; |
smilestone520 | 0:51ebb68301ec | 105 | } |
smilestone520 | 0:51ebb68301ec | 106 | v1_old[0] = v1; |
smilestone520 | 0:51ebb68301ec | 107 | v1Count = 0; |
smilestone520 | 0:51ebb68301ec | 108 | |
smilestone520 | 0:51ebb68301ec | 109 | ///code for PI control/// |
smilestone520 | 0:51ebb68301ec | 110 | v1_err = v1_ref - v1; |
smilestone520 | 0:51ebb68301ec | 111 | |
smilestone520 | 0:51ebb68301ec | 112 | PIout_1 = PIout_1 + Kp1 * v1_err - Ki * v1_err_old; |
smilestone520 | 0:51ebb68301ec | 113 | v1_err_old = v1_err; |
smilestone520 | 0:51ebb68301ec | 114 | |
smilestone520 | 0:51ebb68301ec | 115 | // saturation |
smilestone520 | 0:51ebb68301ec | 116 | if(PIout_1 >= 0.5f)PIout_1 = 0.5f; |
smilestone520 | 0:51ebb68301ec | 117 | else if(PIout_1 <= -0.5f)PIout_1 = -0.5f; |
smilestone520 | 0:51ebb68301ec | 118 | pwm1.write(PIout_1 + 0.5f); |
smilestone520 | 0:51ebb68301ec | 119 | TIM1->CCER |= 0x4; |
smilestone520 | 0:51ebb68301ec | 120 | |
smilestone520 | 0:51ebb68301ec | 121 | //Motor 2 |
smilestone520 | 0:51ebb68301ec | 122 | v2 = (float)v2Count * 50.0f / 12.0f * 60.0f / 29.0f; //unit: rpm |
smilestone520 | 0:51ebb68301ec | 123 | v2_avg = v2_avg + ( v2 - v2_old[9])/10.0f; |
smilestone520 | 0:51ebb68301ec | 124 | for(int i = 9; i > 0; i--) |
smilestone520 | 0:51ebb68301ec | 125 | { |
smilestone520 | 0:51ebb68301ec | 126 | v2_old[i] = v2_old[i-1]; |
smilestone520 | 0:51ebb68301ec | 127 | } |
smilestone520 | 0:51ebb68301ec | 128 | v2_old[0] = v2; |
smilestone520 | 0:51ebb68301ec | 129 | v2Count = 0; |
smilestone520 | 0:51ebb68301ec | 130 | |
smilestone520 | 0:51ebb68301ec | 131 | ///code for PI control/// |
smilestone520 | 0:51ebb68301ec | 132 | v2_err = v2_ref - v2; |
smilestone520 | 0:51ebb68301ec | 133 | |
smilestone520 | 0:51ebb68301ec | 134 | PIout_2 = PIout_2 + Kp2 * v2_err - Ki * v2_err_old; |
smilestone520 | 0:51ebb68301ec | 135 | v2_err_old = v2_err; |
smilestone520 | 0:51ebb68301ec | 136 | |
smilestone520 | 0:51ebb68301ec | 137 | //saturation |
smilestone520 | 0:51ebb68301ec | 138 | if(PIout_2 >= 0.5f)PIout_2 = 0.5f; |
smilestone520 | 0:51ebb68301ec | 139 | else if(PIout_2 <= -0.5f)PIout_2 = -0.5f; |
smilestone520 | 0:51ebb68301ec | 140 | pwm2.write(PIout_2 + 0.5f); |
smilestone520 | 0:51ebb68301ec | 141 | TIM1->CCER |= 0x40; |
smilestone520 | 0:51ebb68301ec | 142 | |
smilestone520 | 0:51ebb68301ec | 143 | timer1_counter ++; |
smilestone520 | 0:51ebb68301ec | 144 | if (timer1_counter == 50) |
smilestone520 | 0:51ebb68301ec | 145 | { |
smilestone520 | 0:51ebb68301ec | 146 | timer1_counter = 0; |
smilestone520 | 0:51ebb68301ec | 147 | if(bluetooth.writeable()) |
smilestone520 | 0:51ebb68301ec | 148 | { |
smilestone520 | 0:51ebb68301ec | 149 | bluetooth.printf("V1: %4.2f V2: %4.2f\n",v1_avg,v2_avg); |
smilestone520 | 0:51ebb68301ec | 150 | } |
smilestone520 | 0:51ebb68301ec | 151 | } |
smilestone520 | 1:571a7ee6023d | 152 | |
roger5641 | 2:12c55b0c9cc2 | 153 | servo_duty = 0.079 + (0.084/180)*servo_angle; // 要修改 |
smilestone520 | 1:571a7ee6023d | 154 | servo.write(servo_duty); |
roger5641 | 2:12c55b0c9cc2 | 155 | //servo = 1; |
roger5641 | 2:12c55b0c9cc2 | 156 | //wait(0.1); |
roger5641 | 2:12c55b0c9cc2 | 157 | //servo = 0; |
roger5641 | 2:12c55b0c9cc2 | 158 | |
smilestone520 | 0:51ebb68301ec | 159 | } |
smilestone520 | 0:51ebb68301ec | 160 | |
smilestone520 | 0:51ebb68301ec | 161 | void CN_interrupt(void) |
smilestone520 | 0:51ebb68301ec | 162 | { |
smilestone520 | 0:51ebb68301ec | 163 | //Motor 1 |
smilestone520 | 0:51ebb68301ec | 164 | stateA_1 = HallA_1.read(); |
smilestone520 | 0:51ebb68301ec | 165 | stateB_1 = HallB_1.read(); |
smilestone520 | 0:51ebb68301ec | 166 | |
smilestone520 | 0:51ebb68301ec | 167 | ///code for state determination/// |
smilestone520 | 0:51ebb68301ec | 168 | if (stateA_1 == 0) |
smilestone520 | 0:51ebb68301ec | 169 | { |
smilestone520 | 0:51ebb68301ec | 170 | if (stateB_1 == 0) |
smilestone520 | 0:51ebb68301ec | 171 | state_1 = 0; |
smilestone520 | 0:51ebb68301ec | 172 | else |
smilestone520 | 0:51ebb68301ec | 173 | state_1 = 1; |
smilestone520 | 0:51ebb68301ec | 174 | } |
smilestone520 | 0:51ebb68301ec | 175 | else |
smilestone520 | 0:51ebb68301ec | 176 | { |
smilestone520 | 0:51ebb68301ec | 177 | if (stateB_1 == 1) |
smilestone520 | 0:51ebb68301ec | 178 | state_1 = 2; |
smilestone520 | 0:51ebb68301ec | 179 | else |
smilestone520 | 0:51ebb68301ec | 180 | state_1 = 3; |
smilestone520 | 0:51ebb68301ec | 181 | } |
smilestone520 | 0:51ebb68301ec | 182 | |
smilestone520 | 0:51ebb68301ec | 183 | //Forward: v1Count +1 |
smilestone520 | 0:51ebb68301ec | 184 | //Inverse: v1Count -1 |
smilestone520 | 0:51ebb68301ec | 185 | if ( (state_1 == (state_1_old + 1)) || (state_1 == 0 && state_1_old == 3) ) |
smilestone520 | 0:51ebb68301ec | 186 | v1Count++; |
smilestone520 | 0:51ebb68301ec | 187 | else if ( (state_1 == (state_1_old - 1)) || (state_1 == 3 && state_1_old == 0)) |
smilestone520 | 0:51ebb68301ec | 188 | v1Count--; |
smilestone520 | 0:51ebb68301ec | 189 | |
smilestone520 | 0:51ebb68301ec | 190 | state_1_old = state_1; |
smilestone520 | 0:51ebb68301ec | 191 | |
smilestone520 | 0:51ebb68301ec | 192 | //Motor 2 |
smilestone520 | 0:51ebb68301ec | 193 | stateA_2 = HallA_2.read(); |
smilestone520 | 0:51ebb68301ec | 194 | stateB_2 = HallB_2.read(); |
smilestone520 | 0:51ebb68301ec | 195 | |
smilestone520 | 0:51ebb68301ec | 196 | ///code for state determination/// |
smilestone520 | 0:51ebb68301ec | 197 | if (stateA_2 == 0) |
smilestone520 | 0:51ebb68301ec | 198 | { |
smilestone520 | 0:51ebb68301ec | 199 | if (stateB_2 == 0) |
smilestone520 | 0:51ebb68301ec | 200 | state_2 = 0; |
smilestone520 | 0:51ebb68301ec | 201 | else |
smilestone520 | 0:51ebb68301ec | 202 | state_2 = 1; |
smilestone520 | 0:51ebb68301ec | 203 | } |
smilestone520 | 0:51ebb68301ec | 204 | else |
smilestone520 | 0:51ebb68301ec | 205 | { |
smilestone520 | 0:51ebb68301ec | 206 | if (stateB_2 == 1) |
smilestone520 | 0:51ebb68301ec | 207 | state_2 = 2; |
smilestone520 | 0:51ebb68301ec | 208 | else |
smilestone520 | 0:51ebb68301ec | 209 | state_2 = 3; |
smilestone520 | 0:51ebb68301ec | 210 | } |
smilestone520 | 0:51ebb68301ec | 211 | |
smilestone520 | 0:51ebb68301ec | 212 | //Forward: v2Count +1 |
smilestone520 | 0:51ebb68301ec | 213 | //Inverse: v2Count -1 |
smilestone520 | 0:51ebb68301ec | 214 | if ( (state_2 == (state_2_old + 1)) || (state_2 == 0 && state_2_old == 3) ) |
smilestone520 | 0:51ebb68301ec | 215 | v2Count++; |
smilestone520 | 0:51ebb68301ec | 216 | else if ( (state_2 == (state_2_old - 1)) || (state_2 == 3 && state_2_old == 0) ) |
smilestone520 | 0:51ebb68301ec | 217 | v2Count--; |
smilestone520 | 0:51ebb68301ec | 218 | |
smilestone520 | 0:51ebb68301ec | 219 | state_2_old = state_2; |
smilestone520 | 1:571a7ee6023d | 220 | |
smilestone520 | 1:571a7ee6023d | 221 | |
smilestone520 | 0:51ebb68301ec | 222 | } |
smilestone520 | 0:51ebb68301ec | 223 | |
smilestone520 | 0:51ebb68301ec | 224 | void init_TIMER(void) |
smilestone520 | 0:51ebb68301ec | 225 | { |
smilestone520 | 0:51ebb68301ec | 226 | timer1.attach_us(&timer1_interrupt, 20000);//10ms interrupt period (100 Hz) |
smilestone520 | 0:51ebb68301ec | 227 | timer1_counter = 0; |
smilestone520 | 0:51ebb68301ec | 228 | } |
smilestone520 | 0:51ebb68301ec | 229 | |
smilestone520 | 0:51ebb68301ec | 230 | void init_PWM(void) |
smilestone520 | 0:51ebb68301ec | 231 | { |
smilestone520 | 0:51ebb68301ec | 232 | pwm1.period_us(50); |
smilestone520 | 0:51ebb68301ec | 233 | pwm1.write(0.5); |
smilestone520 | 0:51ebb68301ec | 234 | TIM1->CCER |= 0x4; |
smilestone520 | 0:51ebb68301ec | 235 | |
smilestone520 | 0:51ebb68301ec | 236 | pwm2.period_us(50); |
smilestone520 | 0:51ebb68301ec | 237 | pwm2.write(0.5); |
smilestone520 | 0:51ebb68301ec | 238 | TIM1->CCER |= 0x40; |
smilestone520 | 0:51ebb68301ec | 239 | } |
smilestone520 | 0:51ebb68301ec | 240 | |
smilestone520 | 0:51ebb68301ec | 241 | void init_CN(void) |
smilestone520 | 0:51ebb68301ec | 242 | { |
smilestone520 | 0:51ebb68301ec | 243 | HallA_1.rise(&CN_interrupt); |
smilestone520 | 0:51ebb68301ec | 244 | HallA_1.fall(&CN_interrupt); |
smilestone520 | 0:51ebb68301ec | 245 | HallB_1.rise(&CN_interrupt); |
smilestone520 | 0:51ebb68301ec | 246 | HallB_1.fall(&CN_interrupt); |
smilestone520 | 0:51ebb68301ec | 247 | |
smilestone520 | 0:51ebb68301ec | 248 | HallA_2.rise(&CN_interrupt); |
smilestone520 | 0:51ebb68301ec | 249 | HallA_2.fall(&CN_interrupt); |
smilestone520 | 0:51ebb68301ec | 250 | HallB_2.rise(&CN_interrupt); |
smilestone520 | 0:51ebb68301ec | 251 | HallB_2.fall(&CN_interrupt); |
smilestone520 | 0:51ebb68301ec | 252 | |
smilestone520 | 0:51ebb68301ec | 253 | stateA_1 = HallA_1.read(); |
smilestone520 | 0:51ebb68301ec | 254 | stateB_1 = HallB_1.read(); |
smilestone520 | 0:51ebb68301ec | 255 | stateA_2 = HallA_2.read(); |
smilestone520 | 0:51ebb68301ec | 256 | stateB_2 = HallB_2.read(); |
smilestone520 | 0:51ebb68301ec | 257 | } |
smilestone520 | 0:51ebb68301ec | 258 | void init_BLUETOOTH(void) |
smilestone520 | 0:51ebb68301ec | 259 | { |
smilestone520 | 0:51ebb68301ec | 260 | bluetooth.baud(115200); |
smilestone520 | 1:571a7ee6023d | 261 | } |