Bayley Wang
/
foc-ed_in_the_bot_compact
robot
BREMS/BREMSConfig.cpp
- Committer:
- bwang
- Date:
- 2017-02-05
- Revision:
- 70:5e39beeb4a21
- Parent:
- 61:85a31897e719
- Child:
- 78:b8df106126a7
File content as of revision 70:5e39beeb4a21:
#include "mbed.h" #include "BREMSConfig.h" #include "BREMSStructs.h" #include "config_pins.h" #include "config_inverter.h" #include "config_motor.h" #include "config_loop.h" void BREMSConfigRegisters(IOStruct *io) { RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; //enable TIM1 clock io->a = new FastPWM(PWMA); io->b = new FastPWM(PWMB); io->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 = (int) ((float) 9e7 / F_SW); 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); set_dtc(io->a, 0.0f); set_dtc(io->b, 0.0f); set_dtc(io->c, 0.0f); } void BREMSZeroCurrent(ReadDataStruct *read) { for (int i = 0; i < 1000; i++){ read->ia_supp_offset += (float) (ADC1->DR); read->ib_supp_offset += (float) (ADC2->DR); ADC1->CR2 |= 0x40000000; wait_us(100); } read->ia_supp_offset /= 1000.0f; read->ib_supp_offset /= 1000.0f; read->ia_supp_offset = read->ia_supp_offset / 4096.0f * AVDD - I_OFFSET; read->ib_supp_offset = read->ib_supp_offset / 4096.0f * AVDD - I_OFFSET; } void BREMSStartupMsg(ReadDataStruct *read, Serial *pc) { 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("Switching Frequency: %f KHz \n\r", F_SW / 1000.0f); pc->printf("Polling Frequency: %f Hz \n\r", F_SLOW_LOOP); pc->printf("Pole pairs: %d\n\r", (int) POLE_PAIRS); pc->printf("Resolver lobes: %d\n\r", (int) RESOLVER_LOBES); pc->printf("Loop KP_D: %f\n\r", KP_D); pc->printf("Loop KI_D: %f\n\r", KI_D); pc->printf("Loop KP_Q: %f\n\r", KP_Q); pc->printf("Loop KI_Q: %f\n\r", KI_Q); pc->printf("Ia offset: %f mV\n\r", read->ia_supp_offset); pc->printf("Ib offset: %f mV\n\r", read->ib_supp_offset); pc->printf("\n\r"); } void BREMSInit(IOStruct *io, ReadDataStruct *read, FOCStruct *foc, ControlStruct *control, bool tune) { io->en = new DigitalOut(EN); io->en->write(0); io->pc = new Serial(USBTX, USBRX); io->pc->baud(921600); io->throttle_in = new PwmIn(TH_PIN, TH_LIMIT_LOW, TH_LIMIT_HIGH); io->pos = new PositionSensorEncoder(CPR, 0); read->vbus = BUS_VOLTAGE; read->w = 0.0f; read->ia_supp_offset = 0.0f; read->ib_supp_offset = 0.0f; read->p_mech = io->pos->GetMechPosition(); BREMSConfigRegisters(io); wait_ms(250); BREMSZeroCurrent(read); BREMSStartupMsg(read, io->pc); control->d_integral = 0.0f; control->q_integral = 0.0f; control->d_filtered = 0.0f; control->q_filtered = 0.0f; control->last_d = 0.0f; control->last_q = 0.0f; control->d_ref = 0.0f; control->q_ref = 0.0f; control->torque_percent = 0.0f; io->en->write(1); }