Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ const voltageLine = 2

const sampleRate = 5
const shutdownSys = true
const batteryKill = 14.9
const batteryWarn = 15.4

// By default, set for 4S batteries, however if a 3S battery is plugged in
// it will dynamically switch to matching thresholds
var batteryKill = 14.9
var batteryWarn = 15.4

var voltageValue float64 = 0

Expand All @@ -34,6 +37,28 @@ func exit_safely(bat *battery.ADS1015) {
}
}

// Sets global variables `batteryKill` and `batteryWarn` depending on what acceptable
// voltage ranges it receives.
// Used voltage chart: https://p.kagi.com/proxy/percent-of-charge-vs-lipoly-pack-voltage_orig.jpg?c=04PBQu2n3SHkJegYIKajlTBUH3xZPAo90_4CORJAQgoeLkwKteL7XnRjeKYjOUffqA-UZkfogB11gN74IRpwkzp1KzzYYuTyH5T0BI222uYS6VgplYu_IkTGjrIXotnWrZYJcIBcY0B9eF4S9liNPAj5FO1nQKjUSAkPuAJP2Gc%3D

func set_battery_thresholds(voltage float64) {
if voltage >= 14.7 && voltage <= 16.9 {
// 4S threshold
batteryKill = 14.90
batteryWarn = 15.40
log.Info().Msg("Detected a 4S battery")
} else if voltage >= 11.0 && voltage <= 12.80 {
// 3S threshold
batteryKill = 11.25
batteryWarn = 11.40
log.Info().Msg("Detected a 3S battery")
} else {
batteryKill = 14.90
batteryWarn = 15.40
log.Info().Msg("Did not detect 3S or 4S battery, defaulting to 4S thresholds")
}
}

// Battery data
func critical_loop() error {
// Initialize the battery sensor
Expand All @@ -48,6 +73,14 @@ func critical_loop() error {
return err
}

log.Info().Msg("Checking initial voltage for dynamic threshold adaptation")
v, err := bat.GetVoltage(voltageLine)
if err != nil {
log.Err(err).Msg("Failed to get battery voltage")
return err
}
set_battery_thresholds(v)

warningDisplayed := false
for {

Expand Down
Loading