Skip to content

Commit

Permalink
Improve ADC accuracy
Browse files Browse the repository at this point in the history
Use the charge current ADC when undocked to determine ADC offset,
tweak the resistors values, and disable SMPS powersaving during ADC.
  • Loading branch information
olliewalsh committed Aug 7, 2023
1 parent bedea29 commit 31da75f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
32 changes: 26 additions & 6 deletions Firmware/LowLevel/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ SerialPIO uiSerial(PIN_UI_TX, PIN_UI_RX, 250);
/**
* @brief Some hardware parameters
*/
#define VIN_R1 10000.0f
#define VIN_R2 1000.0f
#define VIN_R1 9980.0f
#define VIN_R2 998.0f
#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 @@ -601,16 +605,32 @@ void loop() {
if (now - last_status_update_millis > STATUS_CYCLETIME) {
updateNeopixel();

// Disable power saving during ADC
digitalWrite(PIN_SMPS_POWERSAVE, HIGH);
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 undocked use charge current ADC to determine adc offset
if( status_message.v_charge < 3.0f ) {
adc_offset_samples[next_adc_offset_sample++] = (float)analogRead(PIN_ANALOG_CHARGE_CURRENT);
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;
}
digitalWrite(PIN_SMPS_POWERSAVE, LOW);

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
6 changes: 6 additions & 0 deletions Firmware/LowLevel/src/pins.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#pragma once

#ifdef ARDUINO_RASPBERRY_PI_PICO_W
#define PIN_SMPS_POWERSAVE 33
#else
#define PIN_SMPS_POWERSAVE 23
#endif

#ifdef HW_0_9_X
#define PIN_IMU_CS 17
#define PIN_ANALOG_BATTERY_VOLTAGE 27
Expand Down

0 comments on commit 31da75f

Please sign in to comment.