robot

Dependencies:   FastPWM3 mbed

Revision:
42:030e0ec4eac5
Child:
44:3fd6a43b91f0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BREMS/BREMSConfig.cpp	Fri Jan 06 07:54:22 2017 +0000
@@ -0,0 +1,133 @@
+#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);
+}
+
+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("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", 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(115200);
+    
+    io->throttle_in = new PwmIn(TH_PIN, TH_LIMIT_LOW, TH_LIMIT_HIGH);
+    io->pos = new PositionSensorEncoder(CPR, 0);
+    
+    BREMSConfigRegisters(io);
+    
+    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();
+    
+    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;
+    
+    set_dtc(io->a, 0.0f);
+    set_dtc(io->b, 0.0f);
+    set_dtc(io->c, 0.0f);
+    
+    wait_ms(250);
+    BREMSZeroCurrent(read);
+    io->en->write(1);
+}
\ No newline at end of file