Skip to content

Commit 93c3efc

Browse files
committed
Improve ADC accuracy
Use the charge voltage ADC when mowing to determine ADC offset.
1 parent 531f7eb commit 93c3efc

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

Firmware/LowLevel/src/main.cpp

+21-4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ SerialPIO uiSerial(PIN_UI_TX, PIN_UI_RX, 250);
6262
#define R_SHUNT 0.003f
6363
#define CURRENT_SENSE_GAIN 100.0f
6464

65+
int next_adc_offset_sample = 0;
66+
float adc_offset_samples[20] = {0};
67+
float adc_offset = 0.0f;
68+
6569
#define BATT_ABS_MAX 28.7f
6670
#define BATT_ABS_Min 21.7f
6771

@@ -677,15 +681,28 @@ void loop() {
677681
updateNeopixel();
678682

679683
status_message.v_battery =
680-
(float) analogRead(PIN_ANALOG_BATTERY_VOLTAGE) * (3.3f / 4096.0f) * ((VIN_R1 + VIN_R2) / VIN_R2);
681-
status_message.v_charge =
682-
(float) analogRead(PIN_ANALOG_CHARGE_VOLTAGE) * (3.3f / 4096.0f) * ((VIN_R1 + VIN_R2) / VIN_R2);
684+
((float)analogRead(PIN_ANALOG_BATTERY_VOLTAGE) - adc_offset) * (3.33f / 4096.0f) * ((VIN_R1 + VIN_R2) / VIN_R2);
683685
#ifndef IGNORE_CHARGING_CURRENT
684686
status_message.charging_current =
685-
(float) analogRead(PIN_ANALOG_CHARGE_CURRENT) * (3.3f / 4096.0f) / (CURRENT_SENSE_GAIN * R_SHUNT);
687+
((float)analogRead(PIN_ANALOG_CHARGE_CURRENT) - adc_offset) * (3.33f / 4096.0f) / (CURRENT_SENSE_GAIN * R_SHUNT);
686688
#else
687689
status_message.charging_current = -1.0f;
688690
#endif
691+
status_message.v_charge = ((float)analogRead(PIN_ANALOG_CHARGE_VOLTAGE) - adc_offset) * (3.33f / 4096.0f) * ((VIN_R1 + VIN_R2) / VIN_R2);
692+
693+
694+
// If mowing use charge current ADC to determine adc offset
695+
if( last_high_level_state.current_mode == HighLevelMode::MODE_AUTONOMOUS && last_high_level_state.gps_quality != 0 ) {
696+
adc_offset_samples[next_adc_offset_sample++] = (float)analogRead(PIN_ANALOG_CHARGE_VOLTAGE);
697+
next_adc_offset_sample %= 20;
698+
699+
float tmp = 0.0f;
700+
for(int i=0; i<20; i++) {
701+
tmp += adc_offset_samples[i];
702+
}
703+
adc_offset = tmp / 20.0f;
704+
}
705+
689706
status_message.status_bitmask = (status_message.status_bitmask & 0b11111011) | ((charging_allowed & 0b1) << 2);
690707
status_message.status_bitmask = (status_message.status_bitmask & 0b11011111) | ((sound_available & 0b1) << 5);
691708

0 commit comments

Comments
 (0)