From 24222821b52696520df2d3f9555e2fad36a62d19 Mon Sep 17 00:00:00 2001 From: Niklas Kuhrmeyer Date: Thu, 3 Dec 2020 14:10:28 +0100 Subject: [PATCH 1/3] Added cascaded IIR with server commands for up to 2 cascaded IIRs per channel. --- src/main.rs | 67 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 845a9b4..f2c3baa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,9 @@ const SAMPLE_FREQUENCY_KHZ: u32 = 500; // The desired ADC sample processing buffer size. const SAMPLE_BUFFER_SIZE: usize = 1; +// The number of cascaded IIR biquads per channel. Select 1 or 2! +const IIR_CASCADE_LENGTH: usize = 1; + #[link_section = ".sram3.eth"] static mut DES_RING: ethernet::DesRing = ethernet::DesRing::new(); @@ -210,10 +213,11 @@ const APP: () = { pounder: Option>, - #[init([[0.; 5]; 2])] - iir_state: [iir::IIRState; 2], - #[init([iir::IIR { ba: [1., 0., 0., 0., 0.], y_offset: 0., y_min: -SCALE - 1., y_max: SCALE }; 2])] - iir_ch: [iir::IIR; 2], + // Format: iir_state[ch][cascade-no][coeff] + #[init([[[0.; 5]; IIR_CASCADE_LENGTH];2])] + iir_state: [[iir::IIRState; IIR_CASCADE_LENGTH]; 2], + #[init([[iir::IIR { ba: [1., 0., 0., 0., 0.], y_offset: 0., y_min: -SCALE - 1., y_max: SCALE }; IIR_CASCADE_LENGTH]; 2])] + iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2], } #[init] @@ -761,8 +765,11 @@ const APP: () = { for channel in 0..adc_samples.len() { for sample in 0..adc_samples[0].len() { let x = f32::from(adc_samples[channel][sample] as i16); - let y = c.resources.iir_ch[channel] - .update(&mut c.resources.iir_state[channel], x); + let mut y = x; + for i in 0..c.resources.iir_state[channel].len() { + y = c.resources.iir_ch[channel][i] + .update(&mut c.resources.iir_state[channel][i], y); + } // Note(unsafe): The filter limits ensure that the value is in range. // The truncation introduces 1/2 LSB distortion. let y = unsafe { y.to_int_unchecked::() }; @@ -776,6 +783,7 @@ const APP: () = { } #[idle(resources=[net_interface, pounder, mac_addr, eth_mac, iir_state, iir_ch, afes])] + fn idle(mut c: idle::Context) -> ! { let mut socket_set_entries: [_; 8] = Default::default(); let mut sockets = @@ -827,10 +835,23 @@ const APP: () = { let state = c.resources.iir_state.lock(|iir_state| server::Status { t: time, - x0: iir_state[0][0], - y0: iir_state[0][2], - x1: iir_state[1][0], - y1: iir_state[1][2], + x0: iir_state[0][0][0], + y0: iir_state[0][0][2], + x1: iir_state[1][0][0], + y1: iir_state[1][0][2], + }); + + Ok::(state) + }), + // "_b" means cascades 2nd IIR + "stabilizer/iir_b/state": (|| { + let state = c.resources.iir_state.lock(|iir_state| + server::Status { + t: time, + x0: iir_state[0][IIR_CASCADE_LENGTH-1][0], + y0: iir_state[0][IIR_CASCADE_LENGTH-1][2], + x1: iir_state[1][IIR_CASCADE_LENGTH-1][0], + y1: iir_state[1][IIR_CASCADE_LENGTH-1][2], }); Ok::(state) @@ -880,7 +901,7 @@ const APP: () = { return Err(()); } - iir_ch[req.channel as usize] = req.iir; + iir_ch[req.channel as usize][0] = req.iir; Ok::(req) }) @@ -891,7 +912,29 @@ const APP: () = { return Err(()); } - iir_ch[req.channel as usize] = req.iir; + iir_ch[req.channel as usize][0] = req.iir; + + Ok::(req) + }) + }), + "stabilizer/iir_b0/state": server::IirRequest, (|req: server::IirRequest| { + c.resources.iir_ch.lock(|iir_ch| { + if req.channel > 1 { + return Err(()); + } + + iir_ch[req.channel as usize][IIR_CASCADE_LENGTH-1] = req.iir; + + Ok::(req) + }) + }), + "stabilizer/iir_b1/state": server::IirRequest,(|req: server::IirRequest| { + c.resources.iir_ch.lock(|iir_ch| { + if req.channel > 1 { + return Err(()); + } + + iir_ch[req.channel as usize][IIR_CASCADE_LENGTH-1] = req.iir; Ok::(req) }) From b23d5fa0dc6e4d0a98673f1ea2ab50698c209131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Fri, 4 Dec 2020 18:22:38 +0100 Subject: [PATCH 2/3] main.rs: whitespace --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index f2c3baa..632b58a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -783,7 +783,6 @@ const APP: () = { } #[idle(resources=[net_interface, pounder, mac_addr, eth_mac, iir_state, iir_ch, afes])] - fn idle(mut c: idle::Context) -> ! { let mut socket_set_entries: [_; 8] = Default::default(); let mut sockets = From 4f8bdb971bb84846bfc26ce89db075e1f524871b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Fri, 4 Dec 2020 18:22:53 +0100 Subject: [PATCH 3/3] main.rs: style --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 632b58a..1ee6415 100644 --- a/src/main.rs +++ b/src/main.rs @@ -214,7 +214,7 @@ const APP: () = { pounder: Option>, // Format: iir_state[ch][cascade-no][coeff] - #[init([[[0.; 5]; IIR_CASCADE_LENGTH];2])] + #[init([[[0.; 5]; IIR_CASCADE_LENGTH]; 2])] iir_state: [[iir::IIRState; IIR_CASCADE_LENGTH]; 2], #[init([[iir::IIR { ba: [1., 0., 0., 0., 0.], y_offset: 0., y_min: -SCALE - 1., y_max: SCALE }; IIR_CASCADE_LENGTH]; 2])] iir_ch: [[iir::IIR; IIR_CASCADE_LENGTH]; 2],