Skip to content

Commit

Permalink
Current Sensor Calibration (#1387)
Browse files Browse the repository at this point in the history
### Changelist 
Calibrated the current sensor Error based on the data collected for both
channels of the current sensor and for directions of current (charging &
discharging)

### Testing Done
Tested on the car and works amazing

Here's the data and how the calibration works (done in excel)
[50A Calibration Forward
Current.xlsx](https://github.com/user-attachments/files/17836984/50A.Calibration.Forward.Current.xlsx)
[50A Calibration Reverse
Current.xlsx](https://github.com/user-attachments/files/17836986/50A.Calibration.Reverse.Current.xlsx)
[400A Calibration Forward
Current.xlsx](https://github.com/user-attachments/files/17836987/400A.Calibration.Forward.Current.xlsx)
[400A Calibration Reverse
Current.xlsx](https://github.com/user-attachments/files/17836988/400A.Calibration.Reverse.Current.xlsx)


### Resolved Tickets
<!-- Link any tickets that this PR resolves. -->
  • Loading branch information
DJ90864 authored Dec 8, 2024
1 parent f105e4d commit 1ce72a4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion firmware/quadruna/BMS/src/app/app_tractiveSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "app_canTx.h"
#include "app_utils.h"

#define HIGH_RES_MAX_CURRENT_READING (75.0f)
#define HIGH_RES_MAX_CURRENT_READING (50.0f)

// Taken from our cell's datasheet on Confluence:
// https://ubcformulaelectric.atlassian.net/wiki/pages/viewpageattachments.action?pageId=55214081&preview=%2F55214081%2F55214090%2FSLPB9742126-3.7V-5900mAh-10C-V2018.pdf
Expand Down
66 changes: 52 additions & 14 deletions firmware/quadruna/BMS/src/io/io_tractiveSystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,31 @@
// Voltage divider for the TS+ voltage sensing
#define TS_VOLTAGE_DIV (20e+3f / (6 * 1e+6f + 30e3f + 20e3f))

// Offset voltage of output 1. Found through testing
#define OUTPUT_1_OFFSET (2.5052f)
// Sensitivity of output 1: 26.7mV/A
#define OUTPUT_1_SENSITIVITY (1.0f / 26.7e-3f)
// Offset voltage of output 1.
#define OUTPUT_1_OFFSET (2.5f)
// Sensitivity of output 1: 40mV/A
#define OUTPUT_1_SENSITIVITY (1.0f / 40.0e-3f)
// Voltage divider from adc --> current sensor output
#define OUTPUT_1_DIV ((33.0f + 60.4f) / (60.4f))

// Offset voltage of output 2. Found through testing
#define OUTPUT_2_OFFSET (2.492f)
// Sensitivity of output 2: 4.0mV/A
#define OUTPUT_2_SENSITIVITY (1.0f / 4.0e-3f)
// Offset voltage of output 2.
#define OUTPUT_2_OFFSET (2.5f)
// Sensitivity of output 2: 5.2mV/A
#define OUTPUT_2_SENSITIVITY (1.0f / 5.2e-3f)
// Voltage divider from adc --> current sensor output
#define OUTPUT_2_DIV ((33.0f + 60.4f) / (60.4f))

// Current Sensor error calibration parameters (based on experimental data)
#define OUTPUT1_DISCHARGING_ERROR_SLOPE (0.5028f)
#define OUTPUT1_DISCHARGING_ERROR_OFFSET (-0.0894f)
#define OUTPUT1_CHARGING_ERROR_SLOPE (0.5045f)
#define OUTPUT1_CHARGING_ERROR_OFFSET (-0.2677f)

#define OUTPUT2_DISCHARGING_ERROR_SLOPE (0.2417f)
#define OUTPUT2_DISCHARGING_ERROR_OFFSET (2.3634f)
#define OUTPUT2_CHARGING_ERROR_SLOPE (0.2324f)
#define OUTPUT2_CHARGING_ERROR_OFFSET (2.4038f)

static const TractiveSystemConfig *config = NULL;

void io_tractiveSystem_init(const TractiveSystemConfig *ts_config)
Expand Down Expand Up @@ -105,13 +116,27 @@ float io_tractiveSystem_getCurrentHighResolution()
// 60.4k
// Offset Voltage = 2.5V (Datasheet)
//
// Sensitivity = 26.7mV/A
// Sensitivity = 40mV/A

// Output from current sensor:
const float hsnbv_d06_output_1 = adc_voltage * OUTPUT_1_DIV;

// Return the current which corresponds to the output voltage
return -((hsnbv_d06_output_1 - OUTPUT_1_OFFSET) * OUTPUT_1_SENSITIVITY);
// Calculate the current which corresponds to the output voltage (baed on sensor datasheet)
const float high_res_current = ((hsnbv_d06_output_1 - OUTPUT_1_OFFSET) * OUTPUT_1_SENSITIVITY);

// Error Calibration for High Resolution Current Sensor (based on calibration data)
float high_res_curr_calibration = 0.0f;
if (high_res_current > -0.2f)
{
high_res_curr_calibration =
high_res_current * OUTPUT1_DISCHARGING_ERROR_SLOPE + OUTPUT1_DISCHARGING_ERROR_OFFSET;
}
else
{
high_res_curr_calibration = high_res_current * OUTPUT1_CHARGING_ERROR_SLOPE + OUTPUT1_CHARGING_ERROR_OFFSET;
}

return -(high_res_current + high_res_curr_calibration);
}

float io_tractiveSystem_getCurrentLowResolution()
Expand Down Expand Up @@ -142,11 +167,24 @@ float io_tractiveSystem_getCurrentLowResolution()
// 60.4k
// Offset Voltage = 2.5V (Datasheet)
//
// Sensitivity = 4.0mV/A
// Sensitivity = 5.2mV/A

// Output from current sensor:
const float hsnbv_d06_output_2 = adc_voltage * OUTPUT_2_DIV;

// Return the current which corresponds to the output voltage
return -((hsnbv_d06_output_2 - OUTPUT_2_OFFSET) * OUTPUT_2_SENSITIVITY);
// Calculate the current which corresponds to the output voltage (baed on sensor datasheet)
const float low_res_current = ((hsnbv_d06_output_2 - OUTPUT_2_OFFSET) * OUTPUT_2_SENSITIVITY);

// Error Calibration for Low Resolution Current Sensor (based on calibration data)
float low_res_curr_calibration = 0.0f;
if (low_res_current > -0.2f)
{
low_res_curr_calibration = low_res_current * OUTPUT2_DISCHARGING_ERROR_SLOPE + OUTPUT2_DISCHARGING_ERROR_OFFSET;
}
else
{
low_res_curr_calibration = low_res_current * OUTPUT2_CHARGING_ERROR_SLOPE + OUTPUT2_CHARGING_ERROR_OFFSET;
}

return -(low_res_current + low_res_curr_calibration);
}

0 comments on commit 1ce72a4

Please sign in to comment.