Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Temperature calculation and fans for charging #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions Vehicle/Main_Control_Unit/Main_Control_Unit.ino
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,6 @@ void loop() {
*/
if (mcu_status.get_state() < MCU_STATE_READY_TO_DRIVE && timer_motor_controller_send.check()) {
MC_command_message mc_command_message = MC_command_message(0, 0, 1, 0, 0, 0);

// if (mcu_status.get_state() >= MCU_STATE_ENABLING_INVERTER) {
// mc_command_message.set_inverter_enable(true);
// }

mc_command_message.write(tx_msg.buf);
tx_msg.id = ID_MC_COMMAND_MESSAGE;
tx_msg.len = 8;
Expand Down Expand Up @@ -410,6 +405,16 @@ void parse_can_message() {

if (rx_msg.id == ID_BMS_STATUS) {
bms_status.load(rx_msg.buf);

/*
* Turn on accumulator fans when BMS is balancing
*/
if (bms_status.get_state() > BMS_STATE_CHARGING) {
digitalWrite(FAN_1, HIGH);
}
else {
digitalWrite(FAN_1, LOW);
}
}

if (rx_msg.id == ID_BMS_TEMPERATURES) {
Expand Down Expand Up @@ -521,9 +526,22 @@ void read_status_values() {
mcu_status.set_shutdown_f_above_threshold(analogRead(SENSE_SHUTDOWN_F) > SHUTDOWN_F_HIGH);

/*
* Measure the temperature from on-board thermistors
* Calculate the resistance of the thermistor based on the ADC reading
* R = 10k * ((5V / Vout) + 1)
*/
mcu_status.set_temperature(ADC.read_adc(ADC_TEMPSENSE_CHANNEL) * 100); // send temperatures in 0.01 C
double thermistor_resistance = 1e4 * ((4095.0 / ADC.read_adc(ADC_TEMPSENSE_CHANNEL)) + 1);

/*
* Temperature equation (in Kelvin) based on resistance is the following:
* 1/T = 1/T0 + (1/B) * ln(R/R0) (R = thermistor resistance)
* T = 1/(1/T0 + (1/B) * ln(R/R0))
*/
double T0 = 298.15; // 25C in Kelvin
double b = 3380; // B constant of the thermistor
double R0 = 10000; // Resistance of thermistor at 25C
double temperature = 1 / ((1 / T0) + (1 / b) * log(thermistor_resistance / R0)) - (double) 273.15;

mcu_status.set_temperature(temperature * 100); // send temperatures in 0.01 C
}

/*
Expand Down