Implement zeroing

This commit is contained in:
zaubentrucker 2025-02-24 23:22:39 +01:00
parent 79ddd63935
commit def0ecee5c
7 changed files with 54 additions and 31 deletions

View file

@ -18,6 +18,7 @@ pub enum Axis {
#[derive(Debug)]
pub enum GamepadEvent {
AxisPosition(Axis, f32),
NullAxesPressed,
TerminatePressed,
}
@ -95,6 +96,7 @@ impl Gamepad {
}
}
}
_ => {}
}
}
(
@ -115,6 +117,7 @@ impl Gamepad {
Some(GamepadEvent::AxisPosition(Axis::ZPositive, value))
}
ButtonPressed(gilrs::Button::Start, _) => Some(GamepadEvent::TerminatePressed),
ButtonPressed(gilrs::Button::North, _) => Some(GamepadEvent::NullAxesPressed),
_ => None,
}
}

View file

@ -1,4 +1,4 @@
use crate::gamepad::Gamepad;
use crate::gamepad::{Gamepad, GamepadEvent};
use crate::printer::Printer;
use anyhow::{Context, Result, anyhow};
use euclid::{Vector3D, vec3};
@ -38,6 +38,9 @@ pub fn jog(gamepad: &mut Gamepad, mut printer: Printer) -> Result<()> {
loop {
let events = gamepad.get_pending();
if events.iter().any(|e| matches!(e, GamepadEvent::NullAxesPressed)){
printer.set_position(Some(0.), Some(0.), Some(0.)).with_context(|| anyhow!("Zeroing printer position failed"))?;
}
let (set_point_x, set_point_y, set_point_z) = gamepad.speed_set_point(&events);
let distance: PrinterVec = vec3(

View file

@ -0,0 +1,29 @@
use super::*;
/// Set Position
#[derive(Debug)]
pub struct G92Command {
pub x: Option<f64>,
pub y: Option<f64>,
pub z: Option<f64>,
}
impl GcodeCommand for G92Command {
type Reply = ();
fn command(&self) -> String {
fn unpack(letter: &str, o: Option<f64>) -> String {
o.map(|x| format!("{}{:.3}", letter, x)).unwrap_or_default()
}
format!(
"G92 {} {} {}",
unpack("X", self.x),
unpack("Y", self.y),
unpack("Z", self.z),
)
}
fn parse_reply(&self, reply: &str) -> Result<Self::Reply> {
// Usual position reply is swallowed by IO thread
super::parse_empty_reply(self.command(), reply)
}
}

View file

@ -18,6 +18,7 @@ impl GcodeCommand for M114Command {
}
fn parse_reply(&self, reply: &str) -> Result<Self::Reply> {
// Usual position reply is swallowed by IO thread
super::parse_empty_reply(self.command(), reply)
}
}

View file

@ -1,24 +0,0 @@
use super::*;
/// Auto Home
#[derive(Debug)]
pub struct M997Command;
impl GcodeCommand for M997Command {
type Reply = ();
fn command(&self) -> String {
"M997".into()
}
fn parse_reply(&self, reply: &str) -> Result<Self::Reply> {
if !reply.is_empty() {
Err(GcodeReplyError {
sent_command: self.command(),
parsed_input: reply.to_string(),
problem: "Expected no reply".to_string(),
})
} else {
Ok(())
}
}
}

View file

@ -3,6 +3,10 @@
/// - and the meaning of the reply can be parsed with `GcodeCommand::parse_reply(reply)`
///
/// The intended use is though the `Printer::send_gcode()` function.
use regex::Regex;
use std::fmt::Debug;
use lazy_static::lazy_static;
mod g0;
mod g28;
mod g90;
@ -10,19 +14,16 @@ mod g91;
mod m114;
mod m154;
mod m155;
mod m997;
mod g92;
pub use g0::G0Command;
pub use g28::G28Command;
pub use g90::G90Command;
pub use g91::G91Command;
use lazy_static::lazy_static;
pub use g92::G92Command;
pub use m114::M114Command;
pub use m154::M154Command;
pub use m155::M155Command;
pub use m997::M997Command;
use regex::Regex;
use std::fmt::Debug;
type Result<T> = std::result::Result<T, GcodeReplyError>;

View file

@ -5,7 +5,7 @@ use self::gcode::{
G0Command, G28Command, G90Command, GcodeReplyError, M154Command, parse_autoreport_line,
};
use crate::printer::gcode::{
AutoReport, G91Command, M114Command, M155Command, is_temperature_report,
AutoReport, G91Command, G92Command, M114Command, M155Command, is_temperature_report,
};
pub use gcode::GcodeCommand;
use regex::Regex;
@ -385,6 +385,16 @@ impl Printer {
}
}
/// Set the current position of the printer axis in software without physically moving them
pub fn set_position(
&mut self,
x: Option<f64>,
y: Option<f64>,
z: Option<f64>,
) -> Result<(), PrinterError> {
self.send_gcode(G92Command { x, y, z })
}
/// Background thread that handles direct communication with the printer serial port
///
/// Parameters