Skip to content

Commit

Permalink
implemented adc/current sensing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Donahue committed Apr 7, 2024
1 parent 7302632 commit 86231eb
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 43 deletions.
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
130 changes: 91 additions & 39 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ extern CAN_HandleTypeDef hcan2;
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim8;

extern ADC_HandleTypeDef hadc1;

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};

Expand All @@ -28,6 +31,8 @@ can_t can2; // p2p can bus with charger

/* 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 +66,8 @@ uint8_t compute_init()
HAL_TIM_PWM_Start(&htim8, fan_channels[FAN5]);
HAL_TIM_PWM_Start(&htim8, fan_channels[FAN6]);

return 0;

}

void compute_enable_charging(bool enable_charging)
Expand Down Expand Up @@ -148,46 +155,63 @@ 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 = read_ref_voltage();
float vout = read_current();

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

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

return -current;

/* 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

// 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

return -high_current;
// //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 +453,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;
}

7 changes: 7 additions & 0 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,13 @@ static void MX_ADC1_Init(void)
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = 2;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

/* USER CODE END ADC1_Init 2 */

Expand Down
73 changes: 73 additions & 0 deletions Core/Src/stm32f4xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,79 @@ void HAL_MspInit(void)
/* USER CODE END MspInit 1 */
}

/**
* @brief ADC MSP Initialization
* This function configures the hardware resources used in this example
* @param hadc: ADC handle pointer
* @retval None
*/
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hadc->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */

/* USER CODE END ADC1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_ADC1_CLK_ENABLE();

__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**ADC1 GPIO Configuration
PC5 ------> ADC1_IN15
PB0 ------> ADC1_IN8
PB1 ------> ADC1_IN9
*/
GPIO_InitStruct.Pin = I_Sense_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(I_Sense_GPIO_Port, &GPIO_InitStruct);

GPIO_InitStruct.Pin = I_SenseB0_Pin|I_SenseB1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USER CODE BEGIN ADC1_MspInit 1 */

/* USER CODE END ADC1_MspInit 1 */
}

}

/**
* @brief ADC MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hadc: ADC handle pointer
* @retval None
*/
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
{
if(hadc->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspDeInit 0 */

/* USER CODE END ADC1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_ADC1_CLK_DISABLE();

/**ADC1 GPIO Configuration
PC5 ------> ADC1_IN15
PB0 ------> ADC1_IN8
PB1 ------> ADC1_IN9
*/
HAL_GPIO_DeInit(I_Sense_GPIO_Port, I_Sense_Pin);

HAL_GPIO_DeInit(GPIOB, I_SenseB0_Pin|I_SenseB1_Pin);

/* USER CODE BEGIN ADC1_MspDeInit 1 */

/* USER CODE END ADC1_MspDeInit 1 */
}

}

static uint32_t HAL_RCC_CAN1_CLK_ENABLED=0;

/**
Expand Down

0 comments on commit 86231eb

Please sign in to comment.