Skip to content

Commit

Permalink
LOC - Preprocess Optical (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
lylebuist authored Nov 16, 2024
1 parent 36b1999 commit 3701267
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/localisation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
nalgebra = { version = "0.33.0", default-features = false }
heapless = "0.8.0"
libm = "0.2.11"
1 change: 1 addition & 0 deletions lib/localisation/src/preprocessing.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod keyence;
pub mod optical;
59 changes: 59 additions & 0 deletions lib/localisation/src/preprocessing/optical.rs
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
use heapless::Vec;
use libm::sqrtf;

/// Processes the raw optical data to get the magnitude and added to the optical data for each sensor
pub fn process_data(raw_optical_data: Vec<Vec<f64, 2>, 2>) -> Vec<f32, 2> {
let mut optical_data: Vec<f32, 2> = Vec::from_slice(&[0.0, 0.0]).unwrap();

for i in 0..2 {
let mut magnitude: f32 = 0.0;

for data in raw_optical_data[i].clone() {
let data: f32 = data as f32;
magnitude += data * data;
}
optical_data[i] = sqrtf(magnitude);
}

optical_data
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_correct_positive() {
let raw_optical_data: Vec<Vec<f64, 2>, 2> = Vec::from_slice(&[
Vec::from_slice(&[1.0, 1.0]).unwrap(),
Vec::from_slice(&[3.0, 4.0]).unwrap(),
])
.unwrap();
let desired_outcome: Vec<f32, 2> = Vec::from_slice(&[sqrtf(2.0), 5.0]).unwrap();
let result = process_data(raw_optical_data);
assert_eq!(result, desired_outcome);
}

#[test]
fn test_correct_negative() {
let raw_optical_data: Vec<Vec<f64, 2>, 2> = Vec::from_slice(&[
Vec::from_slice(&[-4.0, -6.0]).unwrap(),
Vec::from_slice(&[-3.0, -1.0]).unwrap(),
])
.unwrap();
let desired_outcome: Vec<f32, 2> = Vec::from_slice(&[7.2111025, 3.1622777]).unwrap();
let result = process_data(raw_optical_data);
assert_eq!(result, desired_outcome);
}

#[test]
fn test_correct_zero() {
let raw_optical_data: Vec<Vec<f64, 2>, 2> = Vec::from_slice(&[
Vec::from_slice(&[0.0, 0.0]).unwrap(),
Vec::from_slice(&[0.0, 0.0]).unwrap(),
])
.unwrap();
let desired_outcome: Vec<f32, 2> = Vec::from_slice(&[0.0, 0.0]).unwrap();
let result = process_data(raw_optical_data);
assert_eq!(result, desired_outcome);
}
}

0 comments on commit 3701267

Please sign in to comment.