use std::sync::{Arc, RwLock}; use if_chain::if_chain; use metrics::{describe_gauge, gauge, Gauge}; use serde::{Deserialize, Serialize}; use crate::{ api_interface::InterfaceRequest, charge_controllers::pl::PlState, config::access_config, types::{CarState, ChargeState}, }; pub struct TeslaChargeRateController { pub car_state: Arc>, pub pl_state: Option>>, pub tcrc_state: Arc>, pid: PidLoop, voltage_low: u64, control_enable_gauge: Gauge, } pub enum TcrcRequest { DisableAutomaticControl, EnableAutomaticControl, } #[derive(Clone, Copy, Serialize, Deserialize, Debug)] pub struct TcrcState { pub control_enable: bool, } impl Default for TcrcState { fn default() -> Self { Self { control_enable: true, } } } impl TeslaChargeRateController { pub fn new(car_state: Arc>, pl_state: Option>>) -> Self { describe_gauge!("tcrc_control_enable", "Enable Tesla charge rate control"); Self { car_state, pl_state, tcrc_state: Default::default(), pid: Default::default(), voltage_low: 0, control_enable_gauge: gauge!("tcrc_control_enable"), } } pub fn control_charge_rate(&mut self) -> Option { let delta_time = access_config().pid_controls.loop_time_seconds; if_chain! { if let Some(pl_state) = self.pl_state.as_ref().and_then(|v| v.read().ok()); if let Some(charge_state) = self.car_state.read().ok().and_then(|v| v.charge_state); then { if pl_state.battery_voltage < access_config().shutoff_voltage { self.voltage_low += 1; if (self.voltage_low * delta_time) >= access_config().shutoff_voltage_time_seconds { return Some(InterfaceRequest::StopCharge); } } else { self.voltage_low = 0; if self.tcrc_state.read().is_ok_and(|v| v.control_enable) { return self.pid.step(&pl_state, &charge_state, delta_time as f64).map(InterfaceRequest::SetChargeRate); } } } } None } pub fn process_request(&mut self, message: TcrcRequest) { match message { TcrcRequest::DisableAutomaticControl => { self.tcrc_state .write() .expect("failed to write to tcrc state") .control_enable = false; self.control_enable_gauge.set(0.); } TcrcRequest::EnableAutomaticControl => { self.tcrc_state .write() .expect("failed to write to tcrc state") .control_enable = true; self.control_enable_gauge.set(1.); } } } } #[derive(Default)] struct PidLoop { previous_error: f64, } impl PidLoop { fn step( &mut self, pl_state: &PlState, charge_state: &ChargeState, delta_time: f64, ) -> Option { let error = pl_state.battery_voltage - pl_state.target_voltage; let derivative = (error - self.previous_error) / delta_time; let config = access_config(); let extra_offsets = if pl_state.internal_load_current > 1. { 1. } else { 0. }; let new_target = ((config.pid_controls.proportional_gain * error) + (config.pid_controls.derivative_gain * derivative)) + extra_offsets + (charge_state.charge_amps as f64); self.previous_error = error; let new_target_int = new_target.round() as i64; valid_rate(new_target_int, charge_state.charge_amps) } } fn valid_rate(rate: i64, previous: i64) -> Option { let config = access_config(); let new = rate.clamp(config.min_rate, config.max_rate); if new == previous { None } else { Some(new) } }