duty cycle + better handling of pli everything
This commit is contained in:
parent
c684ef1e04
commit
86d556ea3f
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2262,7 +2262,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tesla-charge-controller"
|
name = "tesla-charge-controller"
|
||||||
version = "0.1.9-prerelease"
|
version = "0.1.9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-channel",
|
"async-channel",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tesla-charge-controller"
|
name = "tesla-charge-controller"
|
||||||
version = "0.1.9-prerelease"
|
version = "0.1.9"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MITNFA"
|
license = "MITNFA"
|
||||||
description = "Controls Tesla charge rate based on solar charge data"
|
description = "Controls Tesla charge rate based on solar charge data"
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
use metrics::{describe_gauge, gauge, Gauge};
|
use metrics::{describe_gauge, gauge, Gauge};
|
||||||
use serialport::SerialPort;
|
use serialport::SerialPort;
|
||||||
|
|
||||||
pub struct Pli {
|
pub struct Pli {
|
||||||
port: Box<dyn SerialPort>,
|
port: Box<dyn SerialPort>,
|
||||||
voltage_gauge: Gauge,
|
voltage_gauge: Gauge,
|
||||||
|
duty_cycle: Gauge,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pli {
|
impl Pli {
|
||||||
|
@ -16,15 +18,23 @@ impl Pli {
|
||||||
|
|
||||||
describe_gauge!("pl_battery_voltage", "Battery voltage");
|
describe_gauge!("pl_battery_voltage", "Battery voltage");
|
||||||
let voltage_gauge = gauge!("pl_battery_voltage");
|
let voltage_gauge = gauge!("pl_battery_voltage");
|
||||||
|
describe_gauge!("pl_duty_cycle", "Duty cycle");
|
||||||
|
let duty_cycle = gauge!("pl_duty_cycle");
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
port,
|
port,
|
||||||
voltage_gauge,
|
voltage_gauge,
|
||||||
|
duty_cycle,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh(&mut self) {
|
pub fn refresh(&mut self) {
|
||||||
let batv = (self.read_ram(50) as f64) * (4. / 10.);
|
if let Ok(batv) = self.read_ram(50) {
|
||||||
self.voltage_gauge.set(batv);
|
self.voltage_gauge.set(((batv * 4) as f64) / 10.);
|
||||||
|
}
|
||||||
|
if let Ok(duty_cycle) = self.read_ram(39) {
|
||||||
|
self.duty_cycle.set((duty_cycle as f64) / 255.);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_command(&mut self, req: [u8; 4]) {
|
fn send_command(&mut self, req: [u8; 4]) {
|
||||||
|
@ -36,17 +46,17 @@ impl Pli {
|
||||||
fn receive<const LENGTH: usize>(&mut self) -> [u8; LENGTH] {
|
fn receive<const LENGTH: usize>(&mut self) -> [u8; LENGTH] {
|
||||||
let mut buf = [0; LENGTH];
|
let mut buf = [0; LENGTH];
|
||||||
match self.port.read_exact(&mut buf) {
|
match self.port.read_exact(&mut buf) {
|
||||||
Ok(_) => {
|
Ok(_) => {}
|
||||||
println!("got buf {buf:?}")
|
Err(e) => log::error!("PLI serial read error: {e:#?}"),
|
||||||
}
|
|
||||||
Err(e) => println!("read error: {e:#?}"),
|
|
||||||
}
|
}
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_ram(&mut self, address: u8) -> u8 {
|
fn read_ram(&mut self, address: u8) -> anyhow::Result<u8> {
|
||||||
self.send_command(command(20, address, 0));
|
self.send_command(command(20, address, 0));
|
||||||
self.receive::<2>()[1]
|
let buf: [u8; 2] = self.receive();
|
||||||
|
if buf[0] == 200 { Some(buf[1]) } else { None }
|
||||||
|
.context(format!("Error from PLI: {}", buf[0]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue