Skip to content

Commit

Permalink
Merge pull request #832 from quartiq/iir-rework
Browse files Browse the repository at this point in the history
iir rework
  • Loading branch information
jordens authored Jan 15, 2024
2 parents ededc69 + b1acca2 commit f8db161
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ console.
### Changed
* Broker is no longer configured at compile time, but is maintained in device memory
* MSRV bumped to v1.66
* The IIR (biquad) filter used for PID action has changed its serialization format.
See also the `iir_coefficients` Python CLI implementation.

## [0.9.0](https://github.com/quartiq/stabilizer/compare/v0.8.1...v0.9.0)

Expand All @@ -35,7 +37,7 @@ console.
* `hitl/streaming.py` no longer requires a prefix
* Streaming now supports UDP multicast addresses

## [v0.8.1](https://github.com/quartiq/stabilizer/compare/v0.8.0...v0.8.1) - 2022-11-14)
## [v0.8.1](https://github.com/quartiq/stabilizer/compare/v0.8.0...v0.8.1) - 2022-11-14

* Fixed the python package dependencies

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ cortex-m-rtic = "1.0"
embedded-hal = "0.2.7"
num_enum = { version = "0.7.2", default-features = false }
paste = "1"
idsp = "0.13"
idsp = "0.14.1"
ad9959 = { path = "ad9959", version = "0.2.1" }
serial-settings = {path = "serial-settings"}
mcp230xx = "1.0"
Expand Down
6 changes: 3 additions & 3 deletions hitl/loopback.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def static_iir_output(output_voltage):
"""
machine_units = voltage_to_machine_units(output_voltage)
return {
'y_min': machine_units,
'y_max': machine_units,
'y_offset': 0,
'ba': [1, 0, 0, 0, 0],
'u': 0,
'min': machine_units,
'max': machine_units,
}


Expand Down
8 changes: 4 additions & 4 deletions py/stabilizer/iir_coefficients.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ async def configure():
# Note: In the future, we will need to Handle higher-order cascades.
await interface.set(f"/iir_ch/{args.channel}/0", {
"ba": coefficients,
"y_min": stabilizer.voltage_to_machine_units(args.y_min),
"y_max": stabilizer.voltage_to_machine_units(args.y_max),
"y_offset": stabilizer.voltage_to_machine_units(
args.y_offset + forward_gain * args.x_offset)
"u": stabilizer.voltage_to_machine_units(
args.y_offset + forward_gain * args.x_offset),
"min": stabilizer.voltage_to_machine_units(args.y_min),
"max": stabilizer.voltage_to_machine_units(args.y_max),
})

asyncio.run(configure())
Expand Down
22 changes: 15 additions & 7 deletions src/bin/dual-iir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ pub struct Settings {
/// * `<n>` specifies which channel to configure. `<n>` := [0, 1]
/// * `<m>` specifies which cascade to configure. `<m>` := [0, 1], depending on [IIR_CASCADE_LENGTH]
///
/// # Value
/// See [iir::IIR#miniconf]
/// See [iir::Biquad]
#[tree(depth(2))]
iir_ch: [[iir::IIR<f32>; IIR_CASCADE_LENGTH]; 2],
iir_ch: [[iir::Biquad<f32>; IIR_CASCADE_LENGTH]; 2],

/// Specified true if DI1 should be used as a "hold" input.
///
Expand Down Expand Up @@ -150,6 +149,9 @@ pub struct Settings {

impl Default for Settings {
fn default() -> Self {
let mut i = iir::Biquad::IDENTITY;
i.set_min(-SCALE);
i.set_max(SCALE);
Self {
// Analog frontend programmable gain amplifier gains (G1, G2, G5, G10)
afe: [Gain::G1, Gain::G1],
Expand All @@ -158,7 +160,7 @@ impl Default for Settings {
// The array is `iir_state[channel-index][cascade-index][coeff-index]`.
// The IIR coefficients can be mapped to other transfer function
// representations, for example as described in https://arxiv.org/abs/1508.06319
iir_ch: [[iir::IIR::new(1., -SCALE, SCALE); IIR_CASCADE_LENGTH]; 2],
iir_ch: [[i; IIR_CASCADE_LENGTH]; 2],

// Permit the DI1 digital input to suppress filter output updates.
allow_hold: false,
Expand Down Expand Up @@ -199,7 +201,7 @@ mod app {
afes: (AFE0, AFE1),
adcs: (Adc0Input, Adc1Input),
dacs: (Dac0Output, Dac1Output),
iir_state: [[iir::Vec5<f32>; IIR_CASCADE_LENGTH]; 2],
iir_state: [[[f32; 4]; IIR_CASCADE_LENGTH]; 2],
generator: FrameGenerator,
cpu_temp_sensor: stabilizer::hardware::cpu_temp_sensor::CpuTempSensor,
}
Expand Down Expand Up @@ -258,7 +260,7 @@ mod app {
afes: stabilizer.afes,
adcs: stabilizer.adcs,
dacs: stabilizer.dacs,
iir_state: [[[0.; 5]; IIR_CASCADE_LENGTH]; 2],
iir_state: [[[0.; 4]; IIR_CASCADE_LENGTH]; 2],
generator,
cpu_temp_sensor: stabilizer.temperature_sensor,
};
Expand Down Expand Up @@ -345,7 +347,13 @@ mod app {
.iter()
.zip(iir_state[channel].iter_mut())
.fold(x, |yi, (ch, state)| {
ch.update(state, yi, hold)
let filter = if hold {
&iir::Biquad::HOLD
} else {
ch
};

filter.update(state, yi)
});

// Note(unsafe): The filter limits must ensure that the value is in range.
Expand Down

0 comments on commit f8db161

Please sign in to comment.