use crate::FilterToDifferentiator; use interface::{Data, Read, Update, Write, UID}; #[derive(Default)] pub struct Differentiator(f64, f64); impl Update for Differentiator {} impl Read for Differentiator { fn read(&mut self, data: Data) { self.0 = *data; } } impl Read for Differentiator { fn read(&mut self, data: Data) { self.1 = *data; } } #[derive(UID)] #[uid(data = f64)] pub enum DifferentiatorToIntegrator {} impl Write for Differentiator { fn write(&mut self) -> Option> { Some(Data::new(self.0 - self.1)) } } #[derive(Default)] pub struct Integrator { gain: f64, mem: Vec, } impl Integrator { pub fn new(gain: f64, n_data: usize) -> Self { Self { gain, mem: vec![0f64; n_data], } } pub fn last(&self) -> Option> { Some(self.mem.clone()) } } impl Update for Integrator {} impl Read for Integrator { fn read(&mut self, data: Data) { self.mem[0] += *data * self.gain; } } #[derive(UID)] #[uid(data = f64)] pub enum IntegratorToDifferentiator {} impl Write for Integrator { fn write(&mut self) -> Option> { self.last().map(|x| Data::new(x[0])) } }