Created
November 23, 2023 05:52
-
-
Save de-sh/5595270e65337fe8d5360b937fe5f7e6 to your computer and use it in GitHub Desktop.
Vehicle Dynamics, a rust library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{default, f64::consts::PI}; | |
use rand::{thread_rng, Rng}; | |
const BASE_RPM: f64 = 750.0; | |
const MAX_RPM: f64 = 5000.0; | |
const WHEEL_RADIUS: f64 = 0.4; // in m | |
const WHEEL_CIRCUMFERENCE: f64 = 2.0 * PI * WHEEL_RADIUS; // in m | |
#[derive(Debug, Default)] | |
enum Gear { | |
#[default] | |
Neutral, | |
First, | |
Second, | |
Third, | |
Fourth, | |
Fifth, | |
Reverse, | |
} | |
#[derive(Debug, Default)] | |
enum HandBrake { | |
Disengaged, | |
Half, | |
#[default] | |
Full, | |
} | |
#[derive(Debug, Default)] | |
struct Car { | |
speed: f64, | |
engine_rpm: u32, | |
wheel_rpm: u32, | |
gear: Gear, | |
accelerator_position: f64, | |
brake_position: f64, | |
clutch_position: f64, | |
hand_brake: HandBrake, | |
} | |
impl Car { | |
fn up_shift(&mut self) { | |
if self.engine_rpm > 3000 { | |
self.gear = match self.gear { | |
Gear::Reverse => Gear::Neutral, | |
Gear::Neutral => Gear::First, | |
Gear::First => Gear::Second, | |
Gear::Second => Gear::Third, | |
Gear::Third => Gear::Fourth, | |
Gear::Fourth => Gear::Fifth, | |
_ => return, | |
}; | |
} | |
} | |
fn down_shift(&mut self) { | |
if self.engine_rpm < 1000 { | |
self.gear = match self.gear { | |
Gear::Fifth => Gear::Fourth, | |
Gear::Fourth => Gear::Third, | |
Gear::Third => Gear::Second, | |
Gear::Second => Gear::First, | |
Gear::First => Gear::Neutral, | |
Gear::Neutral => Gear::Reverse, | |
_ => return, | |
}; | |
} | |
} | |
fn light_acceleration(&mut self) { | |
self.accelerator_position = thread_rng().gen_range(0.0..0.5); | |
} | |
fn hard_acceleration(&mut self) { | |
self.accelerator_position = thread_rng().gen_range(0.5..1.0); | |
} | |
fn transmission_ratio(&self) -> f64 { | |
match self.gear { | |
Gear::Reverse => -0.75, | |
Gear::Neutral => 0.0, | |
Gear::First => 0.75, | |
Gear::Second => 1.25, | |
Gear::Third => 1.75, | |
Gear::Fourth => 2.25, | |
Gear::Fifth => 3.0, | |
} | |
} | |
fn update_rpm(&mut self) { | |
let rpm = (BASE_RPM + (MAX_RPM - BASE_RPM) * self.accelerator_position); | |
self.engine_rpm = rpm as u32; | |
self.wheel_rpm = (rpm / self.transmission_ratio()) as u32; | |
} | |
fn update_speed(&mut self) { | |
let speed = self.wheel_rpm; | |
} | |
fn light_braking(&mut self) { | |
self.brake_position = thread_rng().gen_range(0.0..0.5); | |
} | |
fn hard_braking(&mut self) { | |
self.brake_position = thread_rng().gen_range(0.5..1.0); | |
} | |
fn engage_handbrake(&mut self) { | |
self.hand_brake = | |
if thread_rng().gen_bool(0.05) { HandBrake::Half } else { HandBrake::Full }; | |
} | |
fn disengage_handbrake(&mut self) { | |
self.hand_brake = HandBrake::Disengaged; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment