From c11a70a0b7062e8774ac0b2faaa012b0c920e468 Mon Sep 17 00:00:00 2001 From: Icedsoda <29167146+1cedsoda@users.noreply.github.com> Date: Tue, 27 May 2025 16:22:18 +0200 Subject: [PATCH] Only process io of ethercat devices if they are used by a machine --- control-core/src/machines/new.rs | 4 +- ethercat-hal-derive/src/lib.rs | 12 ++++ ethercat-hal/src/devices/ek1100.rs | 12 ++-- ethercat-hal/src/devices/el1002.rs | 2 + ethercat-hal/src/devices/el1008.rs | 4 +- ethercat-hal/src/devices/el2002.rs | 2 + ethercat-hal/src/devices/el2004.rs | 2 + ethercat-hal/src/devices/el2008.rs | 4 +- ethercat-hal/src/devices/el2024.rs | 4 +- ethercat-hal/src/devices/el2521.rs | 6 +- ethercat-hal/src/devices/el2522.rs | 4 +- ethercat-hal/src/devices/el2634.rs | 4 +- ethercat-hal/src/devices/el2809.rs | 4 +- ethercat-hal/src/devices/el3001.rs | 2 + ethercat-hal/src/devices/el3021.rs | 2 + ethercat-hal/src/devices/el3024.rs | 6 +- ethercat-hal/src/devices/el3204.rs | 2 + ethercat-hal/src/devices/el6021.rs | 4 +- ethercat-hal/src/devices/el7031/mod.rs | 2 + ethercat-hal/src/devices/el7031_0030/mod.rs | 2 + ethercat-hal/src/devices/el7041_0052/mod.rs | 2 + ethercat-hal/src/devices/mod.rs | 17 +++++- server/src/loop.rs | 12 ++++ server/src/machines/extruder1/new.rs | 57 ++++++++++++++---- server/src/machines/winder2/new.rs | 66 +++++++++++++++------ 25 files changed, 187 insertions(+), 51 deletions(-) diff --git a/control-core/src/machines/new.rs b/control-core/src/machines/new.rs index 24a449c8..f972387a 100644 --- a/control-core/src/machines/new.rs +++ b/control-core/src/machines/new.rs @@ -2,7 +2,9 @@ use crate::serial::SerialDevice; use super::identification::DeviceIdentificationIdentified; use anyhow::Error; -use ethercat_hal::{devices::EthercatDevice, helpers::ethercrab_types::EthercrabSubDevicePreoperational}; +use ethercat_hal::{ + devices::EthercatDevice, helpers::ethercrab_types::EthercrabSubDevicePreoperational, +}; use ethercrab::{SubDevice, SubDeviceRef}; use smol::lock::RwLock; use std::sync::Arc; diff --git a/ethercat-hal-derive/src/lib.rs b/ethercat-hal-derive/src/lib.rs index ebf4b5ba..9f74cc55 100644 --- a/ethercat-hal-derive/src/lib.rs +++ b/ethercat-hal-derive/src/lib.rs @@ -254,6 +254,18 @@ pub fn ethercat_device_derive(input: TokenStream) -> TokenStream { self } } + + impl crate::devices::EthercatDeviceUsed for #name { + #[doc="Implemented by the ethercat_hal_derive::EthercatDevice derive macro"] + fn is_used(&self) -> bool { + self.is_used + } + + #[doc="Implemented by the ethercat_hal_derive::EthercatDevice derive macro"] + fn set_used(&mut self, used: bool) { + self.is_used = used; + } + } }; TokenStream::from(expanded) diff --git a/ethercat-hal/src/devices/ek1100.rs b/ethercat-hal/src/devices/ek1100.rs index 55f02d56..726c8b2b 100644 --- a/ethercat-hal/src/devices/ek1100.rs +++ b/ethercat-hal/src/devices/ek1100.rs @@ -3,13 +3,15 @@ use ethercat_hal_derive::EthercatDevice; /// EK1100 bus coupler #[derive(Clone, EthercatDevice)] -pub struct EK1100 {} +pub struct EK1100 { + is_used: bool, +} impl EthercatDeviceProcessing for EK1100 {} impl NewEthercatDevice for EK1100 { fn new() -> Self { - Self {} + Self { is_used: false } } } @@ -19,12 +21,6 @@ impl std::fmt::Debug for EK1100 { } } -impl EK1100 { - pub fn new() -> Self { - Self {} - } -} - pub const EK1100_VENDOR_ID: u32 = 0x2; pub const EK1100_PRODUCT_ID: u32 = 0x044c2c52; pub const EK1100_REVISION_A: u32 = 0x00120000; diff --git a/ethercat-hal/src/devices/el1002.rs b/ethercat-hal/src/devices/el1002.rs index c8a30cd1..3cb0d6db 100644 --- a/ethercat-hal/src/devices/el1002.rs +++ b/ethercat-hal/src/devices/el1002.rs @@ -9,6 +9,7 @@ use ethercat_hal_derive::{EthercatDevice, TxPdo}; #[derive(Clone, EthercatDevice)] pub struct EL1002 { pub txpdo: EL1002TxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL1002 {} @@ -23,6 +24,7 @@ impl NewEthercatDevice for EL1002 { fn new() -> Self { Self { txpdo: EL1002TxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el1008.rs b/ethercat-hal/src/devices/el1008.rs index 65ec22c4..5b5f6866 100644 --- a/ethercat-hal/src/devices/el1008.rs +++ b/ethercat-hal/src/devices/el1008.rs @@ -1,7 +1,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice, SubDeviceIdentityTuple}; +use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use crate::io::digital_input::{DigitalInputDevice, DigitalInputInput, DigitalInputState}; use crate::pdo::{PredefinedPdoAssignment, TxPdo, basic::BoolPdoObject}; -use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use ethercat_hal_derive::{EthercatDevice, TxPdo}; /// EL1008 8-channel digital input device @@ -10,6 +10,7 @@ use ethercat_hal_derive::{EthercatDevice, TxPdo}; #[derive(Clone, EthercatDevice)] pub struct EL1008 { pub txpdo: EL1008TxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL1008 {} @@ -24,6 +25,7 @@ impl NewEthercatDevice for EL1008 { fn new() -> Self { Self { txpdo: EL1008TxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el2002.rs b/ethercat-hal/src/devices/el2002.rs index dd451c7e..0f6a6fc8 100644 --- a/ethercat-hal/src/devices/el2002.rs +++ b/ethercat-hal/src/devices/el2002.rs @@ -10,6 +10,7 @@ use ethercat_hal_derive::{EthercatDevice, RxPdo}; #[derive(EthercatDevice)] pub struct EL2002 { pub rxpdo: EL2002RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL2002 {} @@ -24,6 +25,7 @@ impl NewEthercatDevice for EL2002 { fn new() -> Self { Self { rxpdo: EL2002RxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el2004.rs b/ethercat-hal/src/devices/el2004.rs index 4e998354..7da112d0 100644 --- a/ethercat-hal/src/devices/el2004.rs +++ b/ethercat-hal/src/devices/el2004.rs @@ -8,6 +8,7 @@ use ethercat_hal_derive::{EthercatDevice, RxPdo}; #[derive(EthercatDevice)] pub struct EL2004 { pub rxpdo: EL2004RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL2004 {} @@ -22,6 +23,7 @@ impl NewEthercatDevice for EL2004 { fn new() -> Self { Self { rxpdo: EL2004RxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el2008.rs b/ethercat-hal/src/devices/el2008.rs index 550edaaa..604c3a5b 100644 --- a/ethercat-hal/src/devices/el2008.rs +++ b/ethercat-hal/src/devices/el2008.rs @@ -1,7 +1,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice, SubDeviceIdentityTuple}; +use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use crate::io::digital_output::{DigitalOutputDevice, DigitalOutputOutput, DigitalOutputState}; use crate::pdo::{RxPdo, basic::BoolPdoObject}; -use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use ethercat_hal_derive::{EthercatDevice, RxPdo}; /// EL2008 8-channel digital output device @@ -10,6 +10,7 @@ use ethercat_hal_derive::{EthercatDevice, RxPdo}; #[derive(EthercatDevice)] pub struct EL2008 { pub rxpdo: EL2008RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL2008 {} @@ -24,6 +25,7 @@ impl NewEthercatDevice for EL2008 { fn new() -> Self { Self { rxpdo: EL2008RxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el2024.rs b/ethercat-hal/src/devices/el2024.rs index ac72d37e..f38e0cdd 100644 --- a/ethercat-hal/src/devices/el2024.rs +++ b/ethercat-hal/src/devices/el2024.rs @@ -1,6 +1,6 @@ +use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use crate::io::digital_output::{DigitalOutputDevice, DigitalOutputOutput, DigitalOutputState}; use crate::pdo::{RxPdo, basic::BoolPdoObject}; -use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use ethercat_hal_derive::{EthercatDevice, RxPdo}; use super::{EthercatDeviceProcessing, NewEthercatDevice}; @@ -11,6 +11,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice}; #[derive(EthercatDevice)] pub struct EL2024 { pub rxpdo: EL2024RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL2024 {} @@ -25,6 +26,7 @@ impl NewEthercatDevice for EL2024 { fn new() -> Self { Self { rxpdo: EL2024RxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el2521.rs b/ethercat-hal/src/devices/el2521.rs index 24a05fd2..0f7648fc 100644 --- a/ethercat-hal/src/devices/el2521.rs +++ b/ethercat-hal/src/devices/el2521.rs @@ -1,15 +1,15 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice, SubDeviceIdentityTuple}; use crate::{ coe::{ConfigurableDevice, Configuration}, + helpers::ethercrab_types::EthercrabSubDevicePreoperational, io::pulse_train_output::{ PulseTrainOutputDevice, PulseTrainOutputInput, PulseTrainOutputOutput, PulseTrainOutputState, }, pdo::{ - el252x::{EncControl, EncStatus, PtoControl, PtoStatus, PtoTarget}, PredefinedPdoAssignment, RxPdo, TxPdo, + el252x::{EncControl, EncStatus, PtoControl, PtoStatus, PtoTarget}, }, - helpers::ethercrab_types::EthercrabSubDevicePreoperational, }; use anyhow::Ok; use ethercat_hal_derive::{EthercatDevice, RxPdo, TxPdo}; @@ -20,6 +20,7 @@ pub struct EL2521 { pub configuration: EL2521Configuration, pub txpdo: EL2521TxPdo, pub rxpdo: EL2521RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL2521 {} @@ -40,6 +41,7 @@ impl NewEthercatDevice for EL2521 { configuration: configuration, txpdo, rxpdo, + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el2522.rs b/ethercat-hal/src/devices/el2522.rs index 14bd2f9b..a2382314 100644 --- a/ethercat-hal/src/devices/el2522.rs +++ b/ethercat-hal/src/devices/el2522.rs @@ -1,6 +1,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice, SubDeviceIdentityTuple}; use crate::{ coe::{ConfigurableDevice, Configuration}, + helpers::ethercrab_types::EthercrabSubDevicePreoperational, io::pulse_train_output::{ PulseTrainOutputDevice, PulseTrainOutputInput, PulseTrainOutputOutput, PulseTrainOutputState, @@ -9,7 +10,6 @@ use crate::{ PredefinedPdoAssignment, RxPdo, TxPdo, el252x::{EncControl, EncStatus, PtoControl, PtoStatus, PtoTarget}, }, - helpers::ethercrab_types::EthercrabSubDevicePreoperational, }; use anyhow::Ok; use ethercat_hal_derive::{EthercatDevice, RxPdo, TxPdo}; @@ -20,6 +20,7 @@ pub struct EL2522 { pub configuration: EL2522Configuration, pub txpdo: EL2522TxPdo, pub rxpdo: EL2522RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL2522 {} @@ -40,6 +41,7 @@ impl NewEthercatDevice for EL2522 { configuration: configuration, txpdo, rxpdo, + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el2634.rs b/ethercat-hal/src/devices/el2634.rs index 53704510..0b08583e 100644 --- a/ethercat-hal/src/devices/el2634.rs +++ b/ethercat-hal/src/devices/el2634.rs @@ -1,6 +1,6 @@ +use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use crate::io::digital_output::{DigitalOutputDevice, DigitalOutputOutput, DigitalOutputState}; use crate::pdo::{RxPdo, basic::BoolPdoObject}; -use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use ethercat_hal_derive::{EthercatDevice, RxPdo}; use super::{EthercatDeviceProcessing, NewEthercatDevice}; @@ -11,6 +11,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice}; #[derive(EthercatDevice)] pub struct EL2634 { pub rxpdo: EL2634RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL2634 {} @@ -25,6 +26,7 @@ impl NewEthercatDevice for EL2634 { fn new() -> Self { Self { rxpdo: EL2634RxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el2809.rs b/ethercat-hal/src/devices/el2809.rs index 613b2c42..dbfcf7f5 100644 --- a/ethercat-hal/src/devices/el2809.rs +++ b/ethercat-hal/src/devices/el2809.rs @@ -1,6 +1,6 @@ +use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use crate::io::digital_output::{DigitalOutputDevice, DigitalOutputOutput, DigitalOutputState}; use crate::pdo::{RxPdo, basic::BoolPdoObject}; -use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use ethercat_hal_derive::{EthercatDevice, RxPdo}; use super::{EthercatDeviceProcessing, NewEthercatDevice}; @@ -11,6 +11,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice}; #[derive(EthercatDevice)] pub struct EL2809 { pub rxpdo: EL2809RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL2809 {} @@ -25,6 +26,7 @@ impl NewEthercatDevice for EL2809 { fn new() -> Self { Self { rxpdo: EL2809RxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el3001.rs b/ethercat-hal/src/devices/el3001.rs index f59ee1c1..dc5b9c02 100644 --- a/ethercat-hal/src/devices/el3001.rs +++ b/ethercat-hal/src/devices/el3001.rs @@ -20,6 +20,7 @@ use uom::si::{electric_potential::volt, f64::ElectricPotential}; pub struct EL3001 { pub txpdo: EL3001TxPdo, pub configuration: EL3001Configuration, + is_used: bool, } impl EthercatDeviceProcessing for EL3001 {} @@ -42,6 +43,7 @@ impl NewEthercatDevice for EL3001 { Self { txpdo: configuration.pdo_assignment.txpdo_assignment(), configuration, + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el3021.rs b/ethercat-hal/src/devices/el3021.rs index 123c6c34..46fbc3c7 100644 --- a/ethercat-hal/src/devices/el3021.rs +++ b/ethercat-hal/src/devices/el3021.rs @@ -32,6 +32,7 @@ pub struct EL3021 { pub configuration: EL3021Configuration, pub txpdo: EL3021TxPdo, pub rxpdo: EL3021RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL3021 {} @@ -64,6 +65,7 @@ impl NewEthercatDevice for EL3021 { configuration: configuration.clone(), txpdo: configuration.pdo_assignment.txpdo_assignment(), rxpdo: configuration.pdo_assignment.rxpdo_assignment(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el3024.rs b/ethercat-hal/src/devices/el3024.rs index a51b8c15..108263b8 100644 --- a/ethercat-hal/src/devices/el3024.rs +++ b/ethercat-hal/src/devices/el3024.rs @@ -5,16 +5,16 @@ use crate::pdo::RxPdo; use crate::pdo::TxPdo; use crate::{ coe::{ConfigurableDevice, Configuration}, + helpers::signing_converter_u16::U16SigningConverter, pdo::{ PredefinedPdoAssignment, analog_input::{AiCompact, AiStandard}, }, shared_config::el30xx::{EL30XXChannelConfiguration, EL30XXPresentation}, - helpers::signing_converter_u16::U16SigningConverter, }; use crate::{ - io::analog_input::{AnalogInputDevice, AnalogInputInput, AnalogInputState}, helpers::ethercrab_types::EthercrabSubDevicePreoperational, + io::analog_input::{AnalogInputDevice, AnalogInputInput, AnalogInputState}, }; use ethercat_hal_derive::{EthercatDevice, RxPdo, TxPdo}; use uom::si::electric_current::milliampere; @@ -38,6 +38,7 @@ pub struct EL3024 { pub configuration: EL3024Configuration, pub txpdo: EL3024TxPdo, pub rxpdo: EL3024RxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL3024 {} @@ -73,6 +74,7 @@ impl NewEthercatDevice for EL3024 { configuration: configuration.clone(), txpdo: configuration.pdo_assignment.txpdo_assignment(), rxpdo: configuration.pdo_assignment.rxpdo_assignment(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el3204.rs b/ethercat-hal/src/devices/el3204.rs index 653e17ca..a35467eb 100644 --- a/ethercat-hal/src/devices/el3204.rs +++ b/ethercat-hal/src/devices/el3204.rs @@ -14,6 +14,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice, SubDeviceIdentityTuple} #[derive(EthercatDevice)] pub struct EL3204 { pub txpdo: EL3204TxPdo, + is_used: bool, } impl EthercatDeviceProcessing for EL3204 {} @@ -28,6 +29,7 @@ impl NewEthercatDevice for EL3204 { fn new() -> Self { Self { txpdo: EL3204TxPdo::default(), + is_used: false, } } } diff --git a/ethercat-hal/src/devices/el6021.rs b/ethercat-hal/src/devices/el6021.rs index c03795f7..80b71ad3 100644 --- a/ethercat-hal/src/devices/el6021.rs +++ b/ethercat-hal/src/devices/el6021.rs @@ -1,8 +1,8 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice, SubDeviceIdentityTuple}; use crate::coe::{ConfigurableDevice, Configuration}; +use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use crate::io::serial_interface::{SerialEncoding, SerialInterfaceDevice}; use crate::pdo::{PredefinedPdoAssignment, RxPdo, RxPdoObject, TxPdo, TxPdoObject}; -use crate::helpers::ethercrab_types::EthercrabSubDevicePreoperational; use anyhow::{Error, anyhow}; use bitvec::field::BitField; use bitvec::order::Lsb0; @@ -369,6 +369,7 @@ pub struct EL6021 { pub configuration: EL6021Configuration, pub txpdo: EL6021TxPdo, pub rxpdo: EL6021RxPdo, + is_used: bool, pub output_ts: u64, pub input_ts: u64, pub has_messages_last_toggle: bool, @@ -404,6 +405,7 @@ impl NewEthercatDevice for EL6021 { configuration: configuration.clone(), txpdo: configuration.pdo_assignment.txpdo_assignment(), rxpdo: configuration.pdo_assignment.rxpdo_assignment(), + is_used: false, output_ts: 0, input_ts: 0, has_messages_last_toggle: false, diff --git a/ethercat-hal/src/devices/el7031/mod.rs b/ethercat-hal/src/devices/el7031/mod.rs index 0953d83a..6cb7ff0c 100644 --- a/ethercat-hal/src/devices/el7031/mod.rs +++ b/ethercat-hal/src/devices/el7031/mod.rs @@ -25,6 +25,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice, SubDeviceIdentityTuple} pub struct EL7031 { pub txpdo: EL7031TxPdo, pub rxpdo: EL7031RxPdo, + is_used: bool, pub configuration: EL7031Configuration, pub counter_wrapper: CounterWrapperU16U128, } @@ -100,6 +101,7 @@ impl NewEthercatDevice for EL7031 { Self { txpdo: configuration.pdo_assignment.txpdo_assignment(), rxpdo: configuration.pdo_assignment.rxpdo_assignment(), + is_used: false, configuration, counter_wrapper: CounterWrapperU16U128::new(), } diff --git a/ethercat-hal/src/devices/el7031_0030/mod.rs b/ethercat-hal/src/devices/el7031_0030/mod.rs index 62813e47..10fa2be3 100644 --- a/ethercat-hal/src/devices/el7031_0030/mod.rs +++ b/ethercat-hal/src/devices/el7031_0030/mod.rs @@ -31,6 +31,7 @@ use super::{EthercatDeviceProcessing, NewEthercatDevice, SubDeviceIdentityTuple} pub struct EL7031_0030 { pub txpdo: EL7031_0030TxPdo, pub rxpdo: EL7031_0030RxPdo, + is_used: bool, pub configuration: EL7031_0030Configuration, pub counter_wrapper: CounterWrapperU16U128, } @@ -106,6 +107,7 @@ impl NewEthercatDevice for EL7031_0030 { Self { txpdo: configuration.pdo_assignment.txpdo_assignment(), rxpdo: configuration.pdo_assignment.rxpdo_assignment(), + is_used: false, configuration, counter_wrapper: CounterWrapperU16U128::new(), } diff --git a/ethercat-hal/src/devices/el7041_0052/mod.rs b/ethercat-hal/src/devices/el7041_0052/mod.rs index f0621b44..e10e1742 100644 --- a/ethercat-hal/src/devices/el7041_0052/mod.rs +++ b/ethercat-hal/src/devices/el7041_0052/mod.rs @@ -23,6 +23,7 @@ pub mod pdo; pub struct EL7041_0052 { pub txpdo: pdo::EL7041_0052TxPdo, pub rxpdo: pdo::EL7041_0052RxPdo, + is_used: bool, pub configuration: EL7041_0052Configuration, // encoder wrapping @@ -35,6 +36,7 @@ impl NewEthercatDevice for EL7041_0052 { EL7041_0052 { txpdo: configuration.pdo_assignment.txpdo_assignment(), rxpdo: configuration.pdo_assignment.rxpdo_assignment(), + is_used: false, configuration, counter_wrapper: CounterWrapperU16U128::new(), } diff --git a/ethercat-hal/src/devices/mod.rs b/ethercat-hal/src/devices/mod.rs index b0248b1f..4eba4b09 100644 --- a/ethercat-hal/src/devices/mod.rs +++ b/ethercat-hal/src/devices/mod.rs @@ -52,7 +52,13 @@ use std::{any::Any, fmt::Debug, sync::Arc}; /// provides interface to read and write the PDO data pub trait EthercatDevice where - Self: NewEthercatDevice + EthercatDeviceProcessing + Any + Send + Sync + Debug, + Self: NewEthercatDevice + + EthercatDeviceProcessing + + EthercatDeviceUsed + + Any + + Send + + Sync + + Debug, { /// Input data from the last cycle /// `ts` is the timestamp when the input data was sent by the device @@ -134,6 +140,15 @@ pub trait NewEthercatDevice { Self: Sized; } +/// A trait to ensure a divice can ony be used once +pub trait EthercatDeviceUsed { + /// Returns true if the device is used + fn is_used(&self) -> bool; + + /// Sets the device as used + fn set_used(&mut self, used: bool); +} + /// Casts a `dyn Device` to a specific device type pub async fn downcast_device( device: Arc>, diff --git a/server/src/loop.rs b/server/src/loop.rs index 61c9e34e..8276eb3f 100644 --- a/server/src/loop.rs +++ b/server/src/loop.rs @@ -65,6 +65,12 @@ pub async fn loop_once<'maindevice>(app_state: Arc) -> Result<(), anyh // get device let mut device = ethercat_setup.devices[i].1.as_ref().write().await; + // check if the device is used + if !device.is_used() { + // if the device is not used, we skip it + continue; + } + // put inputs into device device.input_checked(input_bits).or_else(|e| { Err(anyhow::anyhow!( @@ -121,6 +127,12 @@ pub async fn loop_once<'maindevice>(app_state: Arc) -> Result<(), anyh // get device let mut device = ethercat_setup.devices[i].1.as_ref().write().await; + // check if the device is used + if !device.is_used() { + // if the device is not used, we skip it + continue; + } + // pre process outputs device.output_pre_process().or_else(|e| { Err(anyhow::anyhow!( diff --git a/server/src/machines/extruder1/new.rs b/server/src/machines/extruder1/new.rs index 4c545552..b490f333 100644 --- a/server/src/machines/extruder1/new.rs +++ b/server/src/machines/extruder1/new.rs @@ -23,8 +23,8 @@ use control_core::{ use ethercat_hal::{ coe::ConfigurableDevice, devices::{ - downcast_device, - ek1100::EK1100_IDENTITY_A, + EthercatDeviceUsed, downcast_device, + ek1100::{EK1100, EK1100_IDENTITY_A}, el1002::{EL1002, EL1002_IDENTITY_A}, el2004::{EL2004, EL2004_IDENTITY_A, EL2004Port}, el3021::{EL3021, EL3021_IDENTITY_A, EL3021Port}, @@ -92,8 +92,14 @@ impl MachineNewTrait for ExtruderV2 { let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice = get_subdevice_by_index(hardware.subdevices, subdevice_index)?; let subdevice_identity = subdevice.identity(); - match subdevice_identity_to_tuple(&subdevice_identity) { - EK1100_IDENTITY_A => (), + let device = match subdevice_identity_to_tuple(&subdevice_identity) { + EK1100_IDENTITY_A => { + let ethercat_device = get_ethercat_device_by_index( + &hardware.ethercat_devices, + subdevice_index, + )?; + downcast_device::(ethercat_device).await? + } _ => { return Err(anyhow::anyhow!( "[{}::MachineNewTrait/Winder2::new] Device with role 0 is not an EK1100", @@ -101,7 +107,12 @@ impl MachineNewTrait for ExtruderV2 { )); } }; + { + let mut device_guard = device.write().await; + device_guard.set_used(true); + } } + // What is its use ? let _el1002 = { let device_identification = @@ -119,7 +130,7 @@ impl MachineNewTrait for ExtruderV2 { let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice = get_subdevice_by_index(hardware.subdevices, subdevice_index)?; let subdevice_identity = subdevice.identity(); - match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL1002_IDENTITY_A => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -133,7 +144,12 @@ impl MachineNewTrait for ExtruderV2 { module_path!() )); } + }; + { + let mut device_guard = device.write().await; + device_guard.set_used(true); } + device }; let el6021 = { @@ -152,7 +168,7 @@ impl MachineNewTrait for ExtruderV2 { let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice = get_subdevice_by_index(hardware.subdevices, subdevice_index)?; let subdevice_identity = subdevice.identity(); - let el6021 = match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL6021_IDENTITY_A | EL6021_IDENTITY_B | EL6021_IDENTITY_C => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -167,12 +183,16 @@ impl MachineNewTrait for ExtruderV2 { )); } }; - el6021 + device .write() .await .write_config(&subdevice, &EL6021Configuration::default()) .await?; - el6021 + { + let mut device_guard = device.write().await; + device_guard.set_used(true); + } + device }; let el2004 = { @@ -191,7 +211,7 @@ impl MachineNewTrait for ExtruderV2 { let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice = get_subdevice_by_index(hardware.subdevices, subdevice_index)?; let subdevice_identity = subdevice.identity(); - match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL2004_IDENTITY_A => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -205,7 +225,12 @@ impl MachineNewTrait for ExtruderV2 { module_path!() )); } + }; + { + let mut device_guard = device.write().await; + device_guard.set_used(true); } + device }; let el3021 = { @@ -224,7 +249,7 @@ impl MachineNewTrait for ExtruderV2 { let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice = get_subdevice_by_index(hardware.subdevices, subdevice_index)?; let subdevice_identity = subdevice.identity(); - match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL3021_IDENTITY_A => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -238,7 +263,12 @@ impl MachineNewTrait for ExtruderV2 { module_path!() )); } + }; + { + let mut device_guard = device.write().await; + device_guard.set_used(true); } + device }; let el3204 = { @@ -257,7 +287,7 @@ impl MachineNewTrait for ExtruderV2 { let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice = get_subdevice_by_index(hardware.subdevices, subdevice_index)?; let subdevice_identity = subdevice.identity(); - match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL3204_IDENTITY_A | EL3204_IDENTITY_B => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -271,7 +301,12 @@ impl MachineNewTrait for ExtruderV2 { module_path!() )); } + }; + { + let mut device_guard = device.write().await; + device_guard.set_used(true); } + device }; let t1 = TemperatureInput::new(el3204.clone(), EL3204Port::T1); diff --git a/server/src/machines/winder2/new.rs b/server/src/machines/winder2/new.rs index ad01eb61..90613465 100644 --- a/server/src/machines/winder2/new.rs +++ b/server/src/machines/winder2/new.rs @@ -22,6 +22,7 @@ use control_core::machines::new::{ }; use control_core::uom_extensions::velocity::meter_per_minute; use ethercat_hal::coe::ConfigurableDevice; +use ethercat_hal::devices::ek1100::EK1100; use ethercat_hal::devices::el2002::{EL2002, EL2002Port}; use ethercat_hal::devices::el7031::coe::EL7031Configuration; use ethercat_hal::devices::el7031::pdo::EL7031PredefinedPdoAssignment; @@ -35,7 +36,7 @@ use ethercat_hal::devices::el7031_0030::{ }; use ethercat_hal::devices::el7041_0052::coe::EL7041_0052Configuration; use ethercat_hal::devices::el7041_0052::{EL7041_0052, EL7041_0052_IDENTITY_A, EL7041_0052Port}; -use ethercat_hal::devices::{downcast_device, subdevice_identity_to_tuple}; +use ethercat_hal::devices::{EthercatDeviceUsed, downcast_device, subdevice_identity_to_tuple}; use ethercat_hal::devices::{ek1100::EK1100_IDENTITY_A, el2002::EL2002_IDENTITY_A}; use ethercat_hal::io::analog_input::AnalogInput; use ethercat_hal::io::digital_input::DigitalInput; @@ -98,8 +99,14 @@ impl MachineNewTrait for Winder2 { let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice = get_subdevice_by_index(hardware.subdevices, subdevice_index)?; let subdevice_identity = subdevice.identity(); - match subdevice_identity_to_tuple(&subdevice_identity) { - EK1100_IDENTITY_A => (), + let device = match subdevice_identity_to_tuple(&subdevice_identity) { + EK1100_IDENTITY_A => { + let ethercat_device = get_ethercat_device_by_index( + &hardware.ethercat_devices, + subdevice_index, + )?; + downcast_device::(ethercat_device).await? + } _ => { return Err(anyhow::anyhow!( "[{}::MachineNewTrait/Winder2::new] Device with role 0 is not an EK1100", @@ -107,6 +114,10 @@ impl MachineNewTrait for Winder2 { )); } }; + { + let mut device_guard = device.write().await; + device_guard.set_used(true); + } } // Role 1 @@ -128,7 +139,7 @@ impl MachineNewTrait for Winder2 { let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice = get_subdevice_by_index(hardware.subdevices, subdevice_index)?; let subdevice_identity = subdevice.identity(); - match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL2002_IDENTITY_A => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -140,7 +151,12 @@ impl MachineNewTrait for Winder2 { "[{}::MachineNewTrait/Winder2::new] Device with role 1 is not an EL2002", module_path!() ))?, + }; + { + let mut device_guard = device.write().await; + device_guard.set_used(true); } + device }; // Role 2 @@ -165,7 +181,7 @@ impl MachineNewTrait for Winder2 { )?; let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice_identity = subdevice.identity(); - let el7041 = match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL7041_0052_IDENTITY_A => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -178,7 +194,7 @@ impl MachineNewTrait for Winder2 { module_path!() ))?, }; - let el7041_config = EL7041_0052Configuration { + let config = EL7041_0052Configuration { stm_features: shared_config::el70x1::StmFeatures { operation_mode: EL70x1OperationMode::DirectVelocity, ..Default::default() @@ -189,12 +205,16 @@ impl MachineNewTrait for Winder2 { }, ..Default::default() }; - el7041 + device .write() .await - .write_config(&subdevice, &el7041_config) + .write_config(&subdevice, &config) .await?; - (el7041, el7041_config) + { + let mut device_guard = device.write().await; + device_guard.set_used(true); + } + (device, config) }; // Role 3 @@ -219,7 +239,7 @@ impl MachineNewTrait for Winder2 { )?; let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice_identity = subdevice.identity(); - let el7031 = match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL7031_IDENTITY_A | EL7031_IDENTITY_B => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -232,7 +252,7 @@ impl MachineNewTrait for Winder2 { module_path!() ))?, }; - let el7031_config = EL7031Configuration { + let config = EL7031Configuration { stm_features: shared_config::el70x1::StmFeatures { operation_mode: EL70x1OperationMode::DirectVelocity, // Max Speed of 1000 steps/s @@ -248,12 +268,16 @@ impl MachineNewTrait for Winder2 { pdo_assignment: EL7031PredefinedPdoAssignment::VelocityControlCompact, ..Default::default() }; - el7031 + device .write() .await - .write_config(&subdevice, &el7031_config) + .write_config(&subdevice, &config) .await?; - (el7031, el7031_config) + { + let mut device_guard = device.write().await; + device_guard.set_used(true); + } + (device, config) }; // Role 4 @@ -278,7 +302,7 @@ impl MachineNewTrait for Winder2 { )?; let subdevice_index = device_hardware_identification_ethercat.subdevice_index; let subdevice_identity = subdevice.identity(); - let el7031_0030 = match subdevice_identity_to_tuple(&subdevice_identity) { + let device = match subdevice_identity_to_tuple(&subdevice_identity) { EL7031_0030_IDENTITY_A => { let ethercat_device = get_ethercat_device_by_index( &hardware.ethercat_devices, @@ -291,7 +315,7 @@ impl MachineNewTrait for Winder2 { module_path!() ))?, }; - let el7031_0030_config = EL7031_0030Configuration { + let config = EL7031_0030Configuration { stm_features: el7031_0030::coe::StmFeatures { operation_mode: EL70x1OperationMode::DirectVelocity, // Max Speed of 1000 steps/s @@ -306,12 +330,16 @@ impl MachineNewTrait for Winder2 { pdo_assignment: EL7031_0030PredefinedPdoAssignment::VelocityControlCompact, ..Default::default() }; - el7031_0030 + device .write() .await - .write_config(&subdevice, &el7031_0030_config) + .write_config(&subdevice, &config) .await?; - (el7031_0030, el7031_0030_config) + { + let mut device_guard = device.write().await; + device_guard.set_used(true); + } + (device, config) }; let mode = Winder2Mode::Standby;