Skip to content

Commit

Permalink
Thermocouple Converter (#8)
Browse files Browse the repository at this point in the history
* Added thermocouple

* Update lib.rs

* tried seperating tests to the lib file due to some weird errors

* Update thermocouple test and makefile.

---------

Co-authored-by: JamesCouture <[email protected]>
Co-authored-by: Noah Sprenger <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2024
1 parent d472a9d commit 3f5ead3
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ default_to_workspace = false

[tasks.test-host]
dependencies = [
# "test-messages",
"test-thermocouple",
]

# -----------------------
Expand Down Expand Up @@ -39,4 +39,9 @@ args = ["test", "-p", "pressure", "${@}"]

[tasks.test-strain-board]
command = "cargo"
args = ["test", "-p", "strain", "${@}"]
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"}
5 changes: 5 additions & 0 deletions crates/thermocouple-converter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "thermocouple-converter"
version = "0.1.0"
edition = "2021"

88 changes: 88 additions & 0 deletions crates/thermocouple-converter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#![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::*;

Check warning on line 63 in crates/thermocouple-converter/src/lib.rs

View workflow job for this annotation

GitHub Actions / test

unused import: `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);
}
}

0 comments on commit 3f5ead3

Please sign in to comment.