-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preliminary pressure transducer implementation with INA219 (#30)
* 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
1 parent
accdc7a
commit 2e318d9
Showing
6 changed files
with
224 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod pressure_transducer; | ||
pub mod signal_light; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters