ams-master-23/Core/Src/shunt_monitoring.c

87 lines
2.4 KiB
C
Raw Normal View History

2023-03-12 21:06:23 +01:00
#include "shunt_monitoring.h"
#include <string.h>
#include "can.h"
#include "ts_state_machine.h"
#include "util.h"
2023-03-18 20:24:07 +01:00
#include "can-halal.h"
2023-03-15 18:07:38 +01:00
2023-03-12 21:06:23 +01:00
ShuntData shunt_data;
void shunt_init() {
shunt_data.current = 0;
2023-04-24 19:20:21 +02:00
shunt_data.voltage_veh = 0;
shunt_data.voltage_bat = 0;
2023-03-12 21:06:23 +01:00
shunt_data.voltage3 = 0;
shunt_data.busbartemp = 0;
shunt_data.power = 0;
shunt_data.energy = 0;
shunt_data.current_counter = 0;
shunt_data.last_message = 0;
shunt_data.last_current_message = 0;
2023-03-12 21:06:23 +01:00
}
void shunt_check() {
2023-04-30 00:57:42 +02:00
int is_error = 0;
TSErrorKind error_type = TS_ERRORKIND_NONE;
2023-05-31 00:46:49 +02:00
if (HAL_GetTick() - shunt_data.last_message > SHUNT_TIMEOUT) {
is_error = 1;
error_type = TS_ERRORKIND_SHUNT_TIMEOUT;
2023-05-31 00:46:49 +02:00
can_send_error(TS_ERRORKIND_SHUNT_TIMEOUT, 0);
} else if (shunt_data.current >= SHUNT_THRESH_OVERCURRENT) {
2023-04-30 00:57:42 +02:00
is_error = 1;
error_type = TS_ERRORKIND_SHUNT_OVERCURRENT;
can_send_error(TS_ERRORKIND_SHUNT_OVERCURRENT, 0);
2023-05-31 00:46:49 +02:00
} else if (shunt_data.busbartemp >= SHUNT_THRESH_OVERTEMP) {
2023-04-30 00:57:42 +02:00
is_error = 1;
error_type = TS_ERRORKIND_SHUNT_OVERTEMP;
2023-04-30 00:57:42 +02:00
can_send_error(TS_ERRORKIND_SHUNT_OVERTEMP, 0);
}
ts_sm_set_error_source(TS_ERROR_SOURCE_SHUNT, error_type, is_error);
2023-03-12 21:06:23 +01:00
}
2023-03-15 18:07:38 +01:00
void shunt_handle_can_msg(uint16_t id, const uint8_t *data) {
2023-05-31 00:46:49 +02:00
shunt_data.last_message = HAL_GetTick();
2023-03-12 21:06:23 +01:00
// All result messages contain a big-endian 6-byte integer
2023-03-15 18:07:38 +01:00
uint64_t result = ftcan_unmarshal_unsigned(&data, 6);
2023-03-12 21:06:23 +01:00
2023-03-15 18:07:38 +01:00
switch (id) {
2023-03-12 21:06:23 +01:00
case CAN_ID_SHUNT_CURRENT:
shunt_data.current = result;
if (shunt_data.last_current_message > 0) {
uint32_t now = HAL_GetTick();
float dt = (now - shunt_data.last_current_message) * 0.001f;
2024-07-09 21:25:20 +02:00
shunt_data.current_counter += shunt_data.current * dt;
}
shunt_data.last_current_message = HAL_GetTick();
2023-03-12 21:06:23 +01:00
break;
case CAN_ID_SHUNT_VOLTAGE1:
2023-04-24 19:20:21 +02:00
shunt_data.voltage_bat = result;
2023-03-12 21:06:23 +01:00
break;
case CAN_ID_SHUNT_VOLTAGE2:
2023-04-24 19:20:21 +02:00
shunt_data.voltage_veh = result;
2023-03-12 21:06:23 +01:00
break;
case CAN_ID_SHUNT_VOLTAGE3:
shunt_data.voltage3 = result;
break;
case CAN_ID_SHUNT_TEMP:
shunt_data.busbartemp = result;
break;
case CAN_ID_SHUNT_POWER:
shunt_data.power = result;
break;
case CAN_ID_SHUNT_CURRENT_COUNTER:
// TODO: Use this when we get the shunt to emit current counter data (the
// shunt apparently emits As, not mAs)
// shunt_data.current_counter = result * 1000;
2023-03-12 21:06:23 +01:00
break;
case CAN_ID_SHUNT_ENERGY_COUNTER:
shunt_data.energy = result;
break;
}
}