From 8a48645b53fb3ab0be2b4d429ba816c54976f459 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Sun, 29 Dec 2024 12:28:00 +1100 Subject: [PATCH] tristar: add gauges and fix negative temperatures --- charge-controller-supervisor/src/gauges.rs | 14 ++++ charge-controller-supervisor/src/tristar.rs | 76 +++++++++++++++++++-- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/charge-controller-supervisor/src/gauges.rs b/charge-controller-supervisor/src/gauges.rs index aa92623..0f04c11 100644 --- a/charge-controller-supervisor/src/gauges.rs +++ b/charge-controller-supervisor/src/gauges.rs @@ -41,6 +41,8 @@ lazy_static! { &[PL_LABEL] ) .unwrap(); + pub static ref HEATSINK_TEMP: GaugeVec = + register_gauge_vec!("heatsink_temp", "Heatsink temperature", &[TRISTAR_LABEL]).unwrap(); pub static ref TRISTAR_INPUT_VOLTAGE: GaugeVec = register_gauge_vec!("tristar_input_voltage", "Input voltage", &[TRISTAR_LABEL]).unwrap(); pub static ref TRISTAR_CHARGE_CURRENT: GaugeVec = @@ -67,4 +69,16 @@ lazy_static! { &[TRISTAR_LABEL] ) .unwrap(); + pub static ref TRISTAR_TOTAL_AH_CHARGE_DAILY: GaugeVec = register_gauge_vec!( + "tristar_total_ah_charge_daily", + "Total charge daily (Ah)", + &[TRISTAR_LABEL] + ) + .unwrap(); + pub static ref TRISTAR_TOTAL_WH_CHARGE_DAILY: GaugeVec = register_gauge_vec!( + "tristar_total_wh_charge_daily", + "Total charge daily (Wh)", + &[TRISTAR_LABEL] + ) + .unwrap(); } diff --git a/charge-controller-supervisor/src/tristar.rs b/charge-controller-supervisor/src/tristar.rs index 039438a..7f76a89 100644 --- a/charge-controller-supervisor/src/tristar.rs +++ b/charge-controller-supervisor/src/tristar.rs @@ -2,9 +2,10 @@ use prometheus::core::{AtomicI64, GenericGauge}; use tokio_modbus::client::Reader; use crate::gauges::{ - BATTERY_TEMP, BATTERY_VOLTAGE, CHARGE_STATE, INPUT_CURRENT, TARGET_VOLTAGE, + BATTERY_TEMP, BATTERY_VOLTAGE, CHARGE_STATE, HEATSINK_TEMP, INPUT_CURRENT, TARGET_VOLTAGE, TRISTAR_CHARGE_CURRENT, TRISTAR_INPUT_VOLTAGE, TRISTAR_MAX_ARRAY_POWER, TRISTAR_MAX_ARRAY_VOLTAGE, TRISTAR_OPEN_CIRCUIT_VOLTAGE, TRISTAR_POWER_IN, TRISTAR_POWER_OUT, + TRISTAR_TOTAL_AH_CHARGE_DAILY, TRISTAR_TOTAL_WH_CHARGE_DAILY, }; const DEVICE_ID: u8 = 0x01; @@ -53,7 +54,8 @@ pub struct TristarState { battery_voltage: f64, target_voltage: f64, input_current: f64, - battery_temp: u16, + battery_temp: i16, + heatsink_temp: i16, charge_state: ChargeState, tristar_input_voltage: f64, tristar_charge_current: f64, @@ -62,6 +64,15 @@ pub struct TristarState { tristar_max_array_power: f64, tristar_max_array_voltage: f64, tristar_open_circuit_voltage: f64, + daily_charge_amp_hours: f64, + daily_charge_watt_hours: f64, +} + +fn signed(val: u16) -> i16 { + match i16::try_from(val) { + Ok(v) => v, + Err(_) => -i16::try_from(val % 128).unwrap(), + } } impl TristarState { @@ -71,7 +82,8 @@ impl TristarState { battery_voltage: scaling.get_voltage(ram[TristarRamAddress::AdcVbFMed]), target_voltage: scaling.get_voltage(ram[TristarRamAddress::VbRef]), input_current: scaling.get_current(ram[TristarRamAddress::AdcIaFShadow]), - battery_temp: ram[TristarRamAddress::Tbatt], + battery_temp: signed(ram[TristarRamAddress::Tbatt]), + heatsink_temp: signed(ram[TristarRamAddress::THs]), charge_state: ChargeState::from(ram[TristarRamAddress::ChargeState]), tristar_input_voltage: scaling.get_voltage(ram[TristarRamAddress::AdcVaF]), tristar_charge_current: scaling.get_current(ram[TristarRamAddress::AdcIbFShadow]), @@ -80,6 +92,8 @@ impl TristarState { tristar_max_array_power: scaling.get_power(ram[TristarRamAddress::SweepPinMax]), tristar_max_array_voltage: scaling.get_voltage(ram[TristarRamAddress::SweepVmp]), tristar_open_circuit_voltage: scaling.get_voltage(ram[TristarRamAddress::SweepVoc]), + daily_charge_amp_hours: f64::from(ram[TristarRamAddress::AhcDaily]) * 0.1, + daily_charge_watt_hours: f64::from(ram[TristarRamAddress::WhcDaily]), } } } @@ -262,6 +276,9 @@ impl Tristar { BATTERY_TEMP .with_label_values(&[&self.friendly_name]) .set(new_state.battery_temp.into()); + HEATSINK_TEMP + .with_label_values(&[&self.friendly_name]) + .set(new_state.heatsink_temp.into()); TRISTAR_INPUT_VOLTAGE .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_input_voltage); @@ -283,6 +300,12 @@ impl Tristar { TRISTAR_OPEN_CIRCUIT_VOLTAGE .with_label_values(&[&self.friendly_name]) .set(new_state.tristar_open_circuit_voltage); + TRISTAR_TOTAL_AH_CHARGE_DAILY + .with_label_values(&[&self.friendly_name]) + .set(new_state.daily_charge_amp_hours); + TRISTAR_TOTAL_WH_CHARGE_DAILY + .with_label_values(&[&self.friendly_name]) + .set(new_state.daily_charge_watt_hours); self.charge_state_gauges.set(new_state.charge_state); @@ -302,19 +325,64 @@ impl Tristar { } } -enum TristarRamAddress { +pub enum TristarRamAddress { AdcVbFMed = 0x0018, + AdcVbtermF = 0x0019, + AdcVbsF = 0x001A, AdcVaF = 0x001B, AdcIbFShadow = 0x001C, AdcIaFShadow = 0x001D, + AdcP12F = 0x001E, + AdcP3F = 0x001F, + AdcPmeterF = 0x0020, + AdcP18F = 0x0021, + AdcVRef = 0x0022, + THs = 0x0023, + TRts = 0x0024, Tbatt = 0x0025, + AdcVbF1m = 0x0026, + AdcIbF1m = 0x0027, + VbMin = 0x0028, + VbMax = 0x0029, + HourmeterHi = 0x002A, + HourmeterLo = 0x002B, + Fault = 0x002C, + AlarmHi = 0x002E, + AlarmLo = 0x002F, + Dip = 0x0030, + Led = 0x0031, ChargeState = 0x0032, VbRef = 0x0033, + AhcRHi = 0x0034, + AhcRLo = 0x0035, + AhcTHi = 0x0036, + AhcTLo = 0x0037, + KwhcR = 0x0038, + KwhcT = 0x0039, PowerOutShadow = 0x003A, PowerInShadow = 0x003B, SweepPinMax = 0x003C, SweepVmp = 0x003D, SweepVoc = 0x003E, + VbMinDaily = 0x0040, + VbMaxDaily = 0x0041, + VaMaxDaily = 0x0042, + AhcDaily = 0x0043, + WhcDaily = 0x0044, + FlagsDaily = 0x0045, + PoutMaxDaily = 0x0046, + TbMinDaily = 0x0047, + TbMaxDaily = 0x0048, + FaultDaily = 0x0049, + AlarmDailyHi = 0x004B, + AlarmDailyLo = 0x004C, + TimeAbDaily = 0x004D, + TimeEqDaily = 0x004E, + TimeFlDaily = 0x004F, + IbRefSlave = 0x0058, + VbRefSlave = 0x0059, + VaRefFixed = 0x005A, + VaRefFixedPc = 0x005B, } impl std::ops::Index for [u16] {