@@ -62,6 +62,10 @@ SerialPIO uiSerial(PIN_UI_TX, PIN_UI_RX, 250);
62
62
#define R_SHUNT 0 .003f
63
63
#define CURRENT_SENSE_GAIN 100 .0f
64
64
65
+ int next_adc_offset_sample = 0 ;
66
+ float adc_offset_samples[20 ] = {0 };
67
+ float adc_offset = 0 .0f ;
68
+
65
69
#define BATT_ABS_MAX 28 .7f
66
70
#define BATT_ABS_Min 21 .7f
67
71
@@ -677,15 +681,28 @@ void loop() {
677
681
updateNeopixel ();
678
682
679
683
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);
683
685
#ifndef IGNORE_CHARGING_CURRENT
684
686
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);
686
688
#else
687
689
status_message.charging_current = -1 .0f ;
688
690
#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
+
689
706
status_message.status_bitmask = (status_message.status_bitmask & 0b11111011 ) | ((charging_allowed & 0b1 ) << 2 );
690
707
status_message.status_bitmask = (status_message.status_bitmask & 0b11011111 ) | ((sound_available & 0b1 ) << 5 );
691
708
0 commit comments