Bayley Wang
/
foc-ed_in_the_bot_compact
robot
BREMS/BREMSConfig.cpp
- Committer:
- bwang
- Date:
- 2017-03-12
- Revision:
- 84:dd32640942a4
- Parent:
- 82:5e741c5ffd9f
- Child:
- 85:2280526f9bad
File content as of revision 84:dd32640942a4:
#include "mbed.h" #include "BREMSConfig.h" #include "BREMSStructs.h" #include "CommandProcessor.h" #include "PreferenceWriter.h" #include "config_pins.h" #include "config_inverter.h" #include "config_motor.h" #include "config_loop.h" #include "layout.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", "FOC'ed in the Bot Rev A."); } void BREMSInit(IOStruct *io, ReadDataStruct *read, FOCStruct *foc, ControlStruct *control) { io->en = new DigitalOut(EN); io->en->write(0); io->pc = new Serial(USBTX, USBRX); io->pc->baud(115200); io->throttle_in = new PwmIn(TH_PIN, TH_LIMIT_LOW, TH_LIMIT_HIGH, TH_LIMIT_CRAZY); 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(); read->err_throttle_disabled = false; read->err_pos_invalid = false; read->err_not_driving = false; BREMSConfigRegisters(io); wait_ms(250); BREMSZeroCurrent(read); io->pref = new PreferenceWriter(6); BREMSStartupMsg(read, io->pc); cmd_reload(io->pc, io->pref); io->pc->printf("%s", ">"); 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; control->enabled = false; }