Bayley Wang
/
qonly_controller
derp
Diff: main.cpp
- Revision:
- 5:efd3838b79a6
- Parent:
- 4:a6669248ce4d
- Child:
- 6:561d8ab80424
--- a/main.cpp Fri Mar 18 12:07:14 2016 +0000 +++ b/main.cpp Sun Mar 27 03:40:52 2016 +0000 @@ -25,25 +25,133 @@ float last_d = 0.0f, last_q = 0.0f; float d_ref = -0.0f, q_ref = -50.0f; +float d_filtered = 0.0f, q_filtered = 0.0f; + void commutate(); void zero_current(); void config_globals(); void startup_msg(); +unsigned char clip_var(float var, float scale, float offset); extern "C" void TIM1_UP_TIM10_IRQHandler(void) { if (TIM1->SR & TIM_SR_UIF ) { - toggle = 1; + //toggle = 1; ADC1->CR2 |= 0x40000000; volatile int delay; for (delay = 0; delay < 35; delay++); - toggle = 0; adval1 = ADC1->DR; adval2 = ADC2->DR; + //toggle = 0; commutate(); } TIM1->SR = 0x00; } +void commutate() { + p = pos.GetElecPosition() - POS_OFFSET; + if (p < 0) p += 2 * PI; + + float sin_p = sinf(p); + float cos_p = cosf(p); + + //float pos_dac = 0.85f * p / (2 * PI) + 0.05f; + //DAC->DHR12R2 = (unsigned int) (pos_dac * 4096); + + ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE; + ib = ((float) adval2 / 4096.0f * AVDD - I_OFFSET - ib_supp_offset) / I_SCALE; + ic = -ia - ib; + + /* + a,b stall + a,c low torque + b,a lots of amps + b,c extra amps + + */ + float u = ib;//ib; + float v = ic;//ic; + + alpha = u; + beta = 1 / sqrtf(3.0f) * u + 2 / sqrtf(3.0f) * v; + + d = alpha * cos_p - beta * sin_p; + q = -alpha * sin_p - beta * cos_p; + + d_filtered = 0.0f * d_filtered + 1.0f * d; + q_filtered = 0.0f * q_filtered + 1.0f * q; + + float d_err = d_ref - d_filtered; + float q_err = q_ref - q_filtered; + + d_integral += d_err * KI; + q_integral += q_err * KI; + + if (q_integral > INTEGRAL_MAX) q_integral = INTEGRAL_MAX; + if (d_integral > INTEGRAL_MAX) d_integral = INTEGRAL_MAX; + if (q_integral < -INTEGRAL_MAX) q_integral = -INTEGRAL_MAX; + if (d_integral < -INTEGRAL_MAX) d_integral = -INTEGRAL_MAX; + + vd = KP * d_err + d_integral; + vq = KP * q_err + q_integral; + + if (vd < -1.0f) vd = -1.0f; + if (vd > 1.0f) vd = 1.0f; + if (vq < -1.0f) vq = -1.0f; + if (vq > 1.0f) vq = 1.0f; + + //vd = 0.0f; + //vq = 1.0f; + + //DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048); + //DAC->DHR12R2 = (unsigned int) (-vd * 2000 + 2048); + + float valpha = vd * cos_p - vq * sin_p; + float vbeta = vd * sin_p + vq * cos_p; + + float va = valpha; + float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta; + float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta; + + set_dtc(a, 0.5f + 0.5f * va); + set_dtc(b, 0.5f + 0.5f * vb); + set_dtc(c, 0.5f + 0.5f * vc); +} + +int main() { + config_globals(); + startup_msg(); + + for (;;) { + /* + toggle = state; + state = !state; + //pc.printf("01234567890123456789"); + pc.putc(clip_var(p, 30.0f, 0.0f)); + pc.putc(clip_var(ia, 10.0f, 128.0f)); + pc.putc(clip_var(ib, 10.0f, 128.0f)); + pc.putc(clip_var(d, 10.0f, 128.0f)); + pc.putc(clip_var(q, 10.0f, 128.0f)); + pc.putc(clip_var(alpha, 10.0f, 128.0f)); + pc.putc(clip_var(beta, 10.0f, 128.0f)); + pc.putc(clip_var(0.0f, 100.0f, 128.0f)); + pc.putc(clip_var(0.0f, 100.0f, 128.0f)); + pc.putc(0xff); + */ + //pc.printf("%f\n\r", p); + //wait_ms(100); + /* + q_ref = 0.0f; + wait(3); + toggle = state; + state = !state; + q_ref = -50.0f; + wait(3); + toggle = state; + state = !state; + */ + } +} + void zero_current(){ for (int i = 0; i < 1000; i++){ ia_supp_offset += (float) (ADC1->DR); @@ -80,7 +188,7 @@ TIM1->EGR |= TIM_EGR_UG; TIM1->PSC = 0x00; //no prescaler, timer counts up in sync with the peripheral clock - TIM1->ARR = 0x4650; //5 Khz + TIM1->ARR = 0x2328; //15 Khz TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on. TIM1->CR1 |= TIM_CR1_CEN; @@ -131,6 +239,8 @@ void startup_msg() { pc.printf("%s\n\r\n\r", "FOC'ed in the Bot Rev A."); pc.printf("%s\n\r", "====Config Data===="); + pc.printf("Number of pole pairs: %d\n\r", (int) POLE_PAIRS); + pc.printf("Resolver lobes: %d\n\r", (int) RESOLVER_LOBES); pc.printf("Current Sensor Offset: %f mV\n\r", I_OFFSET); pc.printf("Current Sensor Scale: %f mv/A\n\r", I_SCALE); pc.printf("Bus Voltage: %f V\n\r", BUS_VOLTAGE); @@ -141,80 +251,9 @@ pc.printf("\n\r"); } -void commutate() { - p = pos.GetElecPosition() - POS_OFFSET; - if (p < 0) p += 2 * PI; - - float sin_p = sinf(p); - float cos_p = cosf(p); - - //float pos_dac = 0.85f * p / (2 * PI) + 0.05f; - //DAC->DHR12R2 = (unsigned int) (pos_dac * 4096); - - ia = ((float) adval1 / 4096.0f * AVDD - I_OFFSET - ia_supp_offset) / I_SCALE; - ib = ((float) adval2 / 4096.0f * AVDD - I_OFFSET - ib_supp_offset) / I_SCALE; - ic = -ia - ib; - - float u = ib; - float v = ic; - - alpha = u; - beta = 1 / sqrtf(3.0f) * u + 2 / sqrtf(3.0f) * v; - - d = alpha * cos_p - beta * sin_p; - q = -alpha * sin_p - beta * cos_p; - - float d_err = d_ref - d; - float q_err = q_ref - q; - - d_integral += d_err * KI; - q_integral += q_err * KI; - - if (q_integral > INTEGRAL_MAX) q_integral = INTEGRAL_MAX; - if (d_integral > INTEGRAL_MAX) d_integral = INTEGRAL_MAX; - if (q_integral < -INTEGRAL_MAX) q_integral = -INTEGRAL_MAX; - if (d_integral < -INTEGRAL_MAX) d_integral = -INTEGRAL_MAX; - - vd = KP * d_err + d_integral; - vq = KP * q_err + q_integral; - - if (vd < -1.0f) vd = -1.0f; - if (vd > 1.0f) vd = 1.0f; - if (vq < -1.0f) vq = -1.0f; - if (vq > 1.0f) vq = 1.0f; - - DAC->DHR12R2 = (unsigned int) (-q * 20 + 2048); - //DAC->DHR12R2 = (unsigned int) (-vd * 2000 + 2048); - - //vd = 0.0f; - //vq = -1.0f; - - float valpha = vd * cos_p - vq * sin_p; - float vbeta = vd * sin_p + vq * cos_p; - - float va = valpha; - float vb = -0.5f * valpha - sqrtf(3) / 2.0f * vbeta; - float vc = -0.5f * valpha + sqrtf(3) / 2.0f * vbeta; - - set_dtc(a, 0.5f + 0.5f * va); - set_dtc(b, 0.5f + 0.5f * vb); - set_dtc(c, 0.5f + 0.5f * vc); -} - -int main() { - config_globals(); - startup_msg(); - - for (;;) { - /* - q_ref = 0.0f; - wait(3); - toggle = state; - state = !state; - q_ref = -50.0f; - wait(3); - toggle = state; - state = !state; - */ - } -} +unsigned char clip_var(float var, float scale, float offset) { + float x = var * scale + offset; + if (x < 0.0f) x = 0.0f; + if (x > 254.0f) x = 254.0f; + return (unsigned char) x; +} \ No newline at end of file