Use gilrs for controller
This commit is contained in:
parent
7bf752828f
commit
60640371c6
|
@ -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
31
red/src/jogger.rs
Normal 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,
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod printer;
|
||||
pub mod jogger;
|
||||
|
|
|
@ -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();
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue