Skip to content

Commit

Permalink
Fixed code to pass test_env systems tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhwang04 committed Jul 14, 2024
1 parent 47087ef commit e8e4584
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 46 deletions.
140 changes: 107 additions & 33 deletions lib/mock_interfaces/AMSInterface.h
Original file line number Diff line number Diff line change
@@ -1,60 +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;

// #define CANBufferType int // Placeholder define for the mock interface
const unsigned short MAX_PACK_CHARGE = 48600;
const unsigned long DEFAULT_INITIALIZATION_WAIT_INTERVAL = 5000;

class AMSInterface
{
public:
AMSInterface(CANBufferType *msg_output_queue, int sw_ok_pin, float init_temp, float init_volt, float temp_alpha, float volt_alpha)
{
// do nothing
};

/* Overloaded constructor that only takes in software OK pin and uses default voltages and temp*/
AMSInterface(CANBufferType *msg_output_queue, int sw_ok_pin):
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) {};

/* Initialize interface pin mode */
void init(unsigned long curr_millis) {set_heartbeat(curr_millis);}

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

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

/* 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__ */
6 changes: 5 additions & 1 deletion 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
40 changes: 28 additions & 12 deletions test/test_systems/safety_system_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,49 @@


TEST(SafetySystemTest, test_software_shutdown) {
SysClock sys_clock;

SysTick_s curr_tick = sys_clock.tick(0);
// Initial tick
SysTick_s tick_zero;
tick_zero.millis = 100L;
tick_zero.micros = 100000L;

AMSInterface ams_interface(0, 0, 0, 0, 0, 0);
WatchdogInterface wd_interface(0);
SafetySystem safety_system(&ams_interface, &wd_interface);

ams_interface.init(curr_tick.millis);
wd_interface.init(curr_tick.millis);
ams_interface.init(tick_zero);
wd_interface.init(tick_zero.millis);
safety_system.init();

ASSERT_EQ(safety_system.get_software_is_ok(), true);

// 10 milliseconds later
curr_tick = sys_clock.tick(10 * 1000);
ams_interface.set_heartbeat(curr_tick.millis);
SysTick_s tick_one;
tick_one.millis = 110L;
tick_one.micros = 110000L;
ams_interface.set_heartbeat(tick_one.millis);

safety_system.software_shutdown(curr_tick);
safety_system.software_shutdown(tick_one);

ASSERT_EQ(safety_system.get_software_is_ok(), true);

// 45 milliseconds later
curr_tick = sys_clock.tick(45 * 1000);
safety_system.software_shutdown(curr_tick);
// 1999 milliseconds later (should be within 2000ms window)
SysTick_s tick_two;
tick_two.millis = 2109L;
tick_two.micros = 2109000L;

safety_system.software_shutdown(tick_two);

ASSERT_EQ(ams_interface.heartbeat_received(tick_two.millis), true);


// 1999 milliseconds later (should fail)
SysTick_s tick_three;
tick_three.millis = 2110L;
tick_three.micros = 2110000L;

safety_system.software_shutdown(tick_three);

ASSERT_EQ(ams_interface.heartbeat_received(curr_tick.millis), false);
ASSERT_EQ(ams_interface.heartbeat_received(tick_three.millis), false);

ASSERT_EQ(safety_system.get_software_is_ok(), false);

Expand Down

0 comments on commit e8e4584

Please sign in to comment.