Skip to content

Commit

Permalink
Improve ADC accuracy
Browse files Browse the repository at this point in the history
Use the charge voltage ADC when mowing to determine ADC offset.
  • Loading branch information
olliewalsh committed Apr 23, 2024
1 parent 531f7eb commit 93c3efc
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions Firmware/LowLevel/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ SerialPIO uiSerial(PIN_UI_TX, PIN_UI_RX, 250);
#define R_SHUNT 0.003f
#define CURRENT_SENSE_GAIN 100.0f

int next_adc_offset_sample = 0;
float adc_offset_samples[20] = {0};
float adc_offset = 0.0f;

#define BATT_ABS_MAX 28.7f
#define BATT_ABS_Min 21.7f

Expand Down Expand Up @@ -677,15 +681,28 @@ void loop() {
updateNeopixel();

status_message.v_battery =
(float) analogRead(PIN_ANALOG_BATTERY_VOLTAGE) * (3.3f / 4096.0f) * ((VIN_R1 + VIN_R2) / VIN_R2);
status_message.v_charge =
(float) analogRead(PIN_ANALOG_CHARGE_VOLTAGE) * (3.3f / 4096.0f) * ((VIN_R1 + VIN_R2) / VIN_R2);
((float)analogRead(PIN_ANALOG_BATTERY_VOLTAGE) - adc_offset) * (3.33f / 4096.0f) * ((VIN_R1 + VIN_R2) / VIN_R2);
#ifndef IGNORE_CHARGING_CURRENT
status_message.charging_current =
(float) analogRead(PIN_ANALOG_CHARGE_CURRENT) * (3.3f / 4096.0f) / (CURRENT_SENSE_GAIN * R_SHUNT);
((float)analogRead(PIN_ANALOG_CHARGE_CURRENT) - adc_offset) * (3.33f / 4096.0f) / (CURRENT_SENSE_GAIN * R_SHUNT);
#else
status_message.charging_current = -1.0f;
#endif
status_message.v_charge = ((float)analogRead(PIN_ANALOG_CHARGE_VOLTAGE) - adc_offset) * (3.33f / 4096.0f) * ((VIN_R1 + VIN_R2) / VIN_R2);


// If mowing use charge current ADC to determine adc offset
if( last_high_level_state.current_mode == HighLevelMode::MODE_AUTONOMOUS && last_high_level_state.gps_quality != 0 ) {
adc_offset_samples[next_adc_offset_sample++] = (float)analogRead(PIN_ANALOG_CHARGE_VOLTAGE);
next_adc_offset_sample %= 20;

float tmp = 0.0f;
for(int i=0; i<20; i++) {
tmp += adc_offset_samples[i];
}
adc_offset = tmp / 20.0f;
}

status_message.status_bitmask = (status_message.status_bitmask & 0b11111011) | ((charging_allowed & 0b1) << 2);
status_message.status_bitmask = (status_message.status_bitmask & 0b11011111) | ((sound_available & 0b1) << 5);

Expand Down

0 comments on commit 93c3efc

Please sign in to comment.