ethercat_controller/
config.rs

1use std::{error::Error, fs};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Default, Serialize, Deserialize)]
6pub struct Config {
7    pub ethercat: EthercatConfig,
8}
9
10/// Configuration for the Ethercat master
11///
12/// The master id is the id of the master in the Ethercat network
13/// The cycle time is the time in microseconds between each cycle
14/// The command drop time is the time in microseconds to wait for the command to be dropped
15/// The watchdog timeout is the time in milliseconds to wait for the watchdog to be updated
16/// The mailbox wait time is the time in milliseconds to wait for the mailbox to be updated
17#[derive(Debug, Default, Serialize, Deserialize)]
18pub struct EthercatConfig {
19    pub master_id: u32,
20    pub cycle_time_us: u32,
21    pub command_drop_time_us: u32,
22    pub watchdog_timeout_ms: u32,
23    pub mailbox_wait_time_ms: u32,
24}
25
26#[derive(Debug, Serialize, Deserialize)]
27pub enum SlaveConfig {
28    Poulpe(PoulpeKind),
29    Unknown,
30}
31
32/// Configuration for the Poulpe slave
33///
34/// The id is the id of the slave in the Ethercat network
35/// The orbita type is the type of the orbita
36/// The name is the name of the slave
37#[derive(Debug, Default, Serialize, Deserialize)]
38pub struct PoulpeKind {
39    pub id: u16,
40    pub orbita_type: u32,
41    pub name: String,
42}
43
44impl Config {
45    /// Load the configuration from a YAML file
46    ///     
47    /// # Arguments
48    ///
49    /// * `path` - The path to the YAML file
50    ///
51    /// # Returns
52    ///
53    /// * `Result<Self, Box<dyn Error>>` - The result of the operation
54    pub fn from_yaml(path: &str) -> Result<Self, Box<dyn Error>> {
55        let yaml = fs::read_to_string(path)?;
56        Ok(serde_yaml::from_str(&yaml)?)
57    }
58}