![](/media/cache/profiles/916954106a13204fa4909ae2d48e0602.jpg.50x50_q85.jpg)
working version
Fork of foc-ed_in_the_bot_compact by
main.cpp@3:9b20da3f0055, 2016-03-18 (annotated)
- Committer:
- bwang
- Date:
- Fri Mar 18 10:52:45 2016 +0000
- Revision:
- 3:9b20da3f0055
- Parent:
- 2:eabe8feaaabb
- Child:
- 4:a6669248ce4d
code is clean, code works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 0:bac9c3a3a6ca | 1 | #include "mbed.h" |
bwang | 0:bac9c3a3a6ca | 2 | #include "math.h" |
bwang | 0:bac9c3a3a6ca | 3 | #include "PositionSensor.h" |
bwang | 0:bac9c3a3a6ca | 4 | #include "FastPWM.h" |
bwang | 0:bac9c3a3a6ca | 5 | #include "Transforms.h" |
bwang | 0:bac9c3a3a6ca | 6 | #include "config.h" |
bwang | 0:bac9c3a3a6ca | 7 | |
bwang | 1:7b61790f6be9 | 8 | FastPWM *a; |
bwang | 1:7b61790f6be9 | 9 | FastPWM *b; |
bwang | 1:7b61790f6be9 | 10 | FastPWM *c; |
bwang | 0:bac9c3a3a6ca | 11 | DigitalOut en(EN); |
bwang | 1:7b61790f6be9 | 12 | DigitalOut toggle(PC_10); |
bwang | 0:bac9c3a3a6ca | 13 | |
bwang | 0:bac9c3a3a6ca | 14 | PositionSensorEncoder pos(CPR, 0); |
bwang | 0:bac9c3a3a6ca | 15 | |
bwang | 0:bac9c3a3a6ca | 16 | Serial pc(USBTX, USBRX); |
bwang | 0:bac9c3a3a6ca | 17 | |
bwang | 1:7b61790f6be9 | 18 | int state = 0; |
bwang | 1:7b61790f6be9 | 19 | int adval1, adval2; |
bwang | 2:eabe8feaaabb | 20 | float ia, ib, ic, alpha, beta, d, q, vd, vq, p; |
bwang | 2:eabe8feaaabb | 21 | |
bwang | 1:7b61790f6be9 | 22 | float ia_supp_offset = 0.0f, ib_supp_offset = 0.0f; //current sensor offset due to bias resistor inaccuracies, etc (mV) |
bwang | 1:7b61790f6be9 | 23 | |
bwang | 2:eabe8feaaabb | 24 | float d_integral = 0.0f, q_integral = 0.0f; |
bwang | 2:eabe8feaaabb | 25 | float last_d = 0.0f, last_q = 0.0f; |
bwang | 3:9b20da3f0055 | 26 | float d_ref = -0.0f, q_ref = -50.0f; |
bwang | 2:eabe8feaaabb | 27 | |
bwang | 3:9b20da3f0055 | 28 | void main_loop(); |
bwang | 3:9b20da3f0055 | 29 | void zero_current(); |
bwang | 3:9b20da3f0055 | 30 | void config_globals(); |
bwang | 3:9b20da3f0055 | 31 | void startup_msg(); |
bwang | 2:eabe8feaaabb | 32 | |
bwang | 1:7b61790f6be9 | 33 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
bwang | 1:7b61790f6be9 | 34 | if (TIM1->SR & TIM_SR_UIF ) { |
bwang | 1:7b61790f6be9 | 35 | adval1 = ADC1->DR; |
bwang | 1:7b61790f6be9 | 36 | adval2 = ADC2->DR; |
bwang | 1:7b61790f6be9 | 37 | ADC1->CR2 |= 0x40000000; |
bwang | 3:9b20da3f0055 | 38 | //while((ADC1->SR & ADC_SR_EOC) == 0 || (ADC2->SR & ADC_SR_EOC) == 0) {} |
bwang | 1:7b61790f6be9 | 39 | } |
bwang | 1:7b61790f6be9 | 40 | TIM1->SR = 0x00; |
bwang | 1:7b61790f6be9 | 41 | } |
bwang | 1:7b61790f6be9 | 42 | |
bwang | 1:7b61790f6be9 | 43 | void zero_current(){ |
bwang | 1:7b61790f6be9 | 44 | for (int i = 0; i < 1000; i++){ |
bwang | 1:7b61790f6be9 | 45 | ia_supp_offset += (float) (ADC1->DR); |
bwang | 1:7b61790f6be9 | 46 | ib_supp_offset += (float) (ADC2->DR); |
bwang | 1:7b61790f6be9 | 47 | ADC1->CR2 |= 0x40000000; |
bwang | 1:7b61790f6be9 | 48 | wait_us(100); |
bwang | 1:7b61790f6be9 | 49 | } |
bwang | 1:7b61790f6be9 | 50 | ia_supp_offset /= 1000.0f; |
bwang | 1:7b61790f6be9 | 51 | ib_supp_offset /= 1000.0f; |
bwang | 1:7b61790f6be9 | 52 | ia_supp_offset = ia_supp_offset / 4096.0f * AVDD - I_OFFSET; |
bwang | 1:7b61790f6be9 | 53 | ib_supp_offset = ib_supp_offset / 4096.0f * AVDD - I_OFFSET; |
bwang | 1:7b61790f6be9 | 54 | } |
bwang | 0:bac9c3a3a6ca | 55 | |
bwang | 0:bac9c3a3a6ca | 56 | void config_globals() { |
bwang | 0:bac9c3a3a6ca | 57 | pc.baud(115200); |
bwang | 0:bac9c3a3a6ca | 58 | |
bwang | 1:7b61790f6be9 | 59 | //Enable clocks for GPIOs |
bwang | 1:7b61790f6be9 | 60 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; |
bwang | 1:7b61790f6be9 | 61 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; |
bwang | 1:7b61790f6be9 | 62 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; |
bwang | 1:7b61790f6be9 | 63 | |
bwang | 1:7b61790f6be9 | 64 | RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //enable TIM1 clock |
bwang | 1:7b61790f6be9 | 65 | |
bwang | 1:7b61790f6be9 | 66 | a = new FastPWM(PWMA); |
bwang | 1:7b61790f6be9 | 67 | b = new FastPWM(PWMB); |
bwang | 1:7b61790f6be9 | 68 | c = new FastPWM(PWMC); |
bwang | 1:7b61790f6be9 | 69 | |
bwang | 1:7b61790f6be9 | 70 | NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ |
bwang | 1:7b61790f6be9 | 71 | |
bwang | 1:7b61790f6be9 | 72 | TIM1->DIER |= TIM_DIER_UIE; //enable update interrupt |
bwang | 1:7b61790f6be9 | 73 | TIM1->CR1 = 0x40; //CMS = 10, interrupt only when counting up |
bwang | 1:7b61790f6be9 | 74 | TIM1->CR1 |= TIM_CR1_ARPE; //autoreload on, |
bwang | 1:7b61790f6be9 | 75 | TIM1->RCR |= 0x01; //update event once per up/down count of tim1 |
bwang | 1:7b61790f6be9 | 76 | TIM1->EGR |= TIM_EGR_UG; |
bwang | 1:7b61790f6be9 | 77 | |
bwang | 1:7b61790f6be9 | 78 | TIM1->PSC = 0x00; //no prescaler, timer counts up in sync with the peripheral clock |
bwang | 1:7b61790f6be9 | 79 | TIM1->ARR = 0x4650; //5 Khz |
bwang | 1:7b61790f6be9 | 80 | TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on. |
bwang | 1:7b61790f6be9 | 81 | TIM1->CR1 |= TIM_CR1_CEN; |
bwang | 1:7b61790f6be9 | 82 | |
bwang | 1:7b61790f6be9 | 83 | //ADC Setup |
bwang | 1:7b61790f6be9 | 84 | RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1 |
bwang | 1:7b61790f6be9 | 85 | RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2 |
bwang | 1:7b61790f6be9 | 86 | |
bwang | 1:7b61790f6be9 | 87 | ADC->CCR = 0x00000006; //Regular simultaneous mode, 3 channels |
bwang | 1:7b61790f6be9 | 88 | |
bwang | 1:7b61790f6be9 | 89 | ADC1->CR2 |= ADC_CR2_ADON; //ADC1 on |
bwang | 1:7b61790f6be9 | 90 | ADC1->SQR3 = 0x0000004; //PA_4 as ADC1, sequence 0 |
bwang | 0:bac9c3a3a6ca | 91 | |
bwang | 1:7b61790f6be9 | 92 | ADC2->CR2 |= ADC_CR2_ADON; //ADC2 ON |
bwang | 1:7b61790f6be9 | 93 | ADC2->SQR3 = 0x00000008; //PB_0 as ADC2, sequence 1 |
bwang | 1:7b61790f6be9 | 94 | |
bwang | 1:7b61790f6be9 | 95 | GPIOA->MODER |= (1 << 8); |
bwang | 1:7b61790f6be9 | 96 | GPIOA->MODER |= (1 << 9); |
bwang | 1:7b61790f6be9 | 97 | |
bwang | 1:7b61790f6be9 | 98 | GPIOA->MODER |= (1 << 2); |
bwang | 1:7b61790f6be9 | 99 | GPIOA->MODER |= (1 << 3); |
bwang | 1:7b61790f6be9 | 100 | |
bwang | 1:7b61790f6be9 | 101 | GPIOA->MODER |= (1 << 0); |
bwang | 1:7b61790f6be9 | 102 | GPIOA->MODER |= (1 << 1); |
bwang | 1:7b61790f6be9 | 103 | |
bwang | 1:7b61790f6be9 | 104 | GPIOB->MODER |= (1 << 0); |
bwang | 1:7b61790f6be9 | 105 | GPIOB->MODER |= (1 << 1); |
bwang | 1:7b61790f6be9 | 106 | |
bwang | 1:7b61790f6be9 | 107 | GPIOC->MODER |= (1 << 2); |
bwang | 1:7b61790f6be9 | 108 | GPIOC->MODER |= (1 << 3); |
bwang | 1:7b61790f6be9 | 109 | |
bwang | 1:7b61790f6be9 | 110 | //DAC setup |
bwang | 1:7b61790f6be9 | 111 | RCC->APB1ENR |= 0x20000000; |
bwang | 1:7b61790f6be9 | 112 | DAC->CR |= DAC_CR_EN2; |
bwang | 1:7b61790f6be9 | 113 | |
bwang | 1:7b61790f6be9 | 114 | GPIOA->MODER |= (1 << 10); |
bwang | 1:7b61790f6be9 | 115 | GPIOA->MODER |= (1 << 11); |
bwang | 1:7b61790f6be9 | 116 | |
bwang | 1:7b61790f6be9 | 117 | //Zero duty cycles |
bwang | 1:7b61790f6be9 | 118 | set_dtc(a, 0.0f); |
bwang | 1:7b61790f6be9 | 119 | set_dtc(b, 0.0f); |
bwang | 1:7b61790f6be9 | 120 | set_dtc(c, 0.0f); |
bwang | 1:7b61790f6be9 | 121 | |
bwang | 1:7b61790f6be9 | 122 | wait_ms(250); |
bwang | 1:7b61790f6be9 | 123 | zero_current(); |
bwang | 0:bac9c3a3a6ca | 124 | en = 1; |
bwang | 0:bac9c3a3a6ca | 125 | } |
bwang | 0:bac9c3a3a6ca | 126 | |
bwang | 0:bac9c3a3a6ca | 127 | void startup_msg() { |
bwang | 0:bac9c3a3a6ca | 128 | pc.printf("%s\n\r\n\r", "FOC'ed in the Bot Rev A."); |
bwang | 0:bac9c3a3a6ca | 129 | pc.printf("%s\n\r", "====Config Data===="); |
bwang | 0:bac9c3a3a6ca | 130 | pc.printf("Current Sensor Offset: %f mV\n\r", I_OFFSET); |
bwang | 0:bac9c3a3a6ca | 131 | pc.printf("Current Sensor Scale: %f mv/A\n\r", I_SCALE); |
bwang | 0:bac9c3a3a6ca | 132 | pc.printf("Bus Voltage: %f V\n\r", BUS_VOLTAGE); |
bwang | 0:bac9c3a3a6ca | 133 | pc.printf("Loop KP: %f\n\r", KP); |
bwang | 0:bac9c3a3a6ca | 134 | pc.printf("Loop KI: %f\n\r", KI); |
bwang | 1:7b61790f6be9 | 135 | pc.printf("Ia offset: %f mV\n\r", ia_supp_offset); |
bwang | 1:7b61790f6be9 | 136 | pc.printf("Ib offset: %f mV\n\r", ib_supp_offset); |
bwang | 0:bac9c3a3a6ca | 137 | pc.printf("\n\r"); |
bwang | 0:bac9c3a3a6ca | 138 | } |
bwang | 0:bac9c3a3a6ca | 139 | |
bwang | 0:bac9c3a3a6ca | 140 | void main_loop() { |
bwang | 2:eabe8feaaabb | 141 | p = pos.GetElecPosition() - POS_OFFSET; |
bwang | 0:bac9c3a3a6ca | 142 | if (p < 0) p += 2 * PI; |
bwang | 0:bac9c3a3a6ca | 143 | |
bwang | 2:eabe8feaaabb | 144 | float sin_p = sinf(p); |
bwang | 2:eabe8feaaabb | 145 | float cos_p = cosf(p); |
bwang | 2:eabe8feaaabb | 146 | |
bwang | 3:9b20da3f0055 | 147 | float pos_dac = 0.85f * p / (2 * PI) + 0.05f; |
bwang | 3:9b20da3f0055 | 148 | DAC->DHR12R2 = (unsigned int) (pos_dac * 4096); |
bwang | 0:bac9c3a3a6ca | 149 | |
bwang | 1:7b61790f6be9 | 150 | ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE; |
bwang | 1:7b61790f6be9 | 151 | ib = ((float) adval2 / 4096.0f * AVDD - I_OFFSET - ib_supp_offset) / I_SCALE; |
bwang | 2:eabe8feaaabb | 152 | ic = -ia - ib; |
bwang | 0:bac9c3a3a6ca | 153 | |
bwang | 3:9b20da3f0055 | 154 | float u = ib; |
bwang | 3:9b20da3f0055 | 155 | float v = ic; |
bwang | 2:eabe8feaaabb | 156 | |
bwang | 2:eabe8feaaabb | 157 | alpha = u; |
bwang | 2:eabe8feaaabb | 158 | beta = 1 / sqrtf(3.0f) * u + 2 / sqrtf(3.0f) * v; |
bwang | 2:eabe8feaaabb | 159 | |
bwang | 2:eabe8feaaabb | 160 | d = alpha * cos_p - beta * sin_p; |
bwang | 2:eabe8feaaabb | 161 | q = -alpha * sin_p - beta * cos_p; |
bwang | 2:eabe8feaaabb | 162 | |
bwang | 3:9b20da3f0055 | 163 | float d_err = d_ref - d; |
bwang | 3:9b20da3f0055 | 164 | float q_err = q_ref - q; |
bwang | 2:eabe8feaaabb | 165 | |
bwang | 2:eabe8feaaabb | 166 | d_integral += d_err * KI; |
bwang | 2:eabe8feaaabb | 167 | q_integral += q_err * KI; |
bwang | 2:eabe8feaaabb | 168 | |
bwang | 2:eabe8feaaabb | 169 | if (q_integral > INTEGRAL_MAX) q_integral = INTEGRAL_MAX; |
bwang | 2:eabe8feaaabb | 170 | if (d_integral > INTEGRAL_MAX) d_integral = INTEGRAL_MAX; |
bwang | 2:eabe8feaaabb | 171 | if (q_integral < -INTEGRAL_MAX) q_integral = -INTEGRAL_MAX; |
bwang | 2:eabe8feaaabb | 172 | if (d_integral < -INTEGRAL_MAX) d_integral = -INTEGRAL_MAX; |
bwang | 2:eabe8feaaabb | 173 | |
bwang | 2:eabe8feaaabb | 174 | vd = KP * d_err + d_integral; |
bwang | 2:eabe8feaaabb | 175 | vq = KP * q_err + q_integral; |
bwang | 2:eabe8feaaabb | 176 | |
bwang | 2:eabe8feaaabb | 177 | if (vd < -1.0f) vd = -1.0f; |
bwang | 2:eabe8feaaabb | 178 | if (vd > 1.0f) vd = 1.0f; |
bwang | 2:eabe8feaaabb | 179 | if (vq < -1.0f) vq = -1.0f; |
bwang | 2:eabe8feaaabb | 180 | if (vq > 1.0f) vq = 1.0f; |
bwang | 2:eabe8feaaabb | 181 | |
bwang | 3:9b20da3f0055 | 182 | //DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048); |
bwang | 2:eabe8feaaabb | 183 | //DAC->DHR12R2 = (unsigned int) (-vd * 2000 + 2048); |
bwang | 2:eabe8feaaabb | 184 | |
bwang | 2:eabe8feaaabb | 185 | float valpha = vd * cos_p - vq * sin_p; |
bwang | 2:eabe8feaaabb | 186 | float vbeta = vd * sin_p + vq * cos_p; |
bwang | 2:eabe8feaaabb | 187 | |
bwang | 2:eabe8feaaabb | 188 | float va = valpha; |
bwang | 2:eabe8feaaabb | 189 | float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta; |
bwang | 2:eabe8feaaabb | 190 | float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta; |
bwang | 2:eabe8feaaabb | 191 | |
bwang | 2:eabe8feaaabb | 192 | set_dtc(a, 0.5f + 0.5f * va); |
bwang | 2:eabe8feaaabb | 193 | set_dtc(b, 0.5f + 0.5f * vb); |
bwang | 2:eabe8feaaabb | 194 | set_dtc(c, 0.5f + 0.5f * vc); |
bwang | 0:bac9c3a3a6ca | 195 | } |
bwang | 0:bac9c3a3a6ca | 196 | |
bwang | 0:bac9c3a3a6ca | 197 | int main() { |
bwang | 0:bac9c3a3a6ca | 198 | config_globals(); |
bwang | 0:bac9c3a3a6ca | 199 | startup_msg(); |
bwang | 0:bac9c3a3a6ca | 200 | |
bwang | 0:bac9c3a3a6ca | 201 | Ticker loop; |
bwang | 0:bac9c3a3a6ca | 202 | loop.attach_us(main_loop, 200); |
bwang | 0:bac9c3a3a6ca | 203 | |
bwang | 0:bac9c3a3a6ca | 204 | for (;;) { |
bwang | 2:eabe8feaaabb | 205 | /* |
bwang | 2:eabe8feaaabb | 206 | q_ref = 0.0f; |
bwang | 2:eabe8feaaabb | 207 | wait(3); |
bwang | 2:eabe8feaaabb | 208 | toggle = state; |
bwang | 2:eabe8feaaabb | 209 | state = !state; |
bwang | 2:eabe8feaaabb | 210 | q_ref = -50.0f; |
bwang | 2:eabe8feaaabb | 211 | wait(3); |
bwang | 2:eabe8feaaabb | 212 | toggle = state; |
bwang | 2:eabe8feaaabb | 213 | state = !state; |
bwang | 2:eabe8feaaabb | 214 | */ |
bwang | 0:bac9c3a3a6ca | 215 | } |
bwang | 0:bac9c3a3a6ca | 216 | } |