Skip to content

Commit

Permalink
fix(current): remove shunt from current measures
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonidotpy committed Feb 21, 2024
1 parent 8d0a7e8 commit 98caed2
Show file tree
Hide file tree
Showing 12 changed files with 3,005 additions and 3,002 deletions.
2 changes: 1 addition & 1 deletion cellboard/Core/Lib/can
Submodule can updated 39 files
+1 −1 dbc/bms/bms.dbc
+470 −0 dbc/hv_current/hv_current.dbc
+176 −176 dbc/inverters/inverters.dbc
+1 −1 dbc/primary/primary.dbc
+1 −1 dbc/secondary/secondary.dbc
+1 −1 dbc/simulator/simulator.dbc
+23 −23 lib/bms/bms_network.c
+2 −2 lib/bms/bms_network.h
+616 −0 lib/bms/bms_utils_c.c
+187 −0 lib/bms/bms_utils_c.h
+8,930 −0 lib/hv_current/hv_current_network.c
+8,898 −0 lib/hv_current/hv_current_network.h
+2,468 −0 lib/hv_current/hv_current_utils_c.c
+385 −0 lib/hv_current/hv_current_utils_c.h
+48 −0 lib/hv_current/hv_current_watchdog.c
+89 −0 lib/hv_current/hv_current_watchdog.h
+769 −769 lib/inverters/inverters_network.c
+3,576 −3,576 lib/inverters/inverters_network.h
+3,646 −0 lib/inverters/inverters_utils_c.c
+400 −0 lib/inverters/inverters_utils_c.h
+2 −2 lib/inverters/inverters_watchdog.c
+4 −4 lib/inverters/inverters_watchdog.h
+111 −111 lib/primary/primary_network.c
+2 −2 lib/primary/primary_network.h
+4,264 −0 lib/primary/primary_utils_c.c
+995 −0 lib/primary/primary_utils_c.h
+5 −5 lib/secondary/secondary_network.c
+2 −2 lib/secondary/secondary_network.h
+1,242 −0 lib/secondary/secondary_utils_c.c
+287 −0 lib/secondary/secondary_utils_c.h
+5 −5 lib/simulator/simulator_network.c
+2 −2 lib/simulator/simulator_network.h
+348 −0 lib/simulator/simulator_utils_c.c
+88 −0 lib/simulator/simulator_utils_c.h
+471 −0 networks/hv_current/IVT-S_12082020.dbc
+907 −0 proto/hv_current/hv_current.proto
+1,105 −0 proto/hv_current/hv_current_proto_interface.h
+841 −841 proto/inverters/inverters.proto
+249 −249 proto/inverters/inverters_proto_interface.h
2 changes: 1 addition & 1 deletion cellboard/Core/Lib/micro-libs
4 changes: 3 additions & 1 deletion cellboard/STM32Make.make
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ BUILD_DIR = build
# C sources
C_SOURCES = \
Core/Lib/can/lib/bms/bms_network.c \
Core/Lib/can/lib/bms/bms_utils_c.c \
Core/Lib/can/lib/bms/bms_watchdog.c \
Core/Lib/can/lib/primary/primary_network.c \
Core/Lib/can/lib/primary/primary_utils_c.c \
Core/Lib/can/lib/primary/primary_watchdog.c \
Core/Lib/can/lib/secondary/secondary_network.c \
Core/Lib/can/lib/secondary/secondary_utils_c.c \
Core/Lib/can/lib/secondary/secondary_watchdog.c \
Core/Lib/micro-libs/blink/blink.c \
Core/Lib/micro-libs/m95256/m95256.c \
Expand Down Expand Up @@ -225,7 +228,6 @@ vpath %.cpp $(sort $(dir $(CPP_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))

# list of ASM program objects
# list of ASM program objects
UPPER_CASE_ASM_SOURCES = $(filter %.S,$(ASM_SOURCES))
LOWER_CASE_ASM_SOURCES = $(filter %.s,$(ASM_SOURCES))
Expand Down
2 changes: 1 addition & 1 deletion mainboard/Inc/pack/internal_voltage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "../../fenice_config.h"
#include "max22530.h"

#define INTERNAL_VOLTAGE_DIVIDER_RATIO 0.003
#define INTERNAL_VOLTAGE_DIVIDER_RATIO 0.0032 // 0.003
#define CONVERT_VALUE_TO_INTERNAL_ADC_VOLTAGE(x) MAX22530_CONV_VALUE_TO_VOLTAGE(x)
#define CONVERT_VALUE_TO_INTERNAL_VOLTAGE(x) (CONVERT_VALUE_TO_INTERNAL_ADC_VOLTAGE(x) / INTERNAL_VOLTAGE_DIVIDER_RATIO)

Expand Down
4 changes: 3 additions & 1 deletion mainboard/STM32Make.make
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ Src/tim.c \
Src/usart.c \
Src/watchdog.c \
lib/can/lib/bms/bms_network.c \
lib/can/lib/bms/bms_utils_c.c \
lib/can/lib/bms/bms_watchdog.c \
lib/can/lib/primary/primary_network.c \
lib/can/lib/primary/primary_utils_c.c \
lib/can/lib/primary/primary_watchdog.c \
lib/can/lib/secondary/secondary_network.c \
lib/can/lib/secondary/secondary_utils_c.c \
lib/can/lib/secondary/secondary_watchdog.c \
lib/micro-libs/blink/blink.c \
lib/micro-libs/cli/cli.c \
Expand Down Expand Up @@ -256,7 +259,6 @@ vpath %.cpp $(sort $(dir $(CPP_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))

# list of ASM program objects
# list of ASM program objects
UPPER_CASE_ASM_SOURCES = $(filter %.S,$(ASM_SOURCES))
LOWER_CASE_ASM_SOURCES = $(filter %.s,$(ASM_SOURCES))
Expand Down
4 changes: 3 additions & 1 deletion mainboard/Src/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)

/* USER CODE BEGIN 1 */

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) {
#include "current.h"

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef * hadc) {
if (hadc->Instance == ADC_MUX.Instance) {
_feedback_handle_adc_cnv_cmpl_irq();
}
Expand Down
24 changes: 12 additions & 12 deletions mainboard/Src/pack/current.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,42 @@ current_t _current_convert_high(float volt) {
return (CURRENT_DIVIDER_RATIO_INVERSE / CURRENT_SENSITIVITY_HIGH) * (volt - V0H);
}

// TODO: Use defines for shut current conversion
#define CURRENT_SHUNT_VREF_OFFSET 0.454f // 0.9f
#define CURRENT_SHUNT_OP_GAIN 75.f
#define CURRENT_SHUNT_RESISTANCE 1e-4f

current_t _current_convert_shunt(float volt) {
return (volt - 0.468205124) / (1e-4f * 75);
return (volt - CURRENT_SHUNT_VREF_OFFSET) / (CURRENT_SHUNT_OP_GAIN * CURRENT_SHUNT_RESISTANCE);
}

void current_start_measure() {
HAL_ADC_Start_DMA(&ADC_HALL50, (uint32_t *)adc_50, MEASURE_SAMPLE_SIZE);
HAL_ADC_Start_DMA(&ADC_HALL300, (uint32_t *)adc_300, MEASURE_SAMPLE_SIZE);
}


uint32_t current_read(float shunt_adc_val) {
uint32_t time = HAL_GetTick();
uint64_t avg_50 = 0;
uint64_t avg_300 = 0;
float avg_50 = 0;
float avg_300 = 0;
for (size_t i = 0; i < MEASURE_SAMPLE_SIZE; i++) {
avg_50 += adc_50[i];
avg_300 += adc_300[i];
}

// Convert Hall-low (50A)
float volt = avg_50 * (3.3f / 4095 / MEASURE_SAMPLE_SIZE);
float volt = avg_50 * 3.3f / 4095.f / MEASURE_SAMPLE_SIZE;
current[CURRENT_SENSOR_50] = _current_convert_low(volt);

// Convert Hall-high (300A)
volt_300 = avg_300 * (3.3f / 4095 / MEASURE_SAMPLE_SIZE);
volt_300 = avg_300 * 3.3f / 4095.f / MEASURE_SAMPLE_SIZE;
current[CURRENT_SENSOR_300] = _current_convert_high(volt_300);

// Convert Shunt
current[CURRENT_SENSOR_SHUNT] = _current_convert_shunt(shunt_adc_val);

// Check for over-current
// Check for over-currents
error_toggle_check(fabsf(current_get_current()) > PACK_MAX_CURRENT, ERROR_OVER_CURRENT, 0);

return time;
}

Expand All @@ -87,13 +90,10 @@ void current_zero() {
}

current_t current_get_current() {
current_t shunt = fabsf(current[CURRENT_SENSOR_SHUNT]);
current_t hall_50 = fabsf(current[CURRENT_SENSOR_50]);
current_t hall_300 = fabsf(current[CURRENT_SENSOR_300]);

// Return current read from the correct
if (shunt < 4 && hall_50 < 4)
return current[CURRENT_SENSOR_SHUNT];
if (hall_50 < 34 && hall_300 < 34)
return current[CURRENT_SENSOR_50];
return current[CURRENT_SENSOR_300];
Expand All @@ -114,4 +114,4 @@ void current_check_errors() {

// Hall effect sensor disconnected
error_toggle_check(volt_300 < CURRENT_SENSOR_DISCONNECTED_THRESHOLD, ERROR_CONNECTOR_DISCONNECTED, 1);
}
}
13 changes: 11 additions & 2 deletions mainboard/Src/peripherals/can_comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "imd.h"

#include <string.h>
#include <math.h>

#ifdef TEMP_GROUP_ERROR_ENABLE
uint16_t temp_errors[CELLBOARD_COUNT];
Expand Down Expand Up @@ -163,10 +164,18 @@ HAL_StatusTypeDef can_car_send(uint16_t id) {
primary_hv_current_t raw_curr = { 0 };
primary_hv_current_converted_t conv_curr = { 0 };

// current_t shunt_current = fabsf(current_get_current_from_sensor(CURRENT_SENSOR_SHUNT));
// current_t hall50_current = fabsf(current_get_current_from_sensor(CURRENT_SENSOR_50));
// current_t hall300_current = fabsf(current_get_current_from_sensor(CURRENT_SENSOR_300));

// char msg[100] = { 0 };
// sprintf(msg, "Shunt: %.3f, Hall 50: %.3f, Hall 300: %.3f", shunt_current, hall50_current, hall300_current);
// cli_bms_debug(msg, strlen(msg));

conv_curr.current = current_get_current();
conv_curr.power = (conv_curr.current * CONVERT_VALUE_TO_INTERNAL_VOLTAGE(internal_voltage_get_tsp())) / 1000.f;
conv_curr.energy = 0;
conv_curr.soc = 0;
conv_curr.energy = 0; // Not implemented yet
conv_curr.soc = 0; // Not implemented yet

primary_hv_current_conversion_to_raw_struct(&raw_curr, &conv_curr);

Expand Down
2 changes: 1 addition & 1 deletion mainboard/lib/can
Submodule can updated 39 files
+1 −1 dbc/bms/bms.dbc
+470 −0 dbc/hv_current/hv_current.dbc
+176 −176 dbc/inverters/inverters.dbc
+1 −1 dbc/primary/primary.dbc
+1 −1 dbc/secondary/secondary.dbc
+1 −1 dbc/simulator/simulator.dbc
+23 −23 lib/bms/bms_network.c
+2 −2 lib/bms/bms_network.h
+616 −0 lib/bms/bms_utils_c.c
+187 −0 lib/bms/bms_utils_c.h
+8,930 −0 lib/hv_current/hv_current_network.c
+8,898 −0 lib/hv_current/hv_current_network.h
+2,468 −0 lib/hv_current/hv_current_utils_c.c
+385 −0 lib/hv_current/hv_current_utils_c.h
+48 −0 lib/hv_current/hv_current_watchdog.c
+89 −0 lib/hv_current/hv_current_watchdog.h
+769 −769 lib/inverters/inverters_network.c
+3,576 −3,576 lib/inverters/inverters_network.h
+3,646 −0 lib/inverters/inverters_utils_c.c
+400 −0 lib/inverters/inverters_utils_c.h
+2 −2 lib/inverters/inverters_watchdog.c
+4 −4 lib/inverters/inverters_watchdog.h
+111 −111 lib/primary/primary_network.c
+2 −2 lib/primary/primary_network.h
+4,264 −0 lib/primary/primary_utils_c.c
+995 −0 lib/primary/primary_utils_c.h
+5 −5 lib/secondary/secondary_network.c
+2 −2 lib/secondary/secondary_network.h
+1,242 −0 lib/secondary/secondary_utils_c.c
+287 −0 lib/secondary/secondary_utils_c.h
+5 −5 lib/simulator/simulator_network.c
+2 −2 lib/simulator/simulator_network.h
+348 −0 lib/simulator/simulator_utils_c.c
+88 −0 lib/simulator/simulator_utils_c.h
+471 −0 networks/hv_current/IVT-S_12082020.dbc
+907 −0 proto/hv_current/hv_current.proto
+1,105 −0 proto/hv_current/hv_current_proto_interface.h
+841 −841 proto/inverters/inverters.proto
+249 −249 proto/inverters/inverters_proto_interface.h
2 changes: 1 addition & 1 deletion mainboard/lib/micro-libs
4 changes: 2 additions & 2 deletions openblt_mainboard/STM32Make.make
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,13 @@ $(BUILD_DIR):
# flash
#######################################
flash: $(BUILD_DIR)/$(TARGET).elf
"/home/tonidotpy/.config/Code - OSS/User/globalStorage/bmd.stm32-for-vscode/@xpack-dev-tools/openocd/0.12.0-1.1/.content/bin/openocd" -f ./openocd.cfg -c "program $(BUILD_DIR)/$(TARGET).elf verify reset exit"
"/home/tonidotpy/.config/Code - OSS/User/globalStorage/bmd.stm32-for-vscode/@xpack-dev-tools/openocd/0.12.0-2.1/.content/bin/openocd" -f ./openocd.cfg -c "program $(BUILD_DIR)/$(TARGET).elf verify reset exit"

#######################################
# erase
#######################################
erase: $(BUILD_DIR)/$(TARGET).elf
"/home/tonidotpy/.config/Code - OSS/User/globalStorage/bmd.stm32-for-vscode/@xpack-dev-tools/openocd/0.12.0-1.1/.content/bin/openocd" -f ./openocd.cfg -c "init; reset halt; stm32f4x mass_erase 0; exit"
"/home/tonidotpy/.config/Code - OSS/User/globalStorage/bmd.stm32-for-vscode/@xpack-dev-tools/openocd/0.12.0-2.1/.content/bin/openocd" -f ./openocd.cfg -c "init; reset halt; stm32f4x mass_erase 0; exit"

#######################################
# clean up
Expand Down

0 comments on commit 98caed2

Please sign in to comment.