Skip to content

Commit

Permalink
add in Config knobs for moonraker/usb (#157)
Browse files Browse the repository at this point in the history
```
[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"
```
  • Loading branch information
paultag authored Oct 24, 2024
1 parent eaaa3cb commit 18fee8a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
3 changes: 1 addition & 2 deletions src/bin/machine-api/config/moonraker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)),
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 8 additions & 9 deletions src/moonraker/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -91,14 +91,13 @@ impl ControlTrait for Client {
}

async fn hardware_configuration(&self) -> Result<HardwareConfiguration> {
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,
},
})
}
Expand Down
25 changes: 21 additions & 4 deletions src/moonraker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ 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)]
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<Filament>,

/// Currently loaded filament, if possible to determine.
pub loaded_filament_idx: Option<usize>,

/// Specific make/model of Moonraker-based printer.
pub variant: MoonrakerVariant,

Expand All @@ -32,21 +41,29 @@ pub struct Client {
client: MoonrakerClient,
make_model: MachineMakeModel,
volume: Option<Volume>,

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<Volume>) -> Result<Self> {
pub fn new(config: &Config, make_model: MachineMakeModel) -> Result<Self> {
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(),
})
}

/// Return the underling [MoonrakerClient].
pub fn get_client(&self) -> &MoonrakerClient {
&self.client
}

/// Return the underling [Config]
pub(crate) fn get_config(&self) -> &Config {
&self.config
}
}
22 changes: 12 additions & 10 deletions src/usb/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@ 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.
#[derive(Clone)]
pub struct Usb {
client: Arc<Mutex<Client<WriteHalf<SerialStream>, ReadHalf<SerialStream>>>>,
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,
}
}

Expand Down Expand Up @@ -147,14 +150,13 @@ impl ControlTrait for Usb {
}

async fn hardware_configuration(&self) -> Result<HardwareConfiguration> {
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,
},
})
}
Expand Down
12 changes: 11 additions & 1 deletion src/usb/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -29,6 +29,15 @@ pub struct Config {

/// USB Product ID (pid) to scan for. None will match any USB device.
pub product_id: Option<u16>,

/// Extrusion hotend nozzle's diameter.
pub nozzle_diameter: f64,

/// Available filaments.
pub filaments: Vec<Filament>,

/// Currently loaded filament, if possible to determine.
pub loaded_filament_idx: Option<usize>,
}

impl Config {
Expand Down Expand Up @@ -214,6 +223,7 @@ impl Discover for UsbDiscovery {
port_name.clone(),
baud,
),
config.clone(),
),
slicer,
)),
Expand Down

0 comments on commit 18fee8a

Please sign in to comment.