![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A fork of foc-ed_in_the_bot_compact modified to test motors using bayleyw's prius inverter ECU
Fork of foc-ed_in_the_bot_compact by
main.cpp@12:264e942f904f, 2016-06-15 (annotated)
- Committer:
- nki
- Date:
- Wed Jun 15 05:24:52 2016 +0000
- Revision:
- 12:264e942f904f
- Parent:
- 10:6829abb438fc
VFD for induction motor
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" |
nki | 12:264e942f904f | 7 | #include "filters.h" |
bwang | 0:bac9c3a3a6ca | 8 | |
bwang | 1:7b61790f6be9 | 9 | FastPWM *a; |
bwang | 1:7b61790f6be9 | 10 | FastPWM *b; |
bwang | 1:7b61790f6be9 | 11 | FastPWM *c; |
bwang | 0:bac9c3a3a6ca | 12 | DigitalOut en(EN); |
bwang | 1:7b61790f6be9 | 13 | DigitalOut toggle(PC_10); |
nki | 12:264e942f904f | 14 | AnalogIn pot1(PC_5); |
nki | 12:264e942f904f | 15 | AnalogIn pot2(PC_4); |
bwang | 0:bac9c3a3a6ca | 16 | |
nki | 12:264e942f904f | 17 | |
nki | 12:264e942f904f | 18 | //PositionSensorEncoder pos(CPR, 0); |
bwang | 0:bac9c3a3a6ca | 19 | |
bwang | 0:bac9c3a3a6ca | 20 | Serial pc(USBTX, USBRX); |
bwang | 0:bac9c3a3a6ca | 21 | |
bwang | 1:7b61790f6be9 | 22 | int state = 0; |
bwang | 1:7b61790f6be9 | 23 | int adval1, adval2; |
nki | 12:264e942f904f | 24 | float ia, ib, ic, alpha, beta, d, q, vd, vq, p, flux_cmd, speed_cmd, flux_cmd_raw, speed_cmd_raw, volt_cmd; |
bwang | 2:eabe8feaaabb | 25 | |
bwang | 1:7b61790f6be9 | 26 | float ia_supp_offset = 0.0f, ib_supp_offset = 0.0f; //current sensor offset due to bias resistor inaccuracies, etc (mV) |
bwang | 1:7b61790f6be9 | 27 | |
bwang | 2:eabe8feaaabb | 28 | float d_integral = 0.0f, q_integral = 0.0f; |
bwang | 2:eabe8feaaabb | 29 | float last_d = 0.0f, last_q = 0.0f; |
bwang | 3:9b20da3f0055 | 30 | float d_ref = -0.0f, q_ref = -50.0f; |
bwang | 2:eabe8feaaabb | 31 | |
nki | 12:264e942f904f | 32 | MeanFilter filter_speed_cmd(0.999f); |
nki | 12:264e942f904f | 33 | MeanFilter filter_flux_cmd(0.999f); |
nki | 12:264e942f904f | 34 | |
bwang | 4:a6669248ce4d | 35 | void commutate(); |
bwang | 3:9b20da3f0055 | 36 | void zero_current(); |
bwang | 3:9b20da3f0055 | 37 | void config_globals(); |
bwang | 3:9b20da3f0055 | 38 | void startup_msg(); |
bwang | 2:eabe8feaaabb | 39 | |
bwang | 1:7b61790f6be9 | 40 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
bwang | 1:7b61790f6be9 | 41 | if (TIM1->SR & TIM_SR_UIF ) { |
bwang | 4:a6669248ce4d | 42 | toggle = 1; |
bwang | 4:a6669248ce4d | 43 | ADC1->CR2 |= 0x40000000; |
bwang | 4:a6669248ce4d | 44 | volatile int delay; |
bwang | 4:a6669248ce4d | 45 | for (delay = 0; delay < 35; delay++); |
bwang | 4:a6669248ce4d | 46 | toggle = 0; |
bwang | 1:7b61790f6be9 | 47 | adval1 = ADC1->DR; |
bwang | 1:7b61790f6be9 | 48 | adval2 = ADC2->DR; |
bwang | 4:a6669248ce4d | 49 | commutate(); |
bwang | 1:7b61790f6be9 | 50 | } |
bwang | 1:7b61790f6be9 | 51 | TIM1->SR = 0x00; |
bwang | 1:7b61790f6be9 | 52 | } |
bwang | 1:7b61790f6be9 | 53 | |
bwang | 1:7b61790f6be9 | 54 | void zero_current(){ |
bwang | 1:7b61790f6be9 | 55 | for (int i = 0; i < 1000; i++){ |
bwang | 1:7b61790f6be9 | 56 | ia_supp_offset += (float) (ADC1->DR); |
bwang | 1:7b61790f6be9 | 57 | ib_supp_offset += (float) (ADC2->DR); |
bwang | 1:7b61790f6be9 | 58 | ADC1->CR2 |= 0x40000000; |
bwang | 1:7b61790f6be9 | 59 | wait_us(100); |
bwang | 1:7b61790f6be9 | 60 | } |
bwang | 1:7b61790f6be9 | 61 | ia_supp_offset /= 1000.0f; |
bwang | 1:7b61790f6be9 | 62 | ib_supp_offset /= 1000.0f; |
bwang | 1:7b61790f6be9 | 63 | ia_supp_offset = ia_supp_offset / 4096.0f * AVDD - I_OFFSET; |
bwang | 1:7b61790f6be9 | 64 | ib_supp_offset = ib_supp_offset / 4096.0f * AVDD - I_OFFSET; |
bwang | 1:7b61790f6be9 | 65 | } |
bwang | 0:bac9c3a3a6ca | 66 | |
bwang | 0:bac9c3a3a6ca | 67 | void config_globals() { |
bwang | 0:bac9c3a3a6ca | 68 | pc.baud(115200); |
bwang | 0:bac9c3a3a6ca | 69 | |
bwang | 1:7b61790f6be9 | 70 | //Enable clocks for GPIOs |
bwang | 1:7b61790f6be9 | 71 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; |
bwang | 1:7b61790f6be9 | 72 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; |
bwang | 1:7b61790f6be9 | 73 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; |
bwang | 1:7b61790f6be9 | 74 | |
bwang | 1:7b61790f6be9 | 75 | RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //enable TIM1 clock |
bwang | 1:7b61790f6be9 | 76 | |
bwang | 1:7b61790f6be9 | 77 | a = new FastPWM(PWMA); |
bwang | 1:7b61790f6be9 | 78 | b = new FastPWM(PWMB); |
bwang | 1:7b61790f6be9 | 79 | c = new FastPWM(PWMC); |
bwang | 1:7b61790f6be9 | 80 | |
bwang | 1:7b61790f6be9 | 81 | NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ |
bwang | 1:7b61790f6be9 | 82 | |
bwang | 1:7b61790f6be9 | 83 | TIM1->DIER |= TIM_DIER_UIE; //enable update interrupt |
bwang | 1:7b61790f6be9 | 84 | TIM1->CR1 = 0x40; //CMS = 10, interrupt only when counting up |
bwang | 1:7b61790f6be9 | 85 | TIM1->CR1 |= TIM_CR1_ARPE; //autoreload on, |
bwang | 1:7b61790f6be9 | 86 | TIM1->RCR |= 0x01; //update event once per up/down count of tim1 |
bwang | 1:7b61790f6be9 | 87 | TIM1->EGR |= TIM_EGR_UG; |
bwang | 1:7b61790f6be9 | 88 | |
bwang | 1:7b61790f6be9 | 89 | TIM1->PSC = 0x00; //no prescaler, timer counts up in sync with the peripheral clock |
bwang | 1:7b61790f6be9 | 90 | TIM1->ARR = 0x4650; //5 Khz |
bwang | 1:7b61790f6be9 | 91 | TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on. |
bwang | 1:7b61790f6be9 | 92 | TIM1->CR1 |= TIM_CR1_CEN; |
bwang | 1:7b61790f6be9 | 93 | |
bwang | 1:7b61790f6be9 | 94 | //ADC Setup |
bwang | 1:7b61790f6be9 | 95 | RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1 |
bwang | 1:7b61790f6be9 | 96 | RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2 |
bwang | 1:7b61790f6be9 | 97 | |
bwang | 1:7b61790f6be9 | 98 | ADC->CCR = 0x00000006; //Regular simultaneous mode, 3 channels |
bwang | 1:7b61790f6be9 | 99 | |
bwang | 1:7b61790f6be9 | 100 | ADC1->CR2 |= ADC_CR2_ADON; //ADC1 on |
bwang | 1:7b61790f6be9 | 101 | ADC1->SQR3 = 0x0000004; //PA_4 as ADC1, sequence 0 |
bwang | 0:bac9c3a3a6ca | 102 | |
bwang | 1:7b61790f6be9 | 103 | ADC2->CR2 |= ADC_CR2_ADON; //ADC2 ON |
bwang | 1:7b61790f6be9 | 104 | ADC2->SQR3 = 0x00000008; //PB_0 as ADC2, sequence 1 |
bwang | 1:7b61790f6be9 | 105 | |
bwang | 1:7b61790f6be9 | 106 | GPIOA->MODER |= (1 << 8); |
bwang | 1:7b61790f6be9 | 107 | GPIOA->MODER |= (1 << 9); |
bwang | 1:7b61790f6be9 | 108 | |
bwang | 1:7b61790f6be9 | 109 | GPIOA->MODER |= (1 << 2); |
bwang | 1:7b61790f6be9 | 110 | GPIOA->MODER |= (1 << 3); |
bwang | 1:7b61790f6be9 | 111 | |
bwang | 1:7b61790f6be9 | 112 | GPIOA->MODER |= (1 << 0); |
bwang | 1:7b61790f6be9 | 113 | GPIOA->MODER |= (1 << 1); |
bwang | 1:7b61790f6be9 | 114 | |
bwang | 1:7b61790f6be9 | 115 | GPIOB->MODER |= (1 << 0); |
bwang | 1:7b61790f6be9 | 116 | GPIOB->MODER |= (1 << 1); |
bwang | 1:7b61790f6be9 | 117 | |
bwang | 1:7b61790f6be9 | 118 | GPIOC->MODER |= (1 << 2); |
bwang | 1:7b61790f6be9 | 119 | GPIOC->MODER |= (1 << 3); |
bwang | 1:7b61790f6be9 | 120 | |
bwang | 1:7b61790f6be9 | 121 | //DAC setup |
bwang | 1:7b61790f6be9 | 122 | RCC->APB1ENR |= 0x20000000; |
bwang | 1:7b61790f6be9 | 123 | DAC->CR |= DAC_CR_EN2; |
bwang | 1:7b61790f6be9 | 124 | |
bwang | 1:7b61790f6be9 | 125 | GPIOA->MODER |= (1 << 10); |
bwang | 1:7b61790f6be9 | 126 | GPIOA->MODER |= (1 << 11); |
bwang | 1:7b61790f6be9 | 127 | |
bwang | 1:7b61790f6be9 | 128 | //Zero duty cycles |
bwang | 1:7b61790f6be9 | 129 | set_dtc(a, 0.0f); |
bwang | 1:7b61790f6be9 | 130 | set_dtc(b, 0.0f); |
bwang | 1:7b61790f6be9 | 131 | set_dtc(c, 0.0f); |
bwang | 1:7b61790f6be9 | 132 | |
bwang | 1:7b61790f6be9 | 133 | wait_ms(250); |
bwang | 1:7b61790f6be9 | 134 | zero_current(); |
bwang | 0:bac9c3a3a6ca | 135 | en = 1; |
bwang | 0:bac9c3a3a6ca | 136 | } |
bwang | 0:bac9c3a3a6ca | 137 | |
bwang | 0:bac9c3a3a6ca | 138 | void startup_msg() { |
nki | 12:264e942f904f | 139 | pc.printf("%s\n\r\n\r", "Serial Begin"); |
bwang | 0:bac9c3a3a6ca | 140 | pc.printf("\n\r"); |
bwang | 0:bac9c3a3a6ca | 141 | } |
bwang | 0:bac9c3a3a6ca | 142 | |
bwang | 4:a6669248ce4d | 143 | void commutate() { |
nki | 12:264e942f904f | 144 | //p = pos.GetElecPosition() - POS_OFFSET; |
nki | 12:264e942f904f | 145 | p+=1.0f*speed_cmd; //top speed 800 electrical hz |
nki | 12:264e942f904f | 146 | |
bwang | 0:bac9c3a3a6ca | 147 | if (p < 0) p += 2 * PI; |
bwang | 0:bac9c3a3a6ca | 148 | |
bwang | 2:eabe8feaaabb | 149 | float sin_p = sinf(p); |
bwang | 2:eabe8feaaabb | 150 | float cos_p = cosf(p); |
nki | 12:264e942f904f | 151 | //DAC->DHR12R2 = (unsigned int) (speed_cmd * 4096); |
bwang | 2:eabe8feaaabb | 152 | |
nki | 12:264e942f904f | 153 | DAC->DHR12R2 = (unsigned int) ((((sin_p + 1.0f)/2.0f)*volt_cmd) * 3000); |
bwang | 0:bac9c3a3a6ca | 154 | |
bwang | 1:7b61790f6be9 | 155 | ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE; |
bwang | 1:7b61790f6be9 | 156 | ib = ((float) adval2 / 4096.0f * AVDD - I_OFFSET - ib_supp_offset) / I_SCALE; |
bwang | 2:eabe8feaaabb | 157 | ic = -ia - ib; |
bwang | 0:bac9c3a3a6ca | 158 | |
nki | 12:264e942f904f | 159 | vd = volt_cmd; |
nki | 12:264e942f904f | 160 | vq = 0.0f; |
nki | 12:264e942f904f | 161 | |
nki | 12:264e942f904f | 162 | if (vd < -1.0f) vd = -1.0f; |
nki | 12:264e942f904f | 163 | if (vd > 1.0f) vd = 1.0f; |
nki | 12:264e942f904f | 164 | if (vq < -1.0f) vq = -1.0f; |
nki | 12:264e942f904f | 165 | if (vq > 1.0f) vq = 1.0f; |
nki | 12:264e942f904f | 166 | |
nki | 12:264e942f904f | 167 | float valpha = vd * cos_p - vq * sin_p; |
nki | 12:264e942f904f | 168 | float vbeta = vd * sin_p + vq * cos_p; |
nki | 12:264e942f904f | 169 | |
nki | 12:264e942f904f | 170 | float va = valpha; |
nki | 12:264e942f904f | 171 | float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta; |
nki | 12:264e942f904f | 172 | float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta; |
nki | 12:264e942f904f | 173 | |
nki | 12:264e942f904f | 174 | set_dtc(a, 0.5f + 0.5f * va); |
nki | 12:264e942f904f | 175 | set_dtc(b, 0.5f + 0.5f * vb); |
nki | 12:264e942f904f | 176 | set_dtc(c, 0.5f + 0.5f * vc); |
nki | 12:264e942f904f | 177 | } |
nki | 12:264e942f904f | 178 | |
nki | 12:264e942f904f | 179 | int main() { |
nki | 12:264e942f904f | 180 | config_globals(); |
nki | 12:264e942f904f | 181 | startup_msg(); |
nki | 12:264e942f904f | 182 | |
nki | 12:264e942f904f | 183 | for (;;) { |
nki | 12:264e942f904f | 184 | flux_cmd_raw = pot1.read(); |
nki | 12:264e942f904f | 185 | speed_cmd_raw = pot2.read(); |
nki | 12:264e942f904f | 186 | speed_cmd = filter_speed_cmd.Update(speed_cmd_raw); |
nki | 12:264e942f904f | 187 | flux_cmd = filter_flux_cmd.Update(flux_cmd_raw); |
nki | 12:264e942f904f | 188 | |
nki | 12:264e942f904f | 189 | volt_cmd = flux_cmd * speed_cmd; // both values range from 0-1. speed corresponds to 0-x, where x is defined at the top of commutate(). |
nki | 12:264e942f904f | 190 | //increase the coefficient of flux_cmd and cap volt_cmd from 0-1 to adjust location of the transition to constant voltage:frequency |
nki | 12:264e942f904f | 191 | volt_cmd *= 2.0f; |
nki | 12:264e942f904f | 192 | if(volt_cmd > 1.0f) {volt_cmd = 1.0f;} |
nki | 12:264e942f904f | 193 | |
nki | 12:264e942f904f | 194 | |
nki | 12:264e942f904f | 195 | //pc.printf("%f\t%f\r\n", flux_cmd, speed_cmd); |
nki | 12:264e942f904f | 196 | //wait_ms(50); |
nki | 12:264e942f904f | 197 | } |
nki | 12:264e942f904f | 198 | } |
nki | 12:264e942f904f | 199 | |
nki | 12:264e942f904f | 200 | /* |
nki | 12:264e942f904f | 201 | if commutate() runs at 5kHz, |
nki | 12:264e942f904f | 202 | assume a 4 pole motor running at 6000RPM. 100RPS mechanical. 400Hz electrical. speed_cmd(0:1) should map from 0 to 400Hz. |
nki | 12:264e942f904f | 203 | since commutate() runs at 5kHz, position+= x*speed_cmd(0:1) |
nki | 12:264e942f904f | 204 | |
nki | 12:264e942f904f | 205 | in a second, position should be 400*2pi. the increment per loop is 400*2pi/5000 8*pi/50 or 4*pi/25, or about 0.5 |
nki | 12:264e942f904f | 206 | */ |
nki | 12:264e942f904f | 207 | /* |
nki | 10:6829abb438fc | 208 | float u = ia; |
nki | 10:6829abb438fc | 209 | float v = ib; |
bwang | 2:eabe8feaaabb | 210 | |
bwang | 2:eabe8feaaabb | 211 | alpha = u; |
bwang | 2:eabe8feaaabb | 212 | beta = 1 / sqrtf(3.0f) * u + 2 / sqrtf(3.0f) * v; |
bwang | 2:eabe8feaaabb | 213 | |
bwang | 2:eabe8feaaabb | 214 | d = alpha * cos_p - beta * sin_p; |
bwang | 2:eabe8feaaabb | 215 | q = -alpha * sin_p - beta * cos_p; |
bwang | 2:eabe8feaaabb | 216 | |
bwang | 3:9b20da3f0055 | 217 | float d_err = d_ref - d; |
bwang | 3:9b20da3f0055 | 218 | float q_err = q_ref - q; |
bwang | 2:eabe8feaaabb | 219 | |
bwang | 2:eabe8feaaabb | 220 | d_integral += d_err * KI; |
bwang | 2:eabe8feaaabb | 221 | q_integral += q_err * KI; |
bwang | 2:eabe8feaaabb | 222 | |
bwang | 2:eabe8feaaabb | 223 | if (q_integral > INTEGRAL_MAX) q_integral = INTEGRAL_MAX; |
bwang | 2:eabe8feaaabb | 224 | if (d_integral > INTEGRAL_MAX) d_integral = INTEGRAL_MAX; |
bwang | 2:eabe8feaaabb | 225 | if (q_integral < -INTEGRAL_MAX) q_integral = -INTEGRAL_MAX; |
bwang | 2:eabe8feaaabb | 226 | if (d_integral < -INTEGRAL_MAX) d_integral = -INTEGRAL_MAX; |
bwang | 2:eabe8feaaabb | 227 | |
bwang | 2:eabe8feaaabb | 228 | vd = KP * d_err + d_integral; |
bwang | 2:eabe8feaaabb | 229 | vq = KP * q_err + q_integral; |
nki | 12:264e942f904f | 230 | */ |