-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod keyence; | ||
pub mod optical; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |