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

src: Add support to leak detection #66

Merged
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
1 change: 1 addition & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn navigator_benchmark(c: &mut Criterion) {
bench!(set_led(UserLed::Led1, false));
bench!(set_led_toggle(UserLed::Led1));
bench!(get_led(UserLed::Led1));
bench!(read_leak());
}

criterion_group!(benches, navigator_benchmark);
Expand Down
42 changes: 41 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ads1x1x::{
};
use ak09915_rs::{Ak09915, Mode as mag_Mode};
use bmp280::{Bmp280, Bmp280Builder};
use embedded_hal::prelude::_embedded_hal_blocking_delay_DelayMs;
use embedded_hal::{digital::v2::InputPin, prelude::_embedded_hal_blocking_delay_DelayMs};
RaulTrombin marked this conversation as resolved.
Show resolved Hide resolved
use icm20689::{self, AccelRange, Builder as imu_Builder, GyroRange, SpiInterface, ICM20689};
use linux_embedded_hal::spidev::{self, SpidevOptions};
use linux_embedded_hal::sysfs_gpio::Direction;
Expand Down Expand Up @@ -138,6 +138,7 @@ pub struct SensorData {
pub accelerometer: AxisData,
pub magnetometer: AxisData,
pub gyro: AxisData,
pub leak: bool,
}

/// The `Led` struct represents the 3 LEDs on navigator board.
Expand All @@ -164,6 +165,7 @@ pub struct Navigator {
mag: Ak09915<I2cdev>,
led: Led,
neopixel: Strip,
leak: Pin,
}

impl Deref for Pwm {
Expand Down Expand Up @@ -332,6 +334,8 @@ impl NavigatorBuilder {

let led = Led::new();

let leak = Pin::new(27);
Copy link
Member

Choose a reason for hiding this comment

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

//Define leakage sensor Pin

Copy link
Member Author

@patrickelectric patrickelectric Feb 16, 2024

Choose a reason for hiding this comment

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

What do you mean by it ? I'm following the same pattern as in the LED initialization. It's already clear that 27 is the pin for the leak detection in the code.

Copy link
Member

@RaulTrombin RaulTrombin Feb 16, 2024

Choose a reason for hiding this comment

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

just to have some overall explanation, which sensor.
The LEDs are self-explained, but leakage sensors need to attach an extra gadget to GPIO.

Copy link
Member Author

Choose a reason for hiding this comment

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

just to have some overall explanation, which sensor. The LEDs are self-explained, but leakage sensors need to attach an extra gadget to GPIO.

That's documented on the read_leak function.

Copy link
Member

Choose a reason for hiding this comment

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

ok.


Navigator {
adc: (adc),
bmp: (bmp),
Expand All @@ -340,6 +344,7 @@ impl NavigatorBuilder {
imu: (imu),
led: (led),
neopixel: (neopixel),
leak: (leak),
}
}
}
Expand Down Expand Up @@ -384,6 +389,13 @@ impl Navigator {
self.bmp.zero().unwrap();

self.led.set_led_all(false);

self.leak
.export()
.expect("Error: Failed to export leak pin");
self.leak
.set_direction(Direction::In)
.expect("Error: Failed to set leak pin as input");
}

pub fn self_test(&mut self) -> bool {
Expand Down Expand Up @@ -976,6 +988,31 @@ impl Navigator {
}
}

/// Reads the state of leak detector pin from [`Navigator`].
///
/// The value is true when a leak is detected.
///
/// # Examples
///
/// ```no_run
/// use navigator_rs::{Navigator};
/// use std::thread::sleep;
/// use std::time::Duration;
///
/// let mut nav = Navigator::new();
/// nav.init();
///
/// loop {
/// println!("Leak: {}", nav.read_leak());
/// sleep(Duration::from_millis(1000));
/// }
/// ```
pub fn read_leak(&self) -> bool {
self.leak
.is_high()
.expect("Failed to read state of leak pin")
}

/// Reads all sensors and stores on a single structure.
///
/// # Examples
Expand Down Expand Up @@ -1003,6 +1040,7 @@ impl Navigator {
accelerometer: self.read_accel(),
magnetometer: self.read_mag(),
gyro: self.read_gyro(),
leak: self.read_leak(),
}
}

Expand All @@ -1029,6 +1067,7 @@ impl Navigator {
bmp: Bmp,
imu: Imu,
mag: AxisData,
leak: bool,
}

Navigator {
Expand All @@ -1043,6 +1082,7 @@ impl Navigator {
gyroscope: self.read_gyro(),
},
mag: self.read_mag(),
leak: self.read_leak(),
}
}
}
Loading