Skip to content

Commit

Permalink
Feature/adc (#91)
Browse files Browse the repository at this point in the history
* updated pins to adc in ioc

* updated for 5v ref pin

* added driver files

* implemented adc/current sensing

* added DMA to ioc

* added DMA adc checks

* ioc issues

* ^

* updated makefile to add drivers

* verified adc

---------

Co-authored-by: Dylan Donahue <[email protected]>
  • Loading branch information
dyldonahue and Dylan Donahue authored Apr 10, 2024
1 parent 3ab21dc commit fa2114a
Show file tree
Hide file tree
Showing 107 changed files with 149,877 additions and 137,291 deletions.
50 changes: 25 additions & 25 deletions .mxproject

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Core/Inc/bmsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define BMS_CONFIG_H

// Hardware definition
#define NUM_SEGMENTS 4
#define NUM_CHIPS NUM_SEGMENTS*2
#define NUM_CELLS_PER_CHIP 9
#define NUM_SEGMENTS 6
#define NUM_CHIPS NUM_SEGMENTS* 2
#define NUM_CELLS_PER_CHIP 10
#define NUM_THERMS_PER_CHIP 32
#define NUM_RELEVANT_THERMS 5

Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define CHARGE_DETECT 5
#define CHARGER_BAUD 250000U
#define MC_BAUD 1000000U
#define MAX_ADC_RESOLUTION 1023 // 13 bit ADC
#define MAX_ADC_RESOLUTION 4095 // 12 bit ADC



Expand Down
2 changes: 0 additions & 2 deletions Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ void Error_Handler(void);
#define FPGA_Reset_GPIO_Port GPIOC
#define Communication_GPIO_Pin GPIO_PIN_14
#define Communication_GPIO_GPIO_Port GPIOC
#define Communication_GPIOC15_Pin GPIO_PIN_15
#define Communication_GPIOC15_GPIO_Port GPIOC
#define Communication_GPIOC0_Pin GPIO_PIN_0
#define Communication_GPIOC0_GPIO_Port GPIOC
#define SPI_2_CS_Pin GPIO_PIN_1
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/stm32f4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define HAL_MODULE_ENABLED

/* #define HAL_CRYP_MODULE_ENABLED */
/* #define HAL_ADC_MODULE_ENABLED */
#define HAL_ADC_MODULE_ENABLED
#define HAL_CAN_MODULE_ENABLED
/* #define HAL_CRC_MODULE_ENABLED */
/* #define HAL_CAN_LEGACY_MODULE_ENABLED */
Expand Down
1 change: 1 addition & 0 deletions Core/Inc/stm32f4xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA2_Stream0_IRQHandler(void);
/* USER CODE BEGIN EFP */

/* USER CODE END EFP */
Expand Down
148 changes: 109 additions & 39 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ extern CAN_HandleTypeDef hcan2;
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim8;

extern ADC_HandleTypeDef hadc1;
extern DMA_HandleTypeDef hdma_adc1;

TIM_OC_InitTypeDef pwm_config;
ADC_ChannelConfTypeDef adc_config;

const uint32_t fan_channels[6] = {TIM_CHANNEL_3, TIM_CHANNEL_1, TIM_CHANNEL_4, TIM_CHANNEL_3, TIM_CHANNEL_2, TIM_CHANNEL_1};

can_t can1; // main can bus, used by most peripherals
can_t can2; // p2p can bus with charger

uint32_t adc_values[2] = {0};

/* private function defintions */
uint8_t calc_charger_led_state();
float read_ref_voltage();
float read_vout();

uint8_t compute_init()
{
Expand Down Expand Up @@ -61,6 +69,11 @@ uint8_t compute_init()
HAL_TIM_PWM_Start(&htim8, fan_channels[FAN5]);
HAL_TIM_PWM_Start(&htim8, fan_channels[FAN6]);

//HAL_ADC_Start(&hadc1);
//HAL_ADC_Start_DMA(&hadc1, adc_values, 2);

return 0;

}

void compute_enable_charging(bool enable_charging)
Expand Down Expand Up @@ -148,46 +161,75 @@ void compute_set_fault(int fault_state)

int16_t compute_get_pack_current()
{
static const float CURRENT_LOWCHANNEL_MAX = 75.0; // Amps
static const float CURRENT_LOWCHANNEL_MIN = -75.0; // Amps
// static const float CURRENT_SUPPLY_VOLTAGE = 5.038;
static const float CURRENT_ADC_RESOLUTION = 5.0 / MAX_ADC_RESOLUTION;

static const float CURRENT_LOWCHANNEL_OFFSET = 2.517; // Calibrated with current = 0A
static const float CURRENT_HIGHCHANNEL_OFFSET = 2.520; // Calibrated with current = 0A

static const float HIGHCHANNEL_GAIN = 1 / 0.004; // Calibrated with current = 5A, 10A, 20A
static const float LOWCHANNEL_GAIN = 1 / 0.0267;

static const float REF5V_DIV = 19.02 / (19.08 + 19.02); // Resistive divider in kOhm
static const float REF5V_CONV = 1 / REF5V_DIV; // Converting from reading to real value

//TODO ADD BACK THE COMMENTED OUT ANALOG READS
float ref_5V = /*analogRead(MEAS_5VREF_PIN) * */(3.3 / MAX_ADC_RESOLUTION) * REF5V_CONV;
int16_t high_current
= 10
/* * (((5 / ref_5V) * /analogRead(CURRENT_SENSOR_PIN_L) * CURRENT_ADC_RESOLUTION))
- CURRENT_HIGHCHANNEL_OFFSET) */
* HIGHCHANNEL_GAIN; // Channel has a large range with low resolution
int16_t low_current
= 10
/* * (((5 / ref_5V) * (analogRead(CURRENT_SENSOR_PIN_H) * CURRENT_ADC_RESOLUTION))
- CURRENT_LOWCHANNEL_OFFSET) */
* LOWCHANNEL_GAIN; // Channel has a small range with high resolution

// Serial.print("High: ");
// Serial.println(-high_current);
// Serial.print("Low: ");
// Serial.println(-low_current);
// Serial.print("5V: ");
// Serial.println(ref_5V);

// If the current is scoped within the range of the low channel, use the low channel
if (low_current < CURRENT_LOWCHANNEL_MAX - 5.0 || low_current > CURRENT_LOWCHANNEL_MIN + 5.0) {
return -low_current;
}
static const float GAIN = 6.250; // mV/A
static const OFFSET = 0.0; // mV


/* starting equation : Vout = Vref + Voffset + (Gain * Ip) */
//float ref_voltage = -1;
//float vout = -1;

// if (!HAL_DMA_PollForTransfer(&hdma_adc1, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY))
// {
// ref_voltage = 2.5;//adc_values[1] * 2.5 / MAX_ADC_RESOLUTION;
// vout = adc_values[0] * 3.3 / MAX_ADC_RESOLUTION;
// }


// remove once DMA verified working, along with functions themselves

float ref_voltage = read_ref_voltage();
float vout = read_vout();
vout *= 1000; // convert to mV

//if (ref_voltage == -1 || vout == -1) return -1;

int16_t current = (vout - ref_voltage - OFFSET) / (GAIN / 1000); // convert to V

return vout;

/* TEMP keep last years math until above is verified */

// static const float CURRENT_LOWCHANNEL_MAX = 75.0; // Amps
// static const float CURRENT_LOWCHANNEL_MIN = -75.0; // Amps
// // static const float CURRENT_SUPPLY_VOLTAGE = 5.038;
// static const float CURRENT_ADC_RESOLUTION = 5.0 / MAX_ADC_RESOLUTION;

// static const float CURRENT_LOWCHANNEL_OFFSET = 2.517; // Calibrated with current = 0A
// static const float CURRENT_HIGHCHANNEL_OFFSET = 2.520; // Calibrated with current = 0A

return -high_current;
// static const float HIGHCHANNEL_GAIN = 1 / 0.004; // Calibrated with current = 5A, 10A, 20A
// static const float LOWCHANNEL_GAIN = 1 / 0.0267;

// static const float REF5V_DIV = 19.02 / (19.08 + 19.02); // Resistive divider in kOhm
// static const float REF5V_CONV = 1 / REF5V_DIV; // Converting from reading to real value

// //TODO ADD BACK THE COMMENTED OUT ANALOG READS
// float ref_5V = /*analogRead(MEAS_5VREF_PIN) * */(3.3 / MAX_ADC_RESOLUTION) * REF5V_CONV;
// int16_t high_current
// = 10
// /* * (((5 / ref_5V) * /analogRead(CURRENT_SENSOR_PIN_L) * CURRENT_ADC_RESOLUTION))
// - CURRENT_HIGHCHANNEL_OFFSET) */
// * HIGHCHANNEL_GAIN; // Channel has a large range with low resolution
// int16_t low_current
// = 10
// /* * (((5 / ref_5V) * (analogRead(CURRENT_SENSOR_PIN_H) * CURRENT_ADC_RESOLUTION))
// - CURRENT_LOWCHANNEL_OFFSET) */
// * LOWCHANNEL_GAIN; // Channel has a small range with high resolution

// // Serial.print("High: ");
// // Serial.println(-high_current);
// // Serial.print("Low: ");
// // Serial.println(-low_current);
// // Serial.print("5V: ");
// // Serial.println(ref_5V);

// // If the current is scoped within the range of the low channel, use the low channel
// if (low_current < CURRENT_LOWCHANNEL_MAX - 5.0 || low_current > CURRENT_LOWCHANNEL_MIN + 5.0) {
// return -low_current;
// }

// return -high_current;
}

void compute_send_mc_message(uint16_t user_max_charge, uint16_t user_max_discharge)
Expand Down Expand Up @@ -429,3 +471,31 @@ uint8_t calc_charger_led_state(acc_data_t* bms_data)
}
}

float read_ref_voltage()
{
adc_config.Channel = ADC_CHANNEL_9;
if (HAL_ADC_ConfigChannel(&hadc1, &adc_config) != HAL_OK) return -1;

HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

/* scaled to 2.5 as per datasheet */
float ref_voltage = HAL_ADC_GetValue(&hadc1) * 2.5 / MAX_ADC_RESOLUTION;

return ref_voltage;
}

float read_vout()
{
adc_config.Channel = ADC_CHANNEL_15;
if (HAL_ADC_ConfigChannel(&hadc1, &adc_config) != HAL_OK) return -1;

HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

/* scaled to 3.3 */
float vout = HAL_ADC_GetValue(&hadc1) * 3.3 / MAX_ADC_RESOLUTION;

return vout;
}

Loading

0 comments on commit fa2114a

Please sign in to comment.