Skip to content

Commit

Permalink
Fix battery readings
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed Dec 10, 2024
1 parent 5e39603 commit 177565c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
39 changes: 21 additions & 18 deletions cmd/tc2-hat-attiny/attiny.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,46 +610,43 @@ func (a *attiny) makeIndividualAnalogReading(reg1, reg2 Register) (uint16, error
*/

func (a *attiny) readRTCBattery() (float32, error) {
raw, _, err := a.readBattery(rtcBattery1Reg, rtcBattery2Reg)
raw, _, err := a.readBattery(batteryLVDivVal1Reg, batteryLVDivVal2Reg)
if err != nil {
return 0, err
}
hardwareVersion, err := eeprom.GetPowerPCBVersion()
if err != nil {
return 0, err
}
return float32(raw) * 3.3 / 1023, nil
return calculateBatteryVoltage(raw, versionStr(hardwareVersion), rtcResistorVals)
}

func (a *attiny) readLVBattery() (float32, error) {
raw, _, err := a.readBattery(batteryLVDivVal1Reg, batteryLVDivVal2Reg)
if err != nil {
return 0, err
}

var r1, r2, vref float32
hardwareVersion, err := eeprom.GetPowerPCBVersion()
if err != nil {
return 0, err
}

vref, r1, r2, err = getResistorDividerValuesFromVersion(versionStr(hardwareVersion), lvResistorVals)
if err != nil {
return 0, err
}
v := float32(raw) * vref / 1023 // raw is from 0 to 1023, 0 at 0V and 1023 at Vref
return v * (r1 + r2) / (r2), nil
return calculateBatteryVoltage(raw, versionStr(hardwareVersion), lvResistorVals)
}

func (a *attiny) readHVBattery() (float32, error) {
raw, _, err := a.readBattery(batteryLVDivVal1Reg, batteryLVDivVal2Reg)
raw, _, err := a.readBattery(batteryHVDivVal1Reg, batteryLVDivVal2Reg)
if err != nil {
return 0, err
}

var r1, r2, vref float32
hardwareVersion, err := eeprom.GetPowerPCBVersion()
if err != nil {
return 0, err
}
return calculateBatteryVoltage(raw, versionStr(hardwareVersion), hvResistorVals)
}

vref, r1, r2, err = getResistorDividerValuesFromVersion(versionStr(hardwareVersion), hvResistorVals)
func calculateBatteryVoltage(raw uint16, pcbVersion versionStr, resistorVals []rVals) (float32, error) {
vref, r1, r2, err := getResistorDividerValuesFromVersion(pcbVersion, resistorVals)
if err != nil {
return 0, err
}
Expand All @@ -664,9 +661,15 @@ var lvResistorVals = []rVals{
}

var hvResistorVals = []rVals{
{"0.1.4", 3.3, 2000, 560 + 33},
{"0.1.5", 3.325, 2000, 680},
{"0.7.0", 3.3, 2000, 470},
{"0.1.4", 3.3, 2000, 150 + 22},
{"0.1.5", 3.325, 2000, 168},
{"0.7.0", 3.3, 2000, 168},
}

var rtcResistorVals = []rVals{
{"0.1.4", 3.3, 0, 1},
{"0.1.5", 3.325, 0, 1},
{"0.7.0", 3.3, 0, 1},
}

func (a *attiny) checkForErrorCodes(clearErrors bool) ([]ErrorCode, error) {
Expand Down
37 changes: 32 additions & 5 deletions cmd/tc2-hat-attiny/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,38 @@ func TestComparingVersions(t *testing.T) {
assert.True(t, equal)
}

func TestGettingResistorValues(t *testing.T) {
vref, r1, r2, err := getResistorDividerValuesFromVersion(versionStr("0.1.4"), hvResistorVals)
func TestCalculatingBatteryVoltages(t *testing.T) {
tolerance := 0.001
raw := uint16(1023)

// Low battery voltage check for version 0.1.4
batteryVal, err := calculateBatteryVoltage(raw, versionStr("0.1.4"), lvResistorVals)
assert.NoError(t, err)
assert.Equal(t, float32(3.3), vref)
assert.Equal(t, float32(2000), r1)
assert.Equal(t, float32(560+33), r2)
assert.InDelta(t, float32(14.4298482293), batteryVal, tolerance)

// High battery voltage check for version 0.1.4
batteryVal, err = calculateBatteryVoltage(raw, versionStr("0.1.4"), hvResistorVals)
assert.NoError(t, err)
assert.InDelta(t, float32(41.6720930233), batteryVal, tolerance)

// Low battery voltage check for version 0.4.0
batteryVal, err = calculateBatteryVoltage(raw, versionStr("0.4.0"), lvResistorVals)
assert.NoError(t, err)
assert.InDelta(t, float32(13.1044117647), batteryVal, tolerance)

// High battery voltage check for version 0.1.4
batteryVal, err = calculateBatteryVoltage(raw, versionStr("0.4.0"), hvResistorVals)
assert.NoError(t, err)
assert.InDelta(t, float32(42.9083333333), batteryVal, tolerance)

// Low battery voltage check for version 0.7.0
batteryVal, err = calculateBatteryVoltage(raw, versionStr("0.7.0"), lvResistorVals)
assert.NoError(t, err)
assert.InDelta(t, float32(17.3425531915), batteryVal, tolerance)

// High battery voltage check for version 0.7.0
batteryVal, err = calculateBatteryVoltage(raw, versionStr("0.7.0"), hvResistorVals)
assert.NoError(t, err)
assert.InDelta(t, float32(42.5857142857), batteryVal, tolerance)

}

0 comments on commit 177565c

Please sign in to comment.