Skip to content

Commit

Permalink
Add preliminary pressure transducer implementation with INA219 (#30)
Browse files Browse the repository at this point in the history
* Add preliminary function to read from INA219

* Change INA219 base address and calibration value

* Add pressure transducer abstraction

* Add pressure transducer abstraction

* Extract PT demo to new function

* Extract calibration statement to new()

* Extract calibration statement to new()

* Reformatting

* Remove ina219 module, simplify PressureTransducer

* Add mA to PSI scaling factor

* Adjust terminology

* Fix mA to PSI conversion

* Remove unnecessary return statement

* Make read_current private method

* Use From instead of as to convert to f32
  • Loading branch information
samderanova authored May 3, 2024
1 parent accdc7a commit 2e318d9
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 12 deletions.
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"
1 change: 1 addition & 0 deletions pod-operation/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod pressure_transducer;
pub mod signal_light;
57 changes: 57 additions & 0 deletions pod-operation/src/components/pressure_transducer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use ina219::INA219;
use rppal::i2c::I2c;
use tracing::debug;

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

// The calibration value is used to adjust the maximum current measurement
// and precision of measurements.
const INA219_CALIBRATION_VALUE: u16 = 0xffff;

// Even with the calibration values, the readings from the INA219 are not in
// mA. A scaling factor is needed in order to convert the raw reading to mA and
// this is not provided in the INA219 library that we are using. Note that this
// value changes according to the calibration value. The exact formula can be
// found in the INA219 datasheet.
const INA219_SCALING_VALUE: f32 = 160.0;

// The pressure transducer outputs a current between 4 mA and 20 mA with 0 PSI
// and 300 PSI respectively. Assuming a linear interpolation, a 1 mA increase
// results in a 18.75 PSI increase.
const REF_CURRENT_LOW: f32 = 4.0;
const REF_CURRENT_HIGH: f32 = 20.0;
const REF_PRESSURE_LOW: f32 = 0.0;
const REF_PRESSURE_HIGH: f32 = 300.0;

const REF_CURRENT_SPAN: f32 = REF_CURRENT_HIGH - REF_CURRENT_LOW;
const REF_PRESSURE_SPAN: f32 = REF_PRESSURE_HIGH - REF_PRESSURE_LOW;

impl PressureTransducer {
pub fn new(ina219_addr: u8) -> Self {
let device = I2c::new().unwrap();

let mut ina219 = INA219::new(device, ina219_addr);
debug!("Initialized I2C and INA219");

ina219.calibrate(INA219_CALIBRATION_VALUE).unwrap();
debug!("Calibrating INA219");

PressureTransducer { ina: ina219 }
}

// Read current from the INA219 and apply a scaling factor to translate
// the current reading to PSI.
pub fn read(&mut self) -> f32 {
let current = self.read_current();

REF_PRESSURE_LOW + REF_PRESSURE_SPAN * (current - REF_CURRENT_LOW) / REF_CURRENT_SPAN
}

// Read from the INA219 and divide the reading by a scalar factor to
// convert the reading to mA.
fn read_current(&mut self) -> f32 {
f32::from(self.ina.current().unwrap()) / INA219_SCALING_VALUE
}
}
10 changes: 10 additions & 0 deletions pod-operation/src/demo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use tracing::info;

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

pub async fn blink(mut signal_light: SignalLight) {
Expand All @@ -17,3 +18,12 @@ pub async fn blink(mut signal_light: SignalLight) {
i += 1;
}
}

pub async fn read_pressure_transducer(mut pressure_transducer: PressureTransducer) {
info!("Starting pressure transducer demo.");

loop {
tokio::time::sleep(std::time::Duration::new(1, 0)).await;
println!("{:?}", pressure_transducer.read());
}
}
4 changes: 4 additions & 0 deletions pod-operation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod components;
mod demo;
mod handlers;

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

#[tokio::main]
Expand All @@ -23,6 +24,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let signal_light = SignalLight::new();
tokio::spawn(demo::blink(signal_light));

let pressure_transducer = PressureTransducer::new(0x40);
tokio::spawn(demo::read_pressure_transducer(pressure_transducer));

let app = axum::Router::new().layer(layer);

info!("Starting server on port 5000");
Expand Down

0 comments on commit 2e318d9

Please sign in to comment.