Skip to content

Commit

Permalink
Feature/thermistor interface (#97)
Browse files Browse the repository at this point in the history
* added in state of charge estimation
* added in tcmux status telemetry
* added thermistor interface
* added in checks for vn data into CASE for ensuring that if we have a bad velocity estimate we are still ok
* made steering system only use bottom analog sensor
* updated CASE parameters
* removed steering system tests
* switched from normal load gain scheduling to RPM based gain scheduling for TCS and set end bound for SL for fronts to 0.25 and decreased upper p bounds for TCS for front and rear

---------

Co-authored-by: Eric Galluzzi <ericbgalluzzi@gmail.com>
Co-authored-by: CL16gtgh <ryang359@gatech.edu>
Co-authored-by: shaynoorani <113149316+shaynoorani@users.noreply.github.com>
  • Loading branch information
4 people committed Jun 10, 2024
1 parent 5693309 commit cead7ab
Show file tree
Hide file tree
Showing 24 changed files with 947 additions and 270 deletions.
57 changes: 32 additions & 25 deletions include/MCU_rev15_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@


// pindefs
const int ADC1_CS = 34;
const int ADC2_CS = 33;
const int ADC3_CS = 29;
const int ADC1_CS = 34; // Main ECU
const int ADC2_CS = 33; // FL corner board
const int ADC3_CS = 29; // FR corner board
const int SOFTWARE_OK = 8;
const int WATCHDOG_INPUT = 32;

Expand All @@ -32,17 +32,20 @@ const int MCU15_FR_LOADCELL_CHANNEL = 2;

//MCU teensy analog channels
const int MCU15_TEENSY_ADC_CHANNELS = 2;
// const int MCU15_NUM_THERMISTORS = 2;
const int MCU15_THERM_FL = 38;
const int MCU15_THERM_FR = 41;
const int MCU15_THERM_FL_CHANNEL = 0;
const int MCU15_THERM_FR_CHANNEL = 1;
// const int MCU15_THERM_FL_CHANNEL = 0;
// const int MCU15_THERM_FR_CHANNEL = 1;

const int DEFAULT_ANALOG_PINS[MCU15_TEENSY_ADC_CHANNELS] = {MCU15_THERM_FL, MCU15_THERM_FR};
// Time intervals
const unsigned long SETUP_PRESENT_ACTION_INTERVAL = 1000;
const unsigned long BUZZER_ON_INTERVAL = 2000;
const unsigned long INVERTER_ENABLING_TIMEOUT_INTERVAL = 5000;

//glv sense conversion
const float GLV_SENSE_SCALE = .0073 * 4.0865;
// Communication speeds
const unsigned long INV_CAN_BAUDRATE = 500000;
const unsigned long TELEM_CAN_BAUDRATE = 500000;
Expand All @@ -51,17 +54,17 @@ const unsigned long TELEM_CAN_BAUDRATE = 500000;
// All of these values are the PEDAL min/max
// the sensor min/max that trip implaus are calculated
// in the PedalsSystem constructor
const int ACCEL1_PEDAL_MAX = 3330;
const int ACCEL2_PEDAL_MAX = 388;
const int ACCEL1_PEDAL_MAX = 3409; // 3330
const int ACCEL2_PEDAL_MAX = 259; // 388

const int ACCEL1_PEDAL_MIN = 2124;
const int ACCEL1_PEDAL_MIN = 2129;
const int ACCEL2_PEDAL_MIN = 1517;

const int BRAKE1_PEDAL_MAX = 2200;
const int BRAKE2_PEDAL_MAX = 2200;
const int BRAKE1_PEDAL_MAX = 1828; // 2200;
const int BRAKE2_PEDAL_MAX = 1857; // 2200;

const int BRAKE1_PEDAL_MIN = 785;
const int BRAKE2_PEDAL_MIN = 785;
const int BRAKE1_PEDAL_MIN = 1184; // 785; // 1230 to 1750
const int BRAKE2_PEDAL_MIN = 2486; // 785; // 2450 to 1930

const int ACCEL1_PEDAL_OOR_MIN = 90;
const int ACCEL2_PEDAL_OOR_MIN = 90;
Expand All @@ -76,7 +79,7 @@ const int BRAKE1_PEDAL_OOR_MAX = 4000;
const int BRAKE2_PEDAL_OOR_MAX = 4000;

const float DEFAULT_PEDAL_DEADZONE = 0.05f;
const float DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN = 0.10f;
const float DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN = 0.12f;

const float APPS_ACTIVATION_PERCENTAGE = 0.1f;
const float BRAKE_ACTIVATION_PERCENTAGE = 0.05f;
Expand All @@ -85,24 +88,28 @@ const float BRAKE_MECH_THRESH = 0.40f;
// Load Cell Defs to convert raw to lbs
// lbs = (raw + offset) * scale

const float LOADCELL_FL_SCALE = 0.0554;
const float LOADCELL_FL_OFFSET = 19.976 / LOADCELL_FL_SCALE;
const float LOADCELL_FL_SCALE = 0.1053;
const float LOADCELL_FL_OFFSET = 19.674 / LOADCELL_FL_SCALE;

const float LOADCELL_FR_SCALE = 0.0514;
const float LOADCELL_FR_OFFSET = 19.892 / LOADCELL_FR_SCALE;
const float LOADCELL_FR_SCALE = 0.097;
const float LOADCELL_FR_OFFSET = 21.183 / LOADCELL_FR_SCALE;

const float LOADCELL_RL_SCALE = 0.0595;
const float LOADCELL_RL_OFFSET = 7.148 / LOADCELL_RL_SCALE;
const float LOADCELL_RL_SCALE = 0.1149;
const float LOADCELL_RL_OFFSET = 13.526 / LOADCELL_RL_SCALE;

const float LOADCELL_RR_SCALE = 0.06;
const float LOADCELL_RR_OFFSET = 23.761 / LOADCELL_RR_SCALE;
const float LOADCELL_RR_SCALE = 0.118;
const float LOADCELL_RR_OFFSET = 25.721 / LOADCELL_RR_SCALE;

// Steering parameters
const float PRIMARY_STEERING_SENSE_OFFSET = 0.0; // units are degrees
const int SECONDARY_STEERING_SENSE_LEFTMOST_BOUND = 785; // 794 // 812 // 130 deg
const int SECONDARY_STEERING_SENSE_RIGHTMOST_BOUND = 3087; // 3075 // 3179 // 134 deg
const int SECONDARY_STEERING_SENSE_CENTER = 1945; // 1960 // 1970
const float STEERING_RANGE_DEGREES = 257.0f; // 253.0f // 256.05f // 134+130-7(slop)
const int SECONDARY_STEERING_SENSE_LEFTMOST_BOUND = 790; // 785; // 794 // 812 // 130 deg // 128.95
const int SECONDARY_STEERING_SENSE_RIGHTMOST_BOUND = 3133; // 3087; // 3075 // 3179 // 134 deg // 132.79
const int SECONDARY_STEERING_SENSE_CENTER = 1985; // 1945 // 1960 // 1970
const float STEERING_RANGE_DEGREES = 254.74; //257.0f // 253.0f // 256.05f // 134+130-7(slop)
const float STEERING_IIR_ALPHA = 0.7f; // shaves off around 1 deg of max discrepancy

// TC parameters
const float SIMPLE_TC_REAR_TORQUE_SCALE = 1.0;
const float SIMPLE_TC_REGEN_TORQUE_SCALE = 0.4;

#endif /* __MCU15_H__ */
206 changes: 187 additions & 19 deletions lib/interfaces/include/AMSInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include "FlexCAN_T4.h"
#include "HyTech_CAN.h"
#include "SysClock.h"
#include "hytech.h"
#include "MessageQueueDefine.h"

/* Heartbeat Interval is the allowable amount of time between BMS status messages before car delatches */
const unsigned long HEARTBEAT_INTERVAL = 2000; // milliseconds
Expand All @@ -15,6 +18,8 @@ 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 uint16_t MAX_PACK_CHARGE = 48600;
const unsigned long DEFAULT_INITIALIZATION_WAIT_INTERVAL = 5000;


/// @brief this class is for interfacing with the AMS (accumulator management system)
Expand All @@ -26,19 +31,26 @@ class AMSInterface
@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):
AMSInterface(CANBufferType *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) {};
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),
initialization_startup_interval_(DEFAULT_INITIALIZATION_WAIT_INTERVAL) {};

/* 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) {};
AMSInterface(CANBufferType *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 the heartbeat timer */
void init(unsigned long curr_millis);
void init(SysTick_s &initial_tick);

/* Init software OK pin by setting high*/
void set_start_state();
Expand All @@ -60,32 +72,143 @@ class AMSInterface
float get_filtered_max_cell_temp();
/* IIR filter and return filtered min cell voltage */
float get_filtered_min_cell_voltage();
/*gets the derate factor for acc system*/
float get_acc_derate_factor();

/**
* Initializes the charge member variable from the voltage of the minimum cell using the VOLTAGE_LOOKUP_TABLE.
*
* @pre The bms_voltages_ member variable MUST be initialized before this function can be called.
* @return The charge, in coulombs, that the charge member variable is initialized to.
*/
float initialize_charge();

/**
* Calculates SoC based on the energy meter CAN message. Calling calculate_SoC_em()
* will update the SoC_ and charge_ member variables.
*
* @param The current tick that the calculate_SoC_em function should use for integration.
*
* @pre The last_tick_ member variable must be updated correctly.
* @post The charge_ field has the updated charge, and the SoC field contains an updated percentage.
*/
void calculate_SoC_em(const SysTick_s &tick);

/**
* Calculates SoC based on the ACU_SHUNT_MEASUREMENTS CAN message. Calling calculate_SoC_acu()
* will update the SoC_ and charge_ member variables.
*
* @param The current tick that the calculate_SoC_em function should use for integration.
*
* @pre The last_tick_ member variable must be updated correctly.
* @post The charge_ field has the updated charge, and the SoC field contains an updated percentage.
*/
void calculate_SoC_acu(const SysTick_s &tick);

/**
* Retrieves the value of the SoC member variable. This function does NOT recalculate
* the SoC_ variable, it only returns the value that is stored.
*
* @return the current value stored in the SoC_ member variable.
*/
float get_SoC() {return SoC_;}

/**
* This is AMSInterface's tick() function. It behaves correctly regardless of the
* since the functions calculate the elapsed time between the given tick and the stored last_tick_.
*
* @param tick The current system tick.
*/
void tick(const SysTick_s &tick);

//RETRIEVE CAN MESSAGES//
/* read BMS status messages */

/**
* Reads this CAN message into the bms_status_ member variable. This function uses the OLD CAN library.
*/
void retrieve_status_CAN(unsigned long curr_millis, CAN_message_t &recvd_msg);
/* read BMS temperature messages */

/**
* Reads this CAN message into the bms_temperatures_ member variable. This function uses the OLD CAN library.
*/
void retrieve_temp_CAN(CAN_message_t &recvd_msg);
/* read BMS voltage messages */

/**
* Reads this CAN message into the bms_voltages_ member variable. This function
* does NOT apply the fromS() functions on the data. This function uses the NEW CAN library.
*/
void retrieve_voltage_CAN(CAN_message_t &recvd_msg);

/*Updates Acc_derate_factor*/
void calculate_acc_derate_factor();

/**
* Reads this CAN message into the em_measurements_ member variable. This function
* does NOT apply the fromS() functions on the data. This function uses the NEW CAN library.
*/
void retrieve_em_measurement_CAN(CAN_message_t &can_msg);

/**
* Reads this CAN message into the acu_shunt_measurements_ member variable. This function
* does NOT apply the fromS() functions on the data. This function uses the NEW CAN library.
*/
void retrieve_current_shunt_CAN(const CAN_message_t &can_msg);

/**
* Retrieves the current state of the use_em_for_soc member variable.
* @return True if using EM, false if using ACU shunt measurements.
*/
bool is_using_em_for_soc() {return use_em_for_soc_;}

/**
* Setter function for the use_em_for_soc_ member variable.
* @param new_use_em_for_soc The new value for the variable.
*/
void set_use_em_for_soc(bool new_use_em_for_soc) {
use_em_for_soc_ = new_use_em_for_soc;
}

/**
* Puts the current value of SoC_ and charge_ onto msg_queue_.
*/
void enqueue_state_of_charge_CAN();

/** 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_;}


private:
/* Private functions */
// Check if lowest cell temperature is below threshold
bool is_below_pack_charge_critical_low_thresh();
// Check if total pack charge is above threshold
bool is_below_pack_charge_critical_total_thresh();

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

/**
* CAN line that this AMSInterface should write to. This should be the Telemetry CAN line.
*/
CANBufferType *msg_queue_;

/* software OK pin */
int pin_software_ok_;

/* AMS CAN messages */
BMS_status bms_status_;
BMS_temperatures bms_temperatures_;
BMS_voltages bms_voltages_;
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;

/* software OK pin */
int pin_software_ok_;
unsigned long last_heartbeat_time_;

/* IIR filter parameters */
float bms_high_temp;
Expand All @@ -94,6 +217,51 @@ class AMSInterface
float filtered_min_cell_voltage;
float cell_temp_alpha;
float cell_voltage_alpha;

float acc_derate_factor;

/**
* If set to TRUE, then SoC will use EM.
* If set to FALSE, then SoC will use ACU SHUNT.
* Set to TRUE in constructor by default.
*/
bool use_em_for_soc_ = true;

/**
* The charge stored on the accumulator. Stored in coulombs, ranging from
* zero to MAX_PACK_CHARGE.
*/
float charge_;

/**
* Stores the current state of charge of the accumulator. SoC is stored as a
* percentage of MAX_PACK_CHARGE. In every location, this is calculated as
* SoC = (charge / MAX_PACK_CHARGE) * 100;
*/
float SoC_;

/**
* Stores the last Sys_Tick_s struct from the last time the tick() function is called.
*/
SysTick_s last_tick_;

/**
* Stores whether or not this AMSInterface has initialized SoC_ or not.
*/
bool has_initialized_charge_;

bool has_received_bms_voltage_;

unsigned long initialization_startup_interval_;

unsigned long timestamp_start_;


// Check if lowest cell temperature is below threshold
bool is_below_pack_charge_critical_low_thresh();
// Check if total pack charge is above threshold
bool is_below_pack_charge_critical_total_thresh();

};

#endif /* __AMSINTERFACE_H__ */
#endif /* __AMSINTERFACE_H__ */
8 changes: 7 additions & 1 deletion lib/interfaces/include/HytechCANInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ void process_ring_buffer(BufferType &rx_buffer, const InterfaceType &interfaces,
case ID_BMS_VOLTAGES:
interfaces.ams_interface->retrieve_voltage_CAN(recvd_msg);
break;
case ID_EM_MEASUREMENT:
interfaces.ams_interface->retrieve_em_measurement_CAN(recvd_msg);
break;
case ACU_SHUNT_MEASUREMENTS_CANID:
interfaces.ams_interface->retrieve_current_shunt_CAN(recvd_msg);
break;

// MC status msgs
case ID_MC1_STATUS:
Expand Down Expand Up @@ -219,4 +225,4 @@ void enqueue_matlab_msg(bufferType *msg_queue, const CAN_MESSAGE_BUS & structure
}


#endif /* HYTECHCANINTERFACE */
#endif /* HYTECHCANINTERFACE */
Loading

0 comments on commit cead7ab

Please sign in to comment.