Fix reconnecting

This commit is contained in:
Frederik Menke 2023-11-19 02:10:27 +01:00
parent cecc355be4
commit 594a31d983
4 changed files with 55 additions and 5 deletions

View file

@ -10,7 +10,7 @@ use std::time::Duration;
///
/// For a given GCode buffer size on the machine, we can assume
/// a maximum delay of `TIME_PER_MOVEMENT * BUFFER_COUNT`
const TIME_PER_MOVEMENT: Duration = Duration::from_millis(20);
const TIME_PER_MOVEMENT: Duration = Duration::from_millis(50);
/// Movement speed of gantry when going full throttle in x/y-direction
/// in units/min (mm/min)
const FULL_SCALE_SPEED_XY: f64 = 1000.0;
@ -51,6 +51,5 @@ pub async fn jog(gamepad: Arc<Gamepad>, mut printer: Printer) -> Never {
velocity: velocity.into(),
};
printer.send_gcode(Box::new(command)).await;
std::thread::sleep(TIME_PER_MOVEMENT);
}
}

View file

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

View file

@ -13,10 +13,12 @@ mod g0;
mod g28;
mod g91;
mod m114;
mod m997;
pub use g0::G0Command;
pub use g28::G28Command;
pub use g91::G91Command;
pub use m114::M114Command;
pub use m997::M997Command;
use std::fmt::Debug;
type Result<T> = std::result::Result<T, GcodeReplyError>;

View file

@ -3,14 +3,19 @@ pub mod gcode;
use bytes::BytesMut;
use futures::sink::SinkExt;
use futures::stream::StreamExt;
use futures::TryStreamExt;
use std::time::Duration;
use std::{fmt::Write, io, rc::Rc, str};
use tokio;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::time::timeout;
use tokio_serial::SerialPortBuilderExt;
use tokio_util::codec::{Decoder, Encoder};
pub use gcode::{GcodeCommand, GcodeReply};
use crate::printer::gcode::{G91Command, M997Command};
use self::gcode::GcodeReplyError;
#[derive(Debug)]
@ -63,9 +68,30 @@ impl Printer {
// The printer will send some info after connecting. We need to wait for this
// to be received as it will otherwise stop responding for some reason:
while let Some(stuff) = serial_rx.next().await {
if stuff.map(|s| s.contains("Loaded")).unwrap_or(false) {
break;
loop {
if let Ok(message) = timeout(Duration::from_secs(10), serial_rx.next()).await {
match message {
Some(Ok(reply)) => {
println!("got stuff: {:?}", reply);
if reply.contains("Loaded") {
break;
}
if reply.contains("ok") {
break;
}
}
Some(Err(e)) => {
println!("Error reading from serial port: {:?}", e);
}
None => (),
}
} else {
// TODO: Check if port is good by sending some harmless gcode
println!(
"Reading from serial port timed out. Printer might already be initialized."
);
println!("Sending G91 command");
serial_tx.send(G91Command.command() + "\n").await;
}
}