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

Improve ADC accuracy #75

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Limit adc_offset to 3%
  • Loading branch information
olliewalsh committed Apr 23, 2024
commit 6d6980dc2b8f940f40094cf89d7baf23bba4dc65
10 changes: 9 additions & 1 deletion Firmware/LowLevel/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ SerialPIO uiSerial(PIN_UI_TX, PIN_UI_RX, 250);
int next_adc_offset_sample = 0;
float adc_offset_samples[20] = {0};
float adc_offset = 0.0f;
// Limit adc_offset to 3%
#define MAX_ADC_OFFSET_PC 0.03f

#define BATT_ABS_MAX 28.7f
#define BATT_ABS_Min 21.7f
Expand Down Expand Up @@ -700,7 +702,13 @@ void loop() {
for(int i=0; i<20; i++) {
tmp += adc_offset_samples[i];
}
adc_offset = tmp / 20.0f;
float new_adc_offset = tmp / 20.0f;
// Limit maximum offset
if(new_adc_offset > 0.0f) {
adc_offset = min(new_adc_offset, (4096 * MAX_ADC_OFFSET_PC));
} else {
adc_offset = max(new_adc_offset, -(4096 * MAX_ADC_OFFSET_PC));
}
}

status_message.status_bitmask = (status_message.status_bitmask & 0b11111011) | ((charging_allowed & 0b1) << 2);
Expand Down