cafads

Dependencies:   mbed

Fork of Speed by JhengYing Wu

Committer:
smilestone520
Date:
Fri May 27 00:42:44 2016 +0000
Revision:
0:51ebb68301ec
Child:
1:571a7ee6023d
hahaha

Who changed what in which revision?

UserRevisionLine numberNew 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 0:51ebb68301ec 17
smilestone520 0:51ebb68301ec 18 PwmOut pwm1(D7);
smilestone520 0:51ebb68301ec 19 PwmOut pwm1n(D11);
smilestone520 0:51ebb68301ec 20 PwmOut pwm2(D8);
smilestone520 0:51ebb68301ec 21 PwmOut pwm2n(A3);
smilestone520 0:51ebb68301ec 22
smilestone520 0:51ebb68301ec 23 DigitalOut led1(A4);
smilestone520 0:51ebb68301ec 24 DigitalOut led2(A5);
smilestone520 0:51ebb68301ec 25
smilestone520 0:51ebb68301ec 26 //Motor1 sensor
smilestone520 0:51ebb68301ec 27 InterruptIn HallA_1(A1);
smilestone520 0:51ebb68301ec 28 InterruptIn HallB_1(A2);
smilestone520 0:51ebb68301ec 29 //Motor2 sensor
smilestone520 0:51ebb68301ec 30 InterruptIn HallA_2(D13);
smilestone520 0:51ebb68301ec 31 InterruptIn HallB_2(D12);
smilestone520 0:51ebb68301ec 32
smilestone520 0:51ebb68301ec 33
smilestone520 0:51ebb68301ec 34 Ticker timer1;
smilestone520 0:51ebb68301ec 35 void timer1_interrupt(void);
smilestone520 0:51ebb68301ec 36 int timer1_counter;
smilestone520 0:51ebb68301ec 37
smilestone520 0:51ebb68301ec 38 void CN_interrupt(void);
smilestone520 0:51ebb68301ec 39
smilestone520 0:51ebb68301ec 40 void init_TIMER(void);
smilestone520 0:51ebb68301ec 41 void init_PWM(void);
smilestone520 0:51ebb68301ec 42 void init_CN(void);
smilestone520 0:51ebb68301ec 43 void init_BLUETOOTH(void);
smilestone520 0:51ebb68301ec 44
smilestone520 0:51ebb68301ec 45 int8_t stateA_1=0, stateB_1=0, stateA_2=0, stateB_2=0;
smilestone520 0:51ebb68301ec 46 int8_t state_1 = 0, state_1_old = 0, state_2 = 0, state_2_old = 0;
smilestone520 0:51ebb68301ec 47
smilestone520 0:51ebb68301ec 48 int v1Count = 0;
smilestone520 0:51ebb68301ec 49 int v2Count = 0;
smilestone520 0:51ebb68301ec 50
smilestone520 0:51ebb68301ec 51 float v1 = 0.0, v1_ref = 0.0;
smilestone520 0:51ebb68301ec 52 float v1_err = 0.0, v1_err_old = 0.0, PIout_1 = 0.0;
smilestone520 0:51ebb68301ec 53 float v1_old[10] = {}, v1_avg = 0.0;
smilestone520 0:51ebb68301ec 54 float v2 = 0.0, v2_ref = 0.0;
smilestone520 0:51ebb68301ec 55 float v2_err = 0.0, v2_err_old = 0.0, PIout_2 = 0.0;
smilestone520 0:51ebb68301ec 56 float v2_old[10] = {}, v2_avg = 0.0;
smilestone520 0:51ebb68301ec 57 int main() {
smilestone520 0:51ebb68301ec 58
smilestone520 0:51ebb68301ec 59 init_BLUETOOTH();
smilestone520 0:51ebb68301ec 60 init_TIMER();
smilestone520 0:51ebb68301ec 61 init_PWM();
smilestone520 0:51ebb68301ec 62 init_CN();
smilestone520 0:51ebb68301ec 63
smilestone520 0:51ebb68301ec 64 v1_ref = 0.0;
smilestone520 0:51ebb68301ec 65 v2_ref = 0.0;
smilestone520 0:51ebb68301ec 66
smilestone520 0:51ebb68301ec 67 while(1)
smilestone520 0:51ebb68301ec 68 {
smilestone520 0:51ebb68301ec 69 if(bluetooth.readable())
smilestone520 0:51ebb68301ec 70 {
smilestone520 0:51ebb68301ec 71 bluetooth.scanf("%f%f",&v1_ref,&v2_ref);
smilestone520 0:51ebb68301ec 72 v1_ref = -v1_ref + 300.0f;
smilestone520 0:51ebb68301ec 73 v2_ref = v2_ref - 300.0f;
smilestone520 0:51ebb68301ec 74 }
smilestone520 0:51ebb68301ec 75 }
smilestone520 0:51ebb68301ec 76 }
smilestone520 0:51ebb68301ec 77
smilestone520 0:51ebb68301ec 78 void timer1_interrupt(void)
smilestone520 0:51ebb68301ec 79 {
smilestone520 0:51ebb68301ec 80 //Motor 1
smilestone520 0:51ebb68301ec 81 v1 = (float)v1Count * 50.0f / 12.0f * 60.0f / 29.0f; //unit: rpm
smilestone520 0:51ebb68301ec 82 v1_avg = v1_avg + ( v1 - v1_old[9])/10.0f;
smilestone520 0:51ebb68301ec 83 for(int i = 9; i > 0 ; i--)
smilestone520 0:51ebb68301ec 84 {
smilestone520 0:51ebb68301ec 85 v1_old[i] = v1_old[i-1];
smilestone520 0:51ebb68301ec 86 }
smilestone520 0:51ebb68301ec 87 v1_old[0] = v1;
smilestone520 0:51ebb68301ec 88 v1Count = 0;
smilestone520 0:51ebb68301ec 89
smilestone520 0:51ebb68301ec 90 ///code for PI control///
smilestone520 0:51ebb68301ec 91 v1_err = v1_ref - v1;
smilestone520 0:51ebb68301ec 92
smilestone520 0:51ebb68301ec 93 PIout_1 = PIout_1 + Kp1 * v1_err - Ki * v1_err_old;
smilestone520 0:51ebb68301ec 94 v1_err_old = v1_err;
smilestone520 0:51ebb68301ec 95
smilestone520 0:51ebb68301ec 96 // saturation
smilestone520 0:51ebb68301ec 97 if(PIout_1 >= 0.5f)PIout_1 = 0.5f;
smilestone520 0:51ebb68301ec 98 else if(PIout_1 <= -0.5f)PIout_1 = -0.5f;
smilestone520 0:51ebb68301ec 99 pwm1.write(PIout_1 + 0.5f);
smilestone520 0:51ebb68301ec 100 TIM1->CCER |= 0x4;
smilestone520 0:51ebb68301ec 101
smilestone520 0:51ebb68301ec 102 //Motor 2
smilestone520 0:51ebb68301ec 103 v2 = (float)v2Count * 50.0f / 12.0f * 60.0f / 29.0f; //unit: rpm
smilestone520 0:51ebb68301ec 104 v2_avg = v2_avg + ( v2 - v2_old[9])/10.0f;
smilestone520 0:51ebb68301ec 105 for(int i = 9; i > 0; i--)
smilestone520 0:51ebb68301ec 106 {
smilestone520 0:51ebb68301ec 107 v2_old[i] = v2_old[i-1];
smilestone520 0:51ebb68301ec 108 }
smilestone520 0:51ebb68301ec 109 v2_old[0] = v2;
smilestone520 0:51ebb68301ec 110 v2Count = 0;
smilestone520 0:51ebb68301ec 111
smilestone520 0:51ebb68301ec 112 ///code for PI control///
smilestone520 0:51ebb68301ec 113 v2_err = v2_ref - v2;
smilestone520 0:51ebb68301ec 114
smilestone520 0:51ebb68301ec 115 PIout_2 = PIout_2 + Kp2 * v2_err - Ki * v2_err_old;
smilestone520 0:51ebb68301ec 116 v2_err_old = v2_err;
smilestone520 0:51ebb68301ec 117
smilestone520 0:51ebb68301ec 118 //saturation
smilestone520 0:51ebb68301ec 119 if(PIout_2 >= 0.5f)PIout_2 = 0.5f;
smilestone520 0:51ebb68301ec 120 else if(PIout_2 <= -0.5f)PIout_2 = -0.5f;
smilestone520 0:51ebb68301ec 121 pwm2.write(PIout_2 + 0.5f);
smilestone520 0:51ebb68301ec 122 TIM1->CCER |= 0x40;
smilestone520 0:51ebb68301ec 123
smilestone520 0:51ebb68301ec 124 timer1_counter ++;
smilestone520 0:51ebb68301ec 125 if (timer1_counter == 50)
smilestone520 0:51ebb68301ec 126 {
smilestone520 0:51ebb68301ec 127 timer1_counter = 0;
smilestone520 0:51ebb68301ec 128 if(bluetooth.writeable())
smilestone520 0:51ebb68301ec 129 {
smilestone520 0:51ebb68301ec 130 bluetooth.printf("V1: %4.2f V2: %4.2f\n",v1_avg,v2_avg);
smilestone520 0:51ebb68301ec 131 }
smilestone520 0:51ebb68301ec 132 }
smilestone520 0:51ebb68301ec 133 }
smilestone520 0:51ebb68301ec 134
smilestone520 0:51ebb68301ec 135 void CN_interrupt(void)
smilestone520 0:51ebb68301ec 136 {
smilestone520 0:51ebb68301ec 137 //Motor 1
smilestone520 0:51ebb68301ec 138 stateA_1 = HallA_1.read();
smilestone520 0:51ebb68301ec 139 stateB_1 = HallB_1.read();
smilestone520 0:51ebb68301ec 140
smilestone520 0:51ebb68301ec 141 ///code for state determination///
smilestone520 0:51ebb68301ec 142 if (stateA_1 == 0)
smilestone520 0:51ebb68301ec 143 {
smilestone520 0:51ebb68301ec 144 if (stateB_1 == 0)
smilestone520 0:51ebb68301ec 145 state_1 = 0;
smilestone520 0:51ebb68301ec 146 else
smilestone520 0:51ebb68301ec 147 state_1 = 1;
smilestone520 0:51ebb68301ec 148 }
smilestone520 0:51ebb68301ec 149 else
smilestone520 0:51ebb68301ec 150 {
smilestone520 0:51ebb68301ec 151 if (stateB_1 == 1)
smilestone520 0:51ebb68301ec 152 state_1 = 2;
smilestone520 0:51ebb68301ec 153 else
smilestone520 0:51ebb68301ec 154 state_1 = 3;
smilestone520 0:51ebb68301ec 155 }
smilestone520 0:51ebb68301ec 156
smilestone520 0:51ebb68301ec 157 //Forward: v1Count +1
smilestone520 0:51ebb68301ec 158 //Inverse: v1Count -1
smilestone520 0:51ebb68301ec 159 if ( (state_1 == (state_1_old + 1)) || (state_1 == 0 && state_1_old == 3) )
smilestone520 0:51ebb68301ec 160 v1Count++;
smilestone520 0:51ebb68301ec 161 else if ( (state_1 == (state_1_old - 1)) || (state_1 == 3 && state_1_old == 0))
smilestone520 0:51ebb68301ec 162 v1Count--;
smilestone520 0:51ebb68301ec 163
smilestone520 0:51ebb68301ec 164 state_1_old = state_1;
smilestone520 0:51ebb68301ec 165
smilestone520 0:51ebb68301ec 166 //Motor 2
smilestone520 0:51ebb68301ec 167 stateA_2 = HallA_2.read();
smilestone520 0:51ebb68301ec 168 stateB_2 = HallB_2.read();
smilestone520 0:51ebb68301ec 169
smilestone520 0:51ebb68301ec 170 ///code for state determination///
smilestone520 0:51ebb68301ec 171 if (stateA_2 == 0)
smilestone520 0:51ebb68301ec 172 {
smilestone520 0:51ebb68301ec 173 if (stateB_2 == 0)
smilestone520 0:51ebb68301ec 174 state_2 = 0;
smilestone520 0:51ebb68301ec 175 else
smilestone520 0:51ebb68301ec 176 state_2 = 1;
smilestone520 0:51ebb68301ec 177 }
smilestone520 0:51ebb68301ec 178 else
smilestone520 0:51ebb68301ec 179 {
smilestone520 0:51ebb68301ec 180 if (stateB_2 == 1)
smilestone520 0:51ebb68301ec 181 state_2 = 2;
smilestone520 0:51ebb68301ec 182 else
smilestone520 0:51ebb68301ec 183 state_2 = 3;
smilestone520 0:51ebb68301ec 184 }
smilestone520 0:51ebb68301ec 185
smilestone520 0:51ebb68301ec 186 //Forward: v2Count +1
smilestone520 0:51ebb68301ec 187 //Inverse: v2Count -1
smilestone520 0:51ebb68301ec 188 if ( (state_2 == (state_2_old + 1)) || (state_2 == 0 && state_2_old == 3) )
smilestone520 0:51ebb68301ec 189 v2Count++;
smilestone520 0:51ebb68301ec 190 else if ( (state_2 == (state_2_old - 1)) || (state_2 == 3 && state_2_old == 0) )
smilestone520 0:51ebb68301ec 191 v2Count--;
smilestone520 0:51ebb68301ec 192
smilestone520 0:51ebb68301ec 193 state_2_old = state_2;
smilestone520 0:51ebb68301ec 194 }
smilestone520 0:51ebb68301ec 195
smilestone520 0:51ebb68301ec 196 void init_TIMER(void)
smilestone520 0:51ebb68301ec 197 {
smilestone520 0:51ebb68301ec 198 timer1.attach_us(&timer1_interrupt, 20000);//10ms interrupt period (100 Hz)
smilestone520 0:51ebb68301ec 199 timer1_counter = 0;
smilestone520 0:51ebb68301ec 200 }
smilestone520 0:51ebb68301ec 201
smilestone520 0:51ebb68301ec 202 void init_PWM(void)
smilestone520 0:51ebb68301ec 203 {
smilestone520 0:51ebb68301ec 204 pwm1.period_us(50);
smilestone520 0:51ebb68301ec 205 pwm1.write(0.5);
smilestone520 0:51ebb68301ec 206 TIM1->CCER |= 0x4;
smilestone520 0:51ebb68301ec 207
smilestone520 0:51ebb68301ec 208 pwm2.period_us(50);
smilestone520 0:51ebb68301ec 209 pwm2.write(0.5);
smilestone520 0:51ebb68301ec 210 TIM1->CCER |= 0x40;
smilestone520 0:51ebb68301ec 211 }
smilestone520 0:51ebb68301ec 212
smilestone520 0:51ebb68301ec 213 void init_CN(void)
smilestone520 0:51ebb68301ec 214 {
smilestone520 0:51ebb68301ec 215 HallA_1.rise(&CN_interrupt);
smilestone520 0:51ebb68301ec 216 HallA_1.fall(&CN_interrupt);
smilestone520 0:51ebb68301ec 217 HallB_1.rise(&CN_interrupt);
smilestone520 0:51ebb68301ec 218 HallB_1.fall(&CN_interrupt);
smilestone520 0:51ebb68301ec 219
smilestone520 0:51ebb68301ec 220 HallA_2.rise(&CN_interrupt);
smilestone520 0:51ebb68301ec 221 HallA_2.fall(&CN_interrupt);
smilestone520 0:51ebb68301ec 222 HallB_2.rise(&CN_interrupt);
smilestone520 0:51ebb68301ec 223 HallB_2.fall(&CN_interrupt);
smilestone520 0:51ebb68301ec 224
smilestone520 0:51ebb68301ec 225 stateA_1 = HallA_1.read();
smilestone520 0:51ebb68301ec 226 stateB_1 = HallB_1.read();
smilestone520 0:51ebb68301ec 227 stateA_2 = HallA_2.read();
smilestone520 0:51ebb68301ec 228 stateB_2 = HallB_2.read();
smilestone520 0:51ebb68301ec 229 }
smilestone520 0:51ebb68301ec 230
smilestone520 0:51ebb68301ec 231 void init_BLUETOOTH(void)
smilestone520 0:51ebb68301ec 232 {
smilestone520 0:51ebb68301ec 233 bluetooth.baud(115200);
smilestone520 0:51ebb68301ec 234 }