diff --git a/cyclinglib/gain_ratio.py b/cyclinglib/gain_ratio.py new file mode 100644 index 0000000..9c23856 --- /dev/null +++ b/cyclinglib/gain_ratio.py @@ -0,0 +1,32 @@ +from cyclinglib.gear_ratio import GearRatio +from math import pi + +from cyclinglib.speed import Speed + + +class GainRatio: + + def __init__(self, gear_ratio: GearRatio, wheel_circumference_mm: float, + crank_length_mm: float): + self._revolution_length_mm: float = crank_length_mm * 2 * pi + circumference_ratio: float = wheel_circumference_mm / self._revolution_length_mm + self._gain_ratio: float = circumference_ratio * gear_ratio._gear_ratio + + def speed_at_cadence(self, cadence: int) -> Speed: + """ + Cadence is specified in revolutions per minute. + """ + revolutions_per_second = cadence / 60 + meters_per_second_crank = revolutions_per_second * ( + self._revolution_length_mm / 1_000) + return Speed(meters_per_second_crank * self._gain_ratio) + + def __str__(self) -> str: + return f"Gain ratio: {self._gain_ratio}" + + +def calculate_gain_ratio(front_teeth: int, rear_teeth: int, + wheel_circumference_mm: float, + crank_length_mm: float) -> GainRatio: + return GainRatio(GearRatio(front_teeth, rear_teeth), + wheel_circumference_mm, crank_length_mm) diff --git a/cyclinglib/gear_ratio.py b/cyclinglib/gear_ratio.py new file mode 100644 index 0000000..e41cb32 --- /dev/null +++ b/cyclinglib/gear_ratio.py @@ -0,0 +1,7 @@ +class GearRatio: + + def __init__(self, front_teeth: int, rear_teeth: int): + self._gear_ratio: float = front_teeth / rear_teeth + + def __str__(self) -> str: + return f"Gear ratio: {self._gear_ratio}" diff --git a/cyclinglib/speed.py b/cyclinglib/speed.py new file mode 100644 index 0000000..36fb21d --- /dev/null +++ b/cyclinglib/speed.py @@ -0,0 +1,19 @@ +class Speed: + + def __init__(self, meters_per_second: float): + self._meters_per_second: float = meters_per_second + + def meters_per_second(self): + return self._meters_per_second + + def kilometers_per_hour(self): + return self._meters_per_second * 3.6 + + def miles_per_hour(self): + return self._meters_per_second * 2.23693629 + + def __str__(self) -> str: + mps = self.meters_per_second() + kph = self.kilometers_per_hour() + mph = self.miles_per_hour() + return f"m/s: {mps} km/h: {kph} mi/h: {mph}"