You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					129 lines
				
				5.4 KiB
			
		
		
			
		
	
	
					129 lines
				
				5.4 KiB
			| 
											2 years ago
										 | import time
 | ||
| 
											6 years ago
										 | import threading
 | ||
|  | 
 | ||
| 
											2 years ago
										 | from openpilot.common.params import Params
 | ||
| 
											2 years ago
										 | from openpilot.system.hardware import HARDWARE
 | ||
| 
											2 years ago
										 | from openpilot.common.swaglog import cloudlog
 | ||
| 
											1 year ago
										 | from openpilot.system.statsd import statlog
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											3 years ago
										 | CAR_VOLTAGE_LOW_PASS_K = 0.011 # LPF gain for 45s tau (dt/tau / (dt/tau + 1))
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											5 years ago
										 | # While driving, a battery charges completely in about 30-60 minutes
 | ||
|  | CAR_BATTERY_CAPACITY_uWh = 30e6
 | ||
|  | CAR_CHARGING_RATE_W = 45
 | ||
|  | 
 | ||
| 
											3 years ago
										 | VBATT_PAUSE_CHARGING = 11.8           # Lower limit on the LPF car battery voltage
 | ||
| 
											5 years ago
										 | MAX_TIME_OFFROAD_S = 30*3600
 | ||
| 
											5 years ago
										 | MIN_ON_TIME_S = 3600
 | ||
| 
											3 years ago
										 | DELAY_SHUTDOWN_TIME_S = 300 # Wait at least DELAY_SHUTDOWN_TIME_S seconds after offroad_time to shutdown.
 | ||
| 
											3 years ago
										 | VOLTAGE_SHUTDOWN_MIN_OFFROAD_TIME_S = 60
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 | class PowerMonitoring:
 | ||
| 
											6 years ago
										 |   def __init__(self):
 | ||
| 
											5 years ago
										 |     self.params = Params()
 | ||
| 
											6 years ago
										 |     self.last_measurement_time = None           # Used for integration delta
 | ||
| 
											5 years ago
										 |     self.last_save_time = 0                     # Used for saving current value in a param
 | ||
| 
											6 years ago
										 |     self.power_used_uWh = 0                     # Integrated power usage in uWh since going into offroad
 | ||
|  |     self.next_pulsed_measurement_time = None
 | ||
| 
											4 years ago
										 |     self.car_voltage_mV = 12e3                  # Low-passed version of peripheralState voltage
 | ||
|  |     self.car_voltage_instant_mV = 12e3          # Last value of peripheralState voltage
 | ||
| 
											6 years ago
										 |     self.integration_lock = threading.Lock()
 | ||
|  | 
 | ||
| 
											5 years ago
										 |     car_battery_capacity_uWh = self.params.get("CarBatteryCapacity")
 | ||
|  |     if car_battery_capacity_uWh is None:
 | ||
|  |       car_battery_capacity_uWh = 0
 | ||
|  | 
 | ||
|  |     # Reset capacity if it's low
 | ||
|  |     self.car_battery_capacity_uWh = max((CAR_BATTERY_CAPACITY_uWh / 10), int(car_battery_capacity_uWh))
 | ||
|  | 
 | ||
| 
											6 years ago
										 |   # Calculation tick
 | ||
| 
											2 years ago
										 |   def calculate(self, voltage: int | None, ignition: bool):
 | ||
| 
											6 years ago
										 |     try:
 | ||
| 
											2 years ago
										 |       now = time.monotonic()
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 |       # If peripheralState is None, we're probably not in a car, so we don't care
 | ||
| 
											3 years ago
										 |       if voltage is None:
 | ||
| 
											6 years ago
										 |         with self.integration_lock:
 | ||
|  |           self.last_measurement_time = None
 | ||
|  |           self.next_pulsed_measurement_time = None
 | ||
|  |           self.power_used_uWh = 0
 | ||
| 
											6 years ago
										 |         return
 | ||
|  | 
 | ||
| 
											5 years ago
										 |       # Low-pass battery voltage
 | ||
| 
											3 years ago
										 |       self.car_voltage_instant_mV = voltage
 | ||
|  |       self.car_voltage_mV = ((voltage * CAR_VOLTAGE_LOW_PASS_K) + (self.car_voltage_mV * (1 - CAR_VOLTAGE_LOW_PASS_K)))
 | ||
| 
											4 years ago
										 |       statlog.gauge("car_voltage", self.car_voltage_mV / 1e3)
 | ||
| 
											5 years ago
										 | 
 | ||
|  |       # Cap the car battery power and save it in a param every 10-ish seconds
 | ||
|  |       self.car_battery_capacity_uWh = max(self.car_battery_capacity_uWh, 0)
 | ||
|  |       self.car_battery_capacity_uWh = min(self.car_battery_capacity_uWh, CAR_BATTERY_CAPACITY_uWh)
 | ||
|  |       if now - self.last_save_time >= 10:
 | ||
| 
											2 years ago
										 |         self.params.put_nonblocking("CarBatteryCapacity", str(int(self.car_battery_capacity_uWh)))
 | ||
| 
											5 years ago
										 |         self.last_save_time = now
 | ||
|  | 
 | ||
| 
											6 years ago
										 |       # First measurement, set integration time
 | ||
| 
											6 years ago
										 |       with self.integration_lock:
 | ||
|  |         if self.last_measurement_time is None:
 | ||
|  |           self.last_measurement_time = now
 | ||
|  |           return
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 |       if ignition:
 | ||
| 
											5 years ago
										 |         # If there is ignition, we integrate the charging rate of the car
 | ||
|  |         with self.integration_lock:
 | ||
|  |           self.power_used_uWh = 0
 | ||
|  |           integration_time_h = (now - self.last_measurement_time) / 3600
 | ||
|  |           if integration_time_h < 0:
 | ||
|  |             raise ValueError(f"Negative integration time: {integration_time_h}h")
 | ||
|  |           self.car_battery_capacity_uWh += (CAR_CHARGING_RATE_W * 1e6 * integration_time_h)
 | ||
|  |           self.last_measurement_time = now
 | ||
| 
											6 years ago
										 |       else:
 | ||
| 
											5 years ago
										 |         # Get current power draw somehow
 | ||
| 
											4 years ago
										 |         current_power = HARDWARE.get_current_power_draw()
 | ||
| 
											5 years ago
										 | 
 | ||
|  |         # Do the integration
 | ||
|  |         self._perform_integration(now, current_power)
 | ||
| 
											6 years ago
										 |     except Exception:
 | ||
| 
											6 years ago
										 |       cloudlog.exception("Power monitoring calculation failed")
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 |   def _perform_integration(self, t: float, current_power: float) -> None:
 | ||
| 
											6 years ago
										 |     with self.integration_lock:
 | ||
|  |       try:
 | ||
|  |         if self.last_measurement_time:
 | ||
|  |           integration_time_h = (t - self.last_measurement_time) / 3600
 | ||
| 
											6 years ago
										 |           power_used = (current_power * 1000000) * integration_time_h
 | ||
|  |           if power_used < 0:
 | ||
|  |             raise ValueError(f"Negative power used! Integration time: {integration_time_h} h Current Power: {power_used} uWh")
 | ||
|  |           self.power_used_uWh += power_used
 | ||
| 
											5 years ago
										 |           self.car_battery_capacity_uWh -= power_used
 | ||
| 
											6 years ago
										 |           self.last_measurement_time = t
 | ||
|  |       except Exception:
 | ||
|  |         cloudlog.exception("Integration failed")
 | ||
| 
											6 years ago
										 | 
 | ||
|  |   # Get the power usage
 | ||
| 
											4 years ago
										 |   def get_power_used(self) -> int:
 | ||
| 
											6 years ago
										 |     return int(self.power_used_uWh)
 | ||
| 
											5 years ago
										 | 
 | ||
| 
											4 years ago
										 |   def get_car_battery_capacity(self) -> int:
 | ||
| 
											5 years ago
										 |     return int(self.car_battery_capacity_uWh)
 | ||
|  | 
 | ||
|  |   # See if we need to shutdown
 | ||
| 
											2 years ago
										 |   def should_shutdown(self, ignition: bool, in_car: bool, offroad_timestamp: float | None, started_seen: bool):
 | ||
| 
											4 years ago
										 |     if offroad_timestamp is None:
 | ||
| 
											5 years ago
										 |       return False
 | ||
|  | 
 | ||
| 
											2 years ago
										 |     now = time.monotonic()
 | ||
| 
											5 years ago
										 |     should_shutdown = False
 | ||
| 
											3 years ago
										 |     offroad_time = (now - offroad_timestamp)
 | ||
|  |     low_voltage_shutdown = (self.car_voltage_mV < (VBATT_PAUSE_CHARGING * 1e3) and
 | ||
|  |                             offroad_time > VOLTAGE_SHUTDOWN_MIN_OFFROAD_TIME_S)
 | ||
|  |     should_shutdown |= offroad_time > MAX_TIME_OFFROAD_S
 | ||
|  |     should_shutdown |= low_voltage_shutdown
 | ||
| 
											3 years ago
										 |     should_shutdown |= (self.car_battery_capacity_uWh <= 0)
 | ||
|  |     should_shutdown &= not ignition
 | ||
|  |     should_shutdown &= (not self.params.get_bool("DisablePowerDown"))
 | ||
|  |     should_shutdown &= in_car
 | ||
| 
											3 years ago
										 |     should_shutdown &= offroad_time > DELAY_SHUTDOWN_TIME_S
 | ||
| 
											3 years ago
										 |     should_shutdown |= self.params.get_bool("ForcePowerDown")
 | ||
| 
											5 years ago
										 |     should_shutdown &= started_seen or (now > MIN_ON_TIME_S)
 | ||
| 
											5 years ago
										 |     return should_shutdown
 |