Skip to content

Commit 6f89dea

Browse files
committed
idsp 0.14
1 parent e8ed72b commit 6f89dea

File tree

5 files changed

+35
-41
lines changed

5 files changed

+35
-41
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ num-traits = { version = "0.2", default-features = false, features = ["libm"] }
3939
byteorder = { version = "1", default-features = false }
4040
smlang = "0.6.0"
4141
enum-iterator = "1.5.0"
42-
idsp = "0.9.2"
42+
idsp = "0.14.1"
4343

4444
# Note: Keep in-sync with `py/setup.py`
4545
miniconf = "0.9"

py/thermostat/configure_output_ch.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ async def configure():
168168
f"/output_channel/{args.channel}/iir",
169169
{
170170
"ba": coefficients,
171-
"y_offset": args.y_offset + forward_gain * args.x_offset,
172-
"y_min": args.y_min,
173-
"y_max": args.y_max,
171+
"u": args.y_offset + forward_gain * args.x_offset,
172+
"min": args.y_min,
173+
"max": args.y_max,
174174
},
175175
)
176176
for i, weight in enumerate(args.input_weights):

src/main.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use hardware::{
2727
system_timer::SystemTimer,
2828
OutputChannelIdx,
2929
};
30-
use idsp::iir;
3130
use miniconf::Tree;
3231
use net::{Alarm, NetworkState, NetworkUsers};
3332
use serde::Serialize;
@@ -134,7 +133,7 @@ mod app {
134133
dac: Dac,
135134
pwm: Pwm,
136135
adc_internal: AdcInternal,
137-
iir_state: [iir::Vec5<f64>; 4],
136+
iir_state: [[f64; 4]; 4],
138137
}
139138

140139
#[init]
@@ -231,14 +230,16 @@ mod app {
231230
fn settings_update(mut c: settings_update::Context, mut settings: Settings) {
232231
// Limit y_min and y_max values here. Will be incorporated into miniconf response later.
233232
for ch in settings.output_channel.iter_mut() {
234-
ch.iir.y_max = ch
235-
.iir
236-
.y_max
237-
.clamp(-DacCode::MAX_CURRENT as _, DacCode::MAX_CURRENT as _);
238-
ch.iir.y_min = ch
239-
.iir
240-
.y_min
241-
.clamp(-DacCode::MAX_CURRENT as _, DacCode::MAX_CURRENT as _);
233+
ch.iir.set_max(
234+
ch.iir
235+
.max()
236+
.clamp(-DacCode::MAX_CURRENT as _, DacCode::MAX_CURRENT as _),
237+
);
238+
ch.iir.set_min(
239+
ch.iir
240+
.min()
241+
.clamp(-DacCode::MAX_CURRENT as _, DacCode::MAX_CURRENT as _),
242+
);
242243
}
243244

244245
let pwm = c.local.pwm;
@@ -345,7 +346,7 @@ mod app {
345346
fn process_output_channel(mut c: process_output_channel::Context, output_ch: OutputChannelIdx) {
346347
let idx = output_ch as usize;
347348
let current = (c.shared.settings, c.shared.temperature).lock(|settings, temperature| {
348-
settings.output_channel[idx].update(temperature, &mut c.local.iir_state[idx], false)
349+
settings.output_channel[idx].update(temperature, &mut c.local.iir_state[idx])
349350
});
350351
c.shared
351352
.telemetry

src/output_channel.rs

+17-24
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct OutputChannel {
3232
///
3333
/// # Value
3434
/// See [iir::IIR#tree]
35-
pub iir: iir::IIR<f64>,
35+
pub iir: iir::Biquad<f64>,
3636

3737
/// Thermostat input channel weights. Each input temperature of an enabled channel
3838
/// is multiplied by its weight and the accumulated output is fed into the IIR.
@@ -55,27 +55,18 @@ impl Default for OutputChannel {
5555
shutdown: true,
5656
hold: false,
5757
voltage_limit: 0.0,
58-
iir: iir::IIR::default(),
58+
iir: iir::Biquad::default(),
5959
weights: [[None; 4]; 4],
6060
}
6161
}
6262
}
6363

64-
// Global "hold" IIR to apply to a channel iir state [x0,x1,x2,y0,y1] when the output should hold.
65-
const IIR_HOLD: iir::IIR<f64> = iir::IIR {
66-
ba: [0., 0., 0., 1., 0.],
67-
y_offset: 0.,
68-
y_min: f64::MIN,
69-
y_max: f64::MAX,
70-
};
71-
7264
impl OutputChannel {
7365
/// compute weighted iir input, iir state and return the new output
7466
pub fn update(
7567
&mut self,
7668
channel_temperatures: &[[f64; 4]; 4],
77-
iir_state: &mut iir::Vec5<f64>,
78-
hold: bool,
69+
iir_state: &mut [f64; 4],
7970
) -> f32 {
8071
let weighted_temperature = channel_temperatures
8172
.iter()
@@ -85,9 +76,9 @@ impl OutputChannel {
8576
.map(|(t, w)| t * w.unwrap_or(0.) as f64)
8677
.sum();
8778
if self.shutdown || self.hold {
88-
IIR_HOLD.update(iir_state, weighted_temperature, hold) as f32
79+
iir::Biquad::HOLD.update(iir_state, weighted_temperature) as f32
8980
} else {
90-
self.iir.update(iir_state, weighted_temperature, hold) as f32
81+
self.iir.update(iir_state, weighted_temperature) as f32
9182
}
9283
}
9384

@@ -96,14 +87,16 @@ impl OutputChannel {
9687
/// - Normalization of the weights
9788
/// Returns the current limits.
9889
pub fn finalize_settings(&mut self) -> [f32; 2] {
99-
self.iir.y_max = self
100-
.iir
101-
.y_max
102-
.clamp(-Pwm::MAX_CURRENT_LIMIT, Pwm::MAX_CURRENT_LIMIT);
103-
self.iir.y_min = self
104-
.iir
105-
.y_min
106-
.clamp(-Pwm::MAX_CURRENT_LIMIT, Pwm::MAX_CURRENT_LIMIT);
90+
self.iir.set_max(
91+
self.iir
92+
.max()
93+
.clamp(-Pwm::MAX_CURRENT_LIMIT, Pwm::MAX_CURRENT_LIMIT),
94+
);
95+
self.iir.set_min(
96+
self.iir
97+
.min()
98+
.clamp(-Pwm::MAX_CURRENT_LIMIT, Pwm::MAX_CURRENT_LIMIT),
99+
);
107100
self.voltage_limit = self.voltage_limit.clamp(0.0, Pwm::MAX_VOLTAGE_LIMIT);
108101
let divisor: f32 = self
109102
.weights
@@ -121,8 +114,8 @@ impl OutputChannel {
121114
[
122115
// [Pwm::MAX_CURRENT_LIMIT] + 5% is still below 100% duty cycle for the PWM limits and therefore OK.
123116
// Might not be OK for a different shunt resistor or different PWM setup.
124-
(self.iir.y_max + 0.05 * Pwm::MAX_CURRENT_LIMIT).max(0.) as f32,
125-
(self.iir.y_min - 0.05 * Pwm::MAX_CURRENT_LIMIT).min(0.) as f32,
117+
(self.iir.max() + 0.05 * Pwm::MAX_CURRENT_LIMIT).max(0.) as f32,
118+
(self.iir.min() - 0.05 * Pwm::MAX_CURRENT_LIMIT).min(0.) as f32,
126119
]
127120
}
128121
}

0 commit comments

Comments
 (0)