derp

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Fri Mar 18 12:07:14 2016 +0000
Revision:
4:a6669248ce4d
Parent:
3:9b20da3f0055
Child:
5:efd3838b79a6
Child:
7:caebf421f288
moved commutation to current sampling routine

Who changed what in which revision?

UserRevisionLine numberNew 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 4:a6669248ce4d 28 void commutate();
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 4:a6669248ce4d 35 toggle = 1;
bwang 4:a6669248ce4d 36 ADC1->CR2 |= 0x40000000;
bwang 4:a6669248ce4d 37 volatile int delay;
bwang 4:a6669248ce4d 38 for (delay = 0; delay < 35; delay++);
bwang 4:a6669248ce4d 39 toggle = 0;
bwang 1:7b61790f6be9 40 adval1 = ADC1->DR;
bwang 1:7b61790f6be9 41 adval2 = ADC2->DR;
bwang 4:a6669248ce4d 42 commutate();
bwang 1:7b61790f6be9 43 }
bwang 1:7b61790f6be9 44 TIM1->SR = 0x00;
bwang 1:7b61790f6be9 45 }
bwang 1:7b61790f6be9 46
bwang 1:7b61790f6be9 47 void zero_current(){
bwang 1:7b61790f6be9 48 for (int i = 0; i < 1000; i++){
bwang 1:7b61790f6be9 49 ia_supp_offset += (float) (ADC1->DR);
bwang 1:7b61790f6be9 50 ib_supp_offset += (float) (ADC2->DR);
bwang 1:7b61790f6be9 51 ADC1->CR2 |= 0x40000000;
bwang 1:7b61790f6be9 52 wait_us(100);
bwang 1:7b61790f6be9 53 }
bwang 1:7b61790f6be9 54 ia_supp_offset /= 1000.0f;
bwang 1:7b61790f6be9 55 ib_supp_offset /= 1000.0f;
bwang 1:7b61790f6be9 56 ia_supp_offset = ia_supp_offset / 4096.0f * AVDD - I_OFFSET;
bwang 1:7b61790f6be9 57 ib_supp_offset = ib_supp_offset / 4096.0f * AVDD - I_OFFSET;
bwang 1:7b61790f6be9 58 }
bwang 0:bac9c3a3a6ca 59
bwang 0:bac9c3a3a6ca 60 void config_globals() {
bwang 0:bac9c3a3a6ca 61 pc.baud(115200);
bwang 0:bac9c3a3a6ca 62
bwang 1:7b61790f6be9 63 //Enable clocks for GPIOs
bwang 1:7b61790f6be9 64 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
bwang 1:7b61790f6be9 65 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
bwang 1:7b61790f6be9 66 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
bwang 1:7b61790f6be9 67
bwang 1:7b61790f6be9 68 RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //enable TIM1 clock
bwang 1:7b61790f6be9 69
bwang 1:7b61790f6be9 70 a = new FastPWM(PWMA);
bwang 1:7b61790f6be9 71 b = new FastPWM(PWMB);
bwang 1:7b61790f6be9 72 c = new FastPWM(PWMC);
bwang 1:7b61790f6be9 73
bwang 1:7b61790f6be9 74 NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ
bwang 1:7b61790f6be9 75
bwang 1:7b61790f6be9 76 TIM1->DIER |= TIM_DIER_UIE; //enable update interrupt
bwang 1:7b61790f6be9 77 TIM1->CR1 = 0x40; //CMS = 10, interrupt only when counting up
bwang 1:7b61790f6be9 78 TIM1->CR1 |= TIM_CR1_ARPE; //autoreload on,
bwang 1:7b61790f6be9 79 TIM1->RCR |= 0x01; //update event once per up/down count of tim1
bwang 1:7b61790f6be9 80 TIM1->EGR |= TIM_EGR_UG;
bwang 1:7b61790f6be9 81
bwang 1:7b61790f6be9 82 TIM1->PSC = 0x00; //no prescaler, timer counts up in sync with the peripheral clock
bwang 1:7b61790f6be9 83 TIM1->ARR = 0x4650; //5 Khz
bwang 1:7b61790f6be9 84 TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on.
bwang 1:7b61790f6be9 85 TIM1->CR1 |= TIM_CR1_CEN;
bwang 1:7b61790f6be9 86
bwang 1:7b61790f6be9 87 //ADC Setup
bwang 1:7b61790f6be9 88 RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1
bwang 1:7b61790f6be9 89 RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2
bwang 1:7b61790f6be9 90
bwang 1:7b61790f6be9 91 ADC->CCR = 0x00000006; //Regular simultaneous mode, 3 channels
bwang 1:7b61790f6be9 92
bwang 1:7b61790f6be9 93 ADC1->CR2 |= ADC_CR2_ADON; //ADC1 on
bwang 1:7b61790f6be9 94 ADC1->SQR3 = 0x0000004; //PA_4 as ADC1, sequence 0
bwang 0:bac9c3a3a6ca 95
bwang 1:7b61790f6be9 96 ADC2->CR2 |= ADC_CR2_ADON; //ADC2 ON
bwang 1:7b61790f6be9 97 ADC2->SQR3 = 0x00000008; //PB_0 as ADC2, sequence 1
bwang 1:7b61790f6be9 98
bwang 1:7b61790f6be9 99 GPIOA->MODER |= (1 << 8);
bwang 1:7b61790f6be9 100 GPIOA->MODER |= (1 << 9);
bwang 1:7b61790f6be9 101
bwang 1:7b61790f6be9 102 GPIOA->MODER |= (1 << 2);
bwang 1:7b61790f6be9 103 GPIOA->MODER |= (1 << 3);
bwang 1:7b61790f6be9 104
bwang 1:7b61790f6be9 105 GPIOA->MODER |= (1 << 0);
bwang 1:7b61790f6be9 106 GPIOA->MODER |= (1 << 1);
bwang 1:7b61790f6be9 107
bwang 1:7b61790f6be9 108 GPIOB->MODER |= (1 << 0);
bwang 1:7b61790f6be9 109 GPIOB->MODER |= (1 << 1);
bwang 1:7b61790f6be9 110
bwang 1:7b61790f6be9 111 GPIOC->MODER |= (1 << 2);
bwang 1:7b61790f6be9 112 GPIOC->MODER |= (1 << 3);
bwang 1:7b61790f6be9 113
bwang 1:7b61790f6be9 114 //DAC setup
bwang 1:7b61790f6be9 115 RCC->APB1ENR |= 0x20000000;
bwang 1:7b61790f6be9 116 DAC->CR |= DAC_CR_EN2;
bwang 1:7b61790f6be9 117
bwang 1:7b61790f6be9 118 GPIOA->MODER |= (1 << 10);
bwang 1:7b61790f6be9 119 GPIOA->MODER |= (1 << 11);
bwang 1:7b61790f6be9 120
bwang 1:7b61790f6be9 121 //Zero duty cycles
bwang 1:7b61790f6be9 122 set_dtc(a, 0.0f);
bwang 1:7b61790f6be9 123 set_dtc(b, 0.0f);
bwang 1:7b61790f6be9 124 set_dtc(c, 0.0f);
bwang 1:7b61790f6be9 125
bwang 1:7b61790f6be9 126 wait_ms(250);
bwang 1:7b61790f6be9 127 zero_current();
bwang 0:bac9c3a3a6ca 128 en = 1;
bwang 0:bac9c3a3a6ca 129 }
bwang 0:bac9c3a3a6ca 130
bwang 0:bac9c3a3a6ca 131 void startup_msg() {
bwang 0:bac9c3a3a6ca 132 pc.printf("%s\n\r\n\r", "FOC'ed in the Bot Rev A.");
bwang 0:bac9c3a3a6ca 133 pc.printf("%s\n\r", "====Config Data====");
bwang 0:bac9c3a3a6ca 134 pc.printf("Current Sensor Offset: %f mV\n\r", I_OFFSET);
bwang 0:bac9c3a3a6ca 135 pc.printf("Current Sensor Scale: %f mv/A\n\r", I_SCALE);
bwang 0:bac9c3a3a6ca 136 pc.printf("Bus Voltage: %f V\n\r", BUS_VOLTAGE);
bwang 0:bac9c3a3a6ca 137 pc.printf("Loop KP: %f\n\r", KP);
bwang 0:bac9c3a3a6ca 138 pc.printf("Loop KI: %f\n\r", KI);
bwang 1:7b61790f6be9 139 pc.printf("Ia offset: %f mV\n\r", ia_supp_offset);
bwang 1:7b61790f6be9 140 pc.printf("Ib offset: %f mV\n\r", ib_supp_offset);
bwang 0:bac9c3a3a6ca 141 pc.printf("\n\r");
bwang 0:bac9c3a3a6ca 142 }
bwang 0:bac9c3a3a6ca 143
bwang 4:a6669248ce4d 144 void commutate() {
bwang 2:eabe8feaaabb 145 p = pos.GetElecPosition() - POS_OFFSET;
bwang 0:bac9c3a3a6ca 146 if (p < 0) p += 2 * PI;
bwang 0:bac9c3a3a6ca 147
bwang 2:eabe8feaaabb 148 float sin_p = sinf(p);
bwang 2:eabe8feaaabb 149 float cos_p = cosf(p);
bwang 2:eabe8feaaabb 150
bwang 4:a6669248ce4d 151 //float pos_dac = 0.85f * p / (2 * PI) + 0.05f;
bwang 4:a6669248ce4d 152 //DAC->DHR12R2 = (unsigned int) (pos_dac * 4096);
bwang 0:bac9c3a3a6ca 153
bwang 1:7b61790f6be9 154 ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE;
bwang 1:7b61790f6be9 155 ib = ((float) adval2 / 4096.0f * AVDD - I_OFFSET - ib_supp_offset) / I_SCALE;
bwang 2:eabe8feaaabb 156 ic = -ia - ib;
bwang 0:bac9c3a3a6ca 157
bwang 3:9b20da3f0055 158 float u = ib;
bwang 3:9b20da3f0055 159 float v = ic;
bwang 2:eabe8feaaabb 160
bwang 2:eabe8feaaabb 161 alpha = u;
bwang 2:eabe8feaaabb 162 beta = 1 / sqrtf(3.0f) * u + 2 / sqrtf(3.0f) * v;
bwang 2:eabe8feaaabb 163
bwang 2:eabe8feaaabb 164 d = alpha * cos_p - beta * sin_p;
bwang 2:eabe8feaaabb 165 q = -alpha * sin_p - beta * cos_p;
bwang 2:eabe8feaaabb 166
bwang 3:9b20da3f0055 167 float d_err = d_ref - d;
bwang 3:9b20da3f0055 168 float q_err = q_ref - q;
bwang 2:eabe8feaaabb 169
bwang 2:eabe8feaaabb 170 d_integral += d_err * KI;
bwang 2:eabe8feaaabb 171 q_integral += q_err * KI;
bwang 2:eabe8feaaabb 172
bwang 2:eabe8feaaabb 173 if (q_integral > INTEGRAL_MAX) q_integral = INTEGRAL_MAX;
bwang 2:eabe8feaaabb 174 if (d_integral > INTEGRAL_MAX) d_integral = INTEGRAL_MAX;
bwang 2:eabe8feaaabb 175 if (q_integral < -INTEGRAL_MAX) q_integral = -INTEGRAL_MAX;
bwang 2:eabe8feaaabb 176 if (d_integral < -INTEGRAL_MAX) d_integral = -INTEGRAL_MAX;
bwang 2:eabe8feaaabb 177
bwang 2:eabe8feaaabb 178 vd = KP * d_err + d_integral;
bwang 2:eabe8feaaabb 179 vq = KP * q_err + q_integral;
bwang 2:eabe8feaaabb 180
bwang 2:eabe8feaaabb 181 if (vd < -1.0f) vd = -1.0f;
bwang 2:eabe8feaaabb 182 if (vd > 1.0f) vd = 1.0f;
bwang 2:eabe8feaaabb 183 if (vq < -1.0f) vq = -1.0f;
bwang 2:eabe8feaaabb 184 if (vq > 1.0f) vq = 1.0f;
bwang 2:eabe8feaaabb 185
bwang 4:a6669248ce4d 186 DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048);
bwang 2:eabe8feaaabb 187 //DAC->DHR12R2 = (unsigned int) (-vd * 2000 + 2048);
bwang 2:eabe8feaaabb 188
bwang 4:a6669248ce4d 189 //vd = 0.0f;
bwang 4:a6669248ce4d 190 //vq = -1.0f;
bwang 4:a6669248ce4d 191
bwang 2:eabe8feaaabb 192 float valpha = vd * cos_p - vq * sin_p;
bwang 2:eabe8feaaabb 193 float vbeta = vd * sin_p + vq * cos_p;
bwang 2:eabe8feaaabb 194
bwang 2:eabe8feaaabb 195 float va = valpha;
bwang 2:eabe8feaaabb 196 float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta;
bwang 2:eabe8feaaabb 197 float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta;
bwang 2:eabe8feaaabb 198
bwang 2:eabe8feaaabb 199 set_dtc(a, 0.5f + 0.5f * va);
bwang 2:eabe8feaaabb 200 set_dtc(b, 0.5f + 0.5f * vb);
bwang 2:eabe8feaaabb 201 set_dtc(c, 0.5f + 0.5f * vc);
bwang 0:bac9c3a3a6ca 202 }
bwang 0:bac9c3a3a6ca 203
bwang 0:bac9c3a3a6ca 204 int main() {
bwang 0:bac9c3a3a6ca 205 config_globals();
bwang 0:bac9c3a3a6ca 206 startup_msg();
bwang 0:bac9c3a3a6ca 207
bwang 0:bac9c3a3a6ca 208 for (;;) {
bwang 2:eabe8feaaabb 209 /*
bwang 2:eabe8feaaabb 210 q_ref = 0.0f;
bwang 2:eabe8feaaabb 211 wait(3);
bwang 2:eabe8feaaabb 212 toggle = state;
bwang 2:eabe8feaaabb 213 state = !state;
bwang 2:eabe8feaaabb 214 q_ref = -50.0f;
bwang 2:eabe8feaaabb 215 wait(3);
bwang 2:eabe8feaaabb 216 toggle = state;
bwang 2:eabe8feaaabb 217 state = !state;
bwang 2:eabe8feaaabb 218 */
bwang 0:bac9c3a3a6ca 219 }
bwang 0:bac9c3a3a6ca 220 }