Skip to content

Commit

Permalink
convert keyboard input into sensor_msgs Joy messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasw committed Jan 22, 2025
1 parent 3ab2760 commit f0d7d4a
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = [ "launcher", "roslibrust_util", "tf_mcap",
members = [ "launcher", "roslibrust_key_to_joy", "roslibrust_util", "tf_mcap",
"tf_roslibrust",
]

Expand Down
19 changes: 19 additions & 0 deletions roslibrust_key_to_joy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "roslibrust_key_to_joy"
version = "0.1.0"
edition.workspace = true
license.workspace = true
repository.workspace = true
authors.workspace = true
keywords.workspace = true
categories.workspace = true

[dependencies]
crossterm = "0.28.1"
anyhow.workspace = true
clap.workspace = true
roslibrust.workspace = true
tokio.workspace = true

[dependencies.roslibrust_util]
path = "../roslibrust_util"
115 changes: 115 additions & 0 deletions roslibrust_key_to_joy/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//importing in execute! macro
#[macro_use]
extern crate crossterm;

use anyhow::Context;
use clap::command;
use crossterm::cursor;
use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers};
use crossterm::style::Print;
use crossterm::terminal::{disable_raw_mode, Clear, ClearType};
use roslibrust::ros1::NodeHandle;
use roslibrust_util::sensor_msgs;
use std::collections::HashMap;
use std::io::stdout;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// don't want params and remaps to be mutable after init is done
let (nh, _full_node_name, _params, remaps) = {
let mut params = HashMap::<String, String>::new();
params.insert("_name".to_string(), "key_to_joy".to_string());
let mut remaps = HashMap::<String, String>::new();
remaps.insert("joy".to_string(), "joy".to_string());

let (_ns, full_node_name, remaining_args) =
roslibrust_util::get_params_remaps(&mut params, &mut remaps);

// using clap only for version reporting currently
let _matches = command!().get_matches_from(remaining_args);

let ros_master_uri =
std::env::var("ROS_MASTER_URI").unwrap_or("http://localhost:11311".to_string());
let nh = NodeHandle::new(&ros_master_uri, &full_node_name).await?;
// log::info!("connected to roscore at {ros_master_uri}");

(nh, full_node_name, params, remaps)
};

let joy_topic = remaps.get("joy").context("no joy topic found")?;
let joy_pub = nh
.advertise::<sensor_msgs::Joy>(joy_topic, 8, false)
.await?;

let mut stdout = stdout();
// going into raw mode
// clearing the screen, going to top left corner and printing welcoming message
execute!(stdout, Clear(ClearType::All), cursor::MoveTo(0, 0), Print(r#"ctrl + c to exit, ctrl + h to print "Hello world", alt + t to print "crossterm is cool""#))
.unwrap();

/*
x-box
0 is left-right on the left stick
1 is up-down on the left stick
2 the left trigger
5 is the right trigger
*/
let mut joy = sensor_msgs::Joy {
axes: vec![0.0; 5],
..Default::default()
};

// key detection
loop {
// going to top left corner
execute!(stdout, cursor::MoveTo(0, 0)).unwrap();

match read().unwrap() {
Event::Key(KeyEvent {
code: KeyCode::Char('h'),
modifiers: KeyModifiers::CONTROL,
//clearing the screen and printing our message
..
}) => execute!(stdout, Clear(ClearType::All), Print("Hello world!")).unwrap(),
Event::Key(KeyEvent {
code: KeyCode::Char('a'),
..
}) => {
joy.axes[0] -= 0.1;
joy.axes[0] = joy.axes[0].clamp(-1.0, 1.0);
execute!(
stdout,
Clear(ClearType::All),
Print(format!("left {}", joy.axes[0]))
)
.unwrap();
}
Event::Key(KeyEvent {
code: KeyCode::Char('d'),
..
}) => {
joy.axes[0] += 0.1;
joy.axes[0] = joy.axes[0].clamp(-1.0, 1.0);
execute!(
stdout,
Clear(ClearType::All),
Print(format!("right {}", joy.axes[0]))
)
.unwrap();
}
Event::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
..
}) => break,
_ => (),
}

joy_pub.publish(&joy).await?;
}

// disabling raw mode
disable_raw_mode().unwrap();

Ok(())
}

0 comments on commit f0d7d4a

Please sign in to comment.