Skip to content

Commit

Permalink
use Zero,One traits
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Oct 26, 2023
1 parent 8da2fd6 commit f2382c8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
23 changes: 11 additions & 12 deletions src/iir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

use super::{abs, copysign, macc};
use core::iter::Sum;
use num_traits::{clamp, Float, NumCast};
use num_traits::{clamp, Float, One, Zero};

/// IIR state and coefficients type.
///
Expand Down Expand Up @@ -62,11 +62,13 @@ pub struct IIR<T> {
pub y_max: T,
}

impl<T: Float + Default + Sum<T>> IIR<T> {
impl<T: Float + Zero + One + Sum<T>> IIR<T> {
pub fn new(gain: T, y_min: T, y_max: T) -> Self {
let mut ba = [T::zero(); 5];
ba[0] = gain;
Self {
ba: [gain, T::default(), T::default(), T::default(), T::default()],
y_offset: T::default(),
ba,
y_offset: T::zero(),
y_min,
y_max,
}
Expand All @@ -81,28 +83,25 @@ impl<T: Float + Default + Sum<T>> IIR<T> {
/// * `ki` - Integral gain at Nyquist. Sign taken from `kp`.
/// * `g` - Gain limit.
pub fn set_pi(&mut self, kp: T, ki: T, g: T) -> Result<(), &str> {
let zero: T = T::default();
let one: T = NumCast::from(1.0).unwrap();
let two: T = NumCast::from(2.0).unwrap();
let ki = copysign(ki, kp);
let g = copysign(g, kp);
let (a1, b0, b1) = if abs(ki) < T::epsilon() {
(zero, kp, zero)
(T::zero(), kp, T::zero())
} else {
let c = if abs(g) < T::epsilon() {
one
T::one()
} else {
one / (one + ki / g)
T::one() / (T::one() + ki / g)
};
let a1 = two * c - one;
let a1 = (T::one() + T::one()) * c - T::one();
let b0 = ki * c + kp;
let b1 = ki * c - a1 * kp;
if abs(b0 + b1) < T::epsilon() {
return Err("low integrator gain and/or gain limit");
}
(a1, b0, b1)
};
self.ba.copy_from_slice(&[b0, b1, zero, a1, zero]);
self.ba.copy_from_slice(&[b0, b1, T::zero(), a1, T::zero()]);
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/iir_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl IIR {
// Store x0 x0 x1 x2 y1 y2
xy[0] = x0;
// Compute y0 by multiply-accumulate
let y0 = macc_i32(self.y_offset, xy, &self.ba, IIR::SHIFT);
let y0 = macc_i32(self.y_offset, xy, &self.ba, Self::SHIFT);
// Limit y0
let y0 = y0.max(self.y_min).min(self.y_max);
// Store y0 x0 x1 y0 y1 y2
Expand Down
9 changes: 5 additions & 4 deletions src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use core::ops::{Add, Mul, Neg};
use num_traits::Zero;

pub fn abs<T>(x: T) -> T
where
T: PartialOrd + Default + Neg<Output = T>,
T: PartialOrd + Zero + Neg<Output = T>,
{
if x >= T::default() {
if x >= T::zero() {
x
} else {
-x
Expand All @@ -18,9 +19,9 @@ where

pub fn copysign<T>(x: T, y: T) -> T
where
T: PartialOrd + Default + Neg<Output = T>,
T: PartialOrd + Zero + Neg<Output = T>,
{
if (x >= T::default() && y >= T::default()) || (x <= T::default() && y <= T::default()) {
if (x >= T::zero() && y >= T::zero()) || (x <= T::zero() && y <= T::zero()) {
x
} else {
-x
Expand Down

0 comments on commit f2382c8

Please sign in to comment.