Change DegreesCelsius to Celsius

Version bump
This commit is contained in:
Zachary Sunforge
2024-10-22 20:56:59 -07:00
parent 0212119232
commit 60c9bbfee4
5 changed files with 42 additions and 42 deletions

View File

@ -11,7 +11,7 @@ members = [
] ]
[workspace.package] [workspace.package]
version = "0.3.7" version = "0.3.8"
edition = "2021" edition = "2021"
repository = "https://git.bfpower.io/BFPOWER/physical" repository = "https://git.bfpower.io/BFPOWER/physical"
readme = "README.md" readme = "README.md"

View File

@ -1,7 +1,7 @@
# Before upgrading check that everything is available on all tier1 targets here: # Before upgrading check that everything is available on all tier1 targets here:
# https://rust-lang.github.io/rustup-components-history # https://rust-lang.github.io/rustup-components-history
[toolchain] [toolchain]
channel = "1.75" channel = "1.81"
components = [ "rust-src", "rustfmt", "llvm-tools" ] components = [ "rust-src", "rustfmt", "llvm-tools" ]
targets = [ targets = [
"thumbv7em-none-eabi", "thumbv7em-none-eabi",

View File

@ -9,10 +9,10 @@ quantity_type! {Kelvins, "K"}
impl <V: Value> Kelvins<V> { impl <V: Value> Kelvins<V> {
#[inline] #[inline]
pub fn to_degrees_celsius(self) -> DegreesCelsius<V> { pub fn to_celsius(self) -> Celsius<V> {
//TODO: Make const //TODO: Make const
let offset = V::from_f64(KELVIN_CELSIUS_OFFSET).unwrap(); let offset = V::from_f64(KELVIN_CELSIUS_OFFSET).unwrap();
DegreesCelsius(self.0 - offset) Celsius(self.0 - offset)
} }
#[inline] #[inline]
@ -34,9 +34,9 @@ impl<V: Value> DeciKelvins<V> {
} }
//----- Degrees Celsius ---------------------------------- //----- Degrees Celsius ----------------------------------
quantity_type! {DegreesCelsius, ""} quantity_type! {Celsius, ""}
impl <V: Value> DegreesCelsius<V> { impl <V: Value> Celsius<V> {
#[inline] #[inline]
pub fn to_kelvins(self) -> Kelvins<V> { pub fn to_kelvins(self) -> Kelvins<V> {
//TODO: Make const //TODO: Make const
@ -45,20 +45,20 @@ impl <V: Value> DegreesCelsius<V> {
} }
#[inline] #[inline]
pub fn to_deci_degrees_celsius(self) -> DeciDegreesCelsius<V> { pub fn to_deci_celsius(self) -> DeciCelsius<V> {
let multiplier = V::from_u8(DECI).unwrap(); let multiplier = V::from_u8(DECI).unwrap();
DeciDegreesCelsius(self.0 * multiplier) DeciCelsius(self.0 * multiplier)
} }
} }
//----- Deci Degrees Celsius ---------------------------------- //----- Deci Degrees Celsius ----------------------------------
quantity_type! {DeciDegreesCelsius, "d℃"} quantity_type! {DeciCelsius, "d℃"}
impl<V: Value> DeciDegreesCelsius<V> { impl<V: Value> DeciCelsius<V> {
#[inline] #[inline]
pub fn to_degrees_celsius(self) -> DegreesCelsius<V> { pub fn to_celsius(self) -> Celsius<V> {
let divisor = V::from_u8(DECI).unwrap(); let divisor = V::from_u8(DECI).unwrap();
DegreesCelsius(self.0 / divisor) Celsius(self.0 / divisor)
} }
} }
@ -74,24 +74,24 @@ mod tests {
fn convert_f32() { fn convert_f32() {
let kelvins: Kelvins<f32> = 298.15.kelvins(); let kelvins: Kelvins<f32> = 298.15.kelvins();
let deci_kelvins: DeciKelvins<f32> = 2_981.5.deci_kelvins(); let deci_kelvins: DeciKelvins<f32> = 2_981.5.deci_kelvins();
let celsius: DegreesCelsius<f32> = 25.0.degrees_celsius(); let celsius: Celsius<f32> = 25.0.celsius();
let deci_celsius: DeciDegreesCelsius<f32> = 250.0.deci_degrees_celsius(); let deci_celsius: DeciCelsius<f32> = 250.0.deci_celsius();
assert_approx_eq!(f32, kelvins.to_degrees_celsius().0, celsius.0); assert_approx_eq!(f32, kelvins.to_celsius().0, celsius.0);
assert_approx_eq!(f32, celsius.to_kelvins().0, kelvins.0); assert_approx_eq!(f32, celsius.to_kelvins().0, kelvins.0);
assert_approx_eq!(f32, deci_kelvins.to_kelvins().0, kelvins.0); assert_approx_eq!(f32, deci_kelvins.to_kelvins().0, kelvins.0);
assert_approx_eq!(f32, kelvins.to_deci_kelvins().0, deci_kelvins.0); assert_approx_eq!(f32, kelvins.to_deci_kelvins().0, deci_kelvins.0);
assert_approx_eq!(f32, deci_celsius.to_degrees_celsius().0, celsius.0); assert_approx_eq!(f32, deci_celsius.to_celsius().0, celsius.0);
assert_approx_eq!(f32, celsius.to_deci_degrees_celsius().0, deci_celsius.0); assert_approx_eq!(f32, celsius.to_deci_celsius().0, deci_celsius.0);
} }
#[test] #[test]
fn convert_u16() { fn convert_u16() {
let kelvins: Kelvins<u16> = 298.kelvins(); let kelvins: Kelvins<u16> = 298.kelvins();
let degrees_celsius: DegreesCelsius<u16> = 25.degrees_celsius(); let celsius: Celsius<u16> = 25.celsius();
assert_eq!(degrees_celsius.0, kelvins.to_degrees_celsius().0); assert_eq!(celsius.0, kelvins.to_celsius().0);
} }
} }

View File

@ -1,15 +1,15 @@
use crate::error::InvalidValue; use crate::error::InvalidValue;
use crate::quantity::{DeciDegreesCelsius, MilliVolts, Quantity}; use crate::quantity::{DeciCelsius, MilliVolts, Quantity};
#[inline] #[inline]
pub fn convert( pub fn convert(
voltage: MilliVolts<i16>, voltage: MilliVolts<i16>,
) -> Result<DeciDegreesCelsius<i16>, InvalidValue> { ) -> Result<DeciCelsius<i16>, InvalidValue> {
const MIN_VOLTAGE: MilliVolts<i16> = MilliVolts(-550); const MIN_VOLTAGE: MilliVolts<i16> = MilliVolts(-550);
const MAX_VOLTAGE: MilliVolts<i16> = MilliVolts(1_500); const MAX_VOLTAGE: MilliVolts<i16> = MilliVolts(1_500);
if voltage >= MIN_VOLTAGE && voltage <= MAX_VOLTAGE { if voltage >= MIN_VOLTAGE && voltage <= MAX_VOLTAGE {
Ok(DeciDegreesCelsius(voltage.value())) Ok(DeciCelsius(voltage.value()))
} else { } else {
Err(InvalidValue) Err(InvalidValue)
} }

View File

@ -2,11 +2,11 @@
use libm::pow; use libm::pow;
use crate::error::InvalidValue; use crate::error::InvalidValue;
use crate::quantity::{DegreesCelsius, MilliVolts, Quantity}; use crate::quantity::{Celsius, MilliVolts, Quantity};
fn _convert( fn _convert(
voltage: MilliVolts<f64>, voltage: MilliVolts<f64>,
) -> Result<DegreesCelsius<f32>, InvalidValue> { ) -> Result<Celsius<f32>, InvalidValue> {
let mv = voltage.value(); let mv = voltage.value();
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(DegreesCelsius(celsius as f32)) Ok(Celsius(celsius as f32))
} 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(DegreesCelsius(celsius as f32)) Ok(Celsius(celsius as f32))
} 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(DegreesCelsius(celsius as f32)) Ok(Celsius(celsius as f32))
} else { } else {
Err(InvalidValue) Err(InvalidValue)
} }
@ -69,8 +69,8 @@ fn _convert(
#[inline] #[inline]
pub fn convert_direct( pub fn convert_direct(
voltage: MilliVolts<f64>, voltage: MilliVolts<f64>,
r_junction: DegreesCelsius<f32>, r_junction: Celsius<f32>,
) -> Result<DegreesCelsius<f32>, InvalidValue> { ) -> Result<Celsius<f32>, InvalidValue> {
let base_temp = _convert(voltage)?; let base_temp = _convert(voltage)?;
Ok(base_temp + r_junction) Ok(base_temp + r_junction)
@ -85,8 +85,8 @@ pub fn convert_direct(
#[inline] #[inline]
pub fn convert_seebeck( pub fn convert_seebeck(
voltage: MilliVolts<f64>, voltage: MilliVolts<f64>,
r_junction: DegreesCelsius<f32>, r_junction: Celsius<f32>,
) -> Result<DegreesCelsius<f32>, InvalidValue> { ) -> Result<Celsius<f32>, InvalidValue> {
let voltage_correction = temp_to_voltage_seebeck(r_junction)?; let voltage_correction = temp_to_voltage_seebeck(r_junction)?;
_convert(MilliVolts(voltage.0 + voltage_correction.0 as f64)) _convert(MilliVolts(voltage.0 + voltage_correction.0 as f64))
} }
@ -100,14 +100,14 @@ pub fn convert_seebeck(
#[inline] #[inline]
pub fn convert_polynomial( pub fn convert_polynomial(
voltage: MilliVolts<f64>, voltage: MilliVolts<f64>,
r_junction: DegreesCelsius<f64>, r_junction: Celsius<f64>,
) -> Result<DegreesCelsius<f32>, InvalidValue> { ) -> Result<Celsius<f32>, InvalidValue> {
let voltage_correction = temp_to_voltage_poly(r_junction)?; let voltage_correction = temp_to_voltage_poly(r_junction)?;
_convert(MilliVolts(voltage.0 + voltage_correction.0 as f64)) _convert(MilliVolts(voltage.0 + voltage_correction.0 as f64))
} }
pub fn temp_to_voltage_poly( pub fn temp_to_voltage_poly(
temperature: DegreesCelsius<f64>, temperature: Celsius<f64>,
) -> Result<MilliVolts<f32>, InvalidValue> { ) -> Result<MilliVolts<f32>, InvalidValue> {
let celsius = temperature.value(); let celsius = temperature.value();
let cel_pow2 = celsius * celsius; let cel_pow2 = celsius * celsius;
@ -159,7 +159,7 @@ pub fn temp_to_voltage_poly(
#[inline] #[inline]
pub fn temp_to_voltage_seebeck( pub fn temp_to_voltage_seebeck(
temperature: DegreesCelsius<f32>, temperature: Celsius<f32>,
) -> Result<MilliVolts<f32>, InvalidValue> { ) -> Result<MilliVolts<f32>, InvalidValue> {
if temperature.value() >= -2.0 && temperature.value() <= 800.0 { if temperature.value() >= -2.0 && temperature.value() <= 800.0 {
Ok(MilliVolts(0.041 * temperature.value())) Ok(MilliVolts(0.041 * temperature.value()))