Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make shep build #53

Merged
merged 16 commits into from
Nov 11, 2023
4 changes: 2 additions & 2 deletions .mxproject

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Core/Inc/analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @param data
*/
void analyzer_push(AccumulatorData_t* data);
void analyzer_push(acc_data_t* data);

/**
* @brief Calculates the PWM required to drive the fans at the current moment in time
Expand All @@ -29,8 +29,8 @@ uint8_t analyzer_calc_fan_pwm();
/**
* @brief Pointer to the address of the most recent data point
*/
AccumulatorData_t* bmsdata;
extern acc_data_t* bmsdata;

AccumulatorData_t* prevbmsdata;
extern acc_data_t* prevbmsdata;

#endif
1 change: 1 addition & 0 deletions Core/Inc/bmsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define NUM_CHIPS NUM_SEGMENTS*2
#define NUM_CELLS_PER_CHIP 9
#define NUM_THERMS_PER_CHIP 32
#define NUM_RELEVANT_THERMS 5

// Firmware limits
#define MAX_TEMP 65 //degrees C
Expand Down
24 changes: 10 additions & 14 deletions Core/Inc/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#define COMPUTE_H

#include "datastructs.h"
//#include "nerduino.h" Replace
#include "canMsgHandler.h"
#include "stateMachine.h"

#define CURRENT_SENSOR_PIN_L A1
Expand Down Expand Up @@ -36,7 +34,7 @@ void compute_enable_charging(bool enable_charging);
*
* @return Returns a fault if we are not able to communicate with charger
*/
FaultStatus_t compute_send_charging_message(uint16_t voltage_to_set, AccumulatorData_t* bms_data);
int compute_send_charging_message(uint16_t voltage_to_set, acc_data_t* bms_data);

/**
* @brief Returns if charger interlock is engaged, indicating charger LV connector is plugged in
Expand All @@ -51,9 +49,9 @@ bool compute_charger_connected();
*
* @param msg
*/
static void compute_charger_callback(const CAN_message_t& msg);
//static void compute_charger_callback(const CAN_message_t& msg);

static void compute_mc_callback(const CAN_message_t& msg);
//static void compute_mc_callback(const CAN_message_t& msg);

/**
* @brief Sets the desired fan speed
Expand Down Expand Up @@ -82,7 +80,7 @@ void compute_send_mc_message(uint16_t max_charge, uint16_t max_discharge);
*
* @param fault_state
*/
void compute_set_fault(FaultStatus_t fault_state);
void compute_set_fault(int fault_state);

/**
* @brief sends acc status message
Expand All @@ -95,7 +93,7 @@ void compute_set_fault(FaultStatus_t fault_state);
*
* @return Returns a fault if we are not able to send
*/
void compute_send_acc_status_message(AccumulatorData_t* bmsdata);
void compute_send_acc_status_message(acc_data_t* bmsdata);

/**
* @brief sends BMS status message
Expand All @@ -107,7 +105,7 @@ void compute_send_acc_status_message(AccumulatorData_t* bmsdata);
*
* @return Returns a fault if we are not able to send
*/
void compute_send_bms_status_message(AccumulatorData_t* bmsdata, int bms_state, bool balance);
void compute_send_bms_status_message(acc_data_t* bmsdata, int bms_state, bool balance);

/**
* @brief sends shutdown control message
Expand All @@ -127,7 +125,7 @@ void compute_send_shutdown_ctrl_message(uint8_t mpe_state);
*
* @return Returns a fault if we are not able to send
*/
void compute_send_cell_data_message(AccumulatorData_t* bmsdata);
void compute_send_cell_data_message(acc_data_t* bmsdata);

/**
* @brief sends cell voltage message
Expand All @@ -151,14 +149,14 @@ void compute_send_cell_voltage_message(uint8_t cell_id, uint16_t instant_volt,
* @param charge
* @param current
*/
void compute_send_current_message(AccumulatorData_t* bmsdata);
void compute_send_current_message(acc_data_t* bmsdata);

/**
* @brief sends cell temperature message
*
* @return Returns a fault if we are not able to send
*/
void compute_send_cell_temp_message(AccumulatorData_t* bmsdata);
void compute_send_cell_temp_message(acc_data_t* bmsdata);

/**
* @brief sends the average segment temperatures
Expand All @@ -167,8 +165,6 @@ void compute_send_cell_temp_message(AccumulatorData_t* bmsdata);
*
* @return Returns a fault if we are not able to send
*/
void compute_send_segment_temp_message(AccumulatorData_t* bmsdata);

void compute_send_dcl_prefault_message(bool prefault);
void compute_send_segment_temp_message(acc_data_t* bmsdata);

#endif // COMPUTE_H
81 changes: 32 additions & 49 deletions Core/Inc/datastructs.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
#ifndef DATASTRUCTS_H
#define DATASTRUCTS_H

#include <stdbool.h>
#include <stdint.h>
#include "bmsConfig.h"
#include <nerduino.h>
#include <vector>
#include "timer.h"

/**
* @brief Individual chip data
* @note stores thermistor values, voltage readings, and the discharge status
*/
struct ChipData_t {
typedef struct {
/* These are retrieved from the initial LTC comms */
uint16_t voltage_reading[NUM_CELLS_PER_CHIP]; /* store voltage readings from each chip */
int8_t thermistor_reading[NUM_THERMS_PER_CHIP]; /* store all therm readings from each chip */
int8_t thermistor_value[NUM_THERMS_PER_CHIP];
FaultStatus_t error_reading;
int error_reading;

/* These are calculated during the analysis of data */
int8_t cell_temp[NUM_CELLS_PER_CHIP];
float cell_resistance[NUM_CELLS_PER_CHIP];
uint16_t open_cell_voltage[NUM_CELLS_PER_CHIP];

uint8_t bad_volt_diff_count[NUM_CELLS_PER_CHIP];
};
} chipdata_t;

/**
* @brief Enuemrated possible fault codes for the BMS
* @note the values increase at powers of two to perform bitwise operations on a main fault code
* to set or get the error codes
*/
// clang-format off
enum BMSFault_t {
typedef enum {
FAULTS_CLEAR = 0x0,

/* Orion BMS faults */
Expand All @@ -54,30 +55,30 @@ enum BMSFault_t {
CHARGE_LIMIT_ENFORCEMENT_FAULT = 0x20000,

MAX_FAULTS = 0x80000000 /* Maximum allowable fault code */
};
} bms_fault_t;
// clang-format on

/**
* @brief Stores critical values for the pack, and where that critical value can be found
*
*/
struct CriticalCellValue_t {
typedef struct {
int32_t val;
uint8_t chipIndex;
uint8_t cellNum;
};
} crit_cellval_t;

/**
* @brief Represents one "frame" of BMS data
* @note the size of this structure is **9752 bits** (~1.3k bytes), as of October 22, 2022
*/
#define ACCUMULATOR_FRAME_SIZE sizeof(AccumulatorData_t);
#define ACCUMULATOR_FRAME_SIZE sizeof(acc_data_t);

struct AccumulatorData_t {
typedef struct {
/* Array of data from all chips in the system */
ChipData_t chip_data[NUM_CHIPS];
chipdata_t chip_data[NUM_CHIPS];

FaultStatus_t fault_status = NOT_FAULTED;
int fault_status;

int16_t pack_current; /* this value is multiplied by 10 to account for decimal precision */
uint16_t pack_voltage;
Expand All @@ -98,47 +99,29 @@ struct AccumulatorData_t {
uint32_t fault_code;

/* Max, min, and avg thermistor readings */
CriticalCellValue_t max_temp;
CriticalCellValue_t min_temp;
crit_cellval_t max_temp;
crit_cellval_t min_temp;
int8_t avg_temp;

/* Max and min cell resistances */
CriticalCellValue_t max_res;
CriticalCellValue_t min_res;
crit_cellval_t max_res;
crit_cellval_t min_res;

/* Max, min, and avg voltage of the cells */
CriticalCellValue_t max_voltage;
CriticalCellValue_t min_voltage;
crit_cellval_t max_voltage;
crit_cellval_t min_voltage;
uint16_t avg_voltage;
uint16_t delt_voltage;

CriticalCellValue_t max_ocv;
CriticalCellValue_t min_ocv;
crit_cellval_t max_ocv;
crit_cellval_t min_ocv;
uint16_t avg_ocv;
uint16_t delt_ocv;

uint16_t boost_setting;

bool is_charger_connected;
};

/**
* @brief Represents the state of the BMS fault timers
*/
typedef enum {
BEFORE_TIMER_START,
DURING_EVAL

} TSTimerEvalState;

/**
* @brief Represents a timer that can be in one of three states
*/
struct tristate_timer : public Timer {

TSTimerEvalState eval_state = BEFORE_TIMER_START;
int eval_length;
};
} acc_data_t;

/**
* @brief Represents individual BMS states
Expand Down Expand Up @@ -166,27 +149,27 @@ typedef enum {
NEQ, /* fault if {data} not equal to {threshold} */
NOP /* no operation, use for single threshold faults */

} FaultEvalType;
} fault_evalop_t;

/**
* @brief Represents data to be packaged into a fault evaluation
*/
struct fault_eval {
typedef struct {
char* id;
tristate_timer timer;
nertimer_t timer;

int data_1;
FaultEvalType optype_1;
fault_evalop_t optype_1;
int lim_1;

int timeout;
int code;

int data_2 = 0;
FaultEvalType optype_2 = NOP;
int lim_2 = 0;
fault_evalop_t optype_2;
int data_2;
int lim_2;

bool is_faulted = false;
};
bool is_faulted;
} fault_eval_t;

#endif
31 changes: 17 additions & 14 deletions Core/Inc/eepromdirectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#define EEPROMDIRECTORY_H

#include "bmsConfig.h"
#include <stdint.h>
#include <stdbool.h>

#define NUM_EEPROM_FAULTS 5
#define NUM_EEPROM_ITEMS 2
#define EEPROM_ROOT_ADDR 0

/* index 0 = newest, index 4 = oldest */
static uint32_t eeprom_faults[NUM_EEPROM_FAULTS];
Expand All @@ -13,13 +19,10 @@ struct eeprom_partition
uint16_t address; /* start address */
};

struct eeprom_partition eeprom_data[NUM_EEPROM_ITEMS]
{
struct eeprom_partition eeprom_data[NUM_EEPROM_ITEMS];
/* ____________KEY________________ _BYTES_ */
{.id = const_cast<char*>("ROOT"), .size = 1},
{.id = const_cast<char*>("FAULTS"), .size = 21}


};


/**
Expand All @@ -35,14 +38,14 @@ void eepromInit();
* @param key
* @return int
*/
int eepromGetIndex(char *key);
uint16_t eeprom_get_index(char *key);

/**
* @brief returns the key at the passed index
*
*
*/
char *eepromGetKey(int index);
char *eeprom_get_key(int index);



Expand All @@ -53,9 +56,9 @@ char *eepromGetKey(int index);
* @param key
* @param data
*/
void eepromReadData(char *key, void *data);
bool eeprom_read_data_key(char *key, void *data, uint16_t size);

void eepromReadData(uint8_t index, void *data);
bool eeprom_read_data_address(uint16_t address, void *data, uint16_t size);

/**
* @brief loads eeprom with data from passed pointer
Expand All @@ -64,27 +67,27 @@ void eepromReadData(uint8_t index, void *data);
* @param key
* @param data
*/
void eepromWriteData(char *key, void *data);
bool eeprom_write_data_key(char *key, void *data, uint16_t size);

void eepromWriteData(uint8_t index, void *data);
bool eeprom_write_data_address(uint16_t address, void *data, uint16_t size);

/**
* @brief logs fault code in eeprom
*
*
* @param fault_code
*/
void logFault(uint32_t fault_code);
void log_fault(uint32_t fault_code);
/**
* @brief reads all stored faults from eeprom
*
*
* @note this updates a static array of fault codes, should be called before accessing the array
* @note this function is blocking, and will take a few ms to complete. This is why it is kept seperate from logFault(),
* @note this function is blocking, and will take a few ms to complete. This is why it is kept seperate from log_fault(),
* allwing the user more control as to when to use this
*/

void getFaults();
void get_faults();


#endif
2 changes: 1 addition & 1 deletion Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C" {

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include <stdlib.h>
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
Expand Down
Loading