Skip to content

Commit

Permalink
increase resolution for temperature calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain-za committed Jan 1, 2025
1 parent deb565e commit bf3e9e0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
18 changes: 11 additions & 7 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ impl Board {
timer_service,
)
.expect("Failed to create async wifi");
block_on(Self::connect_wifi(&mut wifi)).expect("Failed to connect wifi");
let ip_info = wifi
.wifi()
.sta_netif()
.get_ip_info()
.expect("Failed to get IP info");
log::info!("Wifi DHCP info: {:?}", ip_info);
match block_on(Self::connect_wifi(&mut wifi)) {
Ok(_) => {
let ip_info = wifi
.wifi()
.sta_netif()
.get_ip_info()
.expect("Failed to get IP info");
log::info!("Wifi DHCP info: {:?}", ip_info);
}
Err(e) => log::error!("Failed to connect wifi: {:?}", e),
}
core::mem::forget(wifi);

log::info!("Setting up switches");
Expand Down
8 changes: 4 additions & 4 deletions src/gpio/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Adc<
next_poll: Instant,
samples: Vec<(u16, u16)>,
samples_to_average: usize,
last_reading: (f32, f32),
last_reading: (f64, f64),
}

impl<'a, T, P, M, N> Adc<'a, T, P, M, N>
Expand All @@ -50,7 +50,7 @@ where
}
}

pub fn read(&mut self) -> Option<(f32, f32)> {
pub fn read(&mut self) -> Option<(f64, f64)> {
let raw_temperature = self
.temperature_probe
.read()
Expand All @@ -64,8 +64,8 @@ where
.samples
.iter()
.fold((0, 0), |acc, (t, p)| (acc.0 + *t as u32, acc.1 + *p as u32));
let average_temperature_sample = average_temperature as f32 / self.samples.len() as f32;
let average_pressure_sample = average_pressure as f32 / self.samples.len() as f32;
let average_temperature_sample = average_temperature as f64 / self.samples.len() as f64;
let average_pressure_sample = average_pressure as f64 / self.samples.len() as f64;

self.samples.clear();

Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ fn main() -> Result<()> {

match operational_state {
OperationalState::Idle => {
log::debug!("Boiler temperature: {}", boiler_temperature);
log::debug!("Pump pressure: {}", pump_pressure);
log::debug!("Weight: {}", scale.get_weight());
log::debug!("Flow: {}", scale.get_flow());
log::debug!("Ambient temperature: {}", ambient_temperature);
log::debug!("Ambient temperature: {:.4}", ambient_temperature);
log::debug!("Boiler temperature: {:.4}", boiler_temperature);
log::debug!("Pump pressure: {:.2}", pump_pressure);
log::debug!("Weight: {:.2}", scale.get_weight());
log::debug!("Flow: {:.2}", scale.get_flow());
board.indicator.set_state(indicator::ring::State::Idle);
}
OperationalState::Brewing => {
Expand Down
4 changes: 2 additions & 2 deletions src/sensors/pressure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Storable for SeeedWaterPressureSensor {
}

impl PressureProbe for SeeedWaterPressureSensor {
fn convert_voltage_to_pressure(&self, voltage: f32) -> Result<f32, String> {
Ok((voltage / self.vcc - 0.1) / 0.75)
fn convert_voltage_to_pressure(&self, voltage: f64) -> Result<f32, String> {
Ok(((voltage / self.vcc as f64 - 0.1) / 0.75) as f32)
}
}
11 changes: 6 additions & 5 deletions src/sensors/pt100.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ impl Storable for Pt100 {
}

impl Pt100 {
fn convert_voltage_to_resistance(&self, voltage: f32) -> f32 {
(1800.0 * voltage + self.calibration * 100.0 * 18.0) / (self.calibration * 18.0 - voltage)
fn convert_voltage_to_resistance(&self, voltage: f64) -> f64 {
(1800.0 * voltage + self.calibration as f64 * 100.0 * 18.0)
/ (self.calibration as f64 * 18.0 - voltage)
}
}

impl crate::sensors::traits::TemperatureProbe for Pt100 {
fn convert_voltage_to_degrees(&self, voltage: f32) -> Result<f32, String> {
fn convert_voltage_to_degrees(&self, voltage: f64) -> Result<f32, String> {
let resistance = self.convert_voltage_to_resistance(voltage);
match PT100_TAB.binary_search_by(|&x| {
x.partial_cmp(&resistance)
Expand All @@ -68,13 +69,13 @@ impl crate::sensors::traits::TemperatureProbe for Pt100 {
let r_upper = PT100_TAB[i];

let distance = (resistance - r_lower) / (r_upper - r_lower);
Ok((i - 1) as f32 + distance)
Ok((i - 1) as f32 + distance as f32)
}
}
}
}

const PT100_TAB: [f32; 400] = [
const PT100_TAB: [f64; 400] = [
100.0, 100.39, 100.78, 101.17, 101.56, 101.95, 102.34, 102.73, 103.12, 103.51, 103.9, 104.29,
104.68, 105.07, 105.46, 105.85, 106.24, 106.63, 107.02, 107.4, 107.79, 108.18, 108.57, 108.96,
109.35, 109.73, 110.12, 110.51, 110.9, 111.29, 111.67, 112.06, 112.45, 112.83, 113.22, 113.61,
Expand Down
4 changes: 2 additions & 2 deletions src/sensors/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub trait TemperatureProbe {
fn convert_voltage_to_degrees(&self, voltage: f32) -> Result<f32, String>;
fn convert_voltage_to_degrees(&self, voltage: f64) -> Result<f32, String>;
}
pub trait PressureProbe {
fn convert_voltage_to_pressure(&self, voltage: f32) -> Result<f32, String>;
fn convert_voltage_to_pressure(&self, voltage: f64) -> Result<f32, String>;
}

0 comments on commit bf3e9e0

Please sign in to comment.