Bayley Wang
/
qonly_controller
derp
Diff: main.cpp
- Revision:
- 13:728ddd6939d3
- Parent:
- 12:0811d08424e1
--- a/main.cpp Mon Apr 18 21:06:26 2016 +0000 +++ b/main.cpp Mon Apr 18 21:53:15 2016 +0000 @@ -46,17 +46,12 @@ FastPWM *c; DigitalOut en(EN); -InterruptIn throttle_interrupt(PB_8); +DigitalIn throttle_in(PB_8); PositionSensorEncoder pos(CPR, 0); int adval1, adval2; float ia, ib, ic, alpha, beta, q; double vq = 0.0, q_integral = 0.0, last_q = 0.0; -volatile float throttle = 0.0f, throttle_scaler = 0.0f; - -volatile int edge_seen = 0, watchdog_divider = 0; - -volatile int err_throttle_too_low = 0, err_throttle_too_high = 0; extern "C" void TIM1_UP_TIM10_IRQHandler(void) { if (TIM1->SR & TIM_SR_UIF) { @@ -87,7 +82,7 @@ q = -alpha * sin_p - beta * cos_p; - DAC->DHR12R2 = throttle; + float throttle_scaler = 1.0f; double q_err = Q_REF_MAX * (double) throttle_scaler - q; @@ -103,57 +98,16 @@ //DAC->DHR12R2 = (unsigned int) (vq * 2000 + 2048); //DAC->DHR12R2 = (unsigned int) (err_throttle_too_low * 2000 + 2048); + + vq = 1.0f; *a = 0.5f + 0.5f * vq * sinf(p); *b = 0.5f + 0.5f * vq * sinf(p + 2 * PI / 3); *c = 0.5f + 0.5f * vq * sinf(p - 2 * PI / 3); - - watchdog_divider++; - if (watchdog_divider == 15000) { - watchdog_divider = 0; - edge_seen = 0; - } } TIM1->SR = 0x00; } -void throttle_rise() { - TIM5->CNT = 0; - edge_seen = 1; -} - -void throttle_fall() { - throttle = TIM5->CNT; - - /* - if (throttle < THROTTLE_LOW) { - err_throttle_too_low = 1; - } else { - err_throttle_too_low = 0; - } - - if (throttle > THROTTLE_HIGH) { - err_throttle_too_high = 1; - } else { - err_throttle_too_high = 0; - } - */ - - if (throttle < THROTTLE_MIN) throttle = THROTTLE_MIN; - if (throttle > THROTTLE_MAX) throttle = THROTTLE_MAX; - throttle_scaler = (throttle - THROTTLE_MIN) / (THROTTLE_MAX - THROTTLE_MIN); - - /* - if (err_throttle_too_low || err_throttle_too_high || !edge_seen) { - en = 0; - } else { - en = 1; - } - */ - - edge_seen = 1; -} - int main() { //Enable clocks for GPIOs RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; @@ -180,12 +134,6 @@ TIM1->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on. TIM1->CR1 |= TIM_CR1_CEN; - TIM5->CR1 |= TIM_CR1_ARPE; - TIM5->EGR |= TIM_EGR_UG; - TIM5->PSC = 0x00; - TIM5->ARR = 0xFFFFFFFF; - TIM5->CR1 |= TIM_CR1_CEN; - //ADC Setup RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1 RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2 @@ -224,11 +172,7 @@ *b = 0.0f; *c = 0.0f; - en = 1; + throttle_in.mode(PullUp); - throttle_interrupt.rise(throttle_rise); - throttle_interrupt.fall(throttle_fall); - - for (;;) { - } + en = 1; }