source-pr / source (pull_request) Successful in 12s
Ambient mode holds the hottest sensor a configured margin above the measured intake temperature, which is the efficiency-at-this-ambient operating point; missing either sensor is an error rather than a guess. Max-fan mode pins the fan at its cap, ramps frequency toward the envelope ceiling, and still defers to the thermal guard. step() now returns a Command carrying the frequency and, when the target owns the fan, the duty to apply. The max-fan ramp is explicit rather than a setpoint loop: a P controller fed its own output settles at half the ceiling, which the ramp test caught.
507 lines
16 KiB
Rust
507 lines
16 KiB
Rust
// SPDX-License-Identifier: MIT
|
|
//! Adaptive tuning.
|
|
//!
|
|
//! The miner targets a power draw, a hashrate, or an operating point chosen for the ambient
|
|
//! temperature. Rather than picking a preset, a bounded PID moves the chain frequency inside the
|
|
//! profile envelope each control period, and a thermal guard can always override it downward.
|
|
//!
|
|
//! The controller is deliberately conservative: bounded output, rate limit, deadband, derivative
|
|
//! on the measurement (no setpoint kick) and conditional integration (no windup while saturated).
|
|
|
|
use crate::detect::Envelope;
|
|
|
|
/// What the operator asked for.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub enum TuningTarget {
|
|
/// Hold this DC power draw; efficiency follows from the power point.
|
|
PowerW(f32),
|
|
/// Maximise hashrate up to this value.
|
|
HashrateThs(f32),
|
|
/// Hold the hottest sensor this far above the measured ambient: efficiency at that ambient.
|
|
AmbientC { margin_mc: i32 },
|
|
/// Pin the fan at this duty and maximise hashrate under it.
|
|
MaxFan { max_pwm: u8 },
|
|
}
|
|
|
|
/// Controller tuning.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct PidConfig {
|
|
pub kp: f32,
|
|
pub ki: f32,
|
|
pub kd: f32,
|
|
pub out_min: f32,
|
|
pub out_max: f32,
|
|
/// Maximum output change per second.
|
|
pub max_step_per_s: f32,
|
|
/// Errors inside this band are ignored.
|
|
pub deadband: f32,
|
|
}
|
|
|
|
impl Default for PidConfig {
|
|
fn default() -> Self {
|
|
Self {
|
|
kp: 0.5,
|
|
ki: 0.1,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 1_000.0,
|
|
max_step_per_s: 50.0,
|
|
deadband: 1.0,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A bounded PID with anti-windup and rate limiting.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Pid {
|
|
config: PidConfig,
|
|
integral: f32,
|
|
last_measurement: Option<f32>,
|
|
output: f32,
|
|
}
|
|
|
|
impl Pid {
|
|
pub fn new(config: PidConfig, initial_output: f32) -> Self {
|
|
Self {
|
|
config,
|
|
integral: 0.0,
|
|
last_measurement: None,
|
|
output: initial_output.clamp(config.out_min, config.out_max),
|
|
}
|
|
}
|
|
|
|
pub fn output(&self) -> f32 {
|
|
self.output
|
|
}
|
|
|
|
/// Force the output (used by ramp modes and the thermal guard).
|
|
pub fn set_output(&mut self, value: f32) {
|
|
self.output = value.clamp(self.config.out_min, self.config.out_max);
|
|
}
|
|
|
|
/// Advance one control period and return the new output.
|
|
pub fn update(&mut self, setpoint: f32, measurement: f32, dt_s: f32) -> f32 {
|
|
if !dt_s.is_finite() || dt_s <= 0.0 {
|
|
return self.output;
|
|
}
|
|
let error = setpoint - measurement;
|
|
if error.abs() <= self.config.deadband {
|
|
self.last_measurement = Some(measurement);
|
|
return self.output;
|
|
}
|
|
let derivative = match self.last_measurement {
|
|
Some(previous) => -(measurement - previous) / dt_s,
|
|
None => 0.0,
|
|
};
|
|
let candidate = self.config.kp * error + self.integral + self.config.kd * derivative;
|
|
let saturated = candidate > self.config.out_max || candidate < self.config.out_min;
|
|
// Conditional integration: never accumulate while the actuator is pinned in the direction
|
|
// of the error, or the loop unwinds slowly after a saturation episode.
|
|
let pinning = (candidate > self.config.out_max && error > 0.0)
|
|
|| (candidate < self.config.out_min && error < 0.0);
|
|
if !saturated || !pinning {
|
|
self.integral += self.config.ki * error * dt_s;
|
|
}
|
|
let mut desired = self.config.kp * error + self.integral + self.config.kd * derivative;
|
|
desired = desired.clamp(self.config.out_min, self.config.out_max);
|
|
let max_step = self.config.max_step_per_s * dt_s;
|
|
let step = (desired - self.output).clamp(-max_step, max_step);
|
|
self.output = (self.output + step).clamp(self.config.out_min, self.config.out_max);
|
|
self.last_measurement = Some(measurement);
|
|
self.output
|
|
}
|
|
}
|
|
|
|
/// What the loop needs to know each period.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct Observation {
|
|
pub power_w: Option<f32>,
|
|
pub hashrate_ths: Option<f32>,
|
|
/// Hottest sensor in millidegrees, when telemetry is available.
|
|
pub hottest_mc: Option<i32>,
|
|
/// Intake/enclosure temperature in millidegrees, for the ambient target.
|
|
pub ambient_mc: Option<i32>,
|
|
}
|
|
|
|
/// What the loop wants applied this period.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub struct Command {
|
|
pub frequency_mhz: u16,
|
|
/// `Some` only when the target owns the fan (max-fan mode).
|
|
pub fan_pwm: Option<u8>,
|
|
}
|
|
|
|
/// Why the loop could not produce a command.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum TuningError {
|
|
/// The measurement the target needs is missing this period.
|
|
NoMeasurement,
|
|
}
|
|
|
|
/// The tuning loop: one PID over chain frequency, bounded by the profile envelope.
|
|
#[derive(Clone, Debug)]
|
|
pub struct TuningLoop {
|
|
pid: Pid,
|
|
envelope: Envelope,
|
|
target: TuningTarget,
|
|
ceiling_mc: i32,
|
|
}
|
|
|
|
impl TuningLoop {
|
|
/// Start from the conservative floor of the envelope.
|
|
pub fn new(target: TuningTarget, envelope: Envelope, pid: PidConfig, ceiling_mc: i32) -> Self {
|
|
let config = PidConfig {
|
|
out_min: f32::from(envelope.f_min_mhz),
|
|
out_max: f32::from(envelope.f_max_mhz),
|
|
..pid
|
|
};
|
|
let start = f32::from(envelope.f_min_mhz);
|
|
Self {
|
|
pid: Pid::new(config, start),
|
|
envelope,
|
|
target,
|
|
ceiling_mc,
|
|
}
|
|
}
|
|
|
|
pub fn frequency_mhz(&self) -> u16 {
|
|
self.pid.output().round() as u16
|
|
}
|
|
|
|
/// Fan duty implied by the target, if the target owns the fan.
|
|
fn target_fan(&self) -> Option<u8> {
|
|
match self.target {
|
|
TuningTarget::MaxFan { max_pwm } => Some(max_pwm),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn command(&self) -> Command {
|
|
Command {
|
|
frequency_mhz: self.frequency_mhz(),
|
|
fan_pwm: self.target_fan(),
|
|
}
|
|
}
|
|
|
|
/// One control period. Returns the command to apply, inside the envelope.
|
|
pub fn step(&mut self, observation: Observation, dt_s: f32) -> Result<Command, TuningError> {
|
|
// Thermal guard: a hot sensor always derates, whatever the target says.
|
|
if let Some(hottest) = observation.hottest_mc {
|
|
if hottest >= self.ceiling_mc {
|
|
let derated = (self.pid.output() - 25.0).max(f32::from(self.envelope.f_min_mhz));
|
|
self.pid = Pid::new(
|
|
PidConfig {
|
|
out_min: f32::from(self.envelope.f_min_mhz),
|
|
out_max: f32::from(self.envelope.f_max_mhz),
|
|
..self.pid.config
|
|
},
|
|
derated,
|
|
);
|
|
return Ok(self.command());
|
|
}
|
|
}
|
|
match self.target {
|
|
TuningTarget::PowerW(setpoint) => {
|
|
let measurement = observation.power_w.ok_or(TuningError::NoMeasurement)?;
|
|
self.pid.update(setpoint, measurement, dt_s);
|
|
}
|
|
TuningTarget::HashrateThs(setpoint) => {
|
|
let measurement = observation.hashrate_ths.ok_or(TuningError::NoMeasurement)?;
|
|
self.pid.update(setpoint, measurement, dt_s);
|
|
}
|
|
TuningTarget::AmbientC { margin_mc } => {
|
|
let ambient = observation.ambient_mc.ok_or(TuningError::NoMeasurement)?;
|
|
let hottest = observation.hottest_mc.ok_or(TuningError::NoMeasurement)?;
|
|
let setpoint = ambient as f32 + margin_mc as f32;
|
|
self.pid.update(setpoint, hottest as f32, dt_s);
|
|
}
|
|
TuningTarget::MaxFan { .. } => {
|
|
// No setpoint loop here: ramp toward the envelope ceiling and let the thermal
|
|
// guard be the only thing that stops it.
|
|
let step = self.pid.config.max_step_per_s * dt_s;
|
|
let next = (self.pid.output() + step).min(f32::from(self.envelope.f_max_mhz));
|
|
self.pid.set_output(next);
|
|
}
|
|
}
|
|
Ok(self.command())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn envelope() -> Envelope {
|
|
Envelope {
|
|
v_min_mv: 1100,
|
|
v_max_mv: 1500,
|
|
f_min_mhz: 300,
|
|
f_max_mhz: 700,
|
|
}
|
|
}
|
|
|
|
fn observation(hottest: i32) -> Observation {
|
|
Observation {
|
|
power_w: None,
|
|
hashrate_ths: None,
|
|
hottest_mc: Some(hottest),
|
|
ambient_mc: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn pid_drives_the_plant_to_the_setpoint() {
|
|
let mut pid = Pid::new(
|
|
PidConfig {
|
|
kp: 0.02,
|
|
ki: 0.01,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 700.0,
|
|
max_step_per_s: 1_000.0,
|
|
deadband: 0.0,
|
|
},
|
|
0.0,
|
|
);
|
|
// Plant: 5 W per MHz, so 3 kW needs 600 MHz, inside the controller ceiling.
|
|
let mut power = 0.0_f32;
|
|
for _ in 0..400 {
|
|
let freq = pid.update(3_000.0, power, 1.0);
|
|
power = 5.0 * freq;
|
|
}
|
|
assert!((power - 3_000.0).abs() < 50.0, "power settled at {power}");
|
|
}
|
|
|
|
#[test]
|
|
fn output_is_rate_limited() {
|
|
let mut pid = Pid::new(
|
|
PidConfig {
|
|
kp: 1.0,
|
|
ki: 0.0,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 1_000.0,
|
|
max_step_per_s: 10.0,
|
|
deadband: 0.0,
|
|
},
|
|
0.0,
|
|
);
|
|
assert!((pid.update(1_000.0, 0.0, 1.0) - 10.0).abs() < f32::EPSILON);
|
|
assert!((pid.update(1_000.0, 0.0, 1.0) - 20.0).abs() < f32::EPSILON);
|
|
}
|
|
|
|
#[test]
|
|
fn deadband_holds_the_output() {
|
|
let mut pid = Pid::new(
|
|
PidConfig {
|
|
kp: 1.0,
|
|
ki: 0.1,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 1_000.0,
|
|
max_step_per_s: 100.0,
|
|
deadband: 5.0,
|
|
},
|
|
100.0,
|
|
);
|
|
let before = pid.output();
|
|
assert!((pid.update(102.0, 100.0, 1.0) - before).abs() < f32::EPSILON);
|
|
}
|
|
|
|
#[test]
|
|
fn saturation_does_not_wind_up_the_integral() {
|
|
let mut pid = Pid::new(
|
|
PidConfig {
|
|
kp: 1.0,
|
|
ki: 1.0,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 100.0,
|
|
max_step_per_s: 1_000.0,
|
|
deadband: 0.0,
|
|
},
|
|
0.0,
|
|
);
|
|
for _ in 0..100 {
|
|
pid.update(1_000.0, 0.0, 1.0);
|
|
}
|
|
assert!((pid.output() - 100.0).abs() < 0.01);
|
|
for _ in 0..5 {
|
|
pid.update(0.0, 200.0, 1.0);
|
|
}
|
|
assert!(
|
|
pid.output() < 99.0,
|
|
"output stuck at the rail: {}",
|
|
pid.output()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn power_target_moves_frequency_inside_the_envelope() {
|
|
let mut loop_ = TuningLoop::new(
|
|
TuningTarget::PowerW(2_400.0),
|
|
envelope(),
|
|
PidConfig {
|
|
kp: 0.2,
|
|
ki: 0.05,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 0.0,
|
|
max_step_per_s: 50.0,
|
|
deadband: 0.0,
|
|
},
|
|
95_000,
|
|
);
|
|
let mut freq = loop_.frequency_mhz();
|
|
for _ in 0..100 {
|
|
let power = 4.0 * f32::from(freq);
|
|
let mut obs = observation(70_000);
|
|
obs.power_w = Some(power);
|
|
freq = loop_.step(obs, 1.0).expect("step").frequency_mhz;
|
|
assert!((300..=700).contains(&freq), "out of envelope: {freq}");
|
|
}
|
|
let power = 4.0 * f32::from(freq);
|
|
assert!((power - 2_400.0).abs() < 200.0, "power settled at {power}");
|
|
}
|
|
|
|
#[test]
|
|
fn thermal_guard_derates_regardless_of_target() {
|
|
let mut loop_ = TuningLoop::new(
|
|
TuningTarget::HashrateThs(200.0),
|
|
envelope(),
|
|
PidConfig::default(),
|
|
90_000,
|
|
);
|
|
for _ in 0..50 {
|
|
let mut obs = observation(60_000);
|
|
obs.hashrate_ths = Some(10.0);
|
|
loop_.step(obs, 1.0).expect("step");
|
|
}
|
|
let hot = loop_.frequency_mhz();
|
|
let mut obs = observation(91_000);
|
|
obs.hashrate_ths = Some(10.0);
|
|
let derated = loop_.step(obs, 1.0).expect("step").frequency_mhz;
|
|
assert!(
|
|
derated < hot,
|
|
"thermal guard did not derate: {derated} vs {hot}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn missing_measurement_is_an_error() {
|
|
let mut loop_ = TuningLoop::new(
|
|
TuningTarget::PowerW(3_000.0),
|
|
envelope(),
|
|
PidConfig::default(),
|
|
95_000,
|
|
);
|
|
let mut obs = observation(50_000);
|
|
obs.hashrate_ths = Some(1.0);
|
|
assert_eq!(loop_.step(obs, 1.0), Err(TuningError::NoMeasurement));
|
|
}
|
|
|
|
#[test]
|
|
fn zero_dt_holds_the_output() {
|
|
let mut pid = Pid::new(PidConfig::default(), 42.0);
|
|
assert!((pid.update(100.0, 0.0, 0.0) - 42.0).abs() < f32::EPSILON);
|
|
}
|
|
|
|
#[test]
|
|
fn ambient_target_holds_the_hottest_sensor_near_ambient_plus_margin() {
|
|
let mut loop_ = TuningLoop::new(
|
|
TuningTarget::AmbientC { margin_mc: 20_000 },
|
|
envelope(),
|
|
PidConfig {
|
|
kp: 0.5,
|
|
ki: 0.05,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 0.0,
|
|
max_step_per_s: 50.0,
|
|
deadband: 0.0,
|
|
},
|
|
200_000,
|
|
);
|
|
// Plant: hottest = ambient + 50 mC per MHz.
|
|
let ambient = 40_000;
|
|
let mut freq = loop_.frequency_mhz();
|
|
for _ in 0..300 {
|
|
let hottest = ambient + 50 * i32::from(freq);
|
|
let obs = Observation {
|
|
power_w: None,
|
|
hashrate_ths: None,
|
|
hottest_mc: Some(hottest),
|
|
ambient_mc: Some(ambient),
|
|
};
|
|
freq = loop_.step(obs, 1.0).expect("step").frequency_mhz;
|
|
}
|
|
let hottest = ambient + 50 * i32::from(freq);
|
|
assert!(
|
|
(hottest - (ambient + 20_000)).abs() < 2_000,
|
|
"hottest settled at {hottest}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn ambient_target_needs_an_ambient_sensor() {
|
|
let mut loop_ = TuningLoop::new(
|
|
TuningTarget::AmbientC { margin_mc: 20_000 },
|
|
envelope(),
|
|
PidConfig::default(),
|
|
95_000,
|
|
);
|
|
assert_eq!(
|
|
loop_.step(observation(50_000), 1.0),
|
|
Err(TuningError::NoMeasurement)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn max_fan_mode_pins_the_fan_and_ramps_frequency() {
|
|
let ramp = PidConfig {
|
|
kp: 1.0,
|
|
ki: 0.0,
|
|
kd: 0.0,
|
|
out_min: 0.0,
|
|
out_max: 0.0,
|
|
max_step_per_s: 50.0,
|
|
deadband: 0.0,
|
|
};
|
|
let mut loop_ = TuningLoop::new(
|
|
TuningTarget::MaxFan { max_pwm: 100 },
|
|
envelope(),
|
|
ramp,
|
|
120_000,
|
|
);
|
|
let start = loop_.frequency_mhz();
|
|
let mut last = start;
|
|
for _ in 0..20 {
|
|
let command = loop_.step(observation(70_000), 1.0).expect("step");
|
|
assert_eq!(command.fan_pwm, Some(100));
|
|
last = command.frequency_mhz;
|
|
}
|
|
assert!(last > start, "frequency did not ramp: {last} vs {start}");
|
|
assert!(
|
|
last == 700,
|
|
"ramp did not reach the envelope ceiling: {last}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn max_fan_mode_still_respects_the_temperature_ceiling() {
|
|
let mut loop_ = TuningLoop::new(
|
|
TuningTarget::MaxFan { max_pwm: 100 },
|
|
envelope(),
|
|
PidConfig::default(),
|
|
80_000,
|
|
);
|
|
for _ in 0..50 {
|
|
loop_.step(observation(70_000), 1.0).expect("step");
|
|
}
|
|
let hot = loop_.frequency_mhz();
|
|
let command = loop_.step(observation(85_000), 1.0).expect("step");
|
|
assert!(command.frequency_mhz < hot);
|
|
assert_eq!(command.fan_pwm, Some(100));
|
|
}
|
|
}
|