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

SNS - Low Pressure Sensor #73

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3ac18ee
init commit
siny-over-cosy Feb 5, 2025
ce6e4a6
added low_pressure to include
siny-over-cosy Feb 5, 2025
a426a6e
fixed old comments and updated information
siny-over-cosy Feb 6, 2025
0d8e6d7
added conversion from adc to pressure
siny-over-cosy Feb 6, 2025
0258a9c
added hyped_adc inclusion
siny-over-cosy Feb 9, 2025
c7e1271
moved GRADIENT_LOW to global constant, removed comments
siny-over-cosy Feb 9, 2025
f38d741
fixed syntax error in path
siny-over-cosy Feb 9, 2025
f870df3
changed read_pressure return type to f32
siny-over-cosy Feb 9, 2025
9c52983
changed include order
siny-over-cosy Feb 9, 2025
d3c2d0b
formatting fixes
siny-over-cosy Feb 9, 2025
66e2874
ran clippy
siny-over-cosy Feb 9, 2025
a5744da
added documentation for clippy purposes
siny-over-cosy Feb 9, 2025
2d05367
documentation formatting fix
siny-over-cosy Feb 9, 2025
85a6522
added read_low_pressure include
siny-over-cosy Feb 9, 2025
4c8d255
init commit
siny-over-cosy Feb 9, 2025
af46e82
implemented task
siny-over-cosy Feb 9, 2025
33fb3f6
formatting fixes
siny-over-cosy Feb 9, 2025
321d44e
type fixes
siny-over-cosy Feb 9, 2025
5d56d5a
changed to #[test], fixed type errors
siny-over-cosy Feb 9, 2025
875d404
documentation update
siny-over-cosy Feb 9, 2025
5565fc6
moved read_low_pressure task to bin directory
siny-over-cosy Feb 9, 2025
3606a39
added no_std and no_main
siny-over-cosy Feb 9, 2025
53a3e61
updated imports
siny-over-cosy Feb 9, 2025
0b3c38e
replaced sender with defmt info macro
siny-over-cosy Feb 9, 2025
bd03f7d
changed adc variable to unmutable
siny-over-cosy Feb 9, 2025
e0b29dd
added hyped_adc import
siny-over-cosy Feb 9, 2025
63b51a5
removed no_std and no_main macros
siny-over-cosy Feb 9, 2025
5d950e0
added no_main
siny-over-cosy Feb 9, 2025
35989f9
made main not pub
siny-over-cosy Feb 9, 2025
0bffec6
added panic handler
siny-over-cosy Feb 9, 2025
83d2e41
added that last import line
siny-over-cosy Feb 9, 2025
fd51fb2
removed dummy panic handler
siny-over-cosy Feb 9, 2025
441fab1
changed to convert from adc reading and moved documentation
siny-over-cosy Feb 13, 2025
716d2bf
renamed from read_low_pressure.rs
siny-over-cosy Feb 13, 2025
85cd1a6
formatting fix
siny-over-cosy Feb 13, 2025
0aa3a84
Merge remote-tracking branch 'origin/main' into yu-lei/hype-58-implem…
siny-over-cosy Feb 13, 2025
9825072
removed documetation
siny-over-cosy Feb 13, 2025
229c532
bruh
siny-over-cosy Feb 13, 2025
100ab47
modified to accommodate sensor range value handling
siny-over-cosy 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.

1 change: 1 addition & 0 deletions boards/stm32f767zi/Cargo.lock

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

28 changes: 28 additions & 0 deletions boards/stm32f767zi/src/bin/low_pressure_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![no_std]
#![no_main]

use core::panic::PanicInfo;
use defmt::*;
use embassy_executor::Spawner;
use embassy_stm32::adc::{Adc, AdcChannel};
use embassy_time::{Duration, Timer};
use hyped_boards_stm32f767zi::io::Stm32f767ziAdc;
use hyped_sensors::low_pressure::LowPressure;
use {defmt_rtt as _, panic_probe as _};

/// Test task that just continually reads pressure from low pressure sensor and prints value to console
#[embassy_executor::main]
async fn main(_spawner: Spawner) -> ! {
let p = embassy_stm32::init(Default::default());
info!("Low pressure sensor");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
info!("Low pressure sensor");

not really necessary


let adc = Adc::new(p.ADC1);
let pin = p.PA3;

let mut low_pressure_sensor = LowPressure::new(Stm32f767ziAdc::new(adc, pin.degrade_adc()));

loop {
info!("{}", low_pressure_sensor.read_pressure());
Timer::after(Duration::from_millis(100)).await;
}
Comment on lines +24 to +27
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're loop here will need to match on the SensorValueRange to determine whether it's a critical, warning, or safe reading once that's implemented

}
1 change: 1 addition & 0 deletions lib/sensors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ embassy-sync = { version = "0.6.0", features = ["defmt"], git = "https://github.
hyped_core = { path = "../core" }
hyped_i2c = { path = "../io/hyped_i2c" }
hyped_gpio = { path = "../io/hyped_gpio" }
hyped_adc = { path = "../io/hyped_adc" }
defmt = "0.3"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions lib/sensors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]

pub mod keyence;
pub mod low_pressure;
pub mod temperature;
pub mod time_of_flight;

Expand Down
75 changes: 75 additions & 0 deletions lib/sensors/src/low_pressure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use hyped_adc::HypedAdc;

use crate::SensorValueRange;

/// The low pressure sensor (LPS) (model: SPAN-P10R-G18F-PNLK-PNVBA-L1) is able to detect
/// pressure in range from 0 to 10 bar. The sensor utilises the ADC protocol to get the
/// pressure value.
///
/// Links to datasheets
/// (https://www.festo.com/gb/en/a/download-document/datasheet/8134897)
/// (https://www.festo.com/media/catalog/203714_documentation.pdf)
pub struct LowPressure<T: HypedAdc> {
adc: T,
calculate_bounds: fn(f32) -> SensorValueRange<f32>,
}

const MAX_PRESSURE: f32 = 10.0;
const ADC_RESOLUTION: f32 = 4096.0;
const GRADIENT_LOW: f32 = MAX_PRESSURE / ADC_RESOLUTION;

impl<T: HypedAdc> LowPressure<T> {
/// Create new low pressure sensor instance
pub fn new(adc: T) -> LowPressure<T>{
Self::new_with_bounds(adc, default_calculate_bounds)
}

/// Create new low pressure sensor instance with specified bounds
pub fn new_with_bounds(
adc: T,
calculate_bounds: fn(f32) -> SensorValueRange<f32>,
) -> LowPressure<T> {
LowPressure {
adc,
calculate_bounds,
}
}

/// Convert ADC reading to bar unit and return value to caller
/// The conversion rate is expressed as a linear function of:
/// pressure = (conversion gradient) * (ADC reading) + (minimum pressure value)
/// (y = mx + c0)
/// where conversion gradient is
/// (maximum pressure value - minimum pressure value) / (4096).
/// 4096 is the maximum ADC reading value.
/// Since LPS has a minimum pressure of 0 bar, c0 is 0 and was did not need to be included in
/// the source code.
/// wrapped in a SensorValueRange enum to indicate if the temperature is safe, warning, or critical.
pub fn read_pressure(&mut self) -> SensorValueRange<f32> {
let adc_val = self.adc.read_value() as f32;

// convert to bar unit
let bar_pressure_val: f32 = adc_val * GRADIENT_LOW;
Comment on lines +49 to +52
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm with Samuel that this conversion is correct


(self.calculate_bounds)(
bar_pressure_val
)
}
}

/// Default calculation of the bounds for the low pressure sensor. The bounds are set to:
/// - Safe: 4.0 to 6.0 bar
/// - Warning: 2.0 to 4.0 and 6.0 to 8.0 bar
/// - Critical: below 2.0 and above 8.0 bar
/// TODO: CHANGE WHEN NEW INFO GOTTEN, THESE ARE PLACEHOLDER VALS
pub fn default_calculate_bounds(value: f32) -> SensorValueRange<f32> {
if value <= 2.0 || value >= 8.0 {
SensorValueRange::Critical(value)
}
else if value <= 4.0 || value >= 6.0 {
SensorValueRange::Warning(value)
}
else {
SensorValueRange::Safe(value)
}
}
Loading