From 79f492e5e81510472a8786d32da8493ad0efc4d2 Mon Sep 17 00:00:00 2001 From: Noah <2+noah@noreply.gitea.bluefla.re> Date: Thu, 22 Oct 2026 11:02:02 -0600 Subject: [PATCH] Make the reference server thermal model fan-aware Zones now respond to the fan that actually cools them (round-robin by zone index), so the active fan sweep can be demonstrated end to end without hardware: with two fans and three zones the learned map is fan 0 -> zones 0,2 and fan 1 -> zone 1, persisted to the map file. The settle interval is configurable (ALKANE_FAN_SETTLE_MS) because the real twenty-second settle makes an interactive sweep last minutes. --- src/hal_sim.rs | 39 +++++++++++++++++++++++++++++++++------ src/main.rs | 5 ++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/hal_sim.rs b/src/hal_sim.rs index f7aa9cd..266b1a6 100644 --- a/src/hal_sim.rs +++ b/src/hal_sim.rs @@ -42,6 +42,8 @@ pub struct SimConfig { pub eeprom: bool, /// PSU name and envelope, or `None` to fail identification. pub psu: Option<(String, u16, u16, u16, u16, u16)>, + /// Fans on the simulated chassis; zones are distributed over them. + pub fans: u8, } impl Default for SimConfig { @@ -52,6 +54,7 @@ impl Default for SimConfig { chip: 1, eeprom: false, psu: Some(("APW12".into(), 1200, 1600, 0, 1000, 3600)), + fans: 2, } } } @@ -59,7 +62,16 @@ impl Default for SimConfig { struct State { config: SimConfig, frequency_mhz: u16, - fan_pwm: u8, + /// Duty per fan, so a zone can respond to the fan that actually cools it. + fan_pwm: [u8; 4], +} + +impl State { + /// Which fan cools a zone. Zones are distributed over the fans round-robin. + fn owner(&self, zone: u8) -> usize { + let fans = usize::from(self.config.fans.max(1)); + usize::from(zone) % fans + } } /// Serve one socket path until the program exits. @@ -71,7 +83,7 @@ pub fn serve(path: &Path, config: SimConfig) -> io::Result<()> { let mut state = State { config: config.clone(), frequency_mhz: 0, - fan_pwm: 0, + fan_pwm: [100; 4], }; let _ = serve_connection(stream, &mut state); } @@ -150,14 +162,22 @@ fn respond(opcode: u8, payload: &[u8], state: &mut State) -> Vec { vec![0] } SET_FAN_TARGET => { - state.fan_pwm = payload.get(1).copied().unwrap_or(0); + let fan = usize::from(payload.first().copied().unwrap_or(0)).min(3); + state.fan_pwm[fan] = payload.get(1).copied().unwrap_or(0); vec![0] } READ_TEMPERATURE => { - // Temperature rises with frequency so the tuning loop has something to regulate. + // Temperature rises with frequency so the tuner has something to regulate, and falls + // when the fan that owns the zone is fast so the fan sweep can learn the mapping. + let zone = payload.first().copied().unwrap_or(0); let rise = i32::from(state.frequency_mhz) * 20; + let airflow = if state.fan_pwm[state.owner(zone)] >= 100 { + 0 + } else { + 20_000 + }; let mut body = vec![0_u8]; - body.extend_from_slice(&(45_000 + rise).to_le_bytes()); + body.extend_from_slice(&(45_000 + rise + airflow).to_le_bytes()); body } READ_POWER => { @@ -183,6 +203,7 @@ fn respond(opcode: u8, payload: &[u8], state: &mut State) -> Vec { #[cfg(test)] mod tests { use super::*; + use crate::fanmap::FanProbe; use crate::hal_wire::HalClient; use crate::HardwareBackend; @@ -196,7 +217,7 @@ mod tests { let mut state = State { config: SimConfig::default(), frequency_mhz: 0, - fan_pwm: 0, + fan_pwm: [100; 4], }; let _ = serve_connection(stream, &mut state); }); @@ -217,6 +238,12 @@ mod tests { assert_eq!(psu.name, "APW12"); client.set_chain_frequency(0, 512).expect("freq"); assert_eq!(client.read_power_w().expect("power"), 512 * 4); + // Zone 1 is owned by fan 1: slowing fan 1 must heat zone 1 and not zone 0. + let baseline_0 = client.read_temperature_mc(0).expect("temp"); + let baseline_1 = client.read_temperature_mc(1).expect("temp"); + client.set_fan(1, 40).expect("fan"); + assert_eq!(client.read_temperature_mc(0).expect("temp"), baseline_0); + assert!(client.read_temperature_mc(1).expect("temp") > baseline_1); assert_eq!( client.read_temperature_mc(0).expect("temp"), 45_000 + 512 * 20 diff --git a/src/main.rs b/src/main.rs index 735764b..71fb115 100644 --- a/src/main.rs +++ b/src/main.rs @@ -249,7 +249,10 @@ fn boot_hal(mut args: impl Iterator) -> Result<(), Box low_pwm: 40, high_pwm: 100, repeats: 1, - settle_ms: 20_000, + settle_ms: std::env::var("ALKANE_FAN_SETTLE_MS") + .ok() + .and_then(|v| v.parse().ok()) + .unwrap_or(20_000), }, thresholds: Default::default(), map_path: &map_path,