Bevy MuJoCo

Crates.io MIT/Apache 2.0 Crates.io Rust

https://user-images.githubusercontent.com/97428129/210613348-82a5e59d-96af-42a9-a94a-c47093eb8297.mp4

This is work in progress

Import MJCF files into Bevy and run simulations with MuJoCo.

Implementation Notes

MuJoCo has 2 modes with different coordinate systems for bodies

  1. paused mode where all translations and rotations are extracted from mj_Model in MuJoCo-Rust as body.pos, body.quat in parent's body coordinate system. To make them work nice with bevy the body structure from mujoco has to be transformed to a tree structure with body_tree() call. Then body_tree is spawned into the bevy world recursively — a nice contraption to do it in setup_mujoco.

  2. simulation mode where translations are extracted from sim.xpos() and sim.xquat() — and this time they are in global frame. Since bodies are spawned hierarchically translations and rotations need to be converted to a parent coordinate system — it happens in simulate_physics.

Getting Started

Prerequisites

Usage

```rust // 1. Import bevymujoco use bevymujoco::*; // 2. Setup bevymujoco Plugin. MuJoCo Plugin would spawn entities to the world fn main() { App::new() .addplugins(DefaultPlugins) .insertresource(MuJoCoPluginSettings { modelxmlpath: "assets/unitreea1/scene.xml".tostring(), pausesimulation: false, targetfps: 600.0, // this is not actual fps (bug in bevymujoco), // the bigger the value, the slower the simulation }) .addplugins(MuJoCoPlugin) .addsystems(Startup, setup) .addsystems(Update, robotcontrolloop) .run(); } // 3. You can control your robots here fn robotcontrolloop(mut mujocoresources: ResMut) { // prepare simulation data for the NN let qpos = mujocoresources.state.qpos.clone(); let qvel = mujocoresources.state.qvel.clone(); let cfrcext = mujocoresources.state.cfrc_ext.clone();

// Compute input -> control values here and fill control
// ...
let mut control: Vec<f32> = Vec::new();

mujoco_resources.control.data = input_vec;

} ```

copy build.rs to root of your project to use in with Windows environments. it will copy mujoco.dll to a build dir of your application

To run tests and example initialize mujoco_menagerie submobule with

bash cd bevy_mujoco git submodule init git submodule update

See example for simulating Unitree A1 robot.