Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes and cleanups for balancebot sim #137

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/cu_rp_balancebot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ bevy_mod_picking = { version = "0.20.1", features = ["backend_avian"], optional
avian3d = { version = "0.1.2", default-features = false, features = ["bevy_scene", "collider-from-mesh", "debug-plugin", "parallel"], optional = true }
parry3d = { version = "0.17.1", optional = true }
cached-path = { version = "0.6.1", optional = true }
iyes_perf_ui = { version = "0.3.0", optional = true }

[features]
default = ["logreader", "sim"]
# generates an executable to read the logs
logreader = ["cu29-export"]
# dependencies to build to matrix for copper
sim = ["bevy", "bevy_mod_picking", "avian3d", "parry3d", "cached-path"]
perf-ui = ["iyes_perf_ui"]

[[bin]]
name = "balancebot"
Expand Down
62 changes: 62 additions & 0 deletions examples/cu_rp_balancebot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# BalanceBot: this is a full Copper demo robot

with:

- a physical robot implementation
- a simulation implementation
- a resimulation demoing the deterministic replay
- a log export

## To run the simulation

```bash
$ cd examples/cu_rp_balancebot
$ cargo run --release
```

See the UI help for the navigation.

To debug the game engine side you can add a perf overlay with:

```bash
$ cargo run --release --features perf-ui
```

## To run the resimulation

(you need at least a log in `logs` for example from a simulation run).

```bash
$ cd examples/cu_rp_balancebot
$ cargo run --bin balancebot_resim --release
```

It will recreate the logs from only the inputs of the previous run in `logs/balancebot_resim*.copper`.

## To run on the real robot

You will need to cross compile for Arm:

```bash
cargo build --target armv7-unknown-linux-musleabihf --release --no-default-features
```

Be sure you save your log string index:

```bash
cp -rv ../../target/armv7-unknown-linux-musleabihf/release/cu29_log_index . # or anywhere you want
```

Deploy on the target:

```bash
scp ../../target/armv7-unknown-linux-musleabihf/release/balancebot copperconfig.ron copper7:copper/ # change to match your target
```

## To export logs

```bash
$ cd examples/cu_rp_balancebot
$ cargo run --bin balancebot-logreader --release
```

8 changes: 0 additions & 8 deletions examples/cu_rp_balancebot/copper7-dev.sh

This file was deleted.

7 changes: 0 additions & 7 deletions examples/cu_rp_balancebot/copper7-prod.sh

This file was deleted.

3 changes: 0 additions & 3 deletions examples/cu_rp_balancebot/extract_pids.sh

This file was deleted.

8 changes: 7 additions & 1 deletion examples/cu_rp_balancebot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pub mod tasks;
use cu29_derive::copper_runtime;
use cu29_helpers::basic_copper_setup;
use cu29_log_derive::debug;
use std::path::PathBuf;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};

#[copper_runtime(config = "copperconfig.ron")]
Expand All @@ -15,6 +16,11 @@ const SLAB_SIZE: Option<usize> = Some(1 * 1024 * 1024 * 1024);
fn main() {
static STOP_FLAG: AtomicBool = AtomicBool::new(false);
let logger_path = "logs/balance.copper";

if let Some(parent) = Path::new(logger_path).parent() {
fs::create_dir_all(parent).expect("Failed to create logs directory");
}

let copper_ctx = basic_copper_setup(&PathBuf::from(logger_path), SLAB_SIZE, false, None)
.expect("Failed to setup logger.");
debug!("Logger created at {}.", path = logger_path);
Expand Down
37 changes: 34 additions & 3 deletions examples/cu_rp_balancebot/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ use bevy::pbr::{
use bevy::prelude::*;
use bevy_mod_picking::prelude::*;
use cached_path::{Cache, ProgressBar};

#[cfg(feature = "perf-ui")]
use iyes_perf_ui::PerfUiPlugin;

#[cfg(feature = "perf-ui")]
use iyes_perf_ui::entries::PerfUiCompleteBundle;

use std::path::Path;
use std::{fs, io};

Expand Down Expand Up @@ -59,12 +66,16 @@ pub struct Cart;
pub struct Rod;

pub fn build_world(app: &mut App) -> &mut App {
app.insert_resource(Msaa::Off)
let app = app
.insert_resource(Msaa::Off)
.add_plugins((
DefaultPickingPlugins,
PhysicsPlugins::default().with_length_unit(1000.0),
// EditorPlugin::default(),
))
// we want Bevy to measure these values for us:
.add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin)
.add_plugins(bevy::diagnostic::EntityCountDiagnosticsPlugin)
.insert_resource(DefaultOpaqueRendererMethod::deferred())
.insert_resource(SimulationState::Running)
.insert_resource(CameraControl {
Expand All @@ -76,12 +87,17 @@ pub fn build_world(app: &mut App) -> &mut App {
.insert_resource(Time::<Physics>::default())
.add_systems(Startup, setup_scene)
.add_systems(Startup, setup_ui)
.add_systems(Startup, setup_entities)
.add_systems(Update, setup_entities) // Wait for the cart entity to be loaded
.add_systems(Update, toggle_simulation_state)
.add_systems(Update, camera_control_system)
.add_systems(Update, update_physics)
.add_systems(Update, global_cart_drag_listener)
.add_systems(PostUpdate, reset_sim)
.add_systems(PostUpdate, reset_sim);

#[cfg(feature = "perf-ui")]
app.add_plugins(PerfUiPlugin);

app
}

fn ground_setup(
Expand Down Expand Up @@ -239,6 +255,9 @@ fn setup_scene(
Fxaa::default(),
));

// add the delayed setup flag
commands.insert_resource(SetupCompleted(false));

// add a ground
ground_setup(&mut commands, &mut meshes, &mut materials);
}
Expand Down Expand Up @@ -289,6 +308,8 @@ fn setup_ui(mut commands: Commands) {
},
));
});
#[cfg(feature = "perf-ui")]
commands.spawn(PerfUiCompleteBundle::default());
}

// This needs to match an object / parent object name in the GLTF file (in blender this is the object name).
Expand Down Expand Up @@ -329,12 +350,20 @@ fn global_cart_drag_listener(
}
}

#[derive(Resource)]
struct SetupCompleted(bool);

fn setup_entities(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
query: Query<(Entity, &Name), Without<Cart>>,
mut setup_completed: ResMut<SetupCompleted>,
) {
if setup_completed.0 {
return;
}

// The cart entity will be loaded from the GLTF file, we need to wait for it to be loaded
let cart_entity = match try_to_find_cart_entity(query) {
Some(entity) => entity,
Expand Down Expand Up @@ -442,6 +471,8 @@ fn setup_entities(
transform: Transform::from_xyz(2.0, 4.0, 2.0),
..default()
});

setup_completed.0 = true; // Mark as completed
}

fn reset_sim(
Expand Down
Loading