This is a reference project that shows how to build controllers for the Webots robot simulator using the Rust programming language.
First you must install Webots & Rust.
You can run the example by:
- Install Webots & Rust.
- Clone this repository and switch folders to it.
- Run
cargo build --example rust_epuck_controller
- Open up the
example
world in thesample_project
folder in Webots, and run the simulation.
If you make changes, simply re-build the example and restart the simulation!
Warning: There is a
webots
crates.io package, but that is not this crate. Currently this crate is only published on GitHub, and we may change the name to prevent confusion with the crates.io crate.
Cargo.toml
:
[dependencies]
webots = { git = "https://github.com/katharostech/webots-rust" }
Now you can compile your crate to create a Webots controller.
Once you have compiled the crate, you must copy or link your controller to a Webots project folder.
Webots projects always have a folder named worlds
and you will need to have a controllers
folder next to that. Inside of that create a folder named after your controller, such as my_rust_controller
. Finally, copy your built controller into that folder, and make sure it has the same name as your folder.
Now, in the Webots program, you can select my_rust_controller
as the controller for any robot!
Here's one of the simplest controllers, which works to make the NAO model robot "wave":
use std::time::Duration;
use webots::prelude::*;
struct Participant {
arm_up: bool,
arm_motor: Motor,
}
impl Robot for Participant {
fn time_step(&self) -> std::time::Duration {
Duration::from_secs(1)
}
fn init() -> Self {
let arm_motor = Motor::new("RShoulderPitch");
arm_motor.set_velocity(arm_motor.max_velocity());
Participant {
arm_motor,
arm_up: false,
}
}
fn step(&mut self) {
if !self.arm_up {
self.arm_motor.set_position(self.arm_motor.max_position());
} else {
self.arm_motor.set_position(self.arm_motor.min_position());
}
self.arm_up = !self.arm_up;
}
}
fn main() {
webots::run::<Participant>();
}
Bindgen is used to generate bindings to the Webots C library at compile time. It will look in standard OS installation directories, or else use the WEBOTS_PATH
environment variable to find your local Webots installation. For now, you must have it installed locally to compile this crate. We may change that soon.
You can find the raw, generated bindings in the webots::sys
module, and the rest of the crate contains idiomatic Rust bindings. Though the idiomatic bindings are incomplete, they are easy to write, and we will be extending them as time permits. It's simple to see how they are made, and to add your own if necessary. Pull requests are welcome!
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.