Skip to content

Commit

Permalink
Cleanup Kalman struct
Browse files Browse the repository at this point in the history
  • Loading branch information
maximbaz committed Aug 23, 2021
1 parent 36a6d8f commit 0b89a60
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
20 changes: 9 additions & 11 deletions src/controller/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::als::Als;
use crate::controller::data::{Data, Entry};
use crate::controller::kalman::Kalman;
use itertools::Itertools;
use std::error::Error;
use std::sync::mpsc::{Receiver, Sender};
use std::time::Duration;

Expand Down Expand Up @@ -51,7 +50,7 @@ impl Controller {
prediction_tx,
user_rx,
als,
kalman: Kalman::new(1., 20., 10.),
kalman: Kalman::new(1.0, 20.0, 10.0),
pending_cooldown: 0,
pending: None,
data,
Expand All @@ -60,16 +59,14 @@ impl Controller {
}
}

pub fn adjust(&mut self, luma: Option<u8>) -> Result<(), Box<dyn Error>> {
let lux = self.als.get()?;
let lux = self.kalman.process(lux as f64).round() as u64; // TODO make Kalman::<u64>
if !self.kalman.initialized() {
return Ok(());
}

self.process(lux, luma);
pub fn adjust(&mut self, luma: Option<u8>) {
let lux = self
.kalman
.process(self.als.get().expect("Unable to get ALS value"));

Ok(())
if self.kalman.initialized() {
self.process(lux, luma);
}
}

fn process(&mut self, lux: u64, luma: Option<u8>) {
Expand Down Expand Up @@ -199,6 +196,7 @@ mod tests {
use crate::als::MockAls;
use itertools::iproduct;
use std::collections::HashSet;
use std::error::Error;
use std::sync::mpsc;

fn setup() -> (Controller, Sender<u64>, Receiver<u64>) {
Expand Down
29 changes: 14 additions & 15 deletions src/controller/kalman.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
pub struct Kalman {
steps: u64,
q: f64,
r: f64,
value: Option<f64>,
covariance: f64,
value: Option<f64>,
steps: u64,
}

impl Kalman {
pub fn new(q: f64, r: f64, covariance: f64) -> Kalman {
Kalman {
steps: 0,
q,
r,
value: None,
covariance,
value: None,
steps: 0,
}
}
pub fn process(&mut self, input: f64) -> f64 {

pub fn process(&mut self, next: u64) -> u64 {
self.steps += 1;

match self.value {
None => {
self.value = Some(input);
input
}
Some(x0) => {
None => self.value = Some(next as f64),
Some(prev) => {
let p0 = self.covariance + self.q;
let k = p0 / (p0 + self.r);
let x1 = x0 + k * (input - x0);
let cov = (1.0 - k) * p0;
self.value = Some(x1);
self.covariance = cov;
x1
self.value = Some(prev + k * (next as f64 - prev));
self.covariance = (1.0 - k) * p0;
}
}

self.value.unwrap().round() as u64
}

pub fn initialized(&self) -> bool {
self.steps > 10
}
Expand Down
2 changes: 1 addition & 1 deletion src/frame/capturer/none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Capturer {}
impl super::Capturer for Capturer {
fn run(&self, mut controller: Controller) {
loop {
controller.adjust(None).expect("TODO");
controller.adjust(None);
thread::sleep(Duration::from_secs(1));
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/frame/capturer/wlroots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ impl Capturer {
}

Event::Ready { .. } => {
controller
.borrow_mut()
.adjust(self.processor.luma_percent(&frame).ok())
.expect("TODO");
let luma = self
.processor
.luma_percent(&frame)
.expect("Unable to compute luma percent");

controller.borrow_mut().adjust(Some(luma));

data.destroy();

Expand Down

0 comments on commit 0b89a60

Please sign in to comment.