Experiments with Rust for FRC Robots

Hi everyone,

I’ve been experimenting with Rust for the programming of FRC robots recently, including some async paradigms, and figured I’d share here.

-Jaci

32 Likes

This is really cool!

The button bindings could use a little bit of sugar I think (less-abstract method names would help a lot).

How much do you have to learn about data ownership etc in order to effectively use Rust’s async/await in ways that modify mutually-visible state?

See also First Rust Competition: GitHub - first-rust-competition/first-rust-competition: Rusty Robots: https://crates.io/crates/wpilib and https://crates.io/crates/cargo-frc

1 Like

Yea I think you might be right. There is an .edge method, but it requires a mutable reference as opposed to moving ownership of the button, meaning you have to keep track of the button separately.

As for how much you have to learn, quite a bit. There’s a lot of nuance to object ownership in async frameworks, especially in Rust given the borrow checker etc. Thankfully, there are ways to make that a little easier (using tokio_scoped for example), but you do have to be familiar with interior mutability if you want to have an asyncronous system (since you can’t mutably borrow &mut self from multiple tasks at once, you have to wrap your internal stuff in a Mutex or preferrably an RwLock so you can mutate them). All that being said, there’s no reason you have to use async - robot.rs also provides the behaviour system which is similar to command-based programming.

Something I realise I just failed to touch on is how good Rust is for tracking stateful subsystems. Being able to associate data with enum variants is a godsend when you’re dealing with different control modes, and almost makes it worth it in and of itself:

#[derive(Debug, Clone)]
pub enum ElevatorState {
  Idle,
  Manual { voltage: f64 },
  HeightControl { height: f64 },
}
10 Likes

@Jaci very intriguing concept but it looks like imjac.in/ta is currently down? (sorry, would’ve been a PM except I don’t seem to have that button yet)

1 Like

Yes, sorry I’m currently moving house so my server is down. You can read it here on the GitHub repo: imjac.in-ta/2023-03-22-rust-for-frc.markdown at master · JaciBrunning/imjac.in-ta · GitHub

4 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.