From 5369b928c1c58c1a8cbf8c6c1fef772107949cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Sun, 31 Mar 2019 11:33:18 +0000 Subject: [PATCH] add get_x_offset --- src/iir.rs | 10 +++++++++- src/main.rs | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/iir.rs b/src/iir.rs index dd31b86..6d02e17 100644 --- a/src/iir.rs +++ b/src/iir.rs @@ -27,7 +27,7 @@ fn macc(y0: f32, x: &[f32], a: &[f32]) -> f32 { } impl IIR { - pub fn set_pi(&mut self, kp: f32, ki: f32, g: f32) -> Result<(), &'static str> { + pub fn set_pi(&mut self, kp: f32, ki: f32, g: f32) -> Result<(), &str> { let ki = copysign(ki, kp); let g = copysign(g, kp); let (a1, b0, b1) = @@ -52,6 +52,14 @@ impl IIR { Ok(()) } + pub fn get_x_offset(&self) -> Result { + let b: f32 = self.ba[..3].iter().sum(); + if abs(b) < f32::EPSILON { + return Err("b is zero") + } + Ok(self.y_offset/b) + } + pub fn set_x_offset(&mut self, xo: f32) { let b: f32 = self.ba[..3].iter().sum(); self.y_offset = xo*b; diff --git a/src/main.rs b/src/main.rs index 1a90298..83d7c4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -563,6 +563,7 @@ fn main() -> ! { IIR_CH[1].set_pi(-0.1, -10.*t, 0.).expect("bad coefficients"); IIR_CH[1].set_x_offset(0.1*SCALE); + IIR_CH[1].get_x_offset().expect("bad coefficients"); } cortex_m::interrupt::free(|cs| {