Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LOC - Preprocess Optical #46

Merged
merged 5 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}