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

Add preliminary pressure transducer implementation with INA219 #30

Merged
merged 15 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
161 changes: 150 additions & 11 deletions pod-operation/Cargo.lock

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

3 changes: 2 additions & 1 deletion pod-operation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
rppal = "0.17.1"
rppal = { version = "0.17.1", features = ["hal"] }
ina219 = "0.1.0"
10 changes: 10 additions & 0 deletions pod-operation/src/components/ina219.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use ina219::INA219;
use rppal::i2c::I2c;
use tracing::debug;

pub fn read_current(ina: &mut INA219<I2c>) -> f32 {
taesungh marked this conversation as resolved.
Show resolved Hide resolved
ina.calibrate(0xffff).unwrap();
debug!("Calibrating INA219");

ina.current().unwrap() as f32 / 160.0
taesungh marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions pod-operation/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod ina219;
pub mod pressure_transducer;
pub mod signal_light;
21 changes: 21 additions & 0 deletions pod-operation/src/components/pressure_transducer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::components::ina219::read_current;
use ina219::INA219;
use rppal::i2c::I2c;
use tracing::debug;

pub struct PressureTransducer {
ina: INA219<I2c>,
}

impl PressureTransducer {
pub fn new(ina219_addr: u8) -> Self {
let device = I2c::new().unwrap();
let ina219 = INA219::new(device, ina219_addr);
debug!("Initialized I2C and INA219");
PressureTransducer { ina: ina219 }
}

pub fn read(&mut self) -> f32 {
read_current(&mut self.ina)
}
taesungh marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions pod-operation/src/demo.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use tracing::info;

use crate::components::pressure_transducer::PressureTransducer;
use crate::components::signal_light::SignalLight;

pub async fn blink(mut signal_light: SignalLight) {
let mut i = 0;

let mut pt = PressureTransducer::new(0x40);

info!("Starting blink demo.");
loop {
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
Expand All @@ -14,6 +17,8 @@ pub async fn blink(mut signal_light: SignalLight) {
signal_light.disable();
}

println!("Current reading: {:?}", pt.read());

taesungh marked this conversation as resolved.
Show resolved Hide resolved
i += 1;
}
}