peng_quad/config.rs
1//! Configuration module
2//!
3//! This module contains the configuration for the simulation, quadrotor, PID controller, IMU, maze, camera, mesh, and planner schedule.
4//! The configuration is loaded from a YAML file using the serde library.
5//! The configuration is then used to initialize the simulation, quadrotor, PID controller, IMU, maze, camera, mesh, and planner schedule.
6#[derive(serde::Deserialize)]
7/// Configuration for the simulation
8pub struct Config {
9 /// Simulation configuration
10 pub simulation: SimulationConfig,
11 /// Quadrotor configuration
12 pub quadrotor: QuadrotorConfig,
13 /// PID Controller configuration
14 pub pid_controller: PIDControllerConfig,
15 /// IMU configuration
16 pub imu: ImuConfig,
17 /// Maze configuration
18 pub maze: MazeConfig,
19 /// Camera configuration
20 pub camera: CameraConfig,
21 /// Mesh configuration
22 pub mesh: MeshConfig,
23 /// Planner schedule configuration
24 pub planner_schedule: Vec<PlannerStep>,
25 /// Rerun blueprint path
26 pub rerun_blueprint: String,
27 /// Maximum number of threads for rendering
28 pub max_render_threads: Option<usize>,
29 /// Use rerun.io for recording
30 pub use_rerun: bool,
31 /// Render depth
32 pub render_depth: bool,
33 /// MultiThreading depth rendering
34 pub use_multithreading_depth_rendering: bool,
35 /// Use RK4 for updating quadrotor dynamics_with_controls
36 pub use_rk4_for_dynamics_control: bool,
37 /// Use RK4 for updating quadrotor dynamics without controls
38 pub use_rk4_for_dynamics_update: bool,
39}
40
41#[derive(serde::Deserialize)]
42/// Configuration for a planner step
43pub struct PlannerStep {
44 /// Step number that the planner should be executed (Unit: ms)
45 pub step: usize,
46 /// Type of planner to use
47 pub planner_type: String,
48 /// Parameters for the planner
49 pub params: serde_yaml::Value,
50}
51
52#[derive(serde::Deserialize)]
53/// Configuration for the simulation
54pub struct SimulationConfig {
55 /// Control frequency in Hz
56 pub control_frequency: usize,
57 /// Simulation frequency in Hz
58 pub simulation_frequency: usize,
59 /// Log frequency in Hz
60 pub log_frequency: usize,
61 /// Duration of the simulation in seconds
62 pub duration: f32,
63}
64
65#[derive(serde::Deserialize)]
66/// Configuration for the quadrotor
67pub struct QuadrotorConfig {
68 /// Mass of the quadrotor in kg
69 pub mass: f32,
70 /// Gravity in m/s^2
71 pub gravity: f32,
72 /// Drag coefficient in Ns^2/m^2
73 pub drag_coefficient: f32,
74 /// Inertia matrix in kg*m^2
75 pub inertia_matrix: [f32; 9],
76}
77
78#[derive(serde::Deserialize)]
79/// Configuration for the PID controller
80pub struct PIDControllerConfig {
81 /// Position gains
82 pub pos_gains: PIDGains,
83 /// Attitude gains
84 pub att_gains: PIDGains,
85 /// Maximum integral error for position control
86 pub pos_max_int: [f32; 3],
87 /// Maximum integral error for attitude control
88 pub att_max_int: [f32; 3],
89}
90
91#[derive(serde::Deserialize)]
92/// Configuration for PID gains
93pub struct PIDGains {
94 /// Proportional gains
95 pub kp: [f32; 3],
96 /// Integral gains
97 pub ki: [f32; 3],
98 /// Derivative gains
99 pub kd: [f32; 3],
100}
101
102#[derive(serde::Deserialize, Default)]
103/// Configuration for the IMU
104pub struct ImuConfig {
105 /// Accelerometer noise standard deviation
106 pub accel_noise_std: f32,
107 /// Gyroscope noise standard deviation
108 pub gyro_noise_std: f32,
109 /// Accelerometer bias drift standard deviation
110 pub accel_bias_std: f32,
111 /// Gyroscope bias drift standard deviation
112 pub gyro_bias_std: f32,
113}
114
115#[derive(serde::Deserialize)]
116/// Configuration for the maze
117pub struct MazeConfig {
118 /// Upper bounds of the maze in meters (x, y, z)
119 pub upper_bounds: [f32; 3],
120 /// Lower bounds of the maze in meters (x, y, z)
121 pub lower_bounds: [f32; 3],
122 /// Number of obstacles in the maze
123 pub num_obstacles: usize,
124 /// Obstacle velocity maximum bounds in m/s in (x, y, z) directions
125 pub obstacles_velocity_bounds: [f32; 3],
126 /// Obstacle radius bounds in meters (min, max)
127 pub obstacles_radius_bounds: [f32; 2],
128}
129
130#[derive(serde::Deserialize)]
131/// Configuration for the camera
132pub struct CameraConfig {
133 /// Camera resolution in pixels (width, height)
134 pub resolution: (usize, usize),
135 /// Camera field of view in height in degrees
136 pub fov_vertical: f32,
137 /// Camera near clipping plane in meters
138 pub near: f32,
139 /// Camera far clipping plane in meters
140 pub far: f32,
141 /// Camera transform matrix for depth
142 pub rotation_transform: [f32; 9],
143}
144
145#[derive(serde::Deserialize)]
146/// Configuration for the mesh
147pub struct MeshConfig {
148 /// Division of the 2D mesh, the mesh will be division x division squares
149 pub division: usize,
150 /// Spacing between the squares in meters
151 pub spacing: f32,
152}
153/// Implementation of the Config struct
154impl Config {
155 /// Load configuration from a YAML file.
156 /// # Arguments
157 /// * `filename` - The name of the file to load.
158 /// # Returns
159 /// * The configuration object.
160 /// # Errors
161 /// * If the file cannot be read or the YAML cannot be parsed.
162 pub fn from_yaml(filename: &str) -> Result<Self, Box<dyn std::error::Error>> {
163 let contents = std::fs::read_to_string(filename)?;
164 Ok(serde_yaml::from_str(&contents)?)
165 }
166}