From d3ec4a7f2faa5efbfbc85d523e625d001e71a744 Mon Sep 17 00:00:00 2001 From: hamza Date: Thu, 6 Jun 2024 23:12:15 +0300 Subject: [PATCH] - modified the CAN class with a specific messages - add documentation --- Core/Inc/can.h | 8 ++-- Core/Src/can.c | 113 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 98 insertions(+), 23 deletions(-) diff --git a/Core/Inc/can.h b/Core/Inc/can.h index 42a0831..ee48dec 100644 --- a/Core/Inc/can.h +++ b/Core/Inc/can.h @@ -1,15 +1,13 @@ #ifndef INC_CAN_H #define INC_CAN_H -#include -#include #include "stm32f3xx_hal.h" -#include "stm32f3xx_hal_can.h" -#include "stm32f3xx_hal_def.h" #include "ADBMS_Abstraction.h" #include "main.h" -#include "state_machine.h" #include "can-halal.h" +#include "AMS_HighLevel.h" +#include "state_machine.h" +#include #define CAN_ID_IN 0x501 #define CAN_ID_OUT 0x502 diff --git a/Core/Src/can.c b/Core/Src/can.c index 4ebcb4e..7a5f6af 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -13,25 +13,43 @@ void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); ftcan_add_filter(CAN_ID_IN, 0xFFF); } + /* This function sends the status of the mvbms, the battery and of powerground. once every 1s in states: INACTIVE, PRECHARGE, DISCHARGE, CHARGING, ERROR. once every 0.5s in states: READY, ACTIVE. with format of: CAN Messages: - MVBMS Status (1B), Powerground Status 0-100% (1B) - Battery: SoC (1B), Pack Voltage (2B), Current (1B), + Error bit + MVBMS state + Powerground Status 0-100% + Errors + Battery state of charge + Pack Voltage + Current + Battery temperature (12 bit) Min/Max. Cell Temp (ID, Min Temp, ID, Max Temp)(3B), Min/Max Cell Voltage (ID, Min Voltage, ID, Max Voltage)(3B) -bit 0-2: status -bit 3-7: -bit 8-15: State of Charge from 0-100% -bit 16-31: Battery voltage -bit 32-47: Current measurement -bit 48-50: id of cell with highest temperature -bit 51-62: temperature of the cell with highest temperature (12 bits moved 4 bit to the left) +bit 0 (1b): empty +bit 1-3 (3b): state +bit 4-11 (8b): powerground status +bit 12-19 (8b): error +bit 20-27 (8b): state of charge from 0-100% +bit 28-39 (12b): battery voltage +bit 40-51 (12b): current measurement +bit 52-63 (12b): temperature of the cell with highest temperature + + +bit 0-3 (4b): ID of the sensor with highest temperature +bit 4-7 (4b): ID of the sensor with lowest temperataure +bit 8-19 (12b): temperature of the coldest cell +bit 20-23 (4b): ID of the cell with the lowest voltage +bit 24-35 (12b): lowest cell voltage +bit 36-39 (4b): ID of the cell the the highest voltage +bit 40-51 (12b): highest cell voltage +bit 52-63 (12b): empty */ void can_handle_send_status() { @@ -41,24 +59,83 @@ void can_handle_send_status() { can_delay_manager = HAL_GetTick() + CAN_STATUS_FREQ; uint8_t data[8] = {}; - data[0] = (state.current_state << 5); // save 5 bit since codes are from 0-7 61 bits left - //data[1] = // in 8 bits from 0-100% - ftcan_marshal_unsigned(&data[2], RELAY_BAT_SIDE_VOLTAGE, 2); // Battery voltage 16 bit 45 bit - ftcan_marshal_unsigned(&data[4], CURRENT_MEASUREMENT, 2); // 16 bit measurement + int8_t id_highest_temp = -1; + int16_t highest_temp = INT16_MIN; + sm_check_battery_temperature(&id_highest_temp, &highest_temp); + + data[0] = ((state.current_state << 4) | (powerground_status >> 4)); // 1 bit emptyy | 3 bit state | 4 bit powerground + data[1] = ((powerground_status << 4) | (state.error_source >> 4)); // 4 bit powerground | 4 bit error + data[2] = ((state.error_source << 4) | (0)); // 4 bit error | 4 bit state of charge + data[3] = ((0) + (RELAY_BAT_SIDE_VOLTAGE >> 12)); // 4 bit state of charge | 4 bit battery voltage + data[4] = ((RELAY_BAT_SIDE_VOLTAGE >> 4)); + data[5] = ((CURRENT_MEASUREMENT >> 8)); + data[6] = ((CURRENT_MEASUREMENT & 0x00F0) | (highest_temp >> 12)); + data[7] = ((highest_temp) >> 4); - int8_t id = -1; - int16_t temp = INT16_MIN; - sm_check_cell_temps(&id, &temp); - data[6] = (id << 4) | (temp >> 4); // there are only 7 TMP1075 ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); - ; + int8_t id_lowest_temp = -1; + int16_t lowest_temp = INT16_MIN; + for (int i = 0; i < N_TEMP_SENSORS; i++) { + if (tmp1075_temps[i] < lowest_temp){ + id_lowest_temp = i; + lowest_temp = tmp1075_temps[i]; + } + } + + int8_t id_lowest_volt = -1; + int16_t lowest_volt = INT16_MIN; + int8_t id_highest_volt = -1; + int16_t highest_volt = INT16_MIN; + + for (int i = 0; i < module.sumOfCellMeasurements; i++) { + if (sm_return_cell_voltage(i) < lowest_temp){ + id_lowest_volt = i; + lowest_volt = sm_return_cell_voltage(i); + } + if (sm_return_cell_voltage(i) > highest_temp){ + id_highest_volt = i; + highest_volt = sm_return_cell_voltage(i); + } + } + + data[0] = ((id_highest_temp & 0x0F) << 4 | (id_lowest_temp & 0x0F)); + data[1] = ((lowest_temp) >> 8); + data[2] = ((lowest_temp & 0x00F0) | (id_lowest_volt & 0x0F)); + data[3] = (lowest_volt >> 8); + data[4] = ((lowest_volt & 0x00F0) | (id_highest_volt & 0x0F)); + data[5] = ((highest_volt >> 8)); + data[6] = ((highest_volt & 0x00F0)); + data[7] = 0; + ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); } +/* +bit 0-3 (4b): ID of the sensor with highest temperature +bit 4-7 (4b): ID of the sensor with lowest temperataure +bit 8-19 (12b): temperature of the coldest cell +bit 20-23 (4b): ID of the cell with the lowest voltage +bit 24-35 (12b): lowest cell voltage +bit 36-39 (4b): ID of the cell the the highest voltage +bit 40-51 (12b): highest cell voltage +bit 52-63 (12b): empty +*/ /* can_handle_recieve_command() should only check if the message is valid and then hand it to the sm_handle_ams_in() which handles the state machine transition. + +This function recieves a command from the Autobox with the CAN ID of 0x501. +with format of: +data[0] = target state + 0x0 STATE_INACTIVE | disconnect power to the ESC of powerground. Send it to return the mvbms to idle/monitoring mode. If data[1] != 0 -> assume bad CAN message. + 0x1 STATE_READY | conneect power to the ESC of powerground and but with no PWM signal. If data[1] != 0 -> assume bad CAN message. + 0x2 STATE_ACTIVE | activate powerground at (data[1]) percent. If data[1] > 100 -> assume bad CAN message. + +allowed transitions: + STATE_INACTIVE -> STATE_READY + STATE_READY -> STATE_INACTIVE, STATE_ACTIVE + STATE_ACTIVE -> STATE_INACTIVE, STATE_READY */ void can_handle_recieve_command(const uint8_t *data){ if (data[0] == 0x00 && data[1] == 0x00){