diff --git a/src/transducer/part/lm35.rs b/src/transducer/part/lm35.rs index 0f492da..646cce8 100644 --- a/src/transducer/part/lm35.rs +++ b/src/transducer/part/lm35.rs @@ -1,21 +1,21 @@ use crate::transducer::InvalidValue; use uom::si::electric_potential::volt; -use uom::si::f32; +use uom::si::quantities::{ElectricPotential, ThermodynamicTemperature}; 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] pub fn convert( - voltage: f32::ElectricPotential, -) -> Result { + voltage: ElectricPotential, +) -> Result, InvalidValue> { + const MIN_VOLTS: f32 = -0.55; + const MAX_VOLTS: f32 = 1.50; + const SCALE_FACTOR: f32 = 100.0; + let volts = voltage.get::(); if volts >= MIN_VOLTS && volts <= MAX_VOLTS { let celsius = volts * SCALE_FACTOR; - Ok(f32::ThermodynamicTemperature::new::(celsius)) + Ok(ThermodynamicTemperature::new::(celsius)) } else { Err(InvalidValue) } diff --git a/src/transducer/part/thermocouple/type_k.rs b/src/transducer/part/thermocouple/type_k.rs index 44ff7da..1687050 100644 --- a/src/transducer/part/thermocouple/type_k.rs +++ b/src/transducer/part/thermocouple/type_k.rs @@ -1,12 +1,12 @@ use crate::transducer::InvalidValue; use libm::powf; -use uom::si::electric_potential::{millivolt, volt}; -use uom::si::f32; +use uom::si::electric_potential::millivolt; +use uom::si::quantities::{ElectricPotential, ThermodynamicTemperature}; use uom::si::thermodynamic_temperature::degree_celsius; fn _convert( - voltage: f32::ElectricPotential, -) -> Result { + voltage: ElectricPotential, +) -> Result, InvalidValue> { let mv = voltage.get::(); let mv_pow2 = mv * mv; let mv_pow3 = mv_pow2 * mv; @@ -27,7 +27,7 @@ fn _convert( + -1.0450598E-2 * mv_pow7 + -5.1920577E-4 * mv_pow8; - Ok(f32::ThermodynamicTemperature::new::(celsius)) + Ok(ThermodynamicTemperature::new::(celsius)) } else if mv > 0.0 && mv < 20.644 { let mv_pow7 = mv_pow6 * mv; let mv_pow8 = mv_pow7 * mv; @@ -43,7 +43,7 @@ fn _convert( + 1.057734E-6 * mv_pow8 + -1.052755E-8 * mv_pow9; - Ok(f32::ThermodynamicTemperature::new::(celsius)) + Ok(ThermodynamicTemperature::new::(celsius)) } else if mv >= 20.644 && mv <= 54.886 { let celsius = 1.318058e2 + 4.830222E+1 * mv @@ -53,7 +53,7 @@ fn _convert( + 8.802193E-6 * mv_pow5 + -3.110810E-8 * mv_pow6; - Ok(f32::ThermodynamicTemperature::new::(celsius)) + Ok(ThermodynamicTemperature::new::(celsius)) } else { 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). #[inline] pub fn convert_direct( - voltage: f32::ElectricPotential, - r_junction: f32::ThermodynamicTemperature, -) -> Result { + voltage: ElectricPotential, + r_junction: ThermodynamicTemperature, +) -> Result, InvalidValue> { let base_celsius = _convert(voltage)?.get::(); let r_junction_celsius = r_junction.get::(); - Ok(f32::ThermodynamicTemperature::new::(base_celsius + r_junction_celsius)) + Ok(ThermodynamicTemperature::new::(base_celsius + r_junction_celsius)) } /// 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). #[inline] pub fn convert_seebeck( - voltage: f32::ElectricPotential, - r_junction: f32::ThermodynamicTemperature, -) -> Result { + voltage: ElectricPotential, + r_junction: ThermodynamicTemperature, +) -> Result, InvalidValue> { let voltage_correction = temp_to_voltage_seebeck(r_junction)?; _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). #[inline] pub fn convert_polynomial( - voltage: f32::ElectricPotential, - r_junction: f32::ThermodynamicTemperature, -) -> Result { + voltage: ElectricPotential, + r_junction: ThermodynamicTemperature, +) -> Result, InvalidValue> { let voltage_correction = temp_to_voltage_poly(r_junction)?; _convert(voltage + voltage_correction) } //TODO: This is not working, check libm pow. pub fn temp_to_voltage_poly( - temperature: f32::ThermodynamicTemperature, -) -> Result { + temperature: ThermodynamicTemperature, +) -> Result, InvalidValue> { let celsius = temperature.get::(); let cel_pow2 = celsius * celsius; let cel_pow3 = cel_pow2 * celsius; @@ -135,7 +135,7 @@ pub fn temp_to_voltage_poly( + -0.198892668780E-19 * cel_pow9 + -0.163226974860E-22 * cel_pow10; - Ok(f32::ElectricPotential::new::(mv)) + Ok(ElectricPotential::new::(mv)) } else if celsius >= 0.0 && celsius <= 1372.0 { let base = celsius - 0.126968600000E+03; let exp = -0.118343200000E-03 * (base * base); @@ -153,7 +153,7 @@ pub fn temp_to_voltage_poly( + -0.121047212750E-25 * cel_pow9 + addition; - Ok(f32::ElectricPotential::new::(mv)) + Ok(ElectricPotential::new::(mv)) } else { Err(InvalidValue) } @@ -161,12 +161,12 @@ pub fn temp_to_voltage_poly( #[inline] pub fn temp_to_voltage_seebeck( - temperature: f32::ThermodynamicTemperature, -) -> Result { + temperature: ThermodynamicTemperature, +) -> Result, InvalidValue> { let celsius = temperature.get::(); if celsius >= -2.0 && celsius <= 800.0 { let mv = 0.041 * celsius; - Ok(f32::ElectricPotential::new::(mv)) + Ok(ElectricPotential::new::(mv)) } else { Err(InvalidValue) }