From 68bb4f37b71c6216c7d02feaaa15de91743ffad6 Mon Sep 17 00:00:00 2001 From: JamesCouture <71405359+JamesCouture@users.noreply.github.com> Date: Sat, 12 Oct 2024 11:35:45 -0400 Subject: [PATCH 1/4] Added thermocouple --- Cargo.lock | 4 + crates/thermocouple-converter/Cargo.toml | 6 ++ crates/thermocouple-converter/src/lib.rs | 94 ++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 crates/thermocouple-converter/Cargo.toml create mode 100644 crates/thermocouple-converter/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 4a78f38..06078fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1010,6 +1010,10 @@ dependencies = [ "stm32h7xx-hal", ] +[[package]] +name = "thermocouple-converter" +version = "0.1.0" + [[package]] name = "thiserror" version = "1.0.63" diff --git a/crates/thermocouple-converter/Cargo.toml b/crates/thermocouple-converter/Cargo.toml new file mode 100644 index 0000000..bfc88b6 --- /dev/null +++ b/crates/thermocouple-converter/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "thermocouple-converter" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/crates/thermocouple-converter/src/lib.rs b/crates/thermocouple-converter/src/lib.rs new file mode 100644 index 0000000..eb87ec7 --- /dev/null +++ b/crates/thermocouple-converter/src/lib.rs @@ -0,0 +1,94 @@ +#![no_std] +//! +//! This crate contains code to convert type k thermocouple voltages to temperatures. +//! + +///function for power +/// in: base: f64, exp: i32 +/// out: result: f64 +fn pow(base: f64, exp: i32) -> f64 { + let mut result = 1.0; + if exp < 0 { + result = 1.0/pow(base, -exp); + } else { + for _ in 0..exp { + result *= base; + } + } + return result; +} + +///function to calculate the conversion between voltage to celsius from the thermocoupler. +///in: voltage: f64 +///out: celsius: f64 +pub fn voltage_to_celsius(voltage: f64) -> f64 { + + ///Ranges where polynomial coefficients are for + pub const ENERGY_RANGES: [[f64;2];3] = [ + [-5.891, 0.0], + [0.0, 20.644], + [20.644, 54.886] + ]; + + ///Type K thermocouple coefficients for polynomial calculation + pub const TYPE_K_COEF: [[f64; 10]; 3] = [ + [0.0,25.173462,-1.1662878,-1.0833638,-0.89773540,-0.37342377,-0.086632643,-0.010450598,-0.00051920577,0.0], + [0.0,25.08355,0.07860106,-0.2503131,0.08315270,-0.01228034,0.0009804036,-0.00004413030,0.000001057734,-0.00000001052755], + [-131.8058,48.30222,-1.646031,0.05464731,-0.0009650715,0.000008802193,-0.00000003110810,0.0,0.0,0.0] + ]; + + //define variables + let mut result = 0.0; + let mut i = 0; + + //goes through the different ranges + while i < ENERGY_RANGES.len(){ + if voltage >= ENERGY_RANGES[i][0] && voltage <= ENERGY_RANGES[i][1] { + //calculates the result + for k in 0..TYPE_K_COEF[i].len() { + result += TYPE_K_COEF[i][k] * pow(voltage, k as i32); + } + return result; + }else{ + //if the voltage is not in the range, it goes to the next range + i+=1; + } + } + + return -1.0; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn voltage_to_celsius_test1() { + println!("Test 1: {}", voltage_to_celsius(20.644)); + assert!(between(499.97, 500.0, voltage_to_celsius(20.644))); + } + + #[test] + fn voltage_to_celsius_test2() { + println!("Test 2: {}", voltage_to_celsius(6.138)); + assert!(between(150.01, 150.03, voltage_to_celsius(6.138))); + } + + #[test] + fn voltage_to_celsius_test3() { + println!("Test 3: {}", voltage_to_celsius(0.039)); + assert!(between(0.97, 0.98, voltage_to_celsius(0.039))); + } + + #[test] + fn voltage_to_celsius_test4() { + println!("Test 4: {}", voltage_to_celsius(-0.778)); + assert!(between(-20.03, -20.01, voltage_to_celsius(-0.778))); + } + + #[test] + fn voltage_to_celsius_test5() { + println!("Test 5: {}", voltage_to_celsius(10.0)); + assert!(between(246.1, 246.3, voltage_to_celsius(10.0))); + } +} \ No newline at end of file From 653c4299c52db4a70f68a33607742f16dade8e6a Mon Sep 17 00:00:00 2001 From: JamesCouture <71405359+JamesCouture@users.noreply.github.com> Date: Sat, 12 Oct 2024 12:08:01 -0400 Subject: [PATCH 2/4] Update lib.rs --- crates/thermocouple-converter/src/lib.rs | 25 ++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/thermocouple-converter/src/lib.rs b/crates/thermocouple-converter/src/lib.rs index eb87ec7..5bb2a3d 100644 --- a/crates/thermocouple-converter/src/lib.rs +++ b/crates/thermocouple-converter/src/lib.rs @@ -64,31 +64,36 @@ mod tests { #[test] fn voltage_to_celsius_test1() { - println!("Test 1: {}", voltage_to_celsius(20.644)); - assert!(between(499.97, 500.0, voltage_to_celsius(20.644))); + //println!("Test 1: {}", voltage_to_celsius(20.644)); + let result:f64 = voltage_to_celsius(20.644); + assert!(499.97 <= result && 500.0 >= result); } #[test] fn voltage_to_celsius_test2() { - println!("Test 2: {}", voltage_to_celsius(6.138)); - assert!(between(150.01, 150.03, voltage_to_celsius(6.138))); + // println!("Test 2: {}", voltage_to_celsius(6.138)); + let result:f64 = voltage_to_celsius(6.138); + assert!(150.01 <= result && 150.03 >= result); } #[test] fn voltage_to_celsius_test3() { - println!("Test 3: {}", voltage_to_celsius(0.039)); - assert!(between(0.97, 0.98, voltage_to_celsius(0.039))); + // println!("Test 3: {}", voltage_to_celsius(0.039)); + let result:f64 = voltage_to_celsius(0.039); + assert!(0.97 <= result && 0.98 >= result); } #[test] fn voltage_to_celsius_test4() { - println!("Test 4: {}", voltage_to_celsius(-0.778)); - assert!(between(-20.03, -20.01, voltage_to_celsius(-0.778))); + // println!("Test 4: {}", voltage_to_celsius(-0.778)); + let result:f64 = voltage_to_celsius(-0.778); + assert!(-20.03 <= result && -20.01 >= result); } #[test] fn voltage_to_celsius_test5() { - println!("Test 5: {}", voltage_to_celsius(10.0)); - assert!(between(246.1, 246.3, voltage_to_celsius(10.0))); + // println!("Test 5: {}", voltage_to_celsius(10.0)); + let result:f64 = voltage_to_celsius(10.0); + assert!(246.1 <= result && 246.3 >= result); } } \ No newline at end of file From d03130f8241a4eb80f4e03980d07d91c84eb3709 Mon Sep 17 00:00:00 2001 From: Greacko <123205959+Greacko@users.noreply.github.com> Date: Fri, 1 Nov 2024 12:06:01 -0400 Subject: [PATCH 3/4] tried seperating tests to the lib file due to some weird errors --- crates/thermocouple-converter/src/lib.rs | 40 -------------------- crates/thermocouple-converter/tests/test.rs | 41 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 crates/thermocouple-converter/tests/test.rs diff --git a/crates/thermocouple-converter/src/lib.rs b/crates/thermocouple-converter/src/lib.rs index 5bb2a3d..dde29ec 100644 --- a/crates/thermocouple-converter/src/lib.rs +++ b/crates/thermocouple-converter/src/lib.rs @@ -56,44 +56,4 @@ pub fn voltage_to_celsius(voltage: f64) -> f64 { } return -1.0; -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn voltage_to_celsius_test1() { - //println!("Test 1: {}", voltage_to_celsius(20.644)); - let result:f64 = voltage_to_celsius(20.644); - assert!(499.97 <= result && 500.0 >= result); - } - - #[test] - fn voltage_to_celsius_test2() { - // println!("Test 2: {}", voltage_to_celsius(6.138)); - let result:f64 = voltage_to_celsius(6.138); - assert!(150.01 <= result && 150.03 >= result); - } - - #[test] - fn voltage_to_celsius_test3() { - // println!("Test 3: {}", voltage_to_celsius(0.039)); - let result:f64 = voltage_to_celsius(0.039); - assert!(0.97 <= result && 0.98 >= result); - } - - #[test] - fn voltage_to_celsius_test4() { - // println!("Test 4: {}", voltage_to_celsius(-0.778)); - let result:f64 = voltage_to_celsius(-0.778); - assert!(-20.03 <= result && -20.01 >= result); - } - - #[test] - fn voltage_to_celsius_test5() { - // println!("Test 5: {}", voltage_to_celsius(10.0)); - let result:f64 = voltage_to_celsius(10.0); - assert!(246.1 <= result && 246.3 >= result); - } } \ No newline at end of file diff --git a/crates/thermocouple-converter/tests/test.rs b/crates/thermocouple-converter/tests/test.rs new file mode 100644 index 0000000..81ad112 --- /dev/null +++ b/crates/thermocouple-converter/tests/test.rs @@ -0,0 +1,41 @@ +use crate::voltage_to_celsius; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn voltage_to_celsius_test1() { + //println!("Test 1: {}", voltage_to_celsius(20.644)); + let result:f64 = voltage_to_celsius(20.644); + assert!(499.97 <= result && 500.0 >= result); + } + + #[test] + fn voltage_to_celsius_test2() { + // println!("Test 2: {}", voltage_to_celsius(6.138)); + let result:f64 = voltage_to_celsius(6.138); + assert!(150.01 <= result && 150.03 >= result); + } + + #[test] + fn voltage_to_celsius_test3() { + // println!("Test 3: {}", voltage_to_celsius(0.039)); + let result:f64 = voltage_to_celsius(0.039); + assert!(0.97 <= result && 0.98 >= result); + } + + #[test] + fn voltage_to_celsius_test4() { + // println!("Test 4: {}", voltage_to_celsius(-0.778)); + let result:f64 = voltage_to_celsius(-0.778); + assert!(-20.03 <= result && -20.01 >= result); + } + + #[test] + fn voltage_to_celsius_test5() { + // println!("Test 5: {}", voltage_to_celsius(10.0)); + let result:f64 = voltage_to_celsius(10.0); + assert!(246.1 <= result && 246.3 >= result); + } +} \ No newline at end of file From 6981d2b8d101d76ea107a85ba47bcab17e98794b Mon Sep 17 00:00:00 2001 From: Noah Sprenger Date: Sat, 16 Nov 2024 23:31:22 -0500 Subject: [PATCH 4/4] Update thermocouple test and makefile. --- Makefile.toml | 9 ++++- crates/thermocouple-converter/Cargo.toml | 1 - crates/thermocouple-converter/src/lib.rs | 29 +++++++++++++++ crates/thermocouple-converter/tests/test.rs | 41 --------------------- 4 files changed, 36 insertions(+), 44 deletions(-) delete mode 100644 crates/thermocouple-converter/tests/test.rs diff --git a/Makefile.toml b/Makefile.toml index 9812fe0..093d2cb 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -10,7 +10,7 @@ default_to_workspace = false [tasks.test-host] dependencies = [ - # "test-messages", + "test-thermocouple", ] # ----------------------- @@ -39,4 +39,9 @@ args = ["test", "-p", "pressure", "${@}"] [tasks.test-strain-board] command = "cargo" -args = ["test", "-p", "strain", "${@}"] \ No newline at end of file +args = ["test", "-p", "strain", "${@}"] + +[tasks.test-thermocouple] +command = "cargo" +args = ["test", "-p", "thermocouple-converter", "--target", "${CARGO_MAKE_RUST_TARGET_TRIPLE}"] +env = {RUST_MIN_STACK = "8388608"} \ No newline at end of file diff --git a/crates/thermocouple-converter/Cargo.toml b/crates/thermocouple-converter/Cargo.toml index bfc88b6..b9f42db 100644 --- a/crates/thermocouple-converter/Cargo.toml +++ b/crates/thermocouple-converter/Cargo.toml @@ -3,4 +3,3 @@ name = "thermocouple-converter" version = "0.1.0" edition = "2021" -[dependencies] diff --git a/crates/thermocouple-converter/src/lib.rs b/crates/thermocouple-converter/src/lib.rs index dde29ec..5c2e9df 100644 --- a/crates/thermocouple-converter/src/lib.rs +++ b/crates/thermocouple-converter/src/lib.rs @@ -56,4 +56,33 @@ pub fn voltage_to_celsius(voltage: f64) -> f64 { } return -1.0; +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::voltage_to_celsius; + + #[test] + fn voltage_to_celsius_test1() { + //println!("Test 1: {}", voltage_to_celsius(20.644)); + let result:f64 = voltage_to_celsius(20.644); + assert!(499.97 <= result && 500.0 >= result); + + // println!("Test 2: {}", voltage_to_celsius(6.138)); + let result:f64 = voltage_to_celsius(6.138); + assert!(150.01 <= result && 150.03 >= result); + + // println!("Test 3: {}", voltage_to_celsius(0.039)); + let result:f64 = voltage_to_celsius(0.039); + assert!(0.97 <= result && 0.98 >= result); + + // println!("Test 4: {}", voltage_to_celsius(-0.778)); + let result:f64 = voltage_to_celsius(-0.778); + assert!(-20.03 <= result && -20.01 >= result); + + // println!("Test 5: {}", voltage_to_celsius(10.0)); + let result:f64 = voltage_to_celsius(10.0); + assert!(246.1 <= result && 246.3 >= result); + } } \ No newline at end of file diff --git a/crates/thermocouple-converter/tests/test.rs b/crates/thermocouple-converter/tests/test.rs deleted file mode 100644 index 81ad112..0000000 --- a/crates/thermocouple-converter/tests/test.rs +++ /dev/null @@ -1,41 +0,0 @@ -use crate::voltage_to_celsius; - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn voltage_to_celsius_test1() { - //println!("Test 1: {}", voltage_to_celsius(20.644)); - let result:f64 = voltage_to_celsius(20.644); - assert!(499.97 <= result && 500.0 >= result); - } - - #[test] - fn voltage_to_celsius_test2() { - // println!("Test 2: {}", voltage_to_celsius(6.138)); - let result:f64 = voltage_to_celsius(6.138); - assert!(150.01 <= result && 150.03 >= result); - } - - #[test] - fn voltage_to_celsius_test3() { - // println!("Test 3: {}", voltage_to_celsius(0.039)); - let result:f64 = voltage_to_celsius(0.039); - assert!(0.97 <= result && 0.98 >= result); - } - - #[test] - fn voltage_to_celsius_test4() { - // println!("Test 4: {}", voltage_to_celsius(-0.778)); - let result:f64 = voltage_to_celsius(-0.778); - assert!(-20.03 <= result && -20.01 >= result); - } - - #[test] - fn voltage_to_celsius_test5() { - // println!("Test 5: {}", voltage_to_celsius(10.0)); - let result:f64 = voltage_to_celsius(10.0); - assert!(246.1 <= result && 246.3 >= result); - } -} \ No newline at end of file