Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
BREMS/BREMSConfig.cpp
- Committer:
- bwang
- Date:
- 2018-02-11
- Revision:
- 199:c160a2c03781
- Parent:
- 197:68fd01b73537
- Child:
- 206:5c848ea69028
File content as of revision 199:c160a2c03781:
#include "mbed.h"
#include "BREMSConfig.h"
#include "BREMSStructs.h"
#include "BufferedLogger.h"
#include "CommandProcessor.h"
#include "PreferenceWriter.h"
#include "Filter.h"
#include "hardware.h"
#include "derived.h"
#include "prefs.h"
#include "errors.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 = 0x00; //CMS = 10, interrupt only when counting up
TIM1->CR1 |= TIM_CR1_ARPE; //autoreload on,
TIM1->RCR |= 0x00; //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) (2 * (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, bool tune) {
io->en = new DigitalOut(EN);
io->en->write(0);
io->pc = new Serial(USBTX, USBRX);
io->pc->baud(115200);
init_masks();
BREMSStartupMsg(read, io->pc);
BREMS_mode = MODE_CFG;
io->pref = new PreferenceWriter(6);
cmd_reload(io->pc, io->pref);
if (_PREFS_VALID != 1) {
io->pc->printf("%s\n", "Stored config invalid");
BREMS_mode = MODE_CFG;
cmd_defaults(io->pc);
io->pc->printf("%s\n", "You should probably at least set throttle and current limits!");
}
wait_ms(750);
io->pos = new PositionSensorEncoder(_CPR, 0);
io->logger = new BufferedLogger(_LOG_PACKET_SIZE, (_LOG_PAGE_SIZE-_LOG_HEADER_SIZE)/(_LOG_PACKET_SIZE+1), LOG_TX, LOG_RX, _LOG_BAUD_RATE);
io->throttle_in = new PwmIn(TH_PIN, _TH_LIMIT_LOW, _TH_LIMIT_HIGH);
control->throttle_filter = new MedianFilter(_THROTTLE_FILTER_WINDOW);
control->velocity_filter = new MedianFilter(_W_FILTER_WINDOW);
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);
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;
io->en->write(1);
}