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 - Localizer Board (HYPE-90) #75

Draft
wants to merge 94 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
79fc2f4
Initial commit
Sean-Leishman Oct 15, 2024
8299f39
Fixup types in use of entry preprocess function
Sean-Leishman Oct 15, 2024
213fce5
Update preprocessor main entry functions
Sean-Leishman Oct 23, 2024
d5be831
Clone getQuartiles implementation
Sean-Leishman Oct 23, 2024
e53f612
Ensure all reliable accelerometers are kept in range
Sean-Leishman Oct 24, 2024
a09ae8f
Add additional tests for quartile calculation
Sean-Leishman Oct 28, 2024
9fee8f8
Add handle_outlier tests
Sean-Leishman Oct 31, 2024
36b2976
Use localisation/src/types.rs for constants
Sean-Leishman Nov 1, 2024
62a36b9
Run clippy
Sean-Leishman Nov 1, 2024
d1177d0
Clean-up linter errors
Sean-Leishman Nov 1, 2024
b753953
Fix testing linter warnings
Sean-Leishman Nov 1, 2024
aa4b41c
QoL changes
Sean-Leishman Nov 11, 2024
58bcf9e
Remove extra impl
Sean-Leishman Nov 11, 2024
3cf7fd3
Default impl for clippy
Sean-Leishman Nov 11, 2024
34c7207
Merge remote-tracking branch 'origin/main' into hype-43-loc-accelerom…
Sean-Leishman Nov 11, 2024
6e6a502
initial commit
misha7b Nov 11, 2024
3a9585e
getters & setters
misha7b Nov 11, 2024
b352a14
new name!
misha7b Nov 11, 2024
847c2d9
Add comments and tests
Sean-Leishman Nov 14, 2024
5bc0704
clippy
misha7b Nov 16, 2024
894169c
small fixes to types
misha7b Nov 16, 2024
0ad0161
fmt!
misha7b Nov 16, 2024
98624df
Merge branch 'main' into hype-43-loc-accelerometer-preprocessor
misha7b Nov 16, 2024
266877a
Merge branch 'main' into transfer_navigator
misha7b Nov 16, 2024
c6ad181
Merge branch 'hype-43-loc-accelerometer-preprocessor' into transfer_n…
misha7b Nov 16, 2024
429ec17
small bug fixex
misha7b Nov 16, 2024
70bf0a9
Merge branch 'main' into transfer_navigator
misha7b Nov 16, 2024
61e6cfb
deleted redundant file
misha7b Nov 16, 2024
222c2a1
smallfix
misha7b Nov 16, 2024
24a43c2
initialised kalman filter
misha7b Nov 16, 2024
2392c54
structure changes
misha7b Nov 16, 2024
9199d1f
File name changes
misha7b Nov 16, 2024
8a97f96
working
misha7b Nov 16, 2024
666ca56
spelling
misha7b Nov 16, 2024
c635a37
preprocessor
misha7b Nov 16, 2024
cecf033
format|
misha7b Nov 16, 2024
eeda22a
Fixed preprocess_keyence: changed input vector from bool to int
misha7b Nov 18, 2024
d815e2a
small fixes
misha7b Nov 18, 2024
34105ec
Finished... I think?
misha7b Nov 21, 2024
55c0c26
CLIPPYgit add .! and also half-assed acceleration calculation:
misha7b Nov 21, 2024
39fc6a5
this commit will fail
misha7b Nov 21, 2024
545b4dc
i command you to build
misha7b Nov 21, 2024
6f8d85e
CLIPPY!
misha7b Nov 21, 2024
eaa32c3
simple silly test
misha7b Nov 21, 2024
7bfab5d
removed comment
misha7b Jan 13, 2025
73227cf
fmt
misha7b Jan 13, 2025
3aa1a45
comment
misha7b Jan 13, 2025
6427166
Merge branch 'main' into transfer_navigator
misha7b Jan 13, 2025
f38152e
comment
misha7b Jan 13, 2025
2eb9bd7
Changed from 2 to 1 Optical Flow Sensor
misha7b Jan 20, 2025
dee30e1
small name change
misha7b Jan 27, 2025
20da9e3
Merge remote-tracking branch 'origin/main' into transfer_navigator
misha7b Jan 27, 2025
5adbe56
optical flow scales with num of sensors
misha7b Feb 9, 2025
dc072ad
Merge remote-tracking branch 'refs/remotes/origin/main' into transfer…
misha7b Feb 9, 2025
7796505
small fix
misha7b Feb 9, 2025
50133f1
something abour passing on errors
misha7b Feb 9, 2025
6c17de8
minor test fixes
misha7b Feb 9, 2025
604a43e
RUST FORMAT
misha7b Feb 9, 2025
1057441
test commit
misha7b Feb 9, 2025
f08d8a7
todo:read the sensors
misha7b Feb 9, 2025
4ac1d79
now reading stripe counter
misha7b Feb 9, 2025
7fe19d4
fixex?
misha7b Feb 9, 2025
3309d7c
fixed some more errors
misha7b Feb 9, 2025
0117d02
PLEASE WORK I BEG YOU
misha7b Feb 9, 2025
63482d7
fixed nostd errors
misha7b Feb 9, 2025
719e0be
fmt....
misha7b Feb 9, 2025
ecf9526
WORKWORKWORK
misha7b Feb 9, 2025
ea6d098
changed path
misha7b Feb 9, 2025
fa74759
small fixes to imports and such
misha7b Feb 9, 2025
490337e
fixed some issues checking
misha7b Feb 9, 2025
693adb0
added getters
misha7b Feb 9, 2025
8b3ac47
PLEASE WORK PLEASE: added demfr lib
misha7b Feb 9, 2025
f687d3e
wrong version of defmt
misha7b Feb 9, 2025
59d3957
fmt
misha7b Feb 9, 2025
0a67808
addded second stripe counter
misha7b Feb 9, 2025
4dd888a
Merge remote-tracking branch 'refs/remotes/origin/main' into localize…
misha7b Feb 9, 2025
4ef8799
small fix, only continues when both keyence recieved
misha7b Feb 13, 2025
b397c84
small fix
misha7b Feb 13, 2025
4151184
fmt
misha7b Feb 13, 2025
51e1ea2
changed to 2 pins
misha7b Feb 13, 2025
f404986
pin change
misha7b Feb 13, 2025
2b4c57f
Merge remote-tracking branch 'refs/remotes/origin/main' into localize…
misha7b Feb 13, 2025
05a9d60
Merge remote-tracking branch 'refs/remotes/origin/main' into localize…
misha7b Feb 24, 2025
136281b
what?
misha7b Feb 24, 2025
1ed26b1
unsure
misha7b Feb 24, 2025
8d6791c
libm functions
misha7b Feb 24, 2025
0d53f17
libm fixes
misha7b Feb 24, 2025
d9bb13e
fixed libm and sort in accelerometre
misha7b Feb 24, 2025
bfcfc7d
PLEAE
misha7b Feb 24, 2025
a9aee1f
heapless fix
misha7b Feb 24, 2025
820ae49
fixed sort
misha7b Feb 24, 2025
d3b6c67
shoddy temprary sorting fix
misha7b Feb 24, 2025
404a800
fmt
misha7b Feb 24, 2025
1431e2c
fmt
misha7b Feb 24, 2025
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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions boards/stm32f767zi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ embassy-futures = { version = "0.1.0", git = "https://github.com/embassy-rs/emba

defmt = "0.3"
defmt-rtt = "0.4"
heapless = "0.8.0"

cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] }
cortex-m-rt = "0.7.0"
Expand All @@ -25,6 +26,7 @@ static_cell = "2"

hyped_core = { path = "../../lib/core" }
hyped_sensors = { path = "../../lib/sensors" }
hyped_localisation = { path = "../../lib/localisation" }

hyped_adc = { path = "../../lib/io/hyped_adc" }
hyped_adc_derive = { path = "../../lib/io/hyped_adc/hyped_adc_derive" }
Expand Down
82 changes: 82 additions & 0 deletions boards/stm32f767zi/src/bin/loc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#![no_std]
#![no_main]

use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::{
gpio::{Input, Pull},
init,
};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, watch::Watch};
use embassy_time::{Duration, Timer};
use hyped_boards_stm32f767zi::tasks::read_keyence::read_keyence;
use panic_probe as _;

use heapless::Vec;

use hyped_localisation::{
control::localizer::Localizer,
types::{RawAccelerometerData, NUM_ACCELEROMETERS, NUM_AXIS},
};

/// A Watch to hold the latest Keyence stripe count.
static KEYENCE_1_STRIPE_COUNT: Watch<CriticalSectionRawMutex, u32, 1> = Watch::new();
static KEYENCE_2_STRIPE_COUNT: Watch<CriticalSectionRawMutex, u32, 1> = Watch::new();

#[embassy_executor::main]
async fn main(spawner: Spawner) -> ! {
// Import `init` so that we can initialize board peripherals.
let p = init(Default::default());
let gpio_pin = Input::new(p.PC13, Pull::Down);

// Create a sender and a receiver for the Keyence stripe count.
let sender1 = KEYENCE_1_STRIPE_COUNT.sender();
let mut receiver1 = KEYENCE_1_STRIPE_COUNT.receiver().unwrap();
let sender2 = KEYENCE_2_STRIPE_COUNT.sender();
let mut receiver2 = KEYENCE_2_STRIPE_COUNT.receiver().unwrap();

spawner.spawn(read_keyence(gpio_pin, sender1)).unwrap();
spawner.spawn(read_keyence(gpio_pin, sender2)).unwrap();

info!("Starting localizer loop...");

let mut localizer = Localizer::new();

loop {
// Wait for a new Keyence stripe count.
let (stripe_count1, stripe_count2) = join!(receiver1.get(), receiver2.get());
defmt::info!(
"New Keyence stripe counts: sensor1 = {}, sensor2 = {}",
stripe_count1,
stripe_count2
);

// Create the sensor data. (Optical and accelerometer data are simulated.)
let optical_data: Vec<f64, 2> = Vec::from_slice(&[0.5, 0.5]).unwrap();
let keyence_data: Vec<u32, 2> = Vec::from_slice(&[stripe_count1, stripe_count2]).unwrap();
let accelerometer_data: RawAccelerometerData<NUM_ACCELEROMETERS, NUM_AXIS> =
RawAccelerometerData::from_slice(&[
Vec::from_slice(&[0.0, 0.0, 9.81]).unwrap(),
Vec::from_slice(&[0.0, 0.0, 9.81]).unwrap(),
Vec::from_slice(&[0.0, 0.0, 9.81]).unwrap(),
Vec::from_slice(&[0.0, 0.0, 9.81]).unwrap(),
])
.unwrap();

match localizer.iteration(optical_data, keyence_data, accelerometer_data) {
Ok(()) => {
defmt::info!(
"Iteration OK: displacement = {} m, velocity = {} m/s, acceleration = {} m/s**2",
localizer.displacement(),
localizer.velocity(),
localizer.acceleration()
);
}
Err(e) => {
defmt::error!("Iteration error: {:?}", e);
}
}

Timer::after(Duration::from_millis(100)).await;
}
}
3 changes: 2 additions & 1 deletion lib/localisation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"
[dependencies]
nalgebra = { version = "0.33.0", default-features = false, features = ["libm"] }
heapless = "0.8.0"
libm = "0.2.11"
libm = "0.2.11"
defmt = "0.3"
23 changes: 19 additions & 4 deletions lib/localisation/src/control/localizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Default for Localizer {
}
}

#[derive(Debug)]
#[derive(Debug, defmt::Format)]
pub enum PreprocessorError {
KeyenceUnacceptable,
AccelerometerUnnaceptable,
Expand Down Expand Up @@ -172,6 +172,21 @@ impl Localizer {
}
}

// Getters
impl Localizer {
pub fn displacement(&self) -> f64 {
self.displacement
}

pub fn velocity(&self) -> f64 {
self.velocity
}

pub fn acceleration(&self) -> f64 {
self.acceleration
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -193,9 +208,9 @@ mod tests {

localizer.iteration(optical_data, raw_keyence_data, raw_accelerometer_data)?;

assert_eq!(localizer.displacement, 0.0);
assert_eq!(localizer.velocity, 0.0);
assert_eq!(localizer.acceleration, 0.0);
assert_eq!(localizer.displacement(), 0.0);
assert_eq!(localizer.velocity(), 0.0);
assert_eq!(localizer.acceleration(), 0.0);

Ok(())
}
Expand Down
15 changes: 10 additions & 5 deletions lib/localisation/src/preprocessing/accelerometer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::types::{
NUM_ALLOWED_ACCELEROMETER_OUTLIERS, NUM_AXIS,
};
use heapless::Vec;
use libm;

/// Stores the quartiles of the data and the bounds for outliers
/// which are calculated from the quartiles
Expand Down Expand Up @@ -120,9 +121,11 @@ impl AccelerometerPreprocessor {
) -> Option<AccelerometerData<NUM_ACCELEROMETERS>> {
let accelerometer_data: AccelerometerData<NUM_ACCELEROMETERS> = data
.iter()
.map(|axis| axis.iter().fold(0.0, |acc, val| acc + val * val).sqrt())
.map(|axis| {
let sum = axis.iter().fold(0.0, |acc, val| acc + val * val);
libm::sqrtf(sum)
})
.collect();

let clean_accelerometer_data = self.handle_outliers(accelerometer_data)?;

if self.check_reliable() == SensorChecks::Unacceptable {
Expand Down Expand Up @@ -157,16 +160,18 @@ impl AccelerometerPreprocessor {

pub fn get_quartiles<const SIZE: usize>(&self, data: &AccelerometerData<SIZE>) -> Quartiles {
let mut sorted_data = data.clone();
sorted_data.sort_by(|a, b| a.partial_cmp(b).unwrap());
sorted_data
.as_mut_slice()
.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());

let quartile_keys: Vec<f32, 3> = Vec::from_slice(&[0.25, 0.5, 0.75]).unwrap();
let quartiles: Vec<f32, 3> = quartile_keys
.iter()
.map(|quartile| {
let index_quartile: f32 =
(1.0 + self.num_reliable_accelerometers as f32) * quartile;
let index_quartile_floor = index_quartile.floor() as usize - 1;
let index_quartile_ceil = index_quartile.ceil() as usize - 1;
let index_quartile_floor = libm::floorf(index_quartile) as usize - 1;
let index_quartile_ceil = libm::ceilf(index_quartile) as usize - 1;

(data.get(index_quartile_floor).unwrap_or(&0.0)
+ data.get(index_quartile_ceil).unwrap_or(&0.0))
Expand Down
Loading