use std::sync::Arc; use crate::FilterToDifferentiator; use dos_actors::{ io::{Data, Read, Write}, Update, }; use uid_derive::UID; #[derive(Default)] pub struct Differentiator(f64, f64); impl Update for Differentiator {} impl Read for Differentiator { fn read(&mut self, data: Arc>) { self.0 = **data; } } impl Read for Differentiator { fn read(&mut self, data: Arc>) { self.1 = **data; } } #[derive(UID)] #[uid(data = "f64")] pub enum DifferentiatorToIntegrator {} impl Write for Differentiator { fn write(&mut self) -> Option>> { Some(Arc::new(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: Arc>) { 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| Arc::new(Data::new(x[0]))) } }