Log target voltage (Vreg)

This commit is contained in:
Alex Janka 2024-01-11 13:13:30 +11:00
parent dceefffd45
commit c14454d53c
3 changed files with 12 additions and 2 deletions

2
Cargo.lock generated
View file

@ -2262,7 +2262,7 @@ dependencies = [
[[package]]
name = "tesla-charge-controller"
version = "0.1.15"
version = "0.1.16"
dependencies = [
"anyhow",
"async-channel",

View file

@ -1,6 +1,6 @@
[package]
name = "tesla-charge-controller"
version = "0.1.15"
version = "0.1.16"
edition = "2021"
license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data"

View file

@ -14,6 +14,7 @@ pub struct Pli {
pub state: Arc<RwLock<PlState>>,
port: Box<dyn SerialPort>,
voltage_gauge: Gauge,
target_voltage_gauge: Gauge,
duty_cycle: Gauge,
internal_charge_current: Gauge,
internal_load_current: Gauge,
@ -23,6 +24,7 @@ pub struct Pli {
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct PlState {
pub battery_voltage: f64,
pub target_voltage: f64,
pub duty_cycle: f64,
pub internal_charge_current: f64,
pub internal_load_current: f64,
@ -33,6 +35,7 @@ impl Default for PlState {
fn default() -> Self {
Self {
battery_voltage: Default::default(),
target_voltage: Default::default(),
duty_cycle: Default::default(),
internal_charge_current: Default::default(),
internal_load_current: Default::default(),
@ -126,6 +129,8 @@ impl Pli {
describe_gauge!("pl_battery_voltage", "Battery voltage");
let voltage_gauge = gauge!("pl_battery_voltage");
describe_gauge!("pl_target_voltage", "Target voltage");
let target_voltage_gauge = gauge!("pl_target_voltage");
describe_gauge!("pl_duty_cycle", "Duty cycle");
let duty_cycle = gauge!("pl_duty_cycle");
describe_gauge!("pl_internal_charge_current", "Internal charge current");
@ -137,6 +142,7 @@ impl Pli {
state: Arc::new(RwLock::new(Default::default())),
port,
voltage_gauge,
target_voltage_gauge,
duty_cycle,
internal_charge_current,
internal_load_current,
@ -147,6 +153,7 @@ impl Pli {
pub fn refresh(&mut self) {
if let Ok(new_state) = self.read_state() {
self.voltage_gauge.set(new_state.battery_voltage);
self.target_voltage_gauge.set(new_state.target_voltage);
self.duty_cycle.set(new_state.duty_cycle);
self.internal_charge_current
.set(new_state.internal_charge_current);
@ -181,6 +188,7 @@ impl Pli {
fn read_state(&mut self) -> anyhow::Result<PlState> {
Ok(PlState {
battery_voltage: (self.read_ram(PlRamAddress::Batv)? as f64) * (4. / 10.),
target_voltage: (self.read_ram(PlRamAddress::Vreg)? as f64) * (4. / 10.),
duty_cycle: (self.read_ram(PlRamAddress::Dutycyc)? as f64) / 255.,
internal_charge_current: (self.read_ram(PlRamAddress::Cint)? as f64) * (4. / 10.),
internal_load_current: (self.read_ram(PlRamAddress::Lint)? as f64) * (4. / 10.),
@ -218,6 +226,7 @@ enum PlRamAddress {
Dutycyc,
Batv,
Rstate,
Vreg,
Cint,
Lint,
}
@ -228,6 +237,7 @@ impl From<PlRamAddress> for u8 {
PlRamAddress::Dutycyc => 39,
PlRamAddress::Batv => 50,
PlRamAddress::Rstate => 101,
PlRamAddress::Vreg => 105,
PlRamAddress::Cint => 213,
PlRamAddress::Lint => 217,
}