1- // Previously known as AMS Interface in MCU (HT08)
2-
31#ifndef __AMSSYSTEM_H__
42#define __AMSSYSTEM_H__
53
6- #include < Arduino.h>
7- #include < cstdint>
8-
9- /* Heartbeat Interval is the allowable amount of time between BMS status messages before car delatches */
10- const unsigned long HEARTBEAT_INTERVAL = 2000 ; // milliseconds
11- /* The total pcc threshold is the lowest allowable voltage of the entire pack (in Volts)*/
12- const unsigned long PACK_CHARGE_CRIT_TOTAL_THRESHOLD = 420 ;
13- /* The lowest pcc threshold is the lowest allowable single cell voltage (in 100 microvolts)*/
14- const unsigned long PACK_CHARGE_CRIT_LOWEST_CELL_THRESHOLD = 35000 ; // equivalent to 3.5V
4+ #include < stdint.h>
5+ #include < SharedFirmwareTypes.h>
156
16- const float DEFAULT_INIT_TEMP = 40.0 ;
17- const float DEFAULT_INIT_VOLTAGE = 3.5 ;
18- const float DEFAULT_TEMP_ALPHA = 0.8 ;
19- const float DEFAULT_VOLTAGE_ALPHA = 0.8 ;
20- const uint16_t MAX_PACK_CHARGE = 48600 ;
21- const unsigned long DEFAULT_INITIALIZATION_WAIT_INTERVAL = 5000 ;
22- const int DEFAULT_SW_OK_PIN_ = 37 ; // number from scehmatic for VCR
7+ /* Heartbeat Interval is the allowable amount of milliseconds between BMS status messages before car delatches */
8+ const unsigned long HEARTBEAT_INTERVAL_MS = 2000 ; // milliseconds
239
10+ /* The total PCC threshold is the lowest allowable voltage of the entire pack (in Volts)*/
11+ const unsigned long PACK_CHARGE_CRIT_TOTAL_THRESHOLD_VOLTS = 420 ;
2412
25- // / @brief this class is for interfacing with the AMS (accumulator management system)
13+ /* The lowest pcc threshold is the lowest allowable single cell voltage (in 100 microvolts)*/
14+ const unsigned long PACK_CHARGE_CRIT_LOWEST_CELL_THRESHOLD = 35000 ; // Equivalent to 3.5V
15+
16+ const float DEFAULT_INIT_TEMP = 40.0 ; // Celsius
17+ const float DEFAULT_INIT_VOLTAGE = 3.5 ; // Volts
18+ const float DEFAULT_TEMP_ALPHA = 0.8 ; // IIR filter alpha
19+ const float DEFAULT_VOLTAGE_ALPHA = 0.8 ; // IIR filter alpha
20+ const uint16_t MAX_PACK_CHARGE = 48600 ; // Coulombs
21+
22+
23+ /* *
24+ * Singleton class for communicating with the BMS. If one of the shutdown conditions is met, this class will return a
25+ * false for get_bms_ok.
26+ *
27+ * Shutdown conditions:
28+ * 1) Has not received a message from BMS for more than "HEARTBEAT_INTERVAL_MS" milliseconds.
29+ * 2) Total pack voltage is below critical threshold.
30+ * 3) Lowest cell voltage is below critical threshold.
31+ */
2632class AMSSystem
2733{
2834public:
29- /* Method to get instance of object */
35+
36+ /* *
37+ * Retrieves the singular instance of the AMSSystem
38+ */
3039 static AMSSystem& getInstance ()
3140 {
3241 static AMSSystem instance;
3342 return instance;
3443 }
3544
36- // ***Only use if needed***
37- // // Delete copy constructor and assignment operator to prevent duplication. // Added
38- // AMSInterface(const AMSInterface&) = delete; // Added
39- // AMSInterface& operator=(const AMSInterface&) = delete; // Added
45+ /* *
46+ * Delete copy constructor and assignment operator to prevent duplication.
47+ */
48+ AMSSystem (const AMSSystem&) = delete ;
49+ AMSSystem& operator =(const AMSSystem&) = delete ;
50+
51+ /* *
52+ * Initialize the heartbeat timer.
53+ */
54+ void init (unsigned long curr_micros);
55+
56+ AMSSystemData_s update_ams_system (unsigned long curr_millis, BMSData_s &bms_data);
57+
58+ private:
59+
60+ /* *
61+ * Constructor for the AMS Interface
62+ */
63+ AMSSystem () :
64+ _last_heartbeat_time (0 ),
65+ _filtered_max_cell_temp (DEFAULT_INIT_TEMP),
66+ _filtered_min_cell_voltage (DEFAULT_INIT_VOLTAGE),
67+ _cell_temp_alpha (DEFAULT_TEMP_ALPHA),
68+ _cell_voltage_alpha (DEFAULT_VOLTAGE_ALPHA)
69+ {};
70+
71+ /* AMS last heartbeat time */
72+ unsigned long _last_heartbeat_time;
73+
74+ float _filtered_max_cell_temp;
75+ float _filtered_min_cell_voltage;
76+
77+ /* extra filtered information */
78+ float _filtered_average_cell_voltage;
79+ float _filtered_max_cell_voltage;
80+ float _filtered_average_cell_temp;
81+ float _filtered_min_cell_temp;
4082
41- /* Initialize the heartbeat timer */
42- void init (unsigned long curr_micros);// unsigned long micros
83+ /* IIR alpha values */
84+ float _cell_temp_alpha;
85+ float _cell_voltage_alpha;
4386
44- /* Init software OK pin by setting high */
45- void set_start_state () ;
87+ /* Storage of BMS Struct */
88+ BMSData_s bms_container ;
4689
47- /* Check if the last heartbeat arrived within allowable interval */
48- bool heartbeat_received (unsigned long curr_micros);// micros
4990
50- /* Check if either lowest cell or total pack is below threshold*/
51- bool pack_charge_is_critical ();
91+ /* Check if lowest cell temperature is below threshold */
92+ bool is_below_pack_charge_critical_low_thresh ();
5293
53- // SETTERS//
54- /* set software OK pin */
55- void set_state_ok_high (bool ok_high);
56- /* set the last heartbeat to the current millis time */
57- void set_heartbeat (unsigned long curr_millis);
94+ /* Check if total pack charge is above threshold */
95+ bool is_below_pack_charge_critical_total_thresh ();
5896
59- // GETTERS//
6097 /* IIR filter and return filtered max cell temperature */
6198 float get_filtered_max_cell_temp ();
6299 /* IIR filter and return filtered min cell voltage */
63- float get_filtered_min_cell_voltage ();
64- /* gets the derate factor for acc system*/
65- float get_acc_derate_factor ();
66-
67- /* Updates Acc_derate_factor*/
68- void calculate_acc_derate_factor ();
69-
100+ float get_filtered_min_cell_voltage ();
70101
71- private:
72102
73- // Private constructor for the singleton pattern
74- /* !
75- Constructor for the AMS Interface
76- @param sw_ok_pin The software ok pin number.
77- This pin is connected to the shutdown line and will go low if the AMS times out
78- */
79- AMSSystem (int sw_ok_pin, float init_temp, float init_volt, float temp_alpha, float volt_alpha):
80- _pin_software_ok (sw_ok_pin),
81- filtered_max_cell_temp (init_temp),
82- filtered_min_cell_voltage (init_volt),
83- cell_temp_alpha (temp_alpha),
84- cell_voltage_alpha (volt_alpha) {};
85-
86- /* Overloaded constructor that only takes in software OK pin and uses default voltages and temp*/
87- AMSSystem ():
88- AMSSystem (DEFAULT_SW_OK_PIN_, DEFAULT_INIT_TEMP, DEFAULT_INIT_VOLTAGE, DEFAULT_TEMP_ALPHA, DEFAULT_VOLTAGE_ALPHA) {};
89-
90- /* software OK pin */
91- int _pin_software_ok;
103+ /* IIR filter and return filtered average cell voltage */
104+ float get_filtered_average_cell_voltage ();
105+ /* IIR filter and return filtered max cell voltage */
106+ float get_filtered_max_cell_voltage ();
107+ /* IIR filter and return filtered average cell temp */
108+ float get_filtered_average_cell_temp ();
109+ /* IIR filter and return filtered min cell temp */
110+ float get_filtered_min_cell_temp ();
92111
93- /* AMS last heartbeat time */
94- unsigned long last_heartbeat_time_;
95-
96- /* IIR filter parameters */
97- float bms_high_temp;
98- float bms_low_voltage;
99- float filtered_max_cell_temp;
100- float filtered_min_cell_voltage;
101- float cell_temp_alpha;
102- float cell_voltage_alpha;
103112
104- float acc_derate_factor;
113+ // Checkers (return true if everything is good)
114+ /* Checks if "Total pack voltage is below critical threshold" */
115+ bool check_voltage ();
105116
106- // Check if lowest cell temperature is below threshold
107- bool is_below_pack_charge_critical_low_thresh ();
108- // Check if total pack charge is above threshold
109- bool is_below_pack_charge_critical_total_thresh ();
117+ /* Check if heartbeat received is within interval allowance */
118+ bool check_heartbeat (unsigned long curr_micros);
110119
111120};
112121
113- #endif /* __AMSSYSTEM_H__ */
114-
115- // chage all private variables to be preceded
116- // look at linter from main branch
122+ #endif /* __AMSSYSTEM_H__ */
0 commit comments