derp

Dependencies:   FastPWM3 mbed

Committer:
bwang
Date:
Sun Mar 27 03:40:52 2016 +0000
Revision:
5:efd3838b79a6
Parent:
4:a6669248ce4d
Child:
6:561d8ab80424
broken emrax code

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