Skip to content

Commit

Permalink
Merge pull request #85 from hytech-racing/coulomb_counting
Browse files Browse the repository at this point in the history
Added Coulomb Counting code
  • Loading branch information
jhwang04 committed Aug 3, 2024
2 parents db987fa + e8e4584 commit 481a78c
Show file tree
Hide file tree
Showing 15 changed files with 667 additions and 166 deletions.
18 changes: 11 additions & 7 deletions lib/interfaces/include/AMSInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class AMSInterface
charge_(0.0f),
SoC_(0.0f),
has_initialized_charge_(false),
has_received_bms_voltage_(false),
initialization_startup_interval_(DEFAULT_INITIALIZATION_WAIT_INTERVAL) {};
has_received_bms_voltage_(false) {};

/* Overloaded constructor that only takes in software OK pin and uses default voltages and temp*/
AMSInterface(CANBufferType *msg_output_queue, int sw_ok_pin):
Expand Down Expand Up @@ -248,13 +247,18 @@ class AMSInterface
/**
* Stores whether or not this AMSInterface has initialized SoC_ or not.
*/
bool has_initialized_charge_;
bool has_initialized_charge_;

bool has_received_bms_voltage_;

unsigned long initialization_startup_interval_;
/**
* Stores whether or not this AMSInterface has properly received a bms_voltage CAN
* message.
*/
bool has_received_bms_voltage_;

unsigned long timestamp_start_;
/**
* Stores the time, in milliseconds, when this AMSInterface first received a bms voltages CAN message.
*/
unsigned long timestamp_start_;


// Check if lowest cell temperature is below threshold
Expand Down
1 change: 0 additions & 1 deletion lib/interfaces/include/MessageQueueDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "FlexCAN_T4.h"

// does this need to be 3 params like our original definitions of the buffers?
using CANBufferType = Circular_Buffer<uint8_t, (uint32_t)128, sizeof(CAN_message_t)>;

#endif
32 changes: 16 additions & 16 deletions lib/interfaces/src/AMSInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void AMSInterface::init(SysTick_s &initial_tick) {

last_tick_ = initial_tick;

timestamp_start_ = last_tick_.millis;
timestamp_start_ = -1; //starts at -1

// Initializes the bms_voltages_ member variable to an invalid state. This will
// get overridden once retrieve_voltage_CAN() has been called at least once.
Expand Down Expand Up @@ -78,7 +78,9 @@ float AMSInterface::get_filtered_min_cell_voltage() {

float AMSInterface::initialize_charge() {
int i = 0;
while (HYTECH_low_voltage_ro_fromS(bms_voltages_.low_voltage_ro) - VOLTAGE_LOOKUP_TABLE[i] < 0) {
float lowest_voltage = HYTECH_low_voltage_ro_fromS(bms_voltages_.low_voltage_ro);

while (lowest_voltage - VOLTAGE_LOOKUP_TABLE[i] < 0) {
i++;
}
charge_ = ( (100 - i) / 100.0) * MAX_PACK_CHARGE;
Expand Down Expand Up @@ -116,19 +118,6 @@ void AMSInterface::calculate_SoC_acu(const SysTick_s &tick) {

void AMSInterface::tick(const SysTick_s &tick) {

// If AMSInterface has a valid reading in bms_voltages_ and the charge is not
// yet initialized, then call initialize_charge.
if ((!has_initialized_charge_) && (has_received_bms_voltage_) && ((tick.millis - timestamp_start_) > initialization_startup_interval_)) {

bool bms_voltages_is_invalid = bms_voltages_.low_voltage_ro == 0xFFFFU && bms_voltages_.high_voltage_ro == 0x1111U;

if (!bms_voltages_is_invalid) {
initialize_charge();
has_initialized_charge_ = true;
}

}

// Only calculate the updated SoC if charge has been properly initialized.
if (has_initialized_charge_) {
// Do not edit this block! If both calculate_SoC_em AND calculate_SoC_acu are run,
Expand All @@ -140,11 +129,20 @@ void AMSInterface::tick(const SysTick_s &tick) {
}
}

// If AMSInterface has a valid reading in bms_voltages_ and enough time has passed since init(), then initialize charge
if (!has_initialized_charge_ && ((tick.millis - timestamp_start_) >= DEFAULT_INITIALIZATION_WAIT_INTERVAL)) {

initialize_charge();
has_initialized_charge_ = true;

}

// Send CAN message
// enqueue_state_of_charge_CAN();
STATE_OF_CHARGE_t soc_struct;
soc_struct.charge_percentage_ro = HYTECH_charge_percentage_ro_toS(SoC_);
soc_struct.charge_coulombs_ro = HYTECH_charge_coulombs_ro_toS(charge_);
soc_struct.min_cell_voltage_est_ro = HYTECH_min_cell_voltage_est_ro_toS(VOLTAGE_LOOKUP_TABLE[100 - (int) SoC_]);
enqueue_new_CAN<STATE_OF_CHARGE_t>(&soc_struct, Pack_STATE_OF_CHARGE_hytech);

last_tick_ = tick;
Expand All @@ -162,9 +160,11 @@ void AMSInterface::retrieve_temp_CAN(CAN_message_t &recvd_msg) {

void AMSInterface::retrieve_voltage_CAN(CAN_message_t &can_msg) {
Unpack_BMS_VOLTAGES_hytech(&bms_voltages_, can_msg.buf, can_msg.len);

if (!has_received_bms_voltage_)
{
has_received_bms_voltage_ = true;
timestamp_start_ = last_tick_.millis;
}

}
Expand All @@ -176,6 +176,7 @@ void AMSInterface::retrieve_em_measurement_CAN(CAN_message_t &can_msg) {
void AMSInterface::retrieve_current_shunt_CAN(const CAN_message_t &can_msg) {
Unpack_ACU_SHUNT_MEASUREMENTS_hytech(&acu_shunt_measurements_, can_msg.buf, can_msg.len);
}

void AMSInterface::calculate_acc_derate_factor() {
float voltage_lim_factor = 1.0;
float startDerateVoltage = 3.5;
Expand Down Expand Up @@ -203,7 +204,6 @@ void AMSInterface::calculate_acc_derate_factor() {
float AMSInterface::get_acc_derate_factor() {
calculate_acc_derate_factor();
return acc_derate_factor;

}


140 changes: 111 additions & 29 deletions lib/mock_interfaces/AMSInterface.h
Original file line number Diff line number Diff line change
@@ -1,52 +1,134 @@
#ifndef __AMS_INTERFACE_H__
#define __AMS_INTERFACE_H__
#ifndef __AMSINTERFACE_H__
#define __AMSINTERFACE_H__

#include "SysClock.h"

const unsigned long HEARTBEAT_INTERVAL = 20; // milliseconds
#define CANBufferType_mock int
#define CAN_message_t_mock int

/**
* Mock interface for testing purposes. Please see the real AMSInterface for proper documentation.
*/

const unsigned long HEARTBEAT_INTERVAL = 2000; // milliseconds
const unsigned long PACK_CHARGE_CRIT_TOTAL_THRESHOLD = 420;
const unsigned long PACK_CHARGE_CRIT_LOWEST_CELL_THRESHOLD = 35000;
const unsigned long PACK_CHARGE_CRIT_LOWEST_CELL_THRESHOLD = 35000; //equivalent to 3.5V

const float DEFAULT_INIT_TEMP = 40.0;
const float DEFAULT_INIT_VOLTAGE = 3.5;
const float DEFAULT_TEMP_ALPHA = 0.8;
const float DEFAULT_VOLTAGE_ALPHA = 0.8;
const unsigned short MAX_PACK_CHARGE = 48600;
const unsigned long DEFAULT_INITIALIZATION_WAIT_INTERVAL = 5000;

class AMSInterface
{
public:
AMSInterface(int sw_ok_pin, float init_temp, float init_volt, float temp_alpha, float volt_alpha){
// Set pin mode
};
/* Initialize interface pin mode */
void init(unsigned long curr_millis) {set_heartbeat(curr_millis);}

/* Write to Main ECU */
// Initialize output value

AMSInterface(CANBufferType_mock *msg_output_queue, int sw_ok_pin, float init_temp, float init_volt, float temp_alpha, float volt_alpha):
msg_queue_(msg_output_queue),
pin_software_ok_(sw_ok_pin),
filtered_max_cell_temp(init_temp),
filtered_min_cell_voltage(init_volt),
cell_temp_alpha(temp_alpha),
cell_voltage_alpha(volt_alpha),
use_em_for_soc_(true),
charge_(0.0f),
SoC_(0.0f),
has_initialized_charge_(false),
has_received_bms_voltage_(false) {};

AMSInterface(CANBufferType_mock *msg_output_queue, int sw_ok_pin):
AMSInterface(msg_output_queue, sw_ok_pin, DEFAULT_INIT_TEMP, DEFAULT_INIT_VOLTAGE, DEFAULT_TEMP_ALPHA, DEFAULT_VOLTAGE_ALPHA) {};


void init(SysTick_s &initial_tick) {};
void set_start_state() {};
// Set output value
bool heartbeat_received(unsigned long curr_millis) {return ((curr_millis - last_heartbeat_time_) < HEARTBEAT_INTERVAL);};
bool pack_charge_is_critical() {};

//SETTERS//
void set_state_ok_high(bool ok_high) {};

/* Monitor AMS state */
void set_heartbeat(unsigned long curr_millis) {last_heartbeat_time = curr_millis;}
bool heartbeat_received(unsigned long curr_millis)
{
return ((curr_millis - last_heartbeat_time) < HEARTBEAT_INTERVAL);
void set_heartbeat(unsigned long curr_millis) {last_heartbeat_time_ = curr_millis;};

//GETTERS//
float get_filtered_max_cell_temp() {};
float get_filtered_min_cell_voltage() {};
float get_acc_derate_factor() {};

float initialize_charge() {};
void calculate_SoC_em(const SysTick_s &tick) {};
void calculate_SoC_acu(const SysTick_s &tick) {};
float get_SoC() {return SoC_;}
void tick(const SysTick_s &tick) {};

void retrieve_status_CAN(unsigned long curr_millis, CAN_message_t_mock &recvd_msg) {};
void retrieve_temp_CAN(CAN_message_t_mock &recvd_msg) {};
void retrieve_voltage_CAN(CAN_message_t_mock &recvd_msg) {};
void calculate_acc_derate_factor() {};

void retrieve_em_measurement_CAN(CAN_message_t_mock &can_msg) {};
void retrieve_current_shunt_CAN(const CAN_message_t_mock &can_msg) {};
bool is_using_em_for_soc() {return use_em_for_soc_;}
void set_use_em_for_soc(bool new_use_em_for_soc) {
use_em_for_soc_ = new_use_em_for_soc;
}
bool is_below_pack_charge_critical_low_thresh() {};
bool is_below_pack_charge_critical_total_thresh();
bool pack_charge_is_critical() {};
void enqueue_state_of_charge_CAN() {};

/* IIR filtered AMS readings */
float get_filtered_max_cell_temp();
float get_filtered_min_cell_voltage();
/** Helper function to enqueue CAN messages using the new CAN library*/
//template <typename U>
//void enqueue_new_CAN(U *structure, uint32_t (*pack_function)(U *, uint8_t *, uint8_t *, uint8_t *));

// Getters (for testing purposes)
//BMS_VOLTAGES_t get_bms_voltages() {return bms_voltages_;}
//EM_MEASUREMENT_t get_em_measurements() {return em_measurements_;}
//ACU_SHUNT_MEASUREMENTS_t get_acu_shunt_measurements() {return acu_shunt_measurements_;}


/* Retrieve CAN */
private:

const float VOLTAGE_LOOKUP_TABLE[101] = {3.972, 3.945, 3.918, 3.891, 3.885, 3.874, 3.864, 3.858, 3.847, 3.836, 3.82, 3.815, 3.815, 3.798, 3.788,
3.782, 3.771, 3.755, 3.744, 3.744, 3.733, 3.728, 3.723, 3.712, 3.701, 3.695, 3.69, 3.679, 3.679, 3.668, 3.663, 3.657, 3.647,
3.647, 3.636, 3.625, 3.625, 3.625, 3.614, 3.609, 3.603, 3.603, 3.592, 3.592, 3.592, 3.581, 3.581, 3.571, 3.571, 3.571, 3.56,
3.56, 3.56, 3.549, 3.549, 3.549, 3.549, 3.538, 3.538, 3.551, 3.546, 3.535, 3.535, 3.535, 3.53, 3.524, 3.524, 3.524, 3.513,
3.513, 3.513, 3.503, 3.503, 3.492, 3.492, 3.492, 3.487, 3.481, 3.481, 3.476, 3.471, 3.46, 3.46, 3.449, 3.444, 3.428, 3.428,
3.417, 3.401, 3.39, 3.379, 3.363, 3.331, 3.299, 3.267, 3.213, 3.149, 3.041, 3, 3, 0};

CANBufferType_mock *msg_queue_;

/* software OK pin */
int pin_software_ok_;

/* AMS CAN messages */
// Outbound
//BMS_status bms_status_;
//BMS_temperatures bms_temperatures_;
//ACU_SHUNT_MEASUREMENTS_t acu_shunt_measurements_;
//EM_MEASUREMENT_t em_measurements_;
//BMS_VOLTAGES_t bms_voltages_;

/* AMS last heartbeat time */
unsigned long last_heartbeat_time;
unsigned long last_heartbeat_time_;

/* IIR filter parameters */
float bms_high_temp;
float bms_low_voltage;
float filtered_max_cell_temp;
float filtered_min_cell_voltage;
float cell_temp_alpha;
float cell_voltage_alpha;

float acc_derate_factor;
bool use_em_for_soc_ = true;
float charge_;
float SoC_;
SysTick_s last_tick_;
bool has_initialized_charge_;
bool has_received_bms_voltage_;
unsigned long timestamp_start_;

bool is_below_pack_charge_critical_low_thresh();
bool is_below_pack_charge_critical_total_thresh();

};

#endif /* __AMS_INTERFACE_H__ */
#endif /* __AMSINTERFACE_H__ */
45 changes: 23 additions & 22 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ lib_ignore =
test_ignore=
test_interfaces*
lib_deps=
git+ssh://git@github.com/hytech-racing/CASE_lib.git#v45
SPI
Nanopb
https://github.com/vjmuzik/NativeEthernet.git
https://github.com/RCMast3r/hytech_can#4ffbba4
https://github.com/hytech-racing/HT_params/releases/download/2024-05-07T06_59_33/ht_eth_pb_lib.tar.gz
https://github.com/hytech-racing/shared_firmware_systems.git#af96a63

[env:teensy41]
Expand Down Expand Up @@ -55,37 +59,34 @@ lib_deps =
https://github.com/RCMast3r/spi_libs#2214fee
https://github.com/tonton81/FlexCAN_T4#b928bcb
https://github.com/RCMast3r/hytech_can#testing_new_inv_ids
https://github.com/hytech-racing/HT_CAN/releases/download/99/can_lib.tar.gz
https://github.com/hytech-racing/HT_CAN/releases/download/108/can_lib.tar.gz
git+ssh://git@github.com/hytech-racing/CASE_lib.git#v49


[env:test_can_on_teensy]

[env:test_interfaces]
; Testing variables
test_framework=unity
test_ignore=test_systems*
lib_ignore =
mock_interfaces
mock_systems


; including only the current main file for compiling to keep old main still around for now while
; refactoring
build_src_filter =
-<**/*.cpp>
+<test_can_interface.cpp>
build_unflags = -std=gnu++11
build_flags = -std=c++17
check_tool = clangtidy
check_src_filters =
+<include/*>
+<lib/*>
+<src/*>
-<src/old_main.cpp>

lib_extra_dirs =
platform = teensy
board = teensy41
framework = arduino
upload_protocol = teensy-cli
extra_scripts = pre:extra_script.py
lib_ldf_mode = deep+
lib_deps =
SPI
Nanopb
https://github.com/vjmuzik/NativeEthernet.git
https://github.com/tonton81/FlexCAN_T4#b928bcb
https://github.com/RCMast3r/hytech_can#4ffbba4
https://github.com/hytech-racing/HT_CAN/releases/latest/download/can_lib.tar.gz
https://github.com/hytech-racing/HT_params/releases/download/2024-05-07T06_59_33/ht_eth_pb_lib.tar.gz
https://github.com/hytech-racing/shared_firmware_interfaces.git#feature/thermistor-template
https://github.com/hytech-racing/shared_firmware_systems.git#af96a63
https://github.com/RCMast3r/spi_libs#2214fee
https://github.com/hytech-racing/HT_CAN/releases/download/108/can_lib.tar.gz
git+ssh://git@github.com/hytech-racing/CASE_lib.git#v49


3 changes: 1 addition & 2 deletions src/main_ethernet_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

EthernetUDP protobuf_send_socket;
EthernetUDP protobuf_recv_socket;
CAR_STATE state = CAR_STATE::STARTUP;
ParameterInterface params(state);
ParameterInterface params = ParameterInterface();
ETHInterfaces ethernet_interfaces = {&params};

void init_ethernet_device()
Expand Down
10 changes: 5 additions & 5 deletions src/test_can_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> TEST_CAN2; // Inverter CAN (now both a

CAN_message_t msg;

using CircularBufferType = Circular_Buffer<uint8_t, (uint32_t)16, sizeof(CAN_message_t)>;
using CircularBufferType = CANBufferType;
using InverterInterfaceType = InverterInterface<CircularBufferType>;
InverterInterfaceType fl_inv(&CAN2_txBuffer, ID_MC1_SETPOINTS_COMMAND);
InverterInterfaceType fr_inv(&CAN2_txBuffer, ID_MC2_SETPOINTS_COMMAND);
InverterInterfaceType rl_inv(&CAN2_txBuffer, ID_MC3_SETPOINTS_COMMAND);
InverterInterfaceType rr_inv(&CAN2_txBuffer, ID_MC4_SETPOINTS_COMMAND);
InverterInterfaceType fl_inv = InverterInterfaceType(&CAN2_txBuffer, MC1_SETPOINTS_COMMAND_CANID);
InverterInterfaceType fr_inv = InverterInterfaceType(&CAN2_txBuffer, MC2_SETPOINTS_COMMAND_CANID);
InverterInterfaceType rl_inv = InverterInterfaceType(&CAN2_txBuffer, MC3_SETPOINTS_COMMAND_CANID);
InverterInterfaceType rr_inv = InverterInterfaceType(&CAN2_txBuffer, MC4_SETPOINTS_COMMAND_CANID);

CANInterfaces<CircularBufferType> CAN_interfaces = {&fl_inv, &fr_inv, &rl_inv, &rr_inv, 0, 0, 0};

Expand Down
Loading

0 comments on commit 481a78c

Please sign in to comment.