Clean up code
This commit is contained in:
parent
a65f523636
commit
3b5892e7e0
|
@ -22,6 +22,7 @@ use self::gcode::{G0Command, G28Command, G90Command, GcodeReplyError};
|
||||||
/// This should fit a simple "OK Pnn Bn" reply for GCODE commands that
|
/// This should fit a simple "OK Pnn Bn" reply for GCODE commands that
|
||||||
/// do not return any data.
|
/// do not return any data.
|
||||||
const RECV_BUFFER_CAPACITY: usize = 32;
|
const RECV_BUFFER_CAPACITY: usize = 32;
|
||||||
|
const BAUD_RATE: u32 = 115200;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum PrinterError {
|
pub enum PrinterError {
|
||||||
|
@ -32,8 +33,11 @@ pub enum PrinterError {
|
||||||
// If the "ok" line can't be parsed
|
// If the "ok" line can't be parsed
|
||||||
ConfirmationError { parsed_string: String },
|
ConfirmationError { parsed_string: String },
|
||||||
NoResponseFromPrinter(String),
|
NoResponseFromPrinter(String),
|
||||||
|
// If the initial printer configuration fails
|
||||||
|
InitializationError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct PrinterPosition {
|
pub struct PrinterPosition {
|
||||||
x: f64,
|
x: f64,
|
||||||
y: f64,
|
y: f64,
|
||||||
|
@ -86,10 +90,7 @@ impl Printer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect(port_path: &str) -> Result<Self, PrinterError> {
|
pub async fn connect(port_path: &str) -> Result<Self, PrinterError> {
|
||||||
// TODO: implement auto position reporting.
|
let mut port = tokio_serial::new(port_path, BAUD_RATE)
|
||||||
// For this the whole reply-response control flow won't really work anymore, since the
|
|
||||||
// replies will just be interlaced with position reports.
|
|
||||||
let mut port = tokio_serial::new(port_path, 115200)
|
|
||||||
.open_native_async()
|
.open_native_async()
|
||||||
.expect("Unable to open serial port");
|
.expect("Unable to open serial port");
|
||||||
|
|
||||||
|
@ -141,13 +142,24 @@ impl Printer {
|
||||||
maximum_buffer_capacity: 0, // this is updated on the next call to `send_gcode()`
|
maximum_buffer_capacity: 0, // this is updated on the next call to `send_gcode()`
|
||||||
};
|
};
|
||||||
|
|
||||||
res.send_gcode(G91Command)
|
// This implicitly sets `res.last_buffer_capacity`
|
||||||
.await
|
res.use_absolute_movements().await.map_err(|err| {
|
||||||
.expect("Could not ask for current position!");
|
PrinterError::InitializationError(format!(
|
||||||
|
"Failed to set absolute movements mode: {:?}",
|
||||||
|
err
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
// since we never sent any positioning GCODE, we should be at max-capacity now.
|
// since we never sent any positioning GCODE, we should be at max-capacity now.
|
||||||
res.maximum_buffer_capacity = res.last_buffer_capacity;
|
res.maximum_buffer_capacity = res.last_buffer_capacity;
|
||||||
|
|
||||||
|
res.update_position().await.map_err(|err| {
|
||||||
|
PrinterError::InitializationError(format!(
|
||||||
|
"Failed to get the current position: {:?}",
|
||||||
|
err
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,6 +192,13 @@ impl Printer {
|
||||||
.map_err(|_| make_err())
|
.map_err(|_| make_err())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update the internal position by asking the printer for it
|
||||||
|
async fn update_position(&mut self) -> Result<PrinterPosition, PrinterError> {
|
||||||
|
let res = self.send_gcode(M114Command).await?;
|
||||||
|
self.state.position = res;
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
/// Switch the printer to absolute movement mode.
|
/// Switch the printer to absolute movement mode.
|
||||||
///
|
///
|
||||||
/// This is generally necessary before sending consecutive `self.move_absolute()` commands.
|
/// This is generally necessary before sending consecutive `self.move_absolute()` commands.
|
||||||
|
@ -208,13 +227,13 @@ impl Printer {
|
||||||
/// * `x, y, z` - Whether the axis should be homed. Axis that are set to `false` will not be
|
/// * `x, y, z` - Whether the axis should be homed. Axis that are set to `false` will not be
|
||||||
/// homed.,
|
/// homed.,
|
||||||
pub async fn auto_home(&mut self, x: bool, y: bool, z: bool) -> Result<(), PrinterError> {
|
pub async fn auto_home(&mut self, x: bool, y: bool, z: bool) -> Result<(), PrinterError> {
|
||||||
let res = self.send_gcode(G28Command::new(x, y, z)).await?;
|
self.send_gcode(G28Command::new(x, y, z)).await?;
|
||||||
self.state.position = PrinterPosition {
|
self.state.position = PrinterPosition {
|
||||||
x: 0.0,
|
x: 0.0,
|
||||||
y: 0.0,
|
y: 0.0,
|
||||||
z: 0.0,
|
z: 0.0,
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move the printer by a specific distance
|
/// Move the printer by a specific distance
|
||||||
|
@ -241,7 +260,7 @@ impl Printer {
|
||||||
e: None, // Machine has no e
|
e: None, // Machine has no e
|
||||||
velocity,
|
velocity,
|
||||||
};
|
};
|
||||||
let reply = if let MovementMode::AbsoluteMovements = self.state.movement_mode {
|
if let MovementMode::AbsoluteMovements = self.state.movement_mode {
|
||||||
self.use_relative_movements().await?;
|
self.use_relative_movements().await?;
|
||||||
let res = self.send_gcode(command).await;
|
let res = self.send_gcode(command).await;
|
||||||
self.use_absolute_movements().await?;
|
self.use_absolute_movements().await?;
|
||||||
|
@ -254,7 +273,7 @@ impl Printer {
|
||||||
self.state.position.y += y;
|
self.state.position.y += y;
|
||||||
self.state.position.z += z;
|
self.state.position.z += z;
|
||||||
|
|
||||||
Ok(reply)
|
Ok(())
|
||||||
}
|
}
|
||||||
/// Move the printer to a specific position
|
/// Move the printer to a specific position
|
||||||
///
|
///
|
||||||
|
@ -279,7 +298,7 @@ impl Printer {
|
||||||
e: None, // Machine has no e
|
e: None, // Machine has no e
|
||||||
velocity: Some(velocity),
|
velocity: Some(velocity),
|
||||||
};
|
};
|
||||||
let reply = if let MovementMode::RelativeMovements = self.state.movement_mode {
|
if let MovementMode::RelativeMovements = self.state.movement_mode {
|
||||||
self.use_absolute_movements().await?;
|
self.use_absolute_movements().await?;
|
||||||
let res = self.send_gcode(command).await;
|
let res = self.send_gcode(command).await;
|
||||||
self.use_relative_movements().await?;
|
self.use_relative_movements().await?;
|
||||||
|
@ -292,7 +311,7 @@ impl Printer {
|
||||||
self.state.position.y = y;
|
self.state.position.y = y;
|
||||||
self.state.position.z = z;
|
self.state.position.z = z;
|
||||||
|
|
||||||
Ok(reply)
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue