Switched to preferred calling syntax for UoM. #11

Merged
zack merged 1 commits from uom-style into master 2024-02-23 18:24:07 +00:00
2 changed files with 32 additions and 32 deletions

View File

@ -1,21 +1,21 @@
use crate::transducer::InvalidValue; use crate::transducer::InvalidValue;
use uom::si::electric_potential::volt; use uom::si::electric_potential::volt;
use uom::si::f32; use uom::si::quantities::{ElectricPotential, ThermodynamicTemperature};
use uom::si::thermodynamic_temperature::degree_celsius; use uom::si::thermodynamic_temperature::degree_celsius;
const MIN_VOLTS: f32 = -0.55;
const MAX_VOLTS: f32 = 1.50;
const SCALE_FACTOR: f32 = 100.0;
#[inline] #[inline]
pub fn convert( pub fn convert(
voltage: f32::ElectricPotential, voltage: ElectricPotential<f32>,
) -> Result<f32::ThermodynamicTemperature, InvalidValue> { ) -> Result<ThermodynamicTemperature<f32>, InvalidValue> {
const MIN_VOLTS: f32 = -0.55;
const MAX_VOLTS: f32 = 1.50;
const SCALE_FACTOR: f32 = 100.0;
let volts = voltage.get::<volt>(); let volts = voltage.get::<volt>();
if volts >= MIN_VOLTS && volts <= MAX_VOLTS { if volts >= MIN_VOLTS && volts <= MAX_VOLTS {
let celsius = volts * SCALE_FACTOR; let celsius = volts * SCALE_FACTOR;
Ok(f32::ThermodynamicTemperature::new::<degree_celsius>(celsius)) Ok(ThermodynamicTemperature::new::<degree_celsius>(celsius))
} else { } else {
Err(InvalidValue) Err(InvalidValue)
} }

View File

@ -1,12 +1,12 @@
use crate::transducer::InvalidValue; use crate::transducer::InvalidValue;
use libm::powf; use libm::powf;
use uom::si::electric_potential::{millivolt, volt}; use uom::si::electric_potential::millivolt;
use uom::si::f32; use uom::si::quantities::{ElectricPotential, ThermodynamicTemperature};
use uom::si::thermodynamic_temperature::degree_celsius; use uom::si::thermodynamic_temperature::degree_celsius;
fn _convert( fn _convert(
voltage: f32::ElectricPotential, voltage: ElectricPotential<f32>,
) -> Result<f32::ThermodynamicTemperature, InvalidValue> { ) -> Result<ThermodynamicTemperature<f32>, InvalidValue> {
let mv = voltage.get::<millivolt>(); let mv = voltage.get::<millivolt>();
let mv_pow2 = mv * mv; let mv_pow2 = mv * mv;
let mv_pow3 = mv_pow2 * mv; let mv_pow3 = mv_pow2 * mv;
@ -27,7 +27,7 @@ fn _convert(
+ -1.0450598E-2 * mv_pow7 + -1.0450598E-2 * mv_pow7
+ -5.1920577E-4 * mv_pow8; + -5.1920577E-4 * mv_pow8;
Ok(f32::ThermodynamicTemperature::new::<degree_celsius>(celsius)) Ok(ThermodynamicTemperature::new::<degree_celsius>(celsius))
} else if mv > 0.0 && mv < 20.644 { } else if mv > 0.0 && mv < 20.644 {
let mv_pow7 = mv_pow6 * mv; let mv_pow7 = mv_pow6 * mv;
let mv_pow8 = mv_pow7 * mv; let mv_pow8 = mv_pow7 * mv;
@ -43,7 +43,7 @@ fn _convert(
+ 1.057734E-6 * mv_pow8 + 1.057734E-6 * mv_pow8
+ -1.052755E-8 * mv_pow9; + -1.052755E-8 * mv_pow9;
Ok(f32::ThermodynamicTemperature::new::<degree_celsius>(celsius)) Ok(ThermodynamicTemperature::new::<degree_celsius>(celsius))
} else if mv >= 20.644 && mv <= 54.886 { } else if mv >= 20.644 && mv <= 54.886 {
let celsius = 1.318058e2 let celsius = 1.318058e2
+ 4.830222E+1 * mv + 4.830222E+1 * mv
@ -53,7 +53,7 @@ fn _convert(
+ 8.802193E-6 * mv_pow5 + 8.802193E-6 * mv_pow5
+ -3.110810E-8 * mv_pow6; + -3.110810E-8 * mv_pow6;
Ok(f32::ThermodynamicTemperature::new::<degree_celsius>(celsius)) Ok(ThermodynamicTemperature::new::<degree_celsius>(celsius))
} else { } else {
Err(InvalidValue) Err(InvalidValue)
} }
@ -68,13 +68,13 @@ fn _convert(
/// This function uses the [NIST type K thermocouple linearisation polynomial](https://srdata.nist.gov/its90/type_k/kcoefficients_inverse.html). /// This function uses the [NIST type K thermocouple linearisation polynomial](https://srdata.nist.gov/its90/type_k/kcoefficients_inverse.html).
#[inline] #[inline]
pub fn convert_direct( pub fn convert_direct(
voltage: f32::ElectricPotential, voltage: ElectricPotential<f32>,
r_junction: f32::ThermodynamicTemperature, r_junction: ThermodynamicTemperature<f32>,
) -> Result<f32::ThermodynamicTemperature, InvalidValue> { ) -> Result<ThermodynamicTemperature<f32>, InvalidValue> {
let base_celsius = _convert(voltage)?.get::<degree_celsius>(); let base_celsius = _convert(voltage)?.get::<degree_celsius>();
let r_junction_celsius = r_junction.get::<degree_celsius>(); let r_junction_celsius = r_junction.get::<degree_celsius>();
Ok(f32::ThermodynamicTemperature::new::<degree_celsius>(base_celsius + r_junction_celsius)) Ok(ThermodynamicTemperature::new::<degree_celsius>(base_celsius + r_junction_celsius))
} }
/// Convert from a voltage produced by a type k thermocouple to a temperature using polynomial and /// Convert from a voltage produced by a type k thermocouple to a temperature using polynomial and
@ -85,9 +85,9 @@ pub fn convert_direct(
/// This function uses the [NIST type K thermocouple linearisation polynomial](https://srdata.nist.gov/its90/type_k/kcoefficients_inverse.html). /// This function uses the [NIST type K thermocouple linearisation polynomial](https://srdata.nist.gov/its90/type_k/kcoefficients_inverse.html).
#[inline] #[inline]
pub fn convert_seebeck( pub fn convert_seebeck(
voltage: f32::ElectricPotential, voltage: ElectricPotential<f32>,
r_junction: f32::ThermodynamicTemperature, r_junction: ThermodynamicTemperature<f32>,
) -> Result<f32::ThermodynamicTemperature, InvalidValue> { ) -> Result<ThermodynamicTemperature<f32>, InvalidValue> {
let voltage_correction = temp_to_voltage_seebeck(r_junction)?; let voltage_correction = temp_to_voltage_seebeck(r_junction)?;
_convert(voltage + voltage_correction) _convert(voltage + voltage_correction)
} }
@ -100,17 +100,17 @@ pub fn convert_seebeck(
/// This function uses the [NIST type K thermocouple linearisation polynomial](https://srdata.nist.gov/its90/type_k/kcoefficients_inverse.html). /// This function uses the [NIST type K thermocouple linearisation polynomial](https://srdata.nist.gov/its90/type_k/kcoefficients_inverse.html).
#[inline] #[inline]
pub fn convert_polynomial( pub fn convert_polynomial(
voltage: f32::ElectricPotential, voltage: ElectricPotential<f32>,
r_junction: f32::ThermodynamicTemperature, r_junction: ThermodynamicTemperature<f32>,
) -> Result<f32::ThermodynamicTemperature, InvalidValue> { ) -> Result<ThermodynamicTemperature<f32>, InvalidValue> {
let voltage_correction = temp_to_voltage_poly(r_junction)?; let voltage_correction = temp_to_voltage_poly(r_junction)?;
_convert(voltage + voltage_correction) _convert(voltage + voltage_correction)
} }
//TODO: This is not working, check libm pow. //TODO: This is not working, check libm pow.
pub fn temp_to_voltage_poly( pub fn temp_to_voltage_poly(
temperature: f32::ThermodynamicTemperature, temperature: ThermodynamicTemperature<f32>,
) -> Result<f32::ElectricPotential, InvalidValue> { ) -> Result<ElectricPotential<f32>, InvalidValue> {
let celsius = temperature.get::<degree_celsius>(); let celsius = temperature.get::<degree_celsius>();
let cel_pow2 = celsius * celsius; let cel_pow2 = celsius * celsius;
let cel_pow3 = cel_pow2 * celsius; let cel_pow3 = cel_pow2 * celsius;
@ -135,7 +135,7 @@ pub fn temp_to_voltage_poly(
+ -0.198892668780E-19 * cel_pow9 + -0.198892668780E-19 * cel_pow9
+ -0.163226974860E-22 * cel_pow10; + -0.163226974860E-22 * cel_pow10;
Ok(f32::ElectricPotential::new::<millivolt>(mv)) Ok(ElectricPotential::new::<millivolt>(mv))
} else if celsius >= 0.0 && celsius <= 1372.0 { } else if celsius >= 0.0 && celsius <= 1372.0 {
let base = celsius - 0.126968600000E+03; let base = celsius - 0.126968600000E+03;
let exp = -0.118343200000E-03 * (base * base); let exp = -0.118343200000E-03 * (base * base);
@ -153,7 +153,7 @@ pub fn temp_to_voltage_poly(
+ -0.121047212750E-25 * cel_pow9 + -0.121047212750E-25 * cel_pow9
+ addition; + addition;
Ok(f32::ElectricPotential::new::<millivolt>(mv)) Ok(ElectricPotential::new::<millivolt>(mv))
} else { } else {
Err(InvalidValue) Err(InvalidValue)
} }
@ -161,12 +161,12 @@ pub fn temp_to_voltage_poly(
#[inline] #[inline]
pub fn temp_to_voltage_seebeck( pub fn temp_to_voltage_seebeck(
temperature: f32::ThermodynamicTemperature, temperature: ThermodynamicTemperature<f32>,
) -> Result<f32::ElectricPotential, InvalidValue> { ) -> Result<ElectricPotential<f32>, InvalidValue> {
let celsius = temperature.get::<degree_celsius>(); let celsius = temperature.get::<degree_celsius>();
if celsius >= -2.0 && celsius <= 800.0 { if celsius >= -2.0 && celsius <= 800.0 {
let mv = 0.041 * celsius; let mv = 0.041 * celsius;
Ok(f32::ElectricPotential::new::<millivolt>(mv)) Ok(ElectricPotential::new::<millivolt>(mv))
} else { } else {
Err(InvalidValue) Err(InvalidValue)
} }