From 18fee8a4320728fad186e317028158f8a570a4f5 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Wed, 23 Oct 2024 21:10:55 -0400 Subject: [PATCH] add in Config knobs for moonraker/usb (#157) ``` [machines.neptune] type = "Moonraker" endpoint = "http://myprinter.local" variant = "ElegooNeptune4" slicer.type = "Prusa" slicer.config = "/some/path/to/prusa/config.ini" nozzle_diameter = 0.2 loaded_filament_idx = 0 [[machines.neptune.filaments]] material.type = "pla" ``` --- src/bin/machine-api/config/moonraker.rs | 3 +-- src/lib.rs | 8 ++++---- src/moonraker/control.rs | 17 ++++++++--------- src/moonraker/mod.rs | 25 +++++++++++++++++++++---- src/usb/control.rs | 22 ++++++++++++---------- src/usb/discover.rs | 12 +++++++++++- 6 files changed, 57 insertions(+), 30 deletions(-) diff --git a/src/bin/machine-api/config/moonraker.rs b/src/bin/machine-api/config/moonraker.rs index cc5308a..f7cb6f0 100644 --- a/src/bin/machine-api/config/moonraker.rs +++ b/src/bin/machine-api/config/moonraker.rs @@ -31,13 +31,12 @@ impl Config { key.clone(), RwLock::new(Machine::new( moonraker::Client::new( - &config.endpoint.clone(), + &config, MachineMakeModel { manufacturer, model, serial: None, }, - config.variant.get_max_part_volume(), )?, slicer, )), diff --git a/src/lib.rs b/src/lib.rs index 229d0be..7aef918 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,10 +43,10 @@ use serde::{Deserialize, Serialize}; pub use slicer::AnySlicer; pub use sync::SharedMachine; pub use traits::{ - BuildOptions, Control, FdmHardwareConfiguration, FilamentMaterial, GcodeControl, GcodeSlicer, GcodeTemporaryFile, - HardwareConfiguration, MachineInfo, MachineMakeModel, MachineState, MachineType, SlicerConfiguration, - SuspendControl, TemperatureSensor, TemperatureSensorReading, TemperatureSensors, ThreeMfControl, ThreeMfSlicer, - ThreeMfTemporaryFile, + BuildOptions, Control, FdmHardwareConfiguration, Filament, FilamentMaterial, GcodeControl, GcodeSlicer, + GcodeTemporaryFile, HardwareConfiguration, MachineInfo, MachineMakeModel, MachineState, MachineType, + SlicerConfiguration, SuspendControl, TemperatureSensor, TemperatureSensorReading, TemperatureSensors, + ThreeMfControl, ThreeMfSlicer, ThreeMfTemporaryFile, }; /// A specific file containing a design to be manufactured. diff --git a/src/moonraker/control.rs b/src/moonraker/control.rs index cef811f..5990a30 100644 --- a/src/moonraker/control.rs +++ b/src/moonraker/control.rs @@ -5,9 +5,9 @@ use moonraker::InfoResponse; use super::Client; use crate::{ - traits::Filament, Control as ControlTrait, FdmHardwareConfiguration, FilamentMaterial, - GcodeControl as GcodeControlTrait, GcodeTemporaryFile, HardwareConfiguration, MachineInfo as MachineInfoTrait, - MachineMakeModel, MachineState, MachineType, SuspendControl as SuspendControlTrait, Volume, + Control as ControlTrait, FdmHardwareConfiguration, GcodeControl as GcodeControlTrait, GcodeTemporaryFile, + HardwareConfiguration, MachineInfo as MachineInfoTrait, MachineMakeModel, MachineState, MachineType, + SuspendControl as SuspendControlTrait, Volume, }; /// Information about the connected Moonraker-based printer. @@ -91,14 +91,13 @@ impl ControlTrait for Client { } async fn hardware_configuration(&self) -> Result { + let config = self.get_config(); + Ok(HardwareConfiguration::Fdm { config: FdmHardwareConfiguration { - filaments: vec![Filament { - material: FilamentMaterial::Pla, - ..Default::default() - }], - nozzle_diameter: 0.4, - loaded_filament_idx: None, + filaments: config.filaments.clone(), + nozzle_diameter: config.nozzle_diameter, + loaded_filament_idx: config.loaded_filament_idx, }, }) } diff --git a/src/moonraker/mod.rs b/src/moonraker/mod.rs index ac926cd..c814b76 100644 --- a/src/moonraker/mod.rs +++ b/src/moonraker/mod.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; pub use temperature::TemperatureSensors; pub use variants::MoonrakerVariant; -use crate::{slicer, MachineMakeModel, Volume}; +use crate::{slicer, Filament, MachineMakeModel, Volume}; /// Configuration information for a Moonraker-based endpoint. #[derive(Clone, Debug, Serialize, Deserialize)] @@ -19,6 +19,15 @@ pub struct Config { /// Slicer to use with this printer pub slicer: slicer::Config, + /// Extrusion hotend nozzle's diameter. + pub nozzle_diameter: f64, + + /// Available filaments. + pub filaments: Vec, + + /// Currently loaded filament, if possible to determine. + pub loaded_filament_idx: Option, + /// Specific make/model of Moonraker-based printer. pub variant: MoonrakerVariant, @@ -32,16 +41,19 @@ pub struct Client { client: MoonrakerClient, make_model: MachineMakeModel, volume: Option, + + config: Config, } impl Client { /// Create a new Moonraker based machine. The `base_url` will be /// passed through to [moonraker::Client]. - pub fn new(base_url: &str, make_model: MachineMakeModel, volume: Option) -> Result { + pub fn new(config: &Config, make_model: MachineMakeModel) -> Result { Ok(Self { make_model, - volume, - client: MoonrakerClient::new(base_url)?, + volume: config.variant.get_max_part_volume(), + client: MoonrakerClient::new(&config.endpoint)?, + config: config.clone(), }) } @@ -49,4 +61,9 @@ impl Client { pub fn get_client(&self) -> &MoonrakerClient { &self.client } + + /// Return the underling [Config] + pub(crate) fn get_config(&self) -> &Config { + &self.config + } } diff --git a/src/usb/control.rs b/src/usb/control.rs index a847998..5c888ba 100644 --- a/src/usb/control.rs +++ b/src/usb/control.rs @@ -7,10 +7,11 @@ use tokio::{ }; use tokio_serial::SerialStream; +use super::Config; use crate::{ - gcode::Client, traits::Filament, Control as ControlTrait, FdmHardwareConfiguration, FilamentMaterial, - GcodeControl as GcodeControlTrait, GcodeTemporaryFile, HardwareConfiguration, MachineInfo as MachineInfoTrait, - MachineMakeModel, MachineState, MachineType, Volume, + gcode::Client, Control as ControlTrait, FdmHardwareConfiguration, GcodeControl as GcodeControlTrait, + GcodeTemporaryFile, HardwareConfiguration, MachineInfo as MachineInfoTrait, MachineMakeModel, MachineState, + MachineType, Volume, }; /// Handle to a USB based gcode 3D printer. @@ -18,16 +19,18 @@ use crate::{ pub struct Usb { client: Arc, ReadHalf>>>, machine_info: UsbMachineInfo, + config: Config, } impl Usb { /// Create a new USB-based gcode Machine. - pub fn new(stream: SerialStream, machine_info: UsbMachineInfo) -> Self { + pub fn new(stream: SerialStream, machine_info: UsbMachineInfo, config: Config) -> Self { let (reader, writer) = tokio::io::split(stream); Self { client: Arc::new(Mutex::new(Client::new(writer, reader))), machine_info, + config, } } @@ -147,14 +150,13 @@ impl ControlTrait for Usb { } async fn hardware_configuration(&self) -> Result { + let config = &self.config; + Ok(HardwareConfiguration::Fdm { config: FdmHardwareConfiguration { - filaments: vec![Filament { - material: FilamentMaterial::Pla, - ..Default::default() - }], - nozzle_diameter: 0.4, - loaded_filament_idx: None, + filaments: config.filaments.clone(), + nozzle_diameter: config.nozzle_diameter, + loaded_filament_idx: config.loaded_filament_idx, }, }) } diff --git a/src/usb/discover.rs b/src/usb/discover.rs index 6167451..27c40c7 100644 --- a/src/usb/discover.rs +++ b/src/usb/discover.rs @@ -6,7 +6,7 @@ use tokio::sync::RwLock; use tokio_serial::{SerialPortBuilderExt, SerialPortType}; use super::UsbVariant; -use crate::{slicer, usb, Discover, Machine, MachineMakeModel}; +use crate::{slicer, usb, Discover, Filament, Machine, MachineMakeModel}; /// Configuration block for a USB based device. #[derive(Clone, Debug, Deserialize, Serialize)] @@ -29,6 +29,15 @@ pub struct Config { /// USB Product ID (pid) to scan for. None will match any USB device. pub product_id: Option, + + /// Extrusion hotend nozzle's diameter. + pub nozzle_diameter: f64, + + /// Available filaments. + pub filaments: Vec, + + /// Currently loaded filament, if possible to determine. + pub loaded_filament_idx: Option, } impl Config { @@ -214,6 +223,7 @@ impl Discover for UsbDiscovery { port_name.clone(), baud, ), + config.clone(), ), slicer, )),