From 2c8f1e69894feb082009042d603087ad4ff3a4bc Mon Sep 17 00:00:00 2001 From: "93094049+kraemr@users.noreply.github.com" <93094049+kraemr@users.noreply.github.com> Date: Thu, 29 May 2025 18:12:57 +0200 Subject: [PATCH] refactored get_pressure --- .../extruder1/screw_speed_controller.rs | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/server/src/machines/extruder1/screw_speed_controller.rs b/server/src/machines/extruder1/screw_speed_controller.rs index 76ecf601..c45451ef 100644 --- a/server/src/machines/extruder1/screw_speed_controller.rs +++ b/server/src/machines/extruder1/screw_speed_controller.rs @@ -1,4 +1,4 @@ -use std::time::Instant; +use std::{any, time::Instant}; use control_core::{ actors::{ @@ -8,10 +8,13 @@ use control_core::{ }, controllers::pid::PidController, converters::transmission_converter::TransmissionConverter, + helpers::interpolation::normalize, }; use uom::si::{ angular_velocity::revolution_per_minute, - f64::{AngularVelocity, Frequency, Pressure}, + electric_current::milliampere, + electric_potential::millivolt, + f64::{AngularVelocity, ElectricCurrent, Frequency, Pressure}, frequency::{cycle_per_minute, hertz}, pressure::bar, }; @@ -136,15 +139,34 @@ impl ScrewSpeedController { self.target_pressure } + pub fn get_sensor_current(&self) -> Result { + let phys: ethercat_hal::io::analog_input::physical::AnalogInputValue = self + .pressure_sensor + .get_physical() + .ok_or_else(|| anyhow::anyhow!("no value"))?; + + match phys { + ethercat_hal::io::analog_input::physical::AnalogInputValue::Potential(_) => { + Err(anyhow::anyhow!("Potential is not expected")) + } + ethercat_hal::io::analog_input::physical::AnalogInputValue::Current(quantity) => { + Ok(quantity) + } + } + } + pub fn get_pressure(&mut self) -> Pressure { - let normalized = self.pressure_sensor.get_normalized(); - let normalized = match normalized { - Some(normalized) => normalized, - None => 0.0, + let current_result = self.get_sensor_current(); + let current = match current_result { + Ok(current) => current.get::(), + Err(_) => todo!(), }; - // assuming full scale pressure of 350 bar, which is the max range of our sensors - let pressure: f64 = normalized as f64 * 350.0; - return Pressure::new::(pressure); + let normalized = normalize(current, 4.0, 20.0); + // Our pressure sensor has a range of Up to 350 Bar + + let actual_pressure = (normalized) * 350.0; + + return Pressure::new::(actual_pressure); } pub async fn update(&mut self, now: Instant) {