Source code for suboptimumg.vehicle.powertrain.powertrain
import numpy as np
from ...constants import *
from ..models import VehicleModel
[docs]
class Motor:
def __init__(
self,
vehicle_model: VehicleModel,
):
self.vehicle_model = vehicle_model
self.params = vehicle_model.pwrtn.motor
[docs]
def get_torque_at_rpm(self, motor_rpm: float) -> float:
"""
Get motor torque at a given RPM using piecewise function.
The torque curve has three regions:
1. Flat region (0 to fw_rpm): constant max torque
2. Linear decay (fw_rpm to max_rpm): torque decreases linearly
3. Zero region (above max_rpm): no torque
Parameters
----------
motor_rpm : float
Motor RPM
Returns
-------
float
Motor torque at the given RPM (Nm)
"""
if motor_rpm < self.params.fw_rpm:
# Pre-field weakening: constant max torque
return self.params.max_torque
elif motor_rpm <= self.params.max_rpm:
# Field weakening: linear decrease from max_torque to fw_torque
# Handle edge case where fw_rpm == max_rpm (no field weakening region)
if self.params.max_rpm == self.params.fw_rpm:
return self.params.max_torque
slope = (self.params.max_torque - self.params.fw_torque) / (
self.params.max_rpm - self.params.fw_rpm
)
return self.params.max_torque - slope * (motor_rpm - self.params.fw_rpm)
else:
# Above max RPM: no torque
return 0.0
[docs]
def calculate_max_ground_force_and_motor_power(self, v, ratio):
"""
Calculates force and power output by the motor.
Parameters
----------
v : float
Velocity of the vehicle (m/s)
ratio : float
Gear ratio
Returns
-------
ground_force : float
Force at the ground (N)
motor_power : float
Motor power output (W)
"""
tire_cir = circumference(
in_to_m(self.vehicle_model.tires.tire_radius)
) # meters
# Calculate motor RPM from vehicle velocity
rot_per_sec = v / tire_cir
wheel_rpm = rot_per_sec * 60
motor_rpm = wheel_rpm * ratio # rpm_out = rpm_in / ratio
# Get motor torque from piecewise function
motor_torque = self.get_torque_at_rpm(motor_rpm)
# Clamp motor torque with power limit
motor_torque = min(
motor_torque, self.params.pow_lim / max(rpm_to_rad_s(motor_rpm), 1e-6)
)
# Convert to wheel torque
wheel_torque = (
motor_torque * ratio * self.powertrain_efficiency()
) # torque_out = torque_in * ratio * efficiency
ground_force = wheel_torque / in_to_m(self.vehicle_model.tires.tire_radius)
# Calculate force and power
return ground_force, motor_torque * rpm_to_rad_s(motor_rpm)
[docs]
def powertrain_efficiency(self):
"""
Calculates the system efficiency of the powertrain.
Uses system efficiency if it is set, otherwise uses the product of the individual efficiencies.
Returns
-------
float
System efficiency of the powertrain
"""
# TODO: DIFF EFFICIENCY is set to 100% for now
if self.params.efficiency_method == "sys":
return self.params.system_efficiency
elif self.params.efficiency_method == "indiv":
return (
self.params.moc_efficiency
* self.params.motor_efficiency
* self.params.chain_efficiency
* self.params.diff_efficiency
)
else:
raise ValueError(
"Invalid efficiency method. Must be 'sys' or 'indiv'. Got: ",
self.params.efficiency_method,
)
[docs]
class Powertrain:
def __init__(
self,
vehicle_model: VehicleModel,
):
self.vehicle_model = vehicle_model
self.params = vehicle_model.pwrtn
# Construct Motor object
self.motor = Motor(vehicle_model=vehicle_model)