@@ -56,11 +56,15 @@ SerialPIO uiSerial(PIN_UI_TX, PIN_UI_RX, 250);
5656/* *
5757 * @brief Some hardware parameters
5858 */
59- #define VIN_R1 10000 .0f
60- #define VIN_R2 1000 .0f
59+ #define VIN_R1 9980 .0f
60+ #define VIN_R2 998 .0f
6161#define R_SHUNT 0 .003f
6262#define CURRENT_SENSE_GAIN 100 .0f
6363
64+ int next_adc_offset_sample = 0 ;
65+ float adc_offset_samples[20 ] = {0 };
66+ float adc_offset = 0 .0f ;
67+
6468#define BATT_ABS_MAX 28 .7f
6569#define BATT_ABS_Min 21 .7f
6670
@@ -601,16 +605,32 @@ void loop() {
601605 if (now - last_status_update_millis > STATUS_CYCLETIME) {
602606 updateNeopixel ();
603607
608+ // Disable power saving during ADC
609+ digitalWrite (PIN_SMPS_POWERSAVE, HIGH);
604610 status_message.v_battery =
605- (float ) analogRead (PIN_ANALOG_BATTERY_VOLTAGE) * (3 .3f / 4096 .0f ) * ((VIN_R1 + VIN_R2) / VIN_R2);
606- status_message.v_charge =
607- (float ) analogRead (PIN_ANALOG_CHARGE_VOLTAGE) * (3 .3f / 4096 .0f ) * ((VIN_R1 + VIN_R2) / VIN_R2);
611+ ((float )analogRead (PIN_ANALOG_BATTERY_VOLTAGE) - adc_offset) * (3 .33f / 4096 .0f ) * ((VIN_R1 + VIN_R2) / VIN_R2);
608612#ifndef IGNORE_CHARGING_CURRENT
609613 status_message.charging_current =
610- ( float ) analogRead (PIN_ANALOG_CHARGE_CURRENT) * (3 .3f / 4096 .0f ) / (CURRENT_SENSE_GAIN * R_SHUNT);
614+ (( float )analogRead (PIN_ANALOG_CHARGE_CURRENT) - adc_offset) * (3 .33f / 4096 .0f ) / (CURRENT_SENSE_GAIN * R_SHUNT);
611615#else
612616 status_message.charging_current = -1 .0f ;
613617#endif
618+ status_message.v_charge = ((float )analogRead (PIN_ANALOG_CHARGE_VOLTAGE) - adc_offset) * (3 .33f / 4096 .0f ) * ((VIN_R1 + VIN_R2) / VIN_R2);
619+
620+
621+ // If undocked use charge current ADC to determine adc offset
622+ if ( status_message.v_charge < 3 .0f ) {
623+ adc_offset_samples[next_adc_offset_sample++] = (float )analogRead (PIN_ANALOG_CHARGE_CURRENT);
624+ next_adc_offset_sample %= 20 ;
625+
626+ float tmp = 0 .0f ;
627+ for (int i=0 ; i<20 ; i++) {
628+ tmp += adc_offset_samples[i];
629+ }
630+ adc_offset = tmp / 20 .0f ;
631+ }
632+ digitalWrite (PIN_SMPS_POWERSAVE, LOW);
633+
614634 status_message.status_bitmask = (status_message.status_bitmask & 0b11111011 ) | ((charging_allowed & 0b1 ) << 2 );
615635 status_message.status_bitmask = (status_message.status_bitmask & 0b11011111 ) | ((sound_available & 0b1 ) << 5 );
616636
0 commit comments