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 52 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
52 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
e8be8f7
Changed GPIO pins to stuff on that google doc
bjackson312006 Jan 31, 2025
d1d9e92
Fixed build errors, did not fix formatting errors because i did not l…
bjackson312006 Jan 31, 2025
c35cc8a
Merge branch 'develop' into feature/PDU-24A
bjackson312006 Feb 2, 2025
9ee486f
Formatting
bjackson312006 Feb 2, 2025
7b6b4ba
Merge branch 'develop' into feature/PDU-24A
bjackson312006 Feb 6, 2025
cbe96ad
Added write_radfan_0() and write_radfan_1(). They don't do anything y…
bjackson312006 Feb 14, 2025
7ceee4b
Moved init_pdu() upwards
bjackson312006 Feb 15, 2025
1976f47
Added I2C Detect
bjackson312006 Feb 15, 2025
d2d4f4c
Added I2C Detect 2
bjackson312006 Feb 15, 2025
5d749e2
Test
bjackson312006 Feb 15, 2025
a0ab1c0
Current Sensors read/write working.
bjackson312006 Feb 16, 2025
66469e0
Fixed erros
bjackson312006 Feb 16, 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
Next Next commit
Added current reading to pdu.c, added the sending of current via CAN …
…in monitor.c
bjackson312006 committed Nov 7, 2024
commit 436cd7bebff5836b729bbcdec4e5b870db105456
4 changes: 4 additions & 0 deletions Core/Inc/cerberus_conf.h
Original file line number Diff line number Diff line change
@@ -75,5 +75,9 @@
#define CANID_LV_MONITOR 0x503
#define CANID_PEDALS_ACCEL_MSG 0x504
#define CANID_PEDALS_BRAKE_MSG 0x505
#define CANID_MOTOR_CONT_CURR 0x8000
#define CANID_BATT_FANS_CURR 0xC000
#define CANID_PUMPS_CURR 0xE000
#define CANID_LV_BOARDS_CURR 0xF000
// Reserved for MPU debug message, see yaml for format
#define CANID_EXTRA_MSG 0x701
4 changes: 4 additions & 0 deletions Core/Inc/fault.h
Original file line number Diff line number Diff line change
@@ -24,6 +24,10 @@ typedef enum {
BSPD_PREFAULT = 0x1000,
LV_MONITOR_FAULT = 0x2000,
RTDS_FAULT = 0x4000,
MOTOR_CONTROLLER_CURRENT_FAULT = 0x8000,
BATTBOX_FANS_CURRENT_FAULT = 0xC000,
PUMPS_CURRENT_FAULT = 0xE000,
LV_BOARDS_CURRENT_FAULT = 0xF000,
MAX_FAULTS
} fault_code_t;

11 changes: 11 additions & 0 deletions Core/Inc/pdu.h
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

#include "cmsis_os.h"
#include "pca9539.h"
#include "INA226.h"
#include <stdbool.h>
#include <stdint.h>

@@ -13,6 +14,10 @@ typedef struct {
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;
} pdu_t;

/* Creates a new PDU interface */
@@ -81,6 +86,12 @@ typedef enum {
*/
int8_t read_shutdown(pdu_t *pdu, bool status[MAX_SHUTDOWN_STAGES]);

// Functions for reading current
int8_t read_motor_controller_current(pdu_t *pdu, float *data);
int8_t read_battbox_fans_current(pdu_t *pdu, float *data);
int8_t read_pumps_current(pdu_t *pdu, float *data);
int8_t read_lv_boards_current(pdu_t *pdu, float *data);

/**
* @brief Taskf for sounding RTDS.
*
92 changes: 92 additions & 0 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
@@ -27,6 +27,94 @@
static bool tsms = false;
osMutexId_t tsms_mutex;

void read_motor_controller_current_data(pdu_t *pdu)
{
fault_data_t fault_data = { .id = MOTOR_CONTROLLER_CURRENT_FAULT, .severity = DEFCON5 };
can_msg_t msg = { .id = CANID_MOTOR_CONT_CURR, .len = 4, .data = { 0 } };

float current;
if(read_motor_controller_current(pdu, &current))
{
fault_data.diag = "Failed to read motor controller current";
queue_fault(&fault_data);
}

uint32_t current_int = (uint32_t)(current * 10000); // idk how precise it needs to be

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

void read_battbox_fans_current_data(pdu_t *pdu)
{
fault_data_t fault_data = { .id = BATTBOX_FANS_CURRENT_FAULT, .severity = DEFCON5 };
can_msg_t msg = { .id = CANID_BATT_FANS_CURR, .len = 4, .data = { 0 } };

float current;
if(read_battbox_fans_current(pdu, &current))
{
fault_data.diag = "Failed to read battbox fans current";
queue_fault(&fault_data);
}

uint32_t current_int = (uint32_t)(current * 10000); // idk how precise it needs to be

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

void read_pumps_current_data(pdu_t *pdu)
{
fault_data_t fault_data = { .id = PUMPS_CURRENT_FAULT, .severity = DEFCON5 };
can_msg_t msg = { .id = CANID_PUMPS_CURR, .len = 4, .data = { 0 } };

float current;
if(read_pumps_current(pdu, &current))
{
fault_data.diag = "Failed to read pumps current";
queue_fault(&fault_data);
}

uint32_t current_int = (uint32_t)(current * 10000); // idk how precise it needs to be

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

void read_lv_boards_current_data(pdu_t *pdu)
{
fault_data_t fault_data = { .id = LV_BOARDS_CURRENT_FAULT, .severity = DEFCON5 };
can_msg_t msg = { .id = CANID_LV_BOARDS_CURR, .len = 4, .data = { 0 } };

float current;
if(read_lv_boards_current(pdu, &current))
{
fault_data.diag = "Failed to read lv boards current";
queue_fault(&fault_data);
}

uint32_t current_int = (uint32_t)(current * 10000); // idk how precise it needs to be

memcpy(msg.data, &current_int, msg.len);
if(queue_can_msg(msg))
{
fault_data.diag = "Failed to send lv boards 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.
*/
@@ -120,6 +208,10 @@ void vNonFunctionalDataCollection(void *pv_params)
for (;;) {
read_lv_sense(mpu);
read_fuse_data(pdu);
read_motor_controller_current_data(pdu);
read_battbox_fans_current_data(pdu);
read_pumps_current_data(pdu);
read_lv_boards_current_data(pdu);

/* delay for 1000 ms (1k ticks at 1000 Hz tickrate) */
osDelay(1000);
134 changes: 134 additions & 0 deletions Core/Src/pdu.c
Original file line number Diff line number Diff line change
@@ -18,7 +18,39 @@
#define CTRL_ADDR PCA_I2C_ADDR_2
#define RTDS_DURATION 1750 /* ms at 1kHz tick rate */

#define MOTOR_CONTROLLER_CURRENT_SENSOR_ADDR 0x40 // add INA_I2C_ADDR to driver?
#define BATTBOX_FANS_CURRENT_SENSOR_ADDR 0x42
#define PUMPS_CURRENT_SENSOR_ADDR 0x44
#define LV_BOARDS_CURRENT_SENSOR_ADDR 0x45

static osMutexAttr_t pdu_mutex_attributes;
static I2C_HandleTypeDef *hi2c = NULL; // added this since i need it for wrapper functions (a similar change was made in mpu.c for lsm6dso PR im pretty sure). it's still a param in init_pdu tho

// Wrapper for reading ina226 (current sensor) registers
static inline int ina_read_reg(uint16_t dev_addr, uint8_t reg, uint16_t *data)
{
uint8_t buff[2];
HAL_StatusTypeDef status;

status = HAL_I2C_Mem_Read(hi2c, dev_addr, reg, I2C_MEMADD_SIZE_16BIT, buff, 2, HAL_MAX_DELAY);
if(status != HAL_OK) {return -1;}

*data = (buff[0] << 8) | buff[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see below about endianness

return 0;
}

// Wrapper for writing ina226 (current sensor) registers
static inline int ina_write_reg(uint16_t dev_addr, uint8_t reg, uint16_t *data)
{
uint8_t buff[2];
HAL_StatusTypeDef status;

status = HAL_I2C_Mem_Write(hi2c, dev_addr, reg, I2C_MEMADD_SIZE_16BIT, buff, 2, HAL_MAX_DELAY);
if(status != HAL_OK) {return -1;}

*data = (buff[0] << 8) | buff[1];
jr1221 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

static uint8_t sound_rtds(pdu_t *pdu)
{
@@ -98,6 +130,58 @@ pdu_t *init_pdu(I2C_HandleTypeDef *hi2c)

pdu->hi2c = hi2c;

/* Initialize Motor Controller Current Sensor */
jr1221 marked this conversation as resolved.
Show resolved Hide resolved
pdu->motor_controller_current_sensor = malloc(sizeof(ina226_t));
assert(pdu->motor_controller_current_sensor);
ina226_init(pdu->motor_controller_current_sensor, ina_write_reg, ina_read_reg, MOTOR_CONTROLLER_CURRENT_SENSOR_ADDR);
int status_init = ina226_calibrate(pdu->motor_controller_current_sensor, 0.01f, 3.0f);
if (status_init != 0)
{
printf("\n\rmotor controller current sensor init fail\n\r");
free(pdu->motor_controller_current_sensor);
free(pdu);
return NULL;
}

/* Initialize Battbox Fans Current Sensor */
pdu->battbox_fans_current_sensor = malloc(sizeof(ina226_t));
assert(pdu->battbox_fans_current_sensor);
ina226_init(pdu->battbox_fans_current_sensor, ina_write_reg, ina_read_reg, BATTBOX_FANS_CURRENT_SENSOR_ADDR);
status_init = ina226_calibrate(pdu->battbox_fans_current_sensor, 0.01f, 5.0f);
if (status_init != 0)
{
printf("\n\rbattbox fans current sensor init fail\n\r");
free(pdu->battbox_fans_current_sensor);
free(pdu);
return NULL;
}

/* Initialize Pumps Current Sensor */
pdu->pumps_current_sensor = malloc(sizeof(ina226_t));
assert(pdu->pumps_current_sensor);
ina226_init(pdu->pumps_current_sensor, ina_write_reg, ina_read_reg, PUMPS_CURRENT_SENSOR_ADDR);
status_init = ina226_calibrate(pdu->pumps_current_sensor, 0.01f, 2.0f);
if (status_init != 0)
{
printf("\n\rpumps current sensor init fail\n\r");
free(pdu->pumps_current_sensor);
free(pdu);
return NULL;
}

/* Initialize LV Boards Current Sensor */
pdu->lv_boards_current_sensor = malloc(sizeof(ina226_t));
assert(pdu->lv_boards_current_sensor);
ina226_init(pdu->lv_boards_current_sensor, ina_write_reg, ina_read_reg, LV_BOARDS_CURRENT_SENSOR_ADDR);
status_init = ina226_calibrate(pdu->lv_boards_current_sensor, 0.01f, 1.25f);
if (status_init != 0)
{
printf("\n\rlv boards current sensor init fail\n\r");
free(pdu->lv_boards_current_sensor);
free(pdu);
return NULL;
}

/* Initialize Shutdown GPIO Expander */
pdu->shutdown_expander = malloc(sizeof(pca9539_t));
assert(pdu->shutdown_expander);
@@ -370,3 +454,53 @@ int8_t read_shutdown(pdu_t *pdu, bool status[MAX_SHUTDOWN_STAGES])
osMutexRelease(pdu->mutex);
return 0;
}


static int8_t read_current(pdu_t *pdu, ina226_t *ina, float *data)
{
if(!pdu || !ina || !data) return -1;

float current;

osStatus_t stat = osMutexAcquire(pdu->mutex, MUTEX_TIMEOUT);
if (stat) return stat;

I2C_HandleTypeDef *previous_hi2c = hi2c; // Saves current global hi2c to previous_hi2c
hi2c = pdu->hi2c; // Sets global hi2c to pdu's hi2c before ina226_read_current is called

int status = ina226_read_current(ina, &current);
hi2c = previous_hi2c; // After ina226_read_current is called, restores global hi2c to previous
if(status != 0)
{
osMutexRelease(pdu->mutex);
return status;
}

*data = current;
osMutexRelease(pdu->mutex);
return 0;
}

int8_t read_motor_controller_current(pdu_t *pdu, float *data)
{
return read_current(pdu, pdu->motor_controller_current_sensor, data);
}

int8_t read_battbox_fans_current(pdu_t *pdu, float *data)
{
return read_current(pdu, pdu->battbox_fans_current_sensor, data);
}

int8_t read_pumps_current(pdu_t *pdu, float *data)
{
return read_current(pdu, pdu->pumps_current_sensor, data);
}

int8_t read_lv_boards_current(pdu_t *pdu, float *data)
{
return read_current(pdu, pdu->lv_boards_current_sensor, data);
}




1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c \
Drivers/Embedded-Base/general/src/INA226.c \
Drivers/Embedded-Base/general/src/lsm6dso.c \
Drivers/Embedded-Base/general/src/sht30.c \
Drivers/Embedded-Base/general/src/max7314.c \