RustPhysicsMQ/src/main.rs

126 lines
3.0 KiB
Rust
Raw Normal View History

2025-11-23 18:40:48 +00:00
mod engine;
use engine::*;
2025-11-23 23:43:48 +00:00
mod camera;
2026-02-05 20:26:30 +00:00
mod config;
2025-12-07 00:41:00 +00:00
mod drone;
2026-02-05 20:26:30 +00:00
mod helpers;
2025-11-28 20:39:09 +00:00
mod rendering;
2026-02-05 20:26:30 +00:00
mod simulation;
2025-11-23 23:43:48 +00:00
2025-11-23 18:40:48 +00:00
mod graphics_util;
2026-02-05 20:26:30 +00:00
use crate::drone::input::*;
use crate::simulation::{SimMode, Simulation};
2025-11-28 20:39:09 +00:00
2026-02-05 20:26:30 +00:00
use crate::config::SimulationConfig;
use helpers::list_files;
use std::fs;
const INPUTS_DIR: &str = "inputs/";
const CONFIGS_DIR: &str = "configurations/";
const RESULTS_DIR: &str = "results/";
use std::path::PathBuf;
use std::thread::JoinHandle;
fn main() {
run_batch();
}
fn run_batch() {
let _ = fs::remove_dir_all(RESULTS_DIR);
let _ = fs::create_dir_all(RESULTS_DIR);
let input_files = list_files(INPUTS_DIR);
let config_files = list_files(CONFIGS_DIR);
let mut handles: Vec<JoinHandle<()>> = Default::default();
for input_path in input_files {
for config_path in &config_files {
let cp = config_path.clone();
let ip = input_path.clone();
handles.push(std::thread::spawn(move || {
run(&ip, &cp);
}));
}
}
for handle in handles {
handle.join().unwrap();
}
2026-02-05 20:26:30 +00:00
println!("All simulations completed.");
}
2026-02-05 20:26:30 +00:00
fn run(input_path: &PathBuf, config_path: &PathBuf) {
let input_name = input_path.file_stem().unwrap().to_string_lossy();
2026-01-06 16:41:14 +00:00
2026-02-05 20:26:30 +00:00
let inputs = InputRecording::load_inputs_from_csv(input_path.to_str().unwrap())
.expect("Failed to load input recording");
let config_name = config_path.file_stem().unwrap().to_string_lossy();
let config: SimulationConfig =
toml::from_str(&fs::read_to_string(config_path).unwrap()).expect("Invalid config file");
println!(
"Running simulation: input={} config={}",
input_name, config_name
);
2026-02-05 20:26:30 +00:00
let mut world = World::new(config.tickrate);
let drone = drone::Drone::new(
&mut world,
2026-02-05 20:26:30 +00:00
Box::new(drone::stacked::StackedController::new(config.clone())),
drone::MotorCharacteristics {
2026-02-05 20:26:30 +00:00
max_thrust: config.max_thrust,
max_torque: config.max_torque,
time_constant: config.time_constant,
..Default::default()
},
2026-02-05 20:26:30 +00:00
config.mass,
);
2026-02-05 20:26:30 +00:00
let result_file = format!("{}/{}_{}.csv", RESULTS_DIR, input_name, config_name);
2026-02-05 20:26:30 +00:00
let mut sim = Simulation::new(
drone,
world,
SimMode::Playback(inputs.clone(), 0.0),
Some(result_file),
config.drone_tick_rate,
);
2026-02-05 20:26:30 +00:00
sim.run().unwrap();
}
async fn run_record(output: String) {
println!("Recording inputs to {}", output);
2026-02-05 20:26:30 +00:00
let tickrate = 60000.0;
let mut world = World::new(tickrate);
2026-02-05 20:26:30 +00:00
let drone = drone::Drone::new(
&mut world,
Box::new(drone::pidcontroller::PIDController {
..Default::default()
}),
drone::MotorCharacteristics {
max_thrust: 2.6,
max_torque: 0.5,
..Default::default()
},
0.350,
);
2025-12-07 00:41:00 +00:00
2026-02-05 20:26:30 +00:00
let mut sim = Simulation::new(
drone,
world,
SimMode::Record(InputRecording::default(), output),
None,
600,
);
2026-01-06 16:41:14 +00:00
2026-02-05 20:26:30 +00:00
sim.run_and_render().await.unwrap();
2026-01-06 16:41:14 +00:00
}