# Teleop Keyboard Node in Rust! 🦀

**URL:** <https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555>\
**Category:** Projects\
**Tags:** ros2, rust, teleoperation, humble\
**Created:** [March 10, 2024, 3:04pm UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555 "2024-03-10T15:04:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atharva\_Bhorpe](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/atharva_bhorpe/32/13338_2.png) [@Atharva\_Bhorpe](https://discourse.openrobotics.org/u/Atharva_Bhorpe)\
**Post date:** [March 10, 2024, 3:04pm UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/1 "2024-03-10T15:04:37Z")

</div>

Rust Implementation of the Generic Keyboard Teleop for ROS2: [teleop\_twist\_keyboard\_rust](https://github.com/AtharvaBhorpe/teleop_twist_keyboard_rust)

## Running the Node

```bash

git clone https://github.com/AtharvaBhorpe/teleop_twist_keyboard_rust

cd teleop_twist_keyboard_rust

cargo run

```

Dependencies:

1. r2r
2. termios
3. lazy\_static

Please support the project by star-ing the repo if you liked it!✨  
Any suggestions for optimization or improvement are welcome!

---

<div class="post-metadata">

**Author:** ![Guela\_kais](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/guela_kais/32/19525_2.png) [@Guela\_kais](https://discourse.openrobotics.org/u/Guela_kais)\
**Post date:** [March 13, 2024, 11:50pm UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/2 "2024-03-13T23:50:46Z")

</div>

Yes, I have had a look. The problem is that you can only build a Rust programm that communicates with ros2 when it is started. Why in this way and not so that it can be started directly with ros2 as a node?

---

<div class="post-metadata">

**Author:** ![Atharva\_Bhorpe](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/atharva_bhorpe/32/13338_2.png) [@Atharva\_Bhorpe](https://discourse.openrobotics.org/u/Atharva_Bhorpe)\
**Post date:** [March 15, 2024, 12:24pm UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/3 "2024-03-15T12:24:00Z")

</div>

Thank you for investing your valuable time in my little project !! ✨

- The ros2 client library that I have used [[r2r](https://github.com/sequenceplanner/r2r)] can be implemented without going through colcon and the standard ROS way. This was my first completed project in my journey of learning Rust, and hence I decided to avoid the standard ROS way and try this one.
- However, there is development happening on the official rclrust client library [[ros2\_rust](https://github.com/ros2-rust/ros2_rust)]. This crate is pretty new and the r2r has more features already implemented than ros2\_rust. However, the ros2\_rust is implemented in the standard ROS way. I have added this to the TODO list and started working on it. Using this crate, having a launch file will be possible as well.

Thank you for your valuable feedback and stay tuned for this update!💫

Any more feedback is happily welcomed!

---

<div class="post-metadata">

**Author:** ![jhdcs](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/jhdcs/32/8074_2.png) [@jhdcs](https://discourse.openrobotics.org/u/jhdcs)\
**Post date:** [March 18, 2024, 12:00pm UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/4 "2024-03-18T12:00:02Z")

</div>

As one of the members of the ROS 2 Rust Working Group that’s working on the ros2\_rust client library, I would just like to say that if decide to try it out and you have any feedback as to how well it works for you and how it “feels” to use, please let us know! We are always looking to improve the user experience.

---

<div class="post-metadata">

**Author:** ![Atharva\_Bhorpe](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/atharva_bhorpe/32/13338_2.png) [@Atharva\_Bhorpe](https://discourse.openrobotics.org/u/Atharva_Bhorpe)\
**Post date:** [March 27, 2024, 9:26am UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/5 "2024-03-27T09:26:15Z")

</div>

[Package Update]

Hey peeps!

I have implemented the teleop rust node using the official [rclrust](https://github.com/ros2-rust/ros2_rust) client library, which follows the “ROS way”.  
Please try [this package](https://github.com/AtharvaBhorpe/teleop_twist_keyboard_rust/tree/rclrust) and convey if there are issues or improvements.

@jhdcs Thank you for lending your support! The ros2\_rust client library is simple. I also found the syntax similar to rclcpp, which resulted in a smooth transition. I’m just starting with the client library. I will let you know any comments/queries if I get any.

Cheers!

---

<div class="post-metadata">

**Author:** ![Guela\_kais](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/guela_kais/32/19525_2.png) [@Guela\_kais](https://discourse.openrobotics.org/u/Guela_kais)\
**Post date:** [March 27, 2024, 10:50am UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/6 "2024-03-27T10:50:01Z")

</div>

I really like this implementation. Above all, I like the integration of lazystatic. I didn’t know that before. I’m always happy when I get to know new rust functionalities.  
I am interested in what the exact use case of this node is.

What I don’t like are the `unsafe` parts in the code. This should always be used with caution, as the RUST compiler can/should no longer help with certain errors in these areas and in this way you can intervene very efficiently and also accidentally in the behaviour of other programs.

I have also written a [teleop node](https://gitlab.com/rust_projects3505446/simple_ros2_node/-/blob/more_simple_nodes/src/teleop.rs?ref_type=heads) with rclrs. Maybe some of it will inspire you.

---

<div class="post-metadata">

**Author:** ![Atharva\_Bhorpe](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/atharva_bhorpe/32/13338_2.png) [@Atharva\_Bhorpe](https://discourse.openrobotics.org/u/Atharva_Bhorpe)\
**Post date:** [March 29, 2024, 11:17am UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/7 "2024-03-29T11:17:31Z")

</div>

Thank you for the feedback and for sharing your code. I will continue improving my code and further develop my rust skills; your implementation inspired me.

Cheers!

---

<div class="post-metadata">

**Author:** ![didi270](https://avatars.discourse-cdn.com/v4/letter/d/5fc32e/32.png) [@didi270](https://discourse.openrobotics.org/u/didi270)\
**Post date:** [April 14, 2024, 6:59am UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/8 "2024-04-14T06:59:13Z")

</div>

I would also recommend to remove the unsafe parts. The reason that the access to `static mut` is unsafe, is because it could be changed from any thread without proper synchronisation. However, you are not using multiple threads, you have only the main thread, so you can move all the mutable state to your main function and avoid the lazy\_statics completely. Further I recommend to look into the use of enums within Rust. They are really powerful and could make your key input parsing code much more concise

---

<div class="post-metadata">

**Author:** ![Atharva\_Bhorpe](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/atharva_bhorpe/32/13338_2.png) [@Atharva\_Bhorpe](https://discourse.openrobotics.org/u/Atharva_Bhorpe)\
**Post date:** [April 24, 2024, 1:01pm UTC](https://discourse.openrobotics.org/t/teleop-keyboard-node-in-rust/36555/9 "2024-04-24T13:01:03Z")

</div>

Thank you for these valuable suggestions!  
I was banging my head to find solutions to eliminate the `unsafe` parts.  
Also, to use enums in this case is an amazing excuse for me to learn about and experiment with enums.  
I will try these solutions and update if there are any queries/successes.

Cheers!
