Fixed resistive divider and thermistor functions
This commit is contained in:
@ -16,7 +16,7 @@ members = [
|
||||
]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.4.5"
|
||||
version = "0.4.6"
|
||||
edition = "2021"
|
||||
repository = "https://git.bfpower.io/BFPOWER/physical"
|
||||
readme = "README.md"
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::quantity::{Ohms, Quantity, Volts};
|
||||
use crate::quantity::{Ohms, Volts};
|
||||
|
||||
/// Given the resistance of the second resistor in a resistive voltage divider, calculate the resistance of the first resistor.
|
||||
pub fn solve_r1(
|
||||
@ -6,7 +6,7 @@ pub fn solve_r1(
|
||||
voltage_read: Volts<f32>,
|
||||
r2: Ohms<f32>,
|
||||
) -> Ohms<f32> {
|
||||
Ohms(r2.value() * (voltage_src.value() / voltage_read.value() - 1.0))
|
||||
Ohms(r2.0 * (voltage_src.0 / voltage_read.0 - 1.0))
|
||||
}
|
||||
|
||||
/// Given the resistance of the first resistor in a resistive voltage divider, calculate the resistance of the second resistor.
|
||||
@ -15,5 +15,29 @@ pub fn solve_r2(
|
||||
voltage_read: Volts<f32>,
|
||||
r1: Ohms<f32>,
|
||||
) -> Ohms<f32> {
|
||||
Ohms(r1.value() * (1.0 / (voltage_src.value() / voltage_read.value()) - 1.0))
|
||||
Ohms((r1.0 * voltage_read.0) / (voltage_src.0 - voltage_read.0))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Tests ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::quantity::{OhmsVal, VoltsVal};
|
||||
use super::*;
|
||||
use float_cmp::assert_approx_eq;
|
||||
|
||||
#[test]
|
||||
fn solve_r1_test() {
|
||||
let resistance = solve_r1(3.3.volts(), 2.0.volts(), 1_000.0.ohms());
|
||||
|
||||
assert_approx_eq!(f32, 650.0, resistance.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solve_r2_test() {
|
||||
let resistance = solve_r2(3.3.volts(), 2.0.volts(), 1_000.0.ohms());
|
||||
|
||||
assert_approx_eq!(f32, 1538.462, resistance.0);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use libm::{log, logf};
|
||||
use crate::quantity::{Kelvins, Ohms};
|
||||
use libm::{log, logf};
|
||||
|
||||
/// Convert thermistor resistance to a temperature using beta parameter equation
|
||||
pub fn convert_beta(
|
||||
@ -8,18 +8,31 @@ pub fn convert_beta(
|
||||
reference_temp: Kelvins<f32>,
|
||||
reference_resist: Ohms<f32>,
|
||||
) -> Kelvins<f32> {
|
||||
let kelvins = 1.0 / ((1.0 / reference_temp.0) + (1.0 / beta) * logf(resistance.0 / reference_resist.0));
|
||||
let kelvins = 1.0 / ((logf(resistance.0 / reference_resist.0) / beta) + 1.0 / reference_temp.0);
|
||||
Kelvins(kelvins)
|
||||
}
|
||||
|
||||
/// Convert thermistor resistance to a temperature using Steinhart-Hart equation
|
||||
pub fn convert_steinhart(
|
||||
resistance: Ohms<f64>,
|
||||
a: f64,
|
||||
b: f64,
|
||||
c: f64,
|
||||
) -> Kelvins<f32> {
|
||||
pub fn convert_steinhart(resistance: Ohms<f64>, a: f64, b: f64, c: f64) -> Kelvins<f32> {
|
||||
let log_omhs = log(resistance.0);
|
||||
let kelvins = 1.0 / (a + b * log_omhs + c * log_omhs * log_omhs * log_omhs);
|
||||
Kelvins(kelvins as f32)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Tests ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use float_cmp::assert_approx_eq;
|
||||
|
||||
use crate::quantity::{OhmsVal, KelvinsVal};
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn convert_beta_test() {
|
||||
let temperature = convert_beta(1538.462.ohms(), 3950.0, 298.15.kelvins(), 100_000.0.ohms());
|
||||
|
||||
assert_approx_eq!(f32, 435.31073, temperature.0);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user