fudge factor scales
All checks were successful
Build .deb on release / Build-Deb (push) Successful in 1m54s

This commit is contained in:
Alex Janka 2024-01-25 16:03:19 +11:00
parent 95a0a446a1
commit 4ebd386c05
7 changed files with 34 additions and 18 deletions

2
Cargo.lock generated
View file

@ -2633,7 +2633,7 @@ dependencies = [
[[package]]
name = "tesla-charge-controller"
version = "1.0.28"
version = "1.0.29"
dependencies = [
"chrono",
"clap 4.4.11",

View file

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

View file

@ -128,10 +128,12 @@ pub struct Config {
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
#[serde(default)]
pub struct PidControls {
pub proportional_gain: f64,
pub derivative_gain: f64,
pub loop_time_seconds: u64,
pub load_divisor: f64,
}
impl Default for PidControls {
@ -140,6 +142,7 @@ impl Default for PidControls {
proportional_gain: 1.,
derivative_gain: 1.,
loop_time_seconds: 5,
load_divisor: 30.,
}
}
}
@ -217,5 +220,6 @@ impl Config {
self.duty_cycle_too_low = self.duty_cycle_too_low.clamp(0.0, 1.0);
self.pid_controls.proportional_gain = self.pid_controls.proportional_gain.clamp(0.0, 50.0);
self.pid_controls.derivative_gain = self.pid_controls.derivative_gain.clamp(0.0, 50.0);
self.pid_controls.load_divisor = self.pid_controls.load_divisor.clamp(1.0, 50.0);
}
}

View file

@ -67,6 +67,7 @@ fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
set_proportional_gain,
set_derivative_gain,
set_pid_loop_length,
set_load_divisor,
pid_settings,
metrics,
sync_time,
@ -177,6 +178,12 @@ async fn set_pid_loop_length(time: u64, remote_addr: std::net::IpAddr) {
write_to_config().pid_controls.loop_time_seconds = time;
}
#[post("/pid-settings/load_divisor/<divisor>")]
async fn set_load_divisor(divisor: f64, remote_addr: std::net::IpAddr) {
log::warn!("setting load divisor interval: {remote_addr:?}");
write_to_config().pid_controls.load_divisor = divisor;
}
#[get("/pid-settings/status")]
async fn pid_settings() -> Json<crate::config::PidControls> {
Json(access_config().pid_controls)

View file

@ -99,6 +99,7 @@ struct PidLoop {
previous_error: f64,
proportional_gauge: Gauge,
derivative_gauge: Gauge,
load_gauge: Gauge,
change_request_gauge: Gauge,
}
@ -114,12 +115,18 @@ impl Default for PidLoop {
"Derivative component of requested change to charge rate"
);
let derivative_gauge = gauge!("tcrc_derivative");
describe_gauge!(
"tcrc_load",
"Fudge factor from internal load of requested change to charge rate"
);
let load_gauge = gauge!("tcrc_load");
describe_gauge!("tcrc_change_request", "Requested change to charge rate");
let change_request_gauge = gauge!("tcrc_change_request");
Self {
previous_error: 0.,
proportional_gauge,
derivative_gauge,
load_gauge,
change_request_gauge,
}
}
@ -139,16 +146,14 @@ impl PidLoop {
let proportional_component = config.pid_controls.proportional_gain * error;
let derivative_component = config.pid_controls.derivative_gain * derivative;
let extra_offsets = if pl_state.internal_load_current > 1. {
1.
} else {
0.
};
let extra_offsets =
(pl_state.internal_load_current / config.pid_controls.load_divisor).clamp(0., 2.);
let offset = proportional_component + derivative_component + extra_offsets;
self.proportional_gauge.set(proportional_component);
self.derivative_gauge.set(derivative_component);
self.load_gauge.set(extra_offsets);
self.change_request_gauge.set(offset);
let new_target = offset + (charge_state.charge_amps as f64);

View file

@ -20,8 +20,8 @@
<input type="number" id="derivative-gain" step="0.1" autocomplete="off" />
<button id="set-derivative" onclick="set_derivative()">Set derivative gain</button>
<br><br>
<input type="number" id="pid-length" step="1" autocomplete="off" />
<button id="set-pid-length" onclick="set_pid_length()">Set pid length</button>
<input type="number" id="load-divisor" step="1" max="50" min="1" autocomplete="off" />
<button id="set-load-divisor" onclick="set_load_divisor()">Set load current divisor</button>
</p>
<p>
<a class="outlink" href="/">←Home</a>

View file

@ -138,13 +138,13 @@ function set_derivative() {
}
}
function set_pid_length() {
var set_button = document.getElementById("set-pid-length");
var number_input = document.getElementById("pid-length");
function set_load_divisor() {
var set_button = document.getElementById("set-load-divisor");
var number_input = document.getElementById("load-divisor");
if (!isNaN(number_input.value)) {
set_button.disabled = true;
number_input.disabled = true;
fetch(api_url + "/pid-settings/loop_time_seconds/" + number_input.value, { method: "POST" })
fetch(api_url + "/pid-settings/load_divisor/" + number_input.value, { method: "POST" })
.then(async (response) => {
let delayres = await delay(100);
refresh_gains();
@ -235,11 +235,11 @@ function update_gains(data) {
derivative_set_button.disabled = false;
derivative_number_input.disabled = false;
derivative_number_input.value = data.derivative_gain;
var pid_length_button = document.getElementById("set-pid-length");
var pid_length_input = document.getElementById("pid-length");
pid_length_button.disabled = false;
pid_length_input.disabled = false;
pid_length_input.value = data.loop_time_seconds;
var load_divisor_button = document.getElementById("set-load-divisor");
var load_divisor_input = document.getElementById("load-divisor");
load_divisor_button.disabled = false;
load_divisor_input.disabled = false;
load_divisor_input.value = data.load_divisor;
}
function refresh_buttons() {