diff --git a/common/params.cc b/common/params.cc index 287b5ff..e0e02a6 100644 --- a/common/params.cc +++ b/common/params.cc @@ -243,6 +243,7 @@ std::unordered_map keys = { {"CustomSignals", PERSISTENT}, {"CustomSounds", PERSISTENT}, {"CustomTheme", PERSISTENT}, + {"DeviceShutdown", PERSISTENT}, {"FrogPilotTogglesUpdated", PERSISTENT}, {"GasRegenCmd", PERSISTENT}, {"GoatScream", PERSISTENT}, diff --git a/selfdrive/frogpilot/assets/toggle_icons/icon_time.png b/selfdrive/frogpilot/assets/toggle_icons/icon_time.png new file mode 100644 index 0000000..41cfc0a Binary files /dev/null and b/selfdrive/frogpilot/assets/toggle_icons/icon_time.png differ diff --git a/selfdrive/frogpilot/ui/control_settings.cc b/selfdrive/frogpilot/ui/control_settings.cc index 3594d77..5aafcf6 100644 --- a/selfdrive/frogpilot/ui/control_settings.cc +++ b/selfdrive/frogpilot/ui/control_settings.cc @@ -13,6 +13,7 @@ FrogPilotControlsPanel::FrogPilotControlsPanel(SettingsWindow *parent) : FrogPil {"CESignal", "Turn Signal When Below Highway Speeds", "Switch to 'Experimental Mode' when using turn signals below highway speeds to help assit with turns.", ""}, {"CustomPersonalities", "Custom Driving Personalities", "Customize the driving personality profiles to your driving style.", "../frogpilot/assets/toggle_icons/icon_custom.png"}, + {"DeviceShutdown", "Device Shutdown Timer", "Configure the timer for automatic device shutdown when offroad conserving energy and preventing battery drain.", "../frogpilot/assets/toggle_icons/icon_time.png"}, {"LateralTune", "Lateral Tuning", "Modify openpilot's steering behavior.", "../frogpilot/assets/toggle_icons/icon_lateral_tune.png"}, {"AverageCurvature", "Average Desired Curvature", "Use Pfeiferj's distance-based curvature adjustment for improved curve handling.", ""}, @@ -114,6 +115,13 @@ FrogPilotControlsPanel::FrogPilotControlsPanel(SettingsWindow *parent) : FrogPil relaxedProfile = new FrogPilotDualParamControl(relaxedFollow, relaxedJerk, this, true); addItem(relaxedProfile); + } else if (param == "DeviceShutdown") { + std::map shutdownLabels; + for (int i = 0; i <= 33; ++i) { + shutdownLabels[i] = i == 0 ? "Instant" : i <= 3 ? QString::number(i * 15) + " mins" : QString::number(i - 3) + (i == 4 ? " hour" : " hours"); + } + toggle = new FrogPilotParamValueControl(param, title, desc, icon, 0, 33, shutdownLabels, this, false); + } else if (param == "LateralTune") { FrogPilotParamManageControl *lateralTuneToggle = new FrogPilotParamManageControl(param, title, desc, icon, this); QObject::connect(lateralTuneToggle, &FrogPilotParamManageControl::manageButtonClicked, this, [this]() { diff --git a/selfdrive/thermald/power_monitoring.py b/selfdrive/thermald/power_monitoring.py index 5f0a4fe..32bc1a7 100644 --- a/selfdrive/thermald/power_monitoring.py +++ b/selfdrive/thermald/power_monitoring.py @@ -1,5 +1,6 @@ import time import threading +from datetime import datetime, timedelta from typing import Optional from openpilot.common.params import Params @@ -39,6 +40,11 @@ class PowerMonitoring: self.car_battery_capacity_uWh = max((CAR_BATTERY_CAPACITY_uWh / 10), int(car_battery_capacity_uWh)) # FrogPilot variables + device_shutdown_setting = self.params.get_int("DeviceShutdown") + # If the toggle is set for < 1 hour, configure by 15 minute increments + self.device_shutdown_time = (device_shutdown_setting - 3) * 3600 if device_shutdown_setting >= 4 else device_shutdown_setting * (60 * 15) + self.download_schedule = self.params.get_int("UpdateSchedule") + self.download_time = self.params.get_int("UpdateTime") # Calculation tick def calculate(self, voltage: Optional[int], ignition: bool): @@ -115,19 +121,45 @@ class PowerMonitoring: if offroad_timestamp is None: return False + if self.download_schedule: + self.update_shutdown_time() + now = time.monotonic() should_shutdown = False offroad_time = (now - offroad_timestamp) low_voltage_shutdown = (self.car_voltage_mV < (VBATT_PAUSE_CHARGING * 1e3) and self.car_voltage_instant_mV > (VBATT_INSTANT_PAUSE_CHARGING * 1e3) and offroad_time > VOLTAGE_SHUTDOWN_MIN_OFFROAD_TIME_S) - should_shutdown |= offroad_time > MAX_TIME_OFFROAD_S + should_shutdown |= offroad_time > self.device_shutdown_time should_shutdown |= low_voltage_shutdown should_shutdown |= (self.car_battery_capacity_uWh <= 0) should_shutdown &= not ignition should_shutdown &= (not self.params.get_bool("DisablePowerDown")) should_shutdown &= in_car - should_shutdown &= offroad_time > DELAY_SHUTDOWN_TIME_S + should_shutdown &= offroad_time > DELAY_SHUTDOWN_TIME_S if self.device_shutdown_time else True # If "Instant" is selected for the timer, shutdown immediately should_shutdown |= self.params.get_bool("ForcePowerDown") should_shutdown &= started_seen or (now > MIN_ON_TIME_S) return should_shutdown + + def update_shutdown_time(self): + now = datetime.now() + + # Adjust for daily or weekly schedule + hours = self.download_time // 2 + minutes = (self.download_time % 2) * 30 + + next_update_time = datetime(now.year, now.month, now.day, hours, minutes) + + if now >= next_update_time: + if self.download_schedule == 1: # Daily + next_update_time += timedelta(days=1) + elif self.download_schedule == 2: # Weekly + days_to_next_sunday = (6 - now.weekday()) % 7 or 7 + next_update_time += timedelta(days=days_to_next_sunday) + + # Add one hour buffer to give time for download + next_update_time += timedelta(hours=1) + + # Update shutdown time if next update is within 24 hours + if next_update_time - now <= timedelta(hours=24): + self.device_shutdown_time = (next_update_time - now).total_seconds() diff --git a/selfdrive/ui/qt/offroad/settings.h b/selfdrive/ui/qt/offroad/settings.h index b4f89e9..3b4dca0 100644 --- a/selfdrive/ui/qt/offroad/settings.h +++ b/selfdrive/ui/qt/offroad/settings.h @@ -109,6 +109,7 @@ private: ButtonControl *updateTime; + int deviceShutdown; int schedule; int time; }; diff --git a/selfdrive/ui/qt/offroad/software_settings.cc b/selfdrive/ui/qt/offroad/software_settings.cc index 15a4687..e635ea8 100644 --- a/selfdrive/ui/qt/offroad/software_settings.cc +++ b/selfdrive/ui/qt/offroad/software_settings.cc @@ -62,6 +62,7 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : ListWidget(parent) { } }); time = params.getInt("UpdateTime"); + deviceShutdown = params.getInt("DeviceShutdown") * 3600; updateTime->setValue(hours[time]); addItem(updateTime); @@ -216,7 +217,7 @@ void SoftwarePanel::automaticUpdate() { static bool isDownloadCompleted = false; if (isDownloadCompleted && params.getBool("UpdateAvailable")) { - params.putBool(true); + params.putBool(timer > deviceShutdown ? "DoShutdown" : "DoReboot", true); return; }