Skip to content

Rust bindings to the Webots libcontroller C API. Lets you write Webots controllers in Rust.

License

MIT and 2 other licenses found

Licenses found

MIT
LICENSE
Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

katharostech/webots-rust

Webots libcontroller Rust Bindings

This is a reference project that shows how to build controllers for the Webots robot simulator using the Rust programming language.

Running The Example

First you must install Webots & Rust.

You can run the example by:

  1. Install Webots & Rust.
  2. Clone this repository and switch folders to it.
  3. Run cargo build --example rust_epuck_controller
  4. Open up the example world in the sample_project folder in Webots, and run the simulation.

If you make changes, simply re-build the example and restart the simulation!

Using in Your Own Project

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!

Simple Controller Example

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>();
}

How it Works

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!

License

This project is licensed under either of

at your option.

Contribution

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.

About

Rust bindings to the Webots libcontroller C API. Lets you write Webots controllers in Rust.

Topics

Resources

License

MIT and 2 other licenses found

Licenses found

MIT
LICENSE
Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published