Use gilrs for controller

This commit is contained in:
Frederik Menke 2022-11-27 00:35:19 +01:00
parent 7bf752828f
commit 60640371c6
4 changed files with 72 additions and 34 deletions

View file

@ -9,6 +9,7 @@ edition = "2021"
bytes = "1.2.1"
euclid = "0.22.7"
futures = "0.3.24"
gilrs = "0.10.1"
lazy_static = "1.4.0"
regex = "1.6.0"
tokio = { version = "1.21.0", features = ["full"] }

31
red/src/jogger.rs Normal file
View file

@ -0,0 +1,31 @@
use gilrs::Gilrs;
use tokio::sync::{oneshot, Mutex};
use std::time::Duration;
use std::thread::sleep;
pub struct Jogger {
speed_setpoint: Mutex<(f64, f64, f64)>,
terminator: oneshot::Sender<()>,
}
impl Jogger {
pub async fn new() -> Result<Self, gilrs::Error> {
let (terminate_tx, mut terminate_rx) = oneshot::channel();
tokio::task::spawn_blocking(move || {
let mut gilrs = Gilrs::new().unwrap();
for (_id, gamepad) in gilrs.gamepads() {
println!("{} is {:?}", gamepad.name(), gamepad.power_info());
}
while let Err(oneshot::error::TryRecvError::Empty) = terminate_rx.try_recv() {
sleep(Duration::from_millis(1));
if let Some(event) = gilrs.next_event() {
println!("{}", event.id);
}
}
});
Ok(Jogger {
speed_setpoint: Mutex::new((0.0, 0.0, 0.0)),
terminator: terminate_tx,
})
}
}

View file

@ -1 +1,2 @@
pub mod printer;
pub mod jogger;

View file

@ -1,44 +1,49 @@
#![warn(rust_2018_idioms)]
use futures::never::Never;
use red::controller;
use red::printer::gcode::*;
use red::printer::Printer;
use std::time::Duration;
const DEFAULT_TTY: &str = "/dev/ttyUSB0";
#[tokio::main]
async fn main() -> tokio_serial::Result<()> {
let mut printer = Printer::connect(DEFAULT_TTY).await.unwrap();
printer
.send_gcode(Box::new(M114Command::new()))
.await
.unwrap();
printer
.send_gcode(Box::new(G28Command::new(true, true, true)))
.await
.unwrap();
async fn main() -> Never {
let jogger = controller::Jogger::new().await;
loop {
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
printer
.send_gcode(Box::new(G0Command {
x: Some(50.0),
y: Some(50.0),
z: Some(5.0),
e: None,
velocity: None,
}))
.await
.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
printer
.send_gcode(Box::new(G0Command {
x: Some(25.0),
y: Some(25.0),
z: Some(5.0),
e: None,
velocity: None,
}))
.await
.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
}
Ok(())
// let mut printer = Printer::connect(DEFAULT_TTY).await.unwrap();
// printer
// .send_gcode(Box::new(M114Command::new()))
// .await
// .unwrap();
// printer
// .send_gcode(Box::new(G28Command::new(true, true, true)))
// .await
// .unwrap();
// loop {
// tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// printer
// .send_gcode(Box::new(G0Command {
// x: Some(50.0),
// y: Some(50.0),
// z: Some(5.0),
// e: None,
// velocity: None,
// }))
// .await
// .unwrap();
// tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// printer
// .send_gcode(Box::new(G0Command {
// x: Some(25.0),
// y: Some(25.0),
// z: Some(5.0),
// e: None,
// velocity: None,
// }))
// .await
// .unwrap();
// }
}