carrot/selfdrive/frogpilot/frogpilot_process.py

107 lines
3.5 KiB
Python
Raw Normal View History

2024-05-29 03:43:25 -07:00
import datetime
import http.client
import os
import socket
import time
import urllib.error
import urllib.request
import cereal.messaging as messaging
from cereal import log
from openpilot.common.params import Params
from openpilot.common.realtime import DT_MDL, Priority, config_realtime_process
from openpilot.common.time import system_time_valid
from openpilot.system.hardware import HARDWARE
from openpilot.selfdrive.frogpilot.controls.frogpilot_planner import FrogPilotPlanner
from openpilot.selfdrive.frogpilot.controls.lib.frogpilot_functions import FrogPilotFunctions
2024-05-24 02:41:19 -07:00
from openpilot.selfdrive.frogpilot.controls.lib.frogpilot_variables import FrogPilotVariables
2024-05-29 03:43:25 -07:00
WIFI = log.DeviceState.NetworkType.wifi
def github_pinged(url="https://github.com", timeout=5):
try:
urllib.request.urlopen(url, timeout=timeout)
return True
except (urllib.error.URLError, socket.timeout, http.client.RemoteDisconnected):
return False
2024-05-15 09:38:37 -07:00
def automatic_update_check(params):
update_available = params.get_bool("UpdaterFetchAvailable")
update_ready = params.get_bool("UpdateAvailable")
update_state_idle = params.get("UpdaterState", encoding='utf8') == "idle"
if update_ready:
HARDWARE.reboot()
elif update_available:
os.system("pkill -SIGHUP -f selfdrive.updated.updated")
elif update_state_idle:
os.system("pkill -SIGUSR1 -f selfdrive.updated.updated")
def time_checks(automatic_updates, deviceState, now, params, params_memory):
2024-05-29 03:43:25 -07:00
screen_off = deviceState.screenBrightnessPercent == 0
wifi_connection = deviceState.networkType == WIFI
2024-05-15 09:38:37 -07:00
if screen_off and wifi_connection:
if automatic_updates:
automatic_update_check(params)
2024-05-24 02:41:19 -07:00
def frogpilot_thread(frogpilot_toggles):
2024-05-29 03:43:25 -07:00
config_realtime_process(5, Priority.CTRL_LOW)
params = Params()
params_memory = Params("/dev/shm/params")
frogpilot_functions = FrogPilotFunctions()
frogpilot_planner = FrogPilotPlanner()
2024-05-16 02:01:51 -07:00
current_day = None
2024-05-29 03:43:25 -07:00
first_run = True
time_validated = system_time_valid()
pm = messaging.PubMaster(['frogpilotPlan'])
sm = messaging.SubMaster(['carState', 'controlsState', 'deviceState', 'frogpilotCarControl', 'frogpilotCarState', 'frogpilotNavigation',
'frogpilotPlan', 'liveLocationKalman', 'longitudinalPlan', 'modelV2', 'radarState'],
poll='modelV2', ignore_avg_freq=['radarState'])
while True:
sm.update()
now = datetime.datetime.now()
deviceState = sm['deviceState']
started = deviceState.started
if started and sm.updated['modelV2']:
frogpilot_planner.update(sm['carState'], sm['controlsState'], sm['frogpilotCarControl'], sm['frogpilotCarState'],
2024-05-24 02:41:19 -07:00
sm['frogpilotNavigation'], sm['liveLocationKalman'], sm['modelV2'], sm['radarState'], frogpilot_toggles)
frogpilot_planner.publish(sm, pm, frogpilot_toggles)
if FrogPilotVariables.toggles_updated:
FrogPilotVariables.update_frogpilot_params(started)
2024-05-29 03:43:25 -07:00
if not time_validated:
time_validated = system_time_valid()
if not time_validated:
continue
2024-05-15 09:38:37 -07:00
if now.second == 0 or first_run or params_memory.get_bool("ManualUpdateInitiated"):
2024-05-29 03:43:25 -07:00
if not started and github_pinged():
2024-05-15 09:38:37 -07:00
time_checks(frogpilot_toggles.automatic_updates, deviceState, now, params, params_memory)
2024-05-29 03:43:25 -07:00
2024-05-16 02:01:51 -07:00
if now.day != current_day:
params.remove("FingerprintLogged")
current_day = now.day
2024-05-29 03:43:25 -07:00
first_run = False
time.sleep(DT_MDL)
def main():
2024-05-24 02:41:19 -07:00
frogpilot_thread(FrogPilotVariables.toggles)
2024-05-29 03:43:25 -07:00
if __name__ == "__main__":
main()