Files
StarPilot/openpilot/system/timed.py
firestar5683 80fbb4f6db Feature registration
Register vehicle controls, planner modes, UI actions and Galaxy services after their providers exist. Wire the shared runtime entrypoints and cover their combined behavior.

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
Co-authored-by: Daniel Koepping <elkoled@gmail.com>
Co-authored-by: Danny <dannyirwin31@yahoo.com>
Co-authored-by: DimitriChrysafis <Dimitri.chrysafis@gmail.com>
Co-authored-by: Dom <168790843+firestar5683@users.noreply.github.com>
Co-authored-by: Harald Schäfer <harald.the.engineer@gmail.com>
Co-authored-by: Kacper Rączy <gfw.kra@gmail.com>
Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu>
Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com>
Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com>
Co-authored-by: stef <19478336+stefpi@users.noreply.github.com>
Co-authored-by: whoisdomi <dcantu83@gmail.com>

Co-authored-by: Intelli <contact@intelli.software>
Co-authored-by: haraschax <6804392+haraschax@users.noreply.github.com>
Co-authored-by: prabhaavp <prabhaav@msu.edu>
2026-10-14 20:02:39 -05:00

74 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import datetime
import subprocess
import time
from typing import NoReturn
import openpilot.cereal.messaging as messaging
from openpilot.common.time_helpers import min_date, MAX_DATE, system_time_valid
from openpilot.common.swaglog import cloudlog
from openpilot.common.params import Params
from openpilot.common.gps import get_gps_location_service
def set_time(new_time):
diff = datetime.datetime.now(datetime.UTC).replace(tzinfo=None) - new_time
if abs(diff) < datetime.timedelta(seconds=10):
cloudlog.debug(f"Time diff too small: {diff}")
return
cloudlog.debug(f"Setting time to {new_time}")
try:
subprocess.run(f"TZ=UTC date -s '{new_time}'", shell=True, check=True)
except subprocess.CalledProcessError:
cloudlog.exception("timed.failed_setting_time")
def clock_message():
msg = messaging.new_message('clocks')
msg.valid = system_time_valid()
mono = time.monotonic_ns()
boot = time.clock_gettime_ns(time.CLOCK_BOOTTIME)
if time.monotonic_ns() - mono <= 1_000_000:
# Retain the existing wire fields for replay's clock pair, independently of wall-clock corrections.
msg.logMonoTime = mono
msg.clocks.deprecated.monotonicNanos = mono
msg.clocks.deprecated.bootTimeNanos = boot
msg.clocks.wallTimeNanos = time.time_ns()
return msg
def main() -> NoReturn:
"""
timed has two responsibilities:
- getting the current time from GPS
- publishing the time in the logs
AGNOS will also use NTP to update the time.
"""
params = Params()
gps_location_service = get_gps_location_service(params)
pm = messaging.PubMaster(['clocks'])
sm = messaging.SubMaster([gps_location_service])
while True:
sm.update(1000)
pm.send('clocks', clock_message())
gps = sm[gps_location_service]
gps_time = datetime.datetime.fromtimestamp(gps.unixTimestampMillis / 1000., datetime.UTC).replace(tzinfo=None)
if not sm.updated[gps_location_service] or (time.monotonic() - sm.logMonoTime[gps_location_service] / 1e9) > 2.0:
continue
if not gps.hasFix:
continue
if gps_time < min_date() or gps_time > MAX_DATE:
continue
set_time(gps_time)
time.sleep(10)
if __name__ == "__main__":
main()