Bayley Wang
/
foc-ed_in_the_bot_compact
robot
Diff: main.cpp
- Revision:
- 17:2b852039bb05
- Parent:
- 16:f283d6032fe5
- Child:
- 18:3863ca45cf26
--- a/main.cpp Sun Oct 30 22:41:00 2016 +0000 +++ b/main.cpp Sun Oct 30 22:46:49 2016 +0000 @@ -63,100 +63,7 @@ ib_supp_offset = ib_supp_offset / 4096.0f * AVDD - I_OFFSET; } -void config_globals() { - pc.baud(115200); - - //Enable clocks for GPIOs - RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; - RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; - RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; - - RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //enable TIM1 clock - - a = new FastPWM(PWMA); - b = new FastPWM(PWMB); - c = new FastPWM(PWMC); - - NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ - - TIM1->DIER |= TIM_DIER_UIE; //enable update interrupt - TIM1->CR1 = 0x40; //CMS = 10, interrupt only when counting up - TIM1->CR1 |= TIM_CR1_ARPE; //autoreload on, - TIM1->RCR |= 0x01; //update event once per up/down count of tim1 - 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->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on. - TIM1->CR1 |= TIM_CR1_CEN; - - //ADC Setup - RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1 - RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2 - - ADC->CCR = 0x00000006; //Regular simultaneous mode, 3 channels - - ADC1->CR2 |= ADC_CR2_ADON; //ADC1 on - ADC1->SQR3 = 0x0000004; //PA_4 as ADC1, sequence 0 - - ADC2->CR2 |= ADC_CR2_ADON; //ADC2 ON - ADC2->SQR3 = 0x00000008; //PB_0 as ADC2, sequence 1 - - GPIOA->MODER |= (1 << 8); - GPIOA->MODER |= (1 << 9); - - GPIOA->MODER |= (1 << 2); - GPIOA->MODER |= (1 << 3); - - GPIOA->MODER |= (1 << 0); - GPIOA->MODER |= (1 << 1); - - GPIOB->MODER |= (1 << 0); - GPIOB->MODER |= (1 << 1); - - GPIOC->MODER |= (1 << 2); - GPIOC->MODER |= (1 << 3); - - //DAC setup - RCC->APB1ENR |= 0x20000000; - DAC->CR |= DAC_CR_EN2; - - GPIOA->MODER |= (1 << 10); - GPIOA->MODER |= (1 << 11); - - //Zero duty cycles - set_dtc(a, 0.0f); - set_dtc(b, 0.0f); - set_dtc(c, 0.0f); - - wait_ms(250); - zero_current(); - p_mech = pos.GetMechPosition(); - en = 1; -} - -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("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); - pc.printf("Pole pairs: %d\n\r", (int) POLE_PAIRS); - pc.printf("Resolver lobes: %d\n\r", (int) RESOLVER_LOBES); - pc.printf("Loop KP: %f\n\r", KP); - pc.printf("Loop KI: %f\n\r", KI); - pc.printf("Ia offset: %f mV\n\r", ia_supp_offset); - pc.printf("Ib offset: %f mV\n\r", ib_supp_offset); - pc.printf("\n\r"); -} - -void commutate() { - if(control_enabled && !throttle.get_enabled()) go_disabled(); - if(!control_enabled && throttle.get_enabled()) go_enabled(); - - p = pos.GetElecPosition() - POS_OFFSET; - if (p < 0) p += 2 * PI; - +void update_velocity() { last_p_mech = p_mech; p_mech = pos.GetMechPosition(); float dp_mech = p_mech - last_p_mech; @@ -165,6 +72,16 @@ float loop_period = (float) (TIM1->ARR) / 90.0f; float w_raw = dp_mech * (float) 1e6 / loop_period; //rad/s w = W_FILTER_STRENGTH * w + (1.0f - W_FILTER_STRENGTH) * w_raw; +} + +void commutate() { + if(control_enabled && !throttle.get_enabled()) go_disabled(); + if(!control_enabled && throttle.get_enabled()) go_enabled(); + + update_velocity(); + + p = pos.GetElecPosition() - POS_OFFSET; + if (p < 0) p += 2 * PI; q_ref = throttle.get_throttle() * Q_MAX; d_ref = 0.0f; @@ -266,3 +183,90 @@ if(a > b) return a; return b; } + +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("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); + pc.printf("Pole pairs: %d\n\r", (int) POLE_PAIRS); + pc.printf("Resolver lobes: %d\n\r", (int) RESOLVER_LOBES); + pc.printf("Loop KP: %f\n\r", KP); + pc.printf("Loop KI: %f\n\r", KI); + pc.printf("Ia offset: %f mV\n\r", ia_supp_offset); + pc.printf("Ib offset: %f mV\n\r", ib_supp_offset); + pc.printf("\n\r"); +} + +void config_globals() { + pc.baud(115200); + + //Enable clocks for GPIOs + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; + + RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //enable TIM1 clock + + a = new FastPWM(PWMA); + b = new FastPWM(PWMB); + c = new FastPWM(PWMC); + + NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); //Enable TIM1 IRQ + + TIM1->DIER |= TIM_DIER_UIE; //enable update interrupt + TIM1->CR1 = 0x40; //CMS = 10, interrupt only when counting up + TIM1->CR1 |= TIM_CR1_ARPE; //autoreload on, + TIM1->RCR |= 0x01; //update event once per up/down count of tim1 + 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->CCER |= ~(TIM_CCER_CC1NP); //Interupt when low side is on. + TIM1->CR1 |= TIM_CR1_CEN; + + //ADC Setup + RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // clock for ADC1 + RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // clock for ADC2 + + ADC->CCR = 0x00000006; //Regular simultaneous mode, 3 channels + + ADC1->CR2 |= ADC_CR2_ADON; //ADC1 on + ADC1->SQR3 = 0x0000004; //PA_4 as ADC1, sequence 0 + + ADC2->CR2 |= ADC_CR2_ADON; //ADC2 ON + ADC2->SQR3 = 0x00000008; //PB_0 as ADC2, sequence 1 + + GPIOA->MODER |= (1 << 8); + GPIOA->MODER |= (1 << 9); + + GPIOA->MODER |= (1 << 2); + GPIOA->MODER |= (1 << 3); + + GPIOA->MODER |= (1 << 0); + GPIOA->MODER |= (1 << 1); + + GPIOB->MODER |= (1 << 0); + GPIOB->MODER |= (1 << 1); + + GPIOC->MODER |= (1 << 2); + GPIOC->MODER |= (1 << 3); + + //DAC setup + RCC->APB1ENR |= 0x20000000; + DAC->CR |= DAC_CR_EN2; + + GPIOA->MODER |= (1 << 10); + GPIOA->MODER |= (1 << 11); + + //Zero duty cycles + set_dtc(a, 0.0f); + set_dtc(b, 0.0f); + set_dtc(c, 0.0f); + + wait_ms(250); + zero_current(); + p_mech = pos.GetMechPosition(); + en = 1; +}