mirror of
https://github.com/infiniteCable2/openpilot.git
synced 2026-09-10 10:13:40 +08:00
Merge branch 'master' of https://github.com/sunnypilot/sunnypilot
This commit is contained in:
@@ -16,6 +16,7 @@ from openpilot.common.utils import strip_deprecated_keys
|
||||
from openpilot.common.filter_simple import FirstOrderFilter
|
||||
from openpilot.common.params import Params
|
||||
from openpilot.common.realtime import DT_HW
|
||||
from openpilot.selfdrive.modeld.helpers import MODELS_DIR, usbgpu_compiled
|
||||
from openpilot.selfdrive.selfdrived.alertmanager import set_offroad_alert
|
||||
from openpilot.common.hardware import HARDWARE, COMMA_HARDWARE
|
||||
from openpilot.common.basedir import BASEDIR
|
||||
@@ -238,8 +239,7 @@ def hardware_thread(end_event, hw_queue) -> None:
|
||||
|
||||
fan_controller = FanController(int(1./DT_HW))
|
||||
chestnut = Chestnut()
|
||||
big_model_available = os.path.isfile(os.path.join(BASEDIR, "openpilot/selfdrive/modeld/models/big_driving_supercombo.onnx")) or \
|
||||
os.path.isfile(os.path.join(BASEDIR, "openpilot/selfdrive/modeld/models/big_driving_tinygrad.pkl.chunkmanifest"))
|
||||
big_model_available = (MODELS_DIR / 'big_driving_supercombo.onnx').is_file() or usbgpu_compiled()
|
||||
|
||||
while not end_event.is_set():
|
||||
sm.update(PANDA_STATES_TIMEOUT)
|
||||
|
||||
@@ -4,7 +4,15 @@ Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
|
||||
This file is part of sunnypilot and is licensed under the MIT License.
|
||||
See the LICENSE.md file in the root directory for more details.
|
||||
"""
|
||||
from collections.abc import Callable
|
||||
|
||||
import pyray as rl
|
||||
|
||||
from openpilot.system.ui.lib.application import FontWeight
|
||||
from openpilot.system.ui.sunnypilot.lib.styles import style
|
||||
from openpilot.system.ui.sunnypilot.widgets.list_view import ButtonActionSP
|
||||
from openpilot.system.ui.widgets.label import UnifiedLabel
|
||||
from openpilot.system.ui.widgets.list_view import BUTTON_WIDTH, BUTTON_HEIGHT, TEXT_PADDING, _resolve_value
|
||||
|
||||
|
||||
class NoElideButtonAction(ButtonActionSP):
|
||||
@@ -12,6 +20,38 @@ class NoElideButtonAction(ButtonActionSP):
|
||||
return super().get_width_hint() + 1
|
||||
|
||||
|
||||
class ScrollingButtonAction(ButtonActionSP):
|
||||
"""ButtonActionSP whose value scrolls instead of eliding when it doesn't fit."""
|
||||
|
||||
def __init__(self, text: str | Callable[[], str], width: int = style.BUTTON_ACTION_WIDTH,
|
||||
enabled: bool | Callable[[], bool] = True):
|
||||
super().__init__(text=text, width=width, enabled=enabled)
|
||||
self._value_label = UnifiedLabel("", font_size=style.ITEM_TEXT_FONT_SIZE, font_weight=FontWeight.NORMAL,
|
||||
text_color=self._value_color, scroll=True,
|
||||
alignment_vertical=rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE)
|
||||
|
||||
def set_value(self, value: str | Callable[[], str], color: rl.Color = style.ITEM_TEXT_VALUE_COLOR):
|
||||
if self.value != _resolve_value(value, ""):
|
||||
self._value_label.reset_scroll()
|
||||
super().set_value(value, color)
|
||||
self._value_label.set_text(value)
|
||||
self._value_label.set_text_color(color)
|
||||
|
||||
def _render(self, rect: rl.Rectangle) -> bool:
|
||||
"""Duplicate of ButtonActionSP._render, with the value drawn by a scrolling label"""
|
||||
self._button.set_text(self.text)
|
||||
self._button.set_enabled(_resolve_value(self.enabled))
|
||||
button_rect = rl.Rectangle(rect.x + rect.width - BUTTON_WIDTH, rect.y + (rect.height - BUTTON_HEIGHT) / 2, BUTTON_WIDTH, BUTTON_HEIGHT)
|
||||
self._button.render(button_rect)
|
||||
|
||||
if self.value:
|
||||
self._value_label.render(rl.Rectangle(rect.x, rect.y, rect.width - BUTTON_WIDTH - TEXT_PADDING, rect.height))
|
||||
|
||||
pressed = self._pressed
|
||||
self._pressed = False
|
||||
return pressed
|
||||
|
||||
|
||||
class AlertFadeAnimator:
|
||||
def __init__(self, target_fps: int, duration_on: float = 0.75, rc: float = 0.05):
|
||||
from openpilot.common.filter_simple import FirstOrderFilter
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
"""
|
||||
Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
|
||||
|
||||
This file is part of sunnypilot and is licensed under the MIT License.
|
||||
See the LICENSE.md file in the root directory for more details.
|
||||
"""
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
import pyray as rl
|
||||
|
||||
from openpilot.common.filter_simple import FirstOrderFilter
|
||||
from openpilot.system.ui.lib.application import gui_app, FontWeight
|
||||
from openpilot.system.ui.lib.shader_polygon import draw_polygon, Gradient
|
||||
from openpilot.system.ui.lib.text_measure import measure_text_cached
|
||||
from openpilot.system.ui.sunnypilot.lib.styles import style
|
||||
from openpilot.system.ui.sunnypilot.widgets.list_view import ListItemSP
|
||||
from openpilot.system.ui.widgets.label import UnifiedLabel
|
||||
from openpilot.system.ui.widgets.list_view import ItemAction
|
||||
|
||||
FONT_SIZE = style.ITEM_TEXT_FONT_SIZE
|
||||
ICON_SIZE = 56
|
||||
ICON_PADDING = 12
|
||||
|
||||
BAR_WIDTH = 1100
|
||||
BAR_HEIGHT = 20
|
||||
BAR_GAP = 16
|
||||
BAR_RADIUS = BAR_HEIGHT / 2
|
||||
CAPSULE_POINTS = 24
|
||||
|
||||
RAIL_COLOR = rl.Color(60, 60, 60, 255)
|
||||
FILL_COLOR = rl.Color(30, 121, 232, 255)
|
||||
# rl.WHITE is a tuple; the shimmer path reads .a off the color
|
||||
TEXT_COLOR = rl.Color(255, 255, 255, 255)
|
||||
|
||||
SWEEP_SPEED = 550.0 # px/s
|
||||
SWEEP_BAND = 240.0 # highlight half-width, px
|
||||
SWEEP_DIM = 0.65
|
||||
|
||||
|
||||
class DownloadStatusAction(ItemAction):
|
||||
"""Model download row: a name + percent over a progress rail while downloading, a name + icon otherwise."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(width=BAR_WIDTH)
|
||||
self.name = ""
|
||||
self.status_text = ""
|
||||
self.downloading = False
|
||||
self.text_color = rl.GRAY
|
||||
self.icon: str | None = None
|
||||
self.icon_color: rl.Color | None = None
|
||||
self._font = gui_app.font(FontWeight.NORMAL)
|
||||
# raw progress arrives in steps, one per 128KB chunk the manager publishes
|
||||
self._progress = FirstOrderFilter(0.0, 0.5, 1 / gui_app.target_fps)
|
||||
# integrated per frame; (t * speed) % span jumps whenever the fill width changes
|
||||
self._sweep = 0.0
|
||||
|
||||
self._name_label = UnifiedLabel("", font_size=FONT_SIZE, font_weight=FontWeight.NORMAL, text_color=TEXT_COLOR,
|
||||
alignment=rl.GuiTextAlignment.TEXT_ALIGN_LEFT,
|
||||
alignment_vertical=rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE)
|
||||
self._percent_label = UnifiedLabel("", font_size=FONT_SIZE, font_weight=FontWeight.NORMAL, text_color=TEXT_COLOR,
|
||||
alignment=rl.GuiTextAlignment.TEXT_ALIGN_RIGHT,
|
||||
alignment_vertical=rl.GuiTextAlignmentVertical.TEXT_ALIGN_MIDDLE)
|
||||
|
||||
def update(self, name, downloading=False, progress=0.0, status_text="", text_color=rl.GRAY, icon=None, icon_color=None):
|
||||
if downloading and not self.downloading:
|
||||
self._name_label.reset_shimmer()
|
||||
self._progress.x = progress
|
||||
self._sweep = 0.0
|
||||
self.name = name
|
||||
self.downloading = downloading
|
||||
self.status_text = status_text
|
||||
self.text_color = text_color
|
||||
self.icon = icon
|
||||
self.icon_color = icon_color
|
||||
self._name_label._shimmer = downloading
|
||||
if downloading:
|
||||
self._progress.update(progress)
|
||||
self._sweep += SWEEP_SPEED / gui_app.target_fps
|
||||
|
||||
@property
|
||||
def _idle_text(self) -> str:
|
||||
return f"{self.name} - {self.status_text}" if self.status_text else self.name
|
||||
|
||||
def get_width_hint(self) -> float:
|
||||
if self.downloading:
|
||||
return BAR_WIDTH
|
||||
width = measure_text_cached(self._font, self._idle_text, FONT_SIZE).x
|
||||
if self.icon:
|
||||
width += ICON_SIZE + ICON_PADDING
|
||||
return width
|
||||
|
||||
def _render(self, rect: rl.Rectangle):
|
||||
if self.downloading:
|
||||
self._render_downloading(rect)
|
||||
else:
|
||||
self._render_idle(rect)
|
||||
|
||||
def _sweep_gradient(self, width: float) -> Gradient:
|
||||
# clearance at both ends keeps the wrap offscreen
|
||||
center = (self._sweep % (width + 2 * SWEEP_BAND)) - SWEEP_BAND
|
||||
|
||||
def band(x: float) -> float:
|
||||
return max(0.0, 1.0 - abs(x - center) / SWEEP_BAND)
|
||||
|
||||
# sampling the corners is exact for a piecewise linear band
|
||||
xs = sorted({0.0, width} | {min(max(center + o, 0.0), width) for o in (-SWEEP_BAND, 0.0, SWEEP_BAND)}, reverse=True)
|
||||
# the gradient axis runs right-to-left in screen space
|
||||
stops = [1.0 - x / width for x in xs]
|
||||
# alpha here is the lift over the SWEEP_DIM base, not the final opacity
|
||||
colors = [rl.Color(FILL_COLOR.r, FILL_COLOR.g, FILL_COLOR.b, int(255 * band(x))) for x in xs]
|
||||
return Gradient(start=(0.0, 0.0), end=(1.0, 0.0), colors=colors, stops=stops)
|
||||
|
||||
@staticmethod
|
||||
def _capsule(rect: rl.Rectangle) -> np.ndarray:
|
||||
"""Rounded-end ribbon so the gradient covers the caps."""
|
||||
r = rect.height / 2
|
||||
cy = rect.y + r
|
||||
top, bottom = [], []
|
||||
for i in range(CAPSULE_POINTS):
|
||||
x = rect.x + rect.width * i / (CAPSULE_POINTS - 1)
|
||||
d = min(x - rect.x, rect.x + rect.width - x, r)
|
||||
h = math.sqrt(max(r * r - (r - d) ** 2, 0.0))
|
||||
top.append((x, cy - h))
|
||||
bottom.append((x, cy + h))
|
||||
return np.array(top + bottom[::-1], dtype=np.float32)
|
||||
|
||||
def _draw_fill(self, rail: rl.Rectangle, fill_width: float):
|
||||
if fill_width <= 0:
|
||||
return
|
||||
fill = rl.Rectangle(rail.x, rail.y, fill_width, rail.height)
|
||||
rl.draw_rectangle_rounded(fill, 1.0, 10, rl.Color(FILL_COLOR.r, FILL_COLOR.g, FILL_COLOR.b, int(255 * SWEEP_DIM)))
|
||||
draw_polygon(fill, self._capsule(fill), gradient=self._sweep_gradient(fill_width))
|
||||
|
||||
def _render_downloading(self, rect: rl.Rectangle):
|
||||
percent = f"{int(self._progress.x)}%"
|
||||
text_height = measure_text_cached(self._font, percent, FONT_SIZE).y
|
||||
top = rect.y + (rect.height - (text_height + BAR_GAP + BAR_HEIGHT)) / 2
|
||||
|
||||
text_rect = rl.Rectangle(rect.x, top, rect.width, text_height)
|
||||
self._name_label.set_text(self.name)
|
||||
self._name_label.render(text_rect)
|
||||
self._percent_label.set_text(percent)
|
||||
self._percent_label.render(text_rect)
|
||||
|
||||
rail = rl.Rectangle(rect.x, top + text_height + BAR_GAP, rect.width, BAR_HEIGHT)
|
||||
rl.draw_rectangle_rounded(rail, 1.0, 10, RAIL_COLOR)
|
||||
self._draw_fill(rail, max(0.0, min(rect.width, rect.width * (self._progress.x / 100.0))))
|
||||
|
||||
def _render_idle(self, rect: rl.Rectangle):
|
||||
text = self._idle_text
|
||||
text_size = measure_text_cached(self._font, text, FONT_SIZE)
|
||||
right = rect.x + rect.width
|
||||
|
||||
if self.icon:
|
||||
texture = gui_app.texture(self.icon, ICON_SIZE, ICON_SIZE, keep_aspect_ratio=True)
|
||||
rl.draw_texture_v(texture, rl.Vector2(right - texture.width, rect.y + (rect.height - texture.height) / 2),
|
||||
self.icon_color or self.text_color)
|
||||
right -= texture.width + ICON_PADDING
|
||||
|
||||
rl.draw_text_ex(self._font, text, rl.Vector2(right - text_size.x, rect.y + (rect.height - text_size.y) / 2),
|
||||
FONT_SIZE, 0, self.text_color)
|
||||
|
||||
|
||||
def download_status_item(title):
|
||||
return ListItemSP(title=title, action_item=DownloadStatusAction(), title_color=style.ITEM_TEXT_COLOR)
|
||||
@@ -7,7 +7,7 @@ from openpilot.common.test import OpenpilotTestCase
|
||||
from openpilot.cereal import messaging, log
|
||||
from teleoprtc.tracks import VIDEO_CLOCK_RATE
|
||||
|
||||
from openpilot.system.webrtc.webrtcd import CerealOutgoingMessageProxy, CerealIncomingMessageProxy
|
||||
from openpilot.system.webrtc.webrtcd import CerealOutgoingMessageProxy, CerealIncomingMessageProxy, ServerState, handle_get_stream
|
||||
from openpilot.system.webrtc.device.video import LiveStreamVideoStreamTrack
|
||||
|
||||
|
||||
@@ -80,3 +80,8 @@ class TestStreamSession(OpenpilotTestCase):
|
||||
start_pts = packet.pts
|
||||
assert abs(i + packet.pts - (start_pts + (((time.monotonic_ns() - start_ns) * VIDEO_CLOCK_RATE) // 1_000_000_000))) < 450 #5ms
|
||||
assert bytes(packet) == b""
|
||||
|
||||
def test_stream_rejects_non_json_content_type(self):
|
||||
response = self.loop.run_until_complete(handle_get_stream(ServerState(), b"{}", "text/plain"))
|
||||
|
||||
assert response == (415, b'{"error": "unsupported media type"}', "application/json; charset=utf-8")
|
||||
|
||||
@@ -395,7 +395,10 @@ def _text_response(text: str, status: int = 200) -> tuple[int, bytes, str]:
|
||||
return (status, text.encode(), "text/plain; charset=utf-8")
|
||||
|
||||
|
||||
async def handle_get_stream(state: ServerState, raw_body: bytes) -> tuple[int, bytes, str]:
|
||||
async def handle_get_stream(state: ServerState, raw_body: bytes, content_type: str) -> tuple[int, bytes, str]:
|
||||
if content_type != "application/json":
|
||||
return _json_response({"error": "unsupported media type"}, status=415)
|
||||
|
||||
stream_dict = state.streams
|
||||
body = StreamRequestBody(**json.loads(raw_body))
|
||||
|
||||
@@ -508,7 +511,7 @@ class WebrtcdHandler(BaseHTTPRequestHandler):
|
||||
services = parse_qs(parsed.query).get("services", [""])[0]
|
||||
result = self._run(handle_get_schema(self.server.state, services))
|
||||
elif parsed.path == "/stream":
|
||||
result = self._run(handle_get_stream(self.server.state, self._read_body()))
|
||||
result = self._run(handle_get_stream(self.server.state, self._read_body(), self.headers.get_content_type()))
|
||||
else: # /notify
|
||||
try:
|
||||
payload = json.loads(self._read_body())
|
||||
@@ -611,7 +614,7 @@ def webrtcd_thread(host: str, port: int):
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="WebRTC daemon")
|
||||
parser.add_argument("--host", type=str, default="0.0.0.0", help="Host to listen on")
|
||||
parser.add_argument("--host", type=str, default="127.0.0.1", help="Host to listen on")
|
||||
parser.add_argument("--port", type=int, default=5001, help="Port to listen on")
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user