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

Implement PDU 24A #221

Open
wants to merge 40 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
436cd7b
Added current reading to pdu.c, added the sending of current via CAN …
bjackson312006 Nov 7, 2024
be4306e
Changes in Embedded-Base submodule that were preventing building
bjackson312006 Nov 7, 2024
242e5fe
Updated submodule
bjackson312006 Nov 7, 2024
7d6a26a
Updated submodule
bjackson312006 Nov 7, 2024
1ccf1f7
Formatting
bjackson312006 Nov 7, 2024
c60dfb7
Condensed current into 1 CAN message, changed pca9539 to tca9539
bjackson312006 Nov 8, 2024
75aa2d4
reverted pca9539 to tca9539 change, will do that at the end. also for…
bjackson312006 Nov 9, 2024
f7c7d37
Formatting
bjackson312006 Nov 9, 2024
c2d19db
Formatting
bjackson312006 Nov 9, 2024
3dd772a
Updated pinout according to schematic
bjackson312006 Nov 10, 2024
6affcce
Forgot to remove comments
bjackson312006 Nov 10, 2024
136af62
Updated fuse and shutdown pins in read_fuses and read_shutdown
bjackson312006 Nov 10, 2024
526d1c1
Changed SHUTDOWN_ADDR and CTRL_ADDR to correspond w/ datasheet
bjackson312006 Nov 10, 2024
d70e409
Formattng
bjackson312006 Nov 10, 2024
b66ca51
Formatting
bjackson312006 Nov 10, 2024
dfcab75
Updated read_fuse and read_shutdown in pdu.c based on schematic. Also…
bjackson312006 Nov 10, 2024
d30cc77
pdu.c: Removed global hi2c pointer,
bjackson312006 Nov 13, 2024
8b61fed
Removed functions in pdu.h that I left in by mistake. Also comment ch…
bjackson312006 Nov 13, 2024
32185f4
pdu.c: Corrected ina_write_reg wrapper function
bjackson312006 Nov 13, 2024
bd2889c
Added comment for calibration constants, and made tsms_pin const
bjackson312006 Nov 19, 2024
74e0930
switch to cubemx version 6.13.0
jr1221 Jan 13, 2025
3b97753
Revert "switch to cubemx version 6.13.0"
jr1221 Jan 13, 2025
3c641e2
Merge branch 'develop' into feature/PDU-24A
jr1221 Jan 13, 2025
c6d49ed
fix my merge mistakes
jr1221 Jan 13, 2025
7814f87
fmt
jr1221 Jan 13, 2025
ec16123
add adc2 pc0 pc1 for pump temp sense
jr1221 Jan 13, 2025
e68056d
Added ADC DMA for pump sensors
bjackson312006 Jan 13, 2025
297b86f
Revert accidental formatting changes in main.c
bjackson312006 Jan 13, 2025
ad0d719
Added control line functions for PDU 24A
bjackson312006 Jan 16, 2025
872eb81
Formatting whoops
bjackson312006 Jan 16, 2025
e85fcb9
Reorganized PDU 24A control functions so less messy
bjackson312006 Jan 16, 2025
fa570c8
Removed redundant functions
bjackson312006 Jan 16, 2025
6f2effd
Removed merge conflicts
bjackson312006 Jan 24, 2025
2a4f278
Merge branch 'develop' into feature/PDU-24A
bjackson312006 Jan 24, 2025
7bf8231
Formatting
bjackson312006 Jan 24, 2025
1b93d91
Merge branch 'feature/PDU-24A' of github.com:Northeastern-Electric-Ra…
bjackson312006 Jan 24, 2025
53a279a
Made GPIO #defines consistent with Altium (a few pins are unused)
bjackson312006 Jan 24, 2025
0f9679a
Uncommented Shutdown GPIO init
bjackson312006 Jan 24, 2025
fe59815
Moved pdu #defines to pdu.h since there were a lot
bjackson312006 Jan 24, 2025
c00889c
Changed read_pump_sens to correct CANID in monitor.c
bjackson312006 Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Core/Inc/cerberus_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,7 @@
#define CANID_LV_MONITOR 0x503
#define CANID_PEDALS_ACCEL_MSG 0x504
#define CANID_PEDALS_BRAKE_MSG 0x505
#define CANID_PDU_CURRENT 0x508
#define CANID_PUMP_SENSORS 0x509
// Reserved for MPU debug message, see yaml for format
#define CANID_EXTRA_MSG 0x701
4 changes: 3 additions & 1 deletion Core/Inc/fault.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ typedef enum {
BSPD_PREFAULT = (1 << 14),
LV_MONITOR_FAULT = (1 << 15),
RTDS_FAULT = (1 << 16),
MAX_FAULTS = (1 << 17)
PDU_CURRENT_FAULT = (1 << 17),
PUMP_SENSORS_FAULT = (1 << 18),
MAX_FAULTS = (1 << 19)
} fault_code_t;

typedef struct {
Expand Down
124 changes: 100 additions & 24 deletions Core/Inc/pdu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,51 @@
#define PDU_H

#include <stdbool.h>

#include "fault.h"
#include "cmsis_os.h"
#include "pca9539.h"

#define SOUND_RTDS_FLAG 1U
#include "INA226.h"
#include <stdbool.h>
#include <stdint.h>

typedef struct {
I2C_HandleTypeDef *hi2c;
osMutexId_t *mutex;
pca9539_t *shutdown_expander;
pca9539_t *ctrl_expander;

ina226_t *motor_controller_current_sensor;
ina226_t *battbox_fans_current_sensor;
ina226_t *pumps_current_sensor;
ina226_t *lv_boards_current_sensor;

ADC_HandleTypeDef *pump_sensors_adc;
uint32_t pump_sensors_dma_buf[2];
} pdu_t;

/* Creates a new PDU interface */
pdu_t *init_pdu();
pdu_t *init_pdu(I2C_HandleTypeDef *hi2c, ADC_HandleTypeDef *pump_sensors_adc);

/* Functions to Control PDU */
int8_t write_pump(pdu_t *pdu, bool status);
int8_t write_brakelight(pdu_t *pdu, bool status);
int8_t write_fan_battbox(pdu_t *pdu, bool status);
int8_t write_pump_0(pdu_t *pdu, bool state);
int8_t write_pump_1(pdu_t *pdu, bool state);
int8_t write_24V_12V_buck(pdu_t *pdu, bool state);
int8_t write_brakelight(pdu_t *pdu, bool state);
int8_t write_fan_battbox(pdu_t *pdu, bool state);
int8_t write_rtds(pdu_t *pdu, bool state);

/* Function to Read the Status of Fuses from PDU */
typedef enum {
FUSE_BATTBOX,
FUSE_LVBOX,
FUSE_FAN_RADIATOR,
FUSE_MC,
FUSE_FAN_BATTBOX,
FUSE_PUMP,
FUSE_DASHBOARD,
FUSE_BRAKELIGHT,
FUSE_BRB,
BATTBOX_FUSE_STAT,
LV_BOARDS_FUSE_STAT,
RADFAN_FUSE_STAT,
BUCK_FUSE_STAT,
FANBATTBOX_FUSE_STAT,
PUMP_FUSE_STAT0,
DASHBOARD_FUSE_STAT,
BRKLIGHT_FUSE_STAT,
SD_TO_BRB_FUSE_STAT,
PUMP_FUSE_STAT1,
MAX_FUSES
} fuse_t;

Expand All @@ -57,14 +71,14 @@ int8_t read_tsms_sense(pdu_t *pdu, bool *status);
/* Functions to Read Status of Various Stages of Shutdown Loop */
typedef enum {
CKPT_BRB_CLR, /* Cockpit BRB */
BMS_OK, /* Battery Management System (Shepherd) */
INERTIA_SW_OK, /* Inertia Switch */
SPARE_GPIO1_OK,
IMD_OK, /* Insulation Monitoring Device */
BSPD_OK, /* Brake System Plausbility Device */
BOTS_OK, /* Brake Over Travel Switch */
HVD_INTLK_OK, /* HVD Interlock */
HVC_INTLK_OK, /* HV C Interlock*/
BMS_GOOD, /* Battery Management System (Shepherd) */
INERTIA_SW_GOOD, /* Inertia Switch */
SPARE_GPIO1,
IMD_GOOD, /* Insulation Monitoring Device */
BSPD_GOOD, /* Brake System Plausbility Device */
BOTS_GOOD, /* Brake Over Travel Switch */
HVD_INTLK_GOOD, /* HVD Interlock */
HVC_INTLK_GOOD, /* HV C Interlock*/
//SIDE_BRB_CLR, /* Side BRB */
//TSMS, /* Tractive System Main Switch */
MAX_SHUTDOWN_STAGES
Expand All @@ -79,6 +93,19 @@ typedef enum {
*/
int8_t read_shutdown(pdu_t *pdu, bool status[MAX_SHUTDOWN_STAGES]);

/**
* @brief Read the status of the shutdown loop.
*
* @param pdu Pointer to struct representing the PDU
* @param status Buffer that the data from both sensors will be written to
*/
void read_pump_sensors(pdu_t *pdu, uint32_t pump_sensors_buf[2]);

// Function for reading current
int8_t read_all_current(pdu_t *pdu, float *motor_controller_current,
float *battbox_fans_current, float *pumps_current,
float *lv_boards_current);

/**
* @brief Taskf for sounding RTDS.
*
Expand All @@ -97,4 +124,53 @@ extern const osThreadAttr_t rtds_attributes;
*/
int8_t read_brake_state(pdu_t *pdu, bool *status);

/* CTRL GPIO Expander */
#define CTRL_ADDR PCA_I2C_ADDR_0
#define PIN_PUMP_CTRL_0 0 // P00
#define PIN_PUMP_CTRL_1 1 // P01
#define PIN_24V_12V_BUCK_CTRL 2 // P02
#define PIN_BRKLIGHT_CTRL 3 // P03
#define PIN_FANBATTBOX_CTRL 4 // P04
#define PIN_BATTBOX_FUSE_STAT 5 // P05
#define PIN_LV_BOARDS_FUSE_STAT 6 // P06
#define PIN_RADFAN_FUSE_STAT 7 // P07
#define PIN_BUCK_FUSE_STAT 0 // P10
#define PIN_FANBATTBOX_FUSE_STAT 1 // P11
#define PIN_PUMP_FUSE_STAT0 2 // P12
#define PIN_DASHBOARD_FUSE_STAT 3 // P13
#define PIN_BRKLIGHT_FUSE_STAT 4 // P14
#define PIN_SD_TO_BRB_FUSE_STAT 5 // P15
#define PIN_PUMP_FUSE_STAT1 6 // P16
#define PIN_RTDS_CTRL 7 // P17

/* Shutdown GPIO Expander */
#define SHUTDOWN_ADDR PCA_I2C_ADDR_1
#define PIN_CKPT_BRB_CLR 0 // P00
#define PIN_BMS_GOOD 1 // P01
#define PIN_INERTIA_SW_GOOD 2 // P02
#define PIN_SPARE_GPIO1 3 // P03
#define PIN_IMD_GOOD 4 // P04
#define PIN_BSPD_GOOD 5 // P05
#define PIN_SHUTDOWN_06 6 // P06 (X)
#define PIN_SHUTDOWN_07 7 // P07 (X)
#define PIN_SHUTDOWN_10 0 // P10 (X)
#define PIN_MC_STAT 1 // P11
#define PIN_SPARE_MON 2 // P12
#define PIN_SPARE_FAULT 3 // P13
#define PIN_TSMS_SENSE 4 // P14
#define PIN_BOTS_GOOD 5 // P15
#define PIN_HVD_INTLK_GOOD 6 // P16
#define PIN_HVC_INTLK_GOOD 7 // P17

/* Current Sensors */
#define MOTOR_CONTROLLER_CURRENT_SENSOR_ADDR 0x40
#define BATTBOX_FANS_CURRENT_SENSOR_ADDR 0x42
#define PUMPS_CURRENT_SENSOR_ADDR 0x44
#define LV_BOARDS_CURRENT_SENSOR_ADDR 0x45

/* Misc */
#define MUTEX_TIMEOUT osWaitForever /* ms */
#define RTDS_DURATION 1750 /* ms at 1kHz tick rate */
#define SOUND_RTDS_FLAG 1U

#endif /* PDU_H */
66 changes: 65 additions & 1 deletion Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
ADC_HandleTypeDef hadc3;
DMA_HandleTypeDef hdma_adc1;
DMA_HandleTypeDef hdma_adc2;
DMA_HandleTypeDef hdma_adc3;

CAN_HandleTypeDef hcan1;
Expand Down Expand Up @@ -95,6 +97,7 @@ static void MX_ADC1_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_ADC3_Init(void);
static void MX_IWDG_Init(void);
static void MX_ADC2_Init(void);
void StartDefaultTask(void *argument);

/* USER CODE BEGIN PFP */
Expand Down Expand Up @@ -164,12 +167,13 @@ int main(void)
MX_USART3_UART_Init();
MX_ADC3_Init();
MX_IWDG_Init();
MX_ADC2_Init();
/* USER CODE BEGIN 2 */

/* Create Interfaces to Represent Relevant Hardware */
mpu_t *mpu = init_mpu(&hadc3, &hadc1, GPIOC, GPIOB);
assert(mpu);
pdu_t *pdu = init_pdu(&hi2c2);
pdu_t *pdu = init_pdu(&hi2c2, &hadc2);
assert(pdu);
dti_t *mc = dti_init();
assert(mc);
Expand Down Expand Up @@ -364,6 +368,66 @@ static void MX_ADC1_Init(void)

}

/**
* @brief ADC2 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC2_Init(void)
{

/* USER CODE BEGIN ADC2_Init 0 */

/* USER CODE END ADC2_Init 0 */

ADC_ChannelConfTypeDef sConfig = {0};

/* USER CODE BEGIN ADC2_Init 1 */

/* USER CODE END ADC2_Init 1 */

/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV6;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.ScanConvMode = ENABLE;
hadc2.Init.ContinuousConvMode = ENABLE;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.NbrOfConversion = 2;
hadc2.Init.DMAContinuousRequests = ENABLE;
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_10;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC2_Init 2 */

/* USER CODE END ADC2_Init 2 */

}

/**
* @brief ADC3 Initialization Function
* @param None
Expand Down
76 changes: 76 additions & 0 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,81 @@
static bool tsms = false;
osMutexId_t tsms_mutex;

/**
* @brief Read voltage of Pump Sensors and send a CAN message with the result.
*/
void read_pump_sens(pdu_t *pdu)
{
// put fault stuff tomorrow
fault_data_t fault_data = { .id = PUMP_SENSORS_FAULT,
.severity = DEFCON5 };
can_msg_t msg = { .id = CANID_PUMP_SENSORS, .len = 8, .data = { 0 } };

uint32_t pump_volts_int[2];

read_pump_sensors(pdu, pump_volts_int);

// convert to real voltage
float pump_sensor0_volts_real = (pump_volts_int[0] / 4095.0) * 3.3;
float pump_sensor1_volts_real = (pump_volts_int[1] / 4095.0) * 3.3;

pump_volts_int[0] = (uint32_t)(pump_sensor0_volts_real * 10000);
pump_volts_int[1] = (uint32_t)(pump_sensor1_volts_real * 10000);

memcpy(msg.data, pump_volts_int, msg.len);
if (queue_can_msg(msg)) {
fault_data.diag = "Failed to send pump sensor CAN message";
queue_fault(&fault_data);
}
}

/**
* @brief Read current of MC, battox fans, pumps, and LV boards and send a CAN message with the result.
*/
void read_current(pdu_t *pdu)
{
fault_data_t fault_data = { .id = PDU_CURRENT_FAULT,
.severity = DEFCON5 };
can_msg_t msg = { .id = CANID_PDU_CURRENT, .len = 8, .data = { 0 } };

float motor_controller_current;
float battbox_fans_current;
float pumps_current;
float lv_boards_current;

if (read_all_current(pdu, &motor_controller_current,
&battbox_fans_current, &pumps_current,
&lv_boards_current)) {
fault_data.diag = "Failed to read current";
queue_fault(&fault_data);
}

uint16_t int_motor_controller_current =
(uint16_t)(motor_controller_current * 1000);
uint16_t int_battbox_fans_current =
(uint16_t)(battbox_fans_current * 1000);
uint16_t int_pumps_current = (uint16_t)(pumps_current * 1000);
uint16_t int_lv_boards_current = (uint16_t)(lv_boards_current * 1000);

struct __attribute__((__packed__)) {
uint16_t motor_controller;
uint16_t battbox_fans;
uint16_t pumps_current;
uint16_t lv_boards;
} current_data;

current_data.motor_controller = int_motor_controller_current;
current_data.battbox_fans = int_battbox_fans_current;
current_data.pumps_current = int_pumps_current;
current_data.lv_boards = int_lv_boards_current;

memcpy(msg.data, &current_data, msg.len);
if (queue_can_msg(msg)) {
fault_data.diag = "Failed to send current CAN message";
queue_fault(&fault_data);
}
}

/**
* @brief Read the open cell voltage of the LV batteries and send a CAN message with the result.
*/
Expand Down Expand Up @@ -138,6 +213,7 @@ void vNonFunctionalDataCollection(void *pv_params)
for (;;) {
read_lv_sense(mpu);
read_fuse_data(pdu);
read_current(pdu);

/* delay for 1000 ms (1k ticks at 1000 Hz tickrate) */
osDelay(1000);
Expand Down
Loading
Loading