Skip to content

Commit

Permalink
Making tests and fixing interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
walkermburns committed Feb 9, 2024
1 parent 75790fd commit 127ad41
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 177 deletions.
28 changes: 20 additions & 8 deletions lib/interfaces/include/AMSInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include "HyTech_CAN.h"
#include "SysClock.h"

/* Heartbeat Interval is the allowable amount of time between BMS status messages before car delatches */
const unsigned long HEARTBEAT_INTERVAL = 20; // milliseconds
/* The total pcc threshold is the lowest allowable voltage of the entire pack (in Volts)*/
const unsigned long PACK_CHARGE_CRIT_TOTAL_THRESHOLD = 420;
const unsigned long PACK_CHARGE_CRIT_LOWEST_CELL_THRESHOLD = 35000;
/* The lowest pcc threshold is the lowest allowable single cell voltage (in 100 microvolts)*/
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;
Expand All @@ -17,6 +20,11 @@ const float DEFAULT_VOLTAGE_ALPHA = 0.8;
class AMSInterface
{
public:
/*!
Constructor for the AMS Interface
@param sw_ok_pin The software ok pin number.
This pin is connected to the shutdown line and will go low if the AMS times out
*/
AMSInterface(int sw_ok_pin, float init_temp, float init_volt, float temp_alpha, float volt_alpha):
pin_software_ok_(sw_ok_pin),
filtered_max_cell_temp(init_temp),
Expand All @@ -26,22 +34,26 @@ class AMSInterface
{
// Set pin mode
pinMode(pin_software_ok_, OUTPUT);
};
}

/* Overloaded constructor that only takes in software OK pin, and uses default voltages and temp*/
AMSInterface(int sw_ok_pin):
AMSInterface(sw_ok_pin, DEFAULT_INIT_TEMP, DEFAULT_INIT_VOLTAGE, DEFAULT_TEMP_ALPHA, DEFAULT_VOLTAGE_ALPHA) {};

/* Initialize interface pin mode */
void init(const SysTick_s &tick);
/* Initialize the heartbeat timer */
void init();

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

/* Init software OK pin by setting high*/
void set_start_state();
// Set output value

/* set software OK pin */
void set_state_ok_high(bool ok_high);

/* Monitor AMS state */
void set_heartbeat(unsigned long millis);
bool heartbeat_received(const SysTick_s &tick);
void set_heartbeat();
bool heartbeat_received();
bool is_below_pack_charge_critical_low_thresh();
bool is_below_pack_charge_critical_total_thresh();
bool pack_charge_is_critical();
Expand Down
4 changes: 2 additions & 2 deletions lib/interfaces/include/DashboardInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ class DashboardInterface
private:
/* Pointer to the circular buffer to write new messages */
CANBufferType *msg_queue_;

public:
/* The instantiated data struct used to access data by member functions */
DashComponentInterface_s _data;

public:
/*!
Constructor for new DashboardInterface, All that it is inited with
is the pointer to the telem circular buffer that is used to write new messages
Expand Down
18 changes: 11 additions & 7 deletions lib/interfaces/include/MCUInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ class MCUInterface


public:
MCUInterface(CANBufferType *msg_output_queue, int bms_pin, int imd_pin, int bspd_pin, int sw_ok_pin, int bots_ok_pin, int brake_light_pin): msg_queue_(msg_output_queue),
pin_bms_ok_read_(bms_pin),
pin_imd_ok_read_(imd_pin),
pin_bspd_ok_read_(bspd_pin),
pin_software_ok_read_(sw_ok_pin),
pin_bots_ok_read_(bots_ok_pin),
pin_brake_light_ctrl_(brake_light_pin)
MCUInterface(CANBufferType *msg_output_queue, int bms_pin, int imd_pin, int bspd_pin, int sw_ok_pin, int bots_ok_pin, int brake_light_pin):


msg_queue_(msg_output_queue),
pin_bms_ok_read_(bms_pin),
pin_imd_ok_read_(imd_pin),
pin_bspd_ok_read_(bspd_pin),
pin_software_ok_read_(sw_ok_pin),
pin_bots_ok_read_(bots_ok_pin),
pin_brake_light_ctrl_(brake_light_pin)
{
// Set pin mode
pinMode(pin_brake_light_ctrl_, OUTPUT);
Expand All @@ -69,6 +72,7 @@ class MCUInterface

/* Read from Main ECU */
void read_mcu_status();

/* Write to Main ECU */
void set_brake_light(bool brake_pedal_is_active); // Called from PedalInterface/System

Expand Down
14 changes: 7 additions & 7 deletions lib/interfaces/src/AMSInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "AMSInterface.h"

/* Pin mode output to watchdog reset */
void AMSInterface::init(const SysTick_s &tick) {
void AMSInterface::init() {

set_heartbeat(tick.millis);
set_heartbeat();

}

Expand All @@ -23,13 +23,13 @@ void AMSInterface::set_state_ok_high(bool ok_high) {
}

/* Set AMS last heartbeat receive time */
void AMSInterface::set_heartbeat(unsigned long millis) {
last_heartbeat_time = millis;
void AMSInterface::set_heartbeat() {
last_heartbeat_time = millis();
}

/* Check if AMS heartbeat is received */
bool AMSInterface::heartbeat_received(const SysTick_s &tick) {
return ((tick.millis - last_heartbeat_time) < HEARTBEAT_INTERVAL);
bool AMSInterface::heartbeat_received() {
return ((millis() - last_heartbeat_time) < HEARTBEAT_INTERVAL);
}

/* Check if lowest cell temperature is below threshold */
Expand Down Expand Up @@ -70,7 +70,7 @@ float AMSInterface::get_filtered_min_cell_voltage() {
/* Retrieve CAN messages */
void AMSInterface::retrieve_status_CAN(CAN_message_t &recvd_msg, unsigned long cur_millis) {
bms_status_.load(recvd_msg.buf);
set_heartbeat(cur_millis);
set_heartbeat();
}

void AMSInterface::retrieve_temp_CAN(CAN_message_t &recvd_msg) {
Expand Down
34 changes: 17 additions & 17 deletions lib/interfaces/src/DashboardInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
void DashboardInterface::read(const CAN_message_t &can_msg)
{

DASHBOARD_STATE_t msg;
Unpack_DASHBOARD_STATE_hytech(&msg, can_msg.buf, NULL);
DASHBOARD_STATE_t dash_state;
Unpack_DASHBOARD_STATE_hytech(&dash_state, can_msg.buf, NULL);

_data.dial_mode = static_cast<DialMode_e>(msg.dial_state);
_data.dial_mode = static_cast<DialMode_e>(dash_state.dial_state);

_data.ssok = msg.ssok_above_threshold;
_data.shutdown = msg.shutdown_h_above_threshold;

_data.button.start = msg.start_button;
_data.button.mark = msg.mark_button;
_data.button.mode = msg.mode_button;
_data.button.mc_cycle = msg.motor_controller_cycle_button;
_data.button.launch_ctrl = msg.launch_ctrl_button;
_data.button.torque_mode = msg.torque_mode_button;
_data.button.led_dimmer = msg.led_dimmer_button;
_data.button.left_shifter = msg.left_shifter_button;
_data.button.right_shifter = msg.right_shifter_button;

_data.buzzer_state = msg.drive_buzzer;
_data.ssok = dash_state.ssok_above_threshold;
_data.shutdown = dash_state.shutdown_h_above_threshold;

_data.button.start = dash_state.start_button;
_data.button.mark = dash_state.mark_button;
_data.button.mode = dash_state.mode_button;
_data.button.mc_cycle = dash_state.motor_controller_cycle_button;
_data.button.launch_ctrl = dash_state.launch_ctrl_button;
_data.button.torque_mode = dash_state.torque_mode_button;
_data.button.led_dimmer = dash_state.led_dimmer_button;
_data.button.left_shifter = dash_state.left_shifter_button;
_data.button.right_shifter = dash_state.right_shifter_button;

_data.buzzer_state = dash_state.drive_buzzer;

}

Expand Down
6 changes: 3 additions & 3 deletions lib/mock_interfaces/AMSInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AMSInterface
// Set pin mode
};
/* Initialize interface pin mode */
void init(const SysTick_s &tick);
void init();

/* Write to Main ECU */
// Initialize output value
Expand All @@ -28,8 +28,8 @@ class AMSInterface
void set_state_ok_high(bool ok_high);

/* Monitor AMS state */
void set_heartbeat(const SysTick_s &tick);
bool heartbeat_received(const SysTick_s &tick);
void set_heartbeat();
bool heartbeat_received();
bool is_below_pack_charge_critical_low_thresh();
bool is_below_pack_charge_critical_total_thresh();
bool pack_charge_is_critical();
Expand Down
2 changes: 1 addition & 1 deletion lib/systems/src/SafetySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void SafetySystem::software_shutdown(const SysTick_s &tick) {

// If AMS heartbeat is not received within reasonable interval
// Set software is not ok
if (!ams_->heartbeat_received(tick)) {
if (!ams_->heartbeat_received()) {
software_is_ok = false;
}
if (software_is_ok)
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void setup() {
/* Initialize interface */
main_ecu.init(); // initial shutdown circuit readings,
wd_interface.init(curr_tick); // initialize wd kick time
ams_interface.init(curr_tick); // initialize last heartbeat time
ams_interface.init(); // initialize last heartbeat time

/* Initialize system */
safety_system.init(); // write software_ok high, write wd_input high, set software ok state true
Expand Down
46 changes: 46 additions & 0 deletions test/test_interfaces/AMS_interface_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <Arduino.h>
#include <unity.h>

#include "AMSInterface.h"

void test_AMS_unpacking_BMS_status_message()
{
/*
Need clarification on what these states are.
Why is nothing else in the message read by AMSInterface
*/
BMS_status bms_status{};
bms_status.set_state(5);

}

void test_AMS_unpacking_BMS_temperatures_message()
{

}

void test_AMS_unpacking_BMS_voltages_message()
{

}

void test_AMS_charge_critical_threshold()
{

}

void test_AMS_filtered_readings()
{

}

void test_AMS_heartbeat()
{
//setting arbitrary pin 20
AMSInterface ams_interface(20);
ams_interface.init();
ams_interface.set_start_state();
TEST_ASSERT_EQUAL(true, ams_interface.heartbeat_received());
delay(20);
TEST_ASSERT_EQUAL(false, ams_interface.heartbeat_received());
}
5 changes: 5 additions & 0 deletions test/test_interfaces/MCU_interface_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <Arduino.h>
#include <unity.h>

#include "MCUInterface.h"

Loading

0 comments on commit 127ad41

Please sign in to comment.