Compare commits

..

55 Commits

Author SHA1 Message Date
discountchubbs 10b508929d smoother 2026-10-23 23:17:33 -07:00
discountchubbs 9d98fe4376 he does not like 1ms 2026-10-23 11:56:25 -07:00
discountchubbs 0ce9fca933 decay one line and give full authority in curvy curves; kappa=curv 2026-10-23 11:55:31 -07:00
James Vecellio-Grant 2110ffab00 Merge branch 'master' into laneful-curves 2026-10-23 00:05:23 -07:00
discountchubbs cda505ea6d satisfy royjr 2026-10-22 23:54:18 -07:00
discountchubbs aa559e48a2 smoother release authority 2026-10-22 23:52:10 -07:00
royjr 02c7b60600 fine 2026-10-23 00:35:06 -04:00
royjr d28e027943 keep pill current 2026-10-23 00:12:41 -04:00
royjr f179460982 so I can sleep at night 2026-10-23 00:05:04 -04:00
royjr 16b17e051f tick on new data 2026-10-23 00:03:15 -04:00
royjr 8efe325a71 gate like rainbow 2026-10-22 23:55:28 -04:00
royjr 9c391b4842 cleanup 2026-10-22 23:46:50 -04:00
royjr 6ac88a7be0 move to models 2026-10-22 23:25:32 -04:00
royjr 4dbb49430b tizi 2026-10-22 23:25:32 -04:00
royjr 5de324ab96 ill take it from here thanks 2026-10-22 23:25:32 -04:00
royjr d904487eed sqrt bar 2026-10-22 23:25:32 -04:00
royjr 2ffb986f3b blue only 2026-10-22 23:25:32 -04:00
discountchubbs c4f1dc179f dead 2026-10-22 20:17:03 -07:00
royjr 15bf3dcd94 fade out blue 2026-10-22 18:15:10 -04:00
royjr 6e0ce4a6f9 match width 2026-10-22 18:15:10 -04:00
royjr d45d7e2de7 white is model path 2026-10-22 18:15:10 -04:00
royjr a3ef498512 dont guess 2026-10-22 18:15:10 -04:00
royjr 21f45673fd use blendful trust 2026-10-22 18:15:10 -04:00
royjr fd39f4f820 fade on trust 2026-10-22 18:15:10 -04:00
royjr cb71cdc54d use constants 2026-10-22 18:15:10 -04:00
royjr afd0ce66c9 no tap 2026-10-22 18:15:10 -04:00
royjr 5854346c2f blendful 2026-10-22 18:15:10 -04:00
James Vecellio-Grant 270704ad47 Merge branch 'master' into laneful-curves 2026-10-22 10:55:59 -07:00
James Vecellio-Grant 4ebb756ee5 Merge branch 'master' into laneful-curves 2026-10-22 10:15:23 -07:00
nayan ee7bf203fe ffs 2026-10-21 17:20:08 -04:00
discountchubbs 62cf10ff05 let unwind be handled by laneless 2026-10-21 08:17:55 -07:00
discountchubbs 7707a3e384 interpolate smoothness on straights to reduce mr kumar and his mdv3 jiggle shennanigans 2026-10-20 22:56:10 -07:00
discountchubbs e87a605fb2 relearn ideal sim pose 2026-10-20 18:39:44 -07:00
James Vecellio-Grant 11603b2de7 Update modeld.py 2026-10-20 18:02:52 -07:00
James Vecellio-Grant 644e23f8ef Update modeld.py 2026-10-20 17:55:15 -07:00
discountchubbs 7847dbf148 sm logging + qlog logging 2026-10-20 15:47:46 -07:00
discountchubbs 0af369cae0 blendful 2026-10-20 13:28:20 -07:00
James Vecellio-Grant 77d0c2d665 Ui fix 2026-10-20 07:16:14 -07:00
discountchubbs 4d2d3f69f5 winner takes all trust system in lane lines 2026-10-19 23:11:22 -07:00
discountchubbs e9fc7a2465 less is more 2026-10-19 21:43:35 -07:00
discountchubbs 61154655cd handle one line flicker by trust÷2 2026-10-19 20:25:29 -07:00
discountchubbs ed4e8aeea6 jerk limiting correctin strength so that hecker doesnt spin spin yo wheel 2026-10-19 20:20:45 -07:00
discountchubbs a3ed6bd2f0 blending 50% on sharp curvaruerqw 2026-10-19 16:49:48 -07:00
discountchubbs 46c0b31997 blend 30% plan with action projeciton 2026-10-19 16:28:07 -07:00
discountchubbs 8a50860648 quicker entry 2026-10-19 14:16:46 -07:00
discountchubbs 563fa3c782 sharp curvies have some issues :O 2026-10-19 13:38:52 -07:00
discountchubbs 0b3459fbdc decay trust when model intends to move plant off lane. think going around cones and stuff 2026-10-19 13:10:03 -07:00
discountchubbs 54a22bd8bd blended correction over .3 s 2026-10-19 12:54:53 -07:00
discountchubbs 6c674810ee default to 1.0 while i test variable weighting 2026-10-19 12:38:16 -07:00
discountchubbs e64f67ec7e red diff that 2026-10-19 12:36:35 -07:00
discountchubbs 9c7655c45e modeld_v2: blended laneful using weighted stds 2026-10-19 12:11:27 -07:00
discountchubbs 60c6464a14 Merge remote-tracking branch 'origin/master' into laneful-curves
# Conflicts:
#	openpilot/selfdrive/ui/mici/onroad/model_renderer.py
#	openpilot/sunnypilot/modeld_v2/modeld.py
2026-10-19 12:08:14 -07:00
royjr bf93807e7b simple 2026-10-18 01:06:10 -04:00
royjr f54f2d462e settings 2026-10-18 00:15:49 -04:00
royjr 80494ec3cf init 2026-10-18 00:15:48 -04:00
15 changed files with 298 additions and 9 deletions
+7
View File
@@ -468,6 +468,13 @@ struct ModelDataV2SP @0xa1680744031fdb2d {
laneTurnDirection @0 :TurnDirection;
leftLaneChangeEdgeBlock @1 :Bool;
rightLaneChangeEdgeBlock @2 :Bool;
blendfulCurvature @3 :Float32;
blendfulTrust @4 :Float32;
modelCurvature @5 :Float32;
blendfulGoingAround @6 :Bool;
blendfulLeftProb @7 :Float32;
blendfulRightProb @8 :Float32;
blendfulLaneWidth @9 :Float32;
enum TurnDirection {
none @0;
+1 -1
View File
@@ -94,7 +94,7 @@ _services: dict[str, tuple] = {
"carControlSP": (True, 100., 10),
"carStateSP": (True, 100., 10),
"liveMapDataSP": (True, 1., 1),
"modelDataV2SP": (True, 20., None, QueueSize.BIG),
"modelDataV2SP": (True, 20., 10, QueueSize.BIG),
"liveLocationKalman": (True, 20.),
# debug
+1
View File
@@ -256,6 +256,7 @@ inline static std::unordered_map<std::string, ParamKeyAttributes> keys = {
{"LagdValueCache", {PERSISTENT, FLOAT, "0.2"}},
{"LaneTurnDesire", {PERSISTENT | BACKUP, BOOL, "0"}},
{"LaneTurnValue", {PERSISTENT | BACKUP, FLOAT, "19.0"}},
{"Blendful", {PERSISTENT | BACKUP, BOOL, "0"}},
{"PlanplusControl", {PERSISTENT | BACKUP, FLOAT, "1.0"}},
// mapd
@@ -141,6 +141,7 @@ class ModelRenderer(Widget, ModelRendererSP):
if ui_state.status != UIStatus.DISENGAGED:
self._draw_lane_lines()
self._draw_path()
self.blendful.draw(self._rect)
if render_lead_indicator:
self._update_leads(sm)
@@ -219,6 +220,7 @@ class ModelRenderer(Widget, ModelRendererSP):
# Update road edges using raw points
for road_edge in self._road_edges:
road_edge.projected_points = self._map_line_to_polygon(road_edge.raw_points, line_width_factor, 0.0, max_idx)
self.blendful.update(self, max_idx)
# Update path using raw points
if lead and lead.present:
@@ -123,6 +123,8 @@ class ModelRenderer(Widget, ChevronMetrics, ModelRendererSP):
# Draw elements
self._draw_lane_lines()
self._draw_path()
if self._lateral_active:
self.blendful.draw(self._rect)
if render_lead_indicator and radar_state:
self._draw_lead_indicator()
@@ -172,6 +174,7 @@ class ModelRenderer(Widget, ChevronMetrics, ModelRendererSP):
# Update road edges using raw points
for road_edge in self._road_edges:
road_edge.projected_points = self._map_line_to_polygon(road_edge.raw_points, 0.025, 0.0, max_idx, max_distance)
self.blendful.update(self, max_idx, max_distance)
# Update path using raw points
if lead and lead.present:
@@ -102,13 +102,18 @@ class ModelsLayout(Widget):
self.lagd_toggle = toggle_item_sp(tr("Live Learning Steer Delay"), "", param="LagdToggle")
self.blendful_toggle = toggle_item_sp(tr("Blendful"),
tr("Nudges the car toward the lane center when the lane lines are clear. Drives laneless when they are not."),
param="Blendful")
self.camera_offset = option_item_sp(tr("Adjust Camera Offset"), "CameraOffset", -35, 35,
tr("Virtually shift camera's perspective to move model's center to Left(+ values) or Right (- values)"),
1, None, True, "", style.BUTTON_ACTION_WIDTH, None, True,
lambda v: f"{v / 100:.2f} m")
self.items = [self.small_model_item, self.big_model_item, self.cancel_download_item, self.download_item, self.refresh_item, self.clear_cache_item,
self.lane_turn_desire_toggle, self.lane_turn_value_control, self.lagd_toggle, self.delay_control, self.camera_offset]
self.blendful_toggle, self.lane_turn_desire_toggle, self.lane_turn_value_control, self.lagd_toggle, self.delay_control,
self.camera_offset]
def _update_lagd_description(self, lagd_toggle: bool):
desc = tr("Enable this for the car to learn and adapt its steering response time. Disable to use a fixed steering response time. " +
@@ -322,6 +327,7 @@ class ModelsLayout(Widget):
self.lane_turn_desire_toggle.action_item.set_state(turn_desire)
self.lane_turn_value_control.set_visible(turn_desire and advanced_controls)
self.lagd_toggle.action_item.set_state(live_delay)
self.blendful_toggle.action_item.set_state(ui_state.params.get_bool("Blendful"))
self.delay_control.set_visible(not live_delay and advanced_controls)
new_step = int(round(100 / CV.MPH_TO_KPH)) if ui_state.is_metric else 100
if self.lane_turn_value_control.action_item is not None and self.lane_turn_value_control.action_item.value_change_step != new_step:
@@ -11,7 +11,7 @@ import pyray as rl
from openpilot.cereal import custom
from openpilot.selfdrive.ui.mici.widgets.dialog import BigConfirmationDialog, BigDialog
from openpilot.sunnypilot.models.helpers import ACTIVE_BUNDLE_KEYS, get_selected_bundle
from openpilot.selfdrive.ui.mici.widgets.button import BigButton
from openpilot.selfdrive.ui.mici.widgets.button import BigButton, BigParamControl
from openpilot.selfdrive.ui.ui_state import ui_state, device
from openpilot.selfdrive.ui.sunnypilot.model_info import (active_source, big_model_state, bundles_for_source, carrying_model,
default_model_name, model_cache_size_mb, model_info, queued_name,
@@ -95,7 +95,12 @@ class ModelsLayoutMici(NavScroller):
self.clear_cache_btn.set_click_callback(self._confirm_clear_cache)
self._cache_size_time = 0.0
self.main_items = [self.current_model_info, self.select_model_btn, self.cancel_download_btn, self.refresh_btn, self.clear_cache_btn]
self.blendful_toggle = BigParamControl("blendful", "Blendful",
description="Nudges the car toward the lane center when the lane lines are clear.\n" +
"Drives laneless when they are not.")
self.main_items = [self.current_model_info, self.select_model_btn, self.cancel_download_btn, self.refresh_btn, self.clear_cache_btn,
self.blendful_toggle]
self._scroller.add_widgets(self.main_items)
@property
@@ -205,6 +210,10 @@ class ModelsLayoutMici(NavScroller):
device.set_override_interactive_timeout(None)
self._was_downloading = False
def show_event(self):
super().show_event()
self.blendful_toggle.refresh()
def _update_state(self):
super()._update_state()
@@ -6,6 +6,7 @@ See the LICENSE.md file in the root directory for more details.
"""
import pyray as rl
from openpilot.selfdrive.ui.ui_state import UIStatus
from openpilot.selfdrive.ui.sunnypilot.onroad.blendful import BlendfulRenderer
from openpilot.selfdrive.ui.sunnypilot.onroad.rainbow_path import RainbowPath
LANE_LINE_COLORS_SP = {
@@ -17,3 +18,4 @@ LANE_LINE_COLORS_SP = {
class ModelRendererSP:
def __init__(self):
self.rainbow_path = RainbowPath()
self.blendful = BlendfulRenderer()
@@ -0,0 +1,90 @@
"""
Copyright (c) 2021-, 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 numpy as np
import pyray as rl
from openpilot.common.filter_simple import FirstOrderFilter
from openpilot.common.realtime import DT_MDL
from openpilot.selfdrive.ui.ui_state import ui_state
from openpilot.sunnypilot.modeld_v2.blendful import LANE_WIDTH_CLIP, LOOKAHEAD_TIME, MIN_LOOKAHEAD
from openpilot.system.ui.lib.application import gui_app, FontWeight
from openpilot.system.ui.lib.shader_polygon import draw_polygon
from openpilot.system.ui.lib.text_measure import measure_text_cached
BLENDFUL_COLOR = (0, 200, 255)
FULL_BAR = 1.0
class BlendfulRenderer:
def __init__(self, readout: bool = True):
self._readout = readout
self._center_points = np.empty((0, 2), dtype=np.float32)
self._trust = 0.0
self._font = gui_app.font(FontWeight.BOLD)
self._pull_display = FirstOrderFilter(0.0, 0.5, DT_MDL)
self._trust_display = FirstOrderFilter(0.0, 0.5, DT_MDL)
self._alpha = FirstOrderFilter(0.0, 0.3, DT_MDL)
def update(self, renderer, max_idx: int, *map_args) -> None:
blendful = ui_state.sm['modelDataV2SP']
if ui_state.sm.updated['modelDataV2SP']:
self._alpha.update(float(blendful.blendfulTrust > 0.0))
if blendful.blendfulTrust > 0.0:
self._trust = blendful.blendfulTrust
if self._readout:
lookahead = max(ui_state.sm['carState'].vEgo * LOOKAHEAD_TIME, MIN_LOOKAHEAD)
self._pull_display.update(blendful.blendfulCurvature * lookahead ** 2 / 2)
self._trust_display.update(self._trust)
left, right = renderer._lane_lines[1].raw_points, renderer._lane_lines[2].raw_points
stds = list(ui_state.sm['modelV2'].laneLineStds)
if self._alpha.x < 0.01 or len(left) == 0 or len(right) != len(left) or len(stds) < 3:
self._center_points = np.empty((0, 2), dtype=np.float32)
return
if blendful.blendfulTrust > 0.0:
l_prob, r_prob = blendful.blendfulLeftProb, blendful.blendfulRightProb
half_width = np.clip(right[:, 1] - left[:, 1], *LANE_WIDTH_CLIP) / 2 if l_prob * r_prob > 0.5 else blendful.blendfulLaneWidth / 2
l_var = stds[1] ** 2 / max(l_prob, 1e-4)
r_var = stds[2] ** 2 / max(r_prob, 1e-4)
center = left.copy()
center[:, 1] = (r_var * (left[:, 1] + half_width) + l_var * (right[:, 1] - half_width)) / (l_var + r_var)
self._center_points = renderer._map_line_to_polygon(center, 0.15, 0.0, max_idx, *map_args)
def _color(self, rgb, alpha: float) -> rl.Color:
return rl.Color(*rgb, int(alpha * self._alpha.x))
def draw(self, rect: rl.Rectangle) -> None:
if self._center_points.size and self._trust > 0.05:
draw_polygon(rect, self._center_points, self._color(BLENDFUL_COLOR, np.clip(self._trust, 0.2, 1.0) * 255))
if self._center_points.size and self._readout:
self._draw_readout(rect)
def _draw_readout(self, rect: rl.Rectangle) -> None:
pull, trust = self._pull_display.x, self._trust_display.x
w, h = 190, 76
x, y = rect.x + (rect.width - w) / 2, rect.y + 10
cx, cy = x + w / 2, y + 30
rl.draw_rectangle_rounded(rl.Rectangle(x, y, w, h), 0.5, 12, self._color((0, 0, 0), 170))
text = f"{abs(pull) * 100:.0f}"
text_w = measure_text_cached(self._font, text, 44, 0).x
rl.draw_text_ex(self._font, text, rl.Vector2(cx - text_w / 2, cy - 24), 44, 0, self._color((255, 255, 255), 255))
active = self._color(BLENDFUL_COLOR, np.clip(abs(pull) / 0.1, 0.0, 1.0) * 255)
idle = self._color((255, 255, 255), 40)
lx, rx = x + 18, x + w - 18
rl.draw_triangle(rl.Vector2(lx, cy), rl.Vector2(lx + 16, cy + 12), rl.Vector2(lx + 16, cy - 12), active if pull < 0 else idle)
rl.draw_triangle(rl.Vector2(rx - 16, cy - 12), rl.Vector2(rx - 16, cy + 12), rl.Vector2(rx, cy), active if pull > 0 else idle)
bar = rl.Rectangle(x + 24, y + h - 16, w - 48, 6)
rl.draw_rectangle_rounded(bar, 1.0, 6, self._color((255, 255, 255), 40))
rl.draw_rectangle(int(cx - 1), int(bar.y - 2), 2, int(bar.height + 4), self._color((255, 255, 255), 120))
fill = bar.width / 2 * float(np.sqrt(np.clip(abs(pull) / FULL_BAR, 0.0, 1.0)))
if fill > 1:
fill_x = cx if pull > 0 else cx - fill
rl.draw_rectangle_rounded(rl.Rectangle(fill_x, bar.y, fill, bar.height), 1.0, 6,
self._color(BLENDFUL_COLOR, np.clip(0.3 + 0.7 * trust, 0.0, 1.0) * 255))
@@ -6,6 +6,7 @@ See the LICENSE.md file in the root directory for more details.
"""
from openpilot.common.filter_simple import FirstOrderFilter
from openpilot.selfdrive.ui.ui_state import ui_state, UIStatus
from openpilot.selfdrive.ui.sunnypilot.onroad.blendful import BlendfulRenderer
from openpilot.selfdrive.ui.sunnypilot.onroad.chevron_metrics import ChevronMetrics
from openpilot.selfdrive.ui.sunnypilot.onroad.rainbow_path import RainbowPath
from openpilot.selfdrive.ui.sunnypilot.ui_state import MADSState
@@ -16,6 +17,7 @@ class ModelRendererSP:
def __init__(self):
self.rainbow_path = RainbowPath()
self.chevron_metrics = ChevronMetrics()
self.blendful = BlendfulRenderer(readout=False)
self._width_filter = FirstOrderFilter(0.9, 0.1, 1 / gui_app.target_fps)
@property
@@ -36,7 +36,7 @@ class UIStateSP:
self.is_sp_release: bool = self.params.get_bool("IsReleaseSpBranch")
self.sm_services_ext = [
"modelManagerSP", "selfdriveStateSP", "longitudinalPlanSP", "backupManagerSP",
"lateralTorqueParameters", "carStateSP", "liveMapDataSP", "carParamsSP", "lateralDelay"
"lateralTorqueParameters", "carStateSP", "liveMapDataSP", "carParamsSP", "lateralDelay", "modelDataV2SP"
]
self.sunnylink_state = SunnylinkState()
+140
View File
@@ -0,0 +1,140 @@
"""
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 numpy as np
from openpilot.common.filter_simple import FirstOrderFilter
from openpilot.common.realtime import DT_MDL
from openpilot.sunnypilot.modeld_v2.constants import ModelConstants
LANE_WIDTH_DEFAULT = 3.6 # m
LANE_WIDTH_CLIP = (2.8, 4.5) # m
LOOKAHEAD_TIME = 1.5 # s
MIN_LOOKAHEAD = 10.0 # m
MIN_SPEED = 5.0 # m/s
LINE_STD = (0.2, 0.6)
POINT_STD = 0.1
YIELD = (0.2, 0.45)
X_IDXS = np.array(ModelConstants.X_IDXS)
class Blendful:
def __init__(self):
self.lane_width = FirstOrderFilter(LANE_WIDTH_DEFAULT, 10.0, DT_MDL, initialized=False)
self.line_gone = FirstOrderFilter(np.zeros(2), 2.0, DT_MDL)
self.line_probs_slow = FirstOrderFilter(np.zeros(2), 3.0, DT_MDL)
self.line_probs = FirstOrderFilter(np.zeros(2), 0.5, DT_MDL)
self.usual_offset = FirstOrderFilter(0.0, 10.0, DT_MDL, initialized=False)
self.going_around = False
self.since_turning = 0.0
self.trust = 0.0
self.line_trust = 0.0
self.dk = 0.0 # delta kappa; difference between lane center and model plan
self.l_prob = 0.0
self.r_prob = 0.0
self.model_curvature = 0.0
self.smooth = 0.0 # smoothens lanelines below .53 lat accel
self.curvature = 0.0
def update(self, model_output: dict[str, np.ndarray], v_ego: float, active: bool, model_curvature: float) -> float:
self.l_prob = self.r_prob = 0.0
fading = not active or v_ego < MIN_SPEED
if fading:
self.trust = self.line_trust = 0.0
self.usual_offset.initialized = False
self.lane_width.initialized = False
self.going_around = False
self.model_curvature = model_curvature
target = 0.0 if fading else self._target(model_output, v_ego, model_curvature)
target = self.smooth = self.smooth + (target - self.smooth) * DT_MDL / (DT_MDL + np.interp(abs(model_curvature) * v_ego ** 2, [0.18, 0.53], [0.5, 0.0]))
build = 0.5 / max(v_ego, 1.0) ** 2 * DT_MDL # jerk over 1 second
release = (0.5 if fading else 2.0) / max(v_ego, 1.0) ** 2 * DT_MDL # lets go fast, but past zero it builds like any other pull
k = self.curvature
self.curvature = float(np.clip(target, max(k - release, -build) if k > 0 else k - build, min(k + release, build) if k < 0 else k + build))
return self.curvature
def _target(self, model_output: dict[str, np.ndarray], v_ego: float, model_curvature: float) -> float:
lookahead = max(v_ego * LOOKAHEAD_TIME, MIN_LOOKAHEAD)
lines = model_output['lane_lines'][0, :, :, 0]
stds = model_output['lane_lines_stds'][0, :, :, 0]
lone_weight = np.interp(abs(model_curvature) * v_ego ** 2, [0.18, 0.53], [0.5, 1.0]) # 0.5 on straights, 1.0 in curves
probs = self.line_probs.update(model_output['lane_lines_prob'][0, 1::2][1:3])
absent = (probs < 0.3) & (lone_weight > 0.5)
gone = self.line_gone.update((model_output['lane_lines_prob'][0, 1::2][1:3] < 0.3).astype(float))
missing = np.where(absent, 1.0, gone)
dropping = bool(np.any((probs < 0.6 * self.line_probs_slow.update(model_output['lane_lines_prob'][0, 1::2][1:3])) & ~absent))
l_prob = float(probs[0] * np.interp(np.interp(0.5 * lookahead, X_IDXS, stds[1]), LINE_STD, [1.0, 0.0]))
r_prob = float(probs[1] * np.interp(np.interp(0.5 * lookahead, X_IDXS, stds[2]), LINE_STD, [1.0, 0.0]))
self.l_prob, self.r_prob = l_prob, r_prob
# one line steadily on its own still centers at lone_weight. a line that is disappearing gives control back to laneless
if missing[1] > 0.8 and l_prob > 0.5:
lane_trust = l_prob * lone_weight
elif missing[0] > 0.8 and r_prob > 0.5:
lane_trust = r_prob * lone_weight
elif dropping:
lane_trust = min(l_prob, r_prob)
else:
lane_trust = l_prob + r_prob - l_prob * r_prob
if lone_weight > 0.5 and lane_trust < self.line_trust:
lane_trust = self.line_trust + (lane_trust - self.line_trust) * DT_MDL / (lone_weight - 0.5 + DT_MDL)
self.line_trust = lane_trust
if lane_trust <= 0.0:
self.trust = 0.0
return 0.0
if l_prob + r_prob <= 0.0: # keep the last fit while trust lets go
self.trust = min(self.trust, lane_trust)
return float(self.trust * self.dk)
win = (X_IDXS >= 0.5 * lookahead) & (X_IDXS <= 1.5 * lookahead)
x = X_IDXS[win]
half_width = self.lane_width.x / 2
if l_prob * r_prob > 0.5:
width = np.interp(lookahead, X_IDXS, lines[2]) - np.interp(lookahead, X_IDXS, lines[1])
self.lane_width.update(float(np.clip(width, *LANE_WIDTH_CLIP)))
half_width = np.clip(lines[2, win] - lines[1, win], *LANE_WIDTH_CLIP) / 2
l_var = stds[1, win] ** 2 / max(l_prob, 1e-4)
r_var = stds[2, win] ** 2 / max(r_prob, 1e-4)
center = (r_var * (lines[1, win] + half_width) + l_var * (lines[2, win] - half_width)) / (l_var + r_var)
weight = 1.0 / (l_var * r_var / (l_var + r_var) + POINT_STD ** 2)
# Fit curvature to place the plant in center: e ~ dk x^2 / 2
plan_x = np.interp(x, model_output['plan'][0, :, 0], model_output['plan'][0, :, 1])
plan_share = np.interp(abs(model_curvature) * lookahead ** 2 / 2, [0.5, 2.0], [0.3, 0.5]) # blending up to 50% plan with action during sharp curvature
error = np.clip(center - ((1 - plan_share) * model_curvature * x ** 2 / 2 + plan_share * plan_x), -1.0, 1.0)
dk = 2.0 * np.sum(weight * error * x ** 2) / np.sum(weight * x ** 4)
# A model planning to end up further from its usual spot in the lane than the plant is now is likely going around something
hw_now = np.clip(lines[2, 0] - lines[1, 0], *LANE_WIDTH_CLIP) / 2 if l_prob * r_prob > 0.5 else self.lane_width.x / 2
center_now = (l_prob * (lines[1, 0] + hw_now) + r_prob * (lines[2, 0] - hw_now)) / (l_prob + r_prob)
plan_y = np.interp(lookahead, model_output['plan'][0, :, 0], model_output['plan'][0, :, 1])
offset = plan_y - np.interp(lookahead, x, center)
if min(gone) > 0.5: # both lines gone: turns, intersections, relearn ideal model position
self.usual_offset.initialized = False
usual = self.usual_offset.x if self.usual_offset.initialized else offset
swerve = abs(offset - usual) - 0.25 * abs(model_curvature) * lookahead ** 2 / 2
away = swerve - abs(-center_now - usual)
lane_curvature = 2.0 * np.polyfit(x, center, 2)[0]
inside_curve = abs(lane_curvature) * lookahead ** 2 / 2 > 0.1 and (offset - usual) * lane_curvature > 0
self.since_turning = 0.0 if abs(model_curvature) * lookahead ** 2 / 2 > 0.5 else self.since_turning + DT_MDL
if inside_curve:
self.going_around = False
elif away > YIELD[0] and self.since_turning > 2.0:
self.going_around = True
elif swerve < YIELD[0] / 2:
self.going_around = False
self.usual_offset.update_alpha(20.0 if self.going_around else 10.0)
self.usual_offset.update(offset)
if inside_curve:
lane_trust *= np.interp(abs(offset) - abs(center_now) - 0.25 * abs(model_curvature) * lookahead ** 2 / 2, (0.5, 1.0), [1.0, 0.0])
else:
lane_trust *= np.interp(swerve, (YIELD[0] / 2, YIELD[0]), [1.0, 0.0]) if self.going_around else np.interp(away, YIELD, [1.0, 0.0])
if lane_trust < self.trust:
dk = float(np.clip(dk, min(self.dk, 0.0), max(self.dk, 0.0)))
self.trust = lane_trust if lane_trust < self.trust else self.trust + (lane_trust - self.trust) * DT_MDL / (1.5 + DT_MDL)
self.dk = dk
return float(self.trust * dk)
+19 -2
View File
@@ -7,6 +7,7 @@ See the LICENSE.md file in the root directory for more details.
"""
from collections.abc import Callable
from functools import partial
import gc
import os
os.environ['GMMU'] = '0'
@@ -39,6 +40,7 @@ from openpilot.selfdrive.modeld.modeld import ChestnutGpuState
from openpilot.sunnypilot.modeld_v2.fill_model_msg import fill_model_msg, fill_pose_msg, PublishState, get_curvature_from_output
from openpilot.sunnypilot.modeld_v2.parse_model_outputs import Parser
from openpilot.sunnypilot.modeld_v2.blendful import Blendful
from openpilot.sunnypilot.modeld_v2.constants import ModelConstants, Plan
from openpilot.sunnypilot.modeld_v2.meta_helper import load_meta_constants
from openpilot.sunnypilot.modeld_v2.camera_offset_helper import CameraOffsetHelper
@@ -228,7 +230,8 @@ class ModelState(ModelStateBase):
return outputs
def get_action_from_model(self, model_output: dict[str, np.ndarray], prev_action: log.ModelDataV2.Action,
lat_action_t: float, long_action_t: float, v_ego: float) -> log.ModelDataV2.Action:
lat_action_t: float, long_action_t: float, v_ego: float,
blendful: Callable[[float], float] | None = None) -> log.ModelDataV2.Action:
if 'action' not in model_output:
plan = model_output['plan'][0]
desired_accel = get_accel_from_plan(plan[:, Plan.VELOCITY][:, 0], plan[:, Plan.ACCELERATION][:, 0], self.constants.T_IDXS,
@@ -240,6 +243,8 @@ class ModelState(ModelStateBase):
else:
desired_accel = model_output['action'][0, 1]
desired_curvature = model_output['action'][0, 0] / (max(1.0, v_ego))**2
if blendful is not None:
desired_curvature += blendful(desired_curvature)
stop = v_ego < 0.3 and desired_accel < 0.1
desired_accel = smooth_value(desired_accel, prev_action.desiredAcceleration, self.LONG_SMOOTH_SECONDS)
@@ -366,6 +371,8 @@ def main(demo=False):
DH = DesireHelper()
meta_constants = load_meta_constants()
RELC = RoadEdgeLaneChangeController()
blendful = Blendful()
blendful_enabled = False
while True:
# Keep receiving frames until we are at least 1 frame ahead of previous extra frame
@@ -409,6 +416,7 @@ def main(demo=False):
model.lat_delay = get_lat_delay(params, sm["lateralDelay"].lateralDelay)
model.PLANPLUS_CONTROL = params.get("PlanplusControl", return_default=True)
camera_offset_helper.set_offset(params.get("CameraOffset", return_default=True))
blendful_enabled = params.get_bool("Blendful")
lat_delay = model.lat_delay + model.LAT_SMOOTH_SECONDS
if sm.updated["extrinsicsCalibration"] and sm.seen['narrowRoadCameraState'] and sm.seen['deviceState']:
device_from_calib_euler = np.array(sm["extrinsicsCalibration"].rpyCalib, dtype=np.float32)
@@ -482,7 +490,9 @@ def main(demo=False):
if not sm['carControl'].latActive:
prev_action.desiredCurvature = sm['controlsState'].curvature
action = model.get_action_from_model(model_output, prev_action, lat_action_t, long_action_t, v_ego)
blendful_active = blendful_enabled and desire == log.Desire.none and DH.lane_change_state == log.LaneChangeState.off
action = model.get_action_from_model(model_output, prev_action, lat_action_t, long_action_t, v_ego,
partial(blendful.update, model_output, v_ego, blendful_active))
prev_action = action
fill_model_msg(drivingdata_send, modelv2_send, model_output, action,
publish_state, meta_main.frame_id, meta_extra.frame_id, frame_id,
@@ -499,6 +509,13 @@ def main(demo=False):
modelv2_send.modelV2.meta.laneChangeDirection = DH.lane_change_direction
mdv2sp_send.valid = modelv2_send.valid
mdv2sp_send.modelDataV2SP.laneTurnDirection = DH.lane_turn_direction
mdv2sp_send.modelDataV2SP.blendfulCurvature = blendful.curvature
mdv2sp_send.modelDataV2SP.blendfulTrust = float(blendful.trust)
mdv2sp_send.modelDataV2SP.modelCurvature = float(blendful.model_curvature)
mdv2sp_send.modelDataV2SP.blendfulGoingAround = bool(blendful.going_around)
mdv2sp_send.modelDataV2SP.blendfulLeftProb = blendful.l_prob
mdv2sp_send.modelDataV2SP.blendfulRightProb = blendful.r_prob
mdv2sp_send.modelDataV2SP.blendfulLaneWidth = float(blendful.lane_width.x)
drivingdata_send.drivingModelData.meta.laneChangeState = DH.lane_change_state
drivingdata_send.drivingModelData.meta.laneChangeDirection = DH.lane_change_direction
@@ -2021,8 +2021,14 @@
{
"id": "model_behavior",
"title": "Model Behavior",
"description": "Lane desire and lead-vehicle awareness tuning",
"description": "Lane desire, Blendful lane centering, and lead-vehicle awareness tuning",
"items": [
{
"key": "Blendful",
"widget": "toggle",
"title": "Blendful Mode",
"description": "Nudges the car toward the lane center when the lane lines are clear. Drives laneless when they are not."
},
{
"key": "LaneTurnDesire",
"widget": "toggle",
@@ -9,8 +9,12 @@ description: Driving model behavior and camera calibration
sections:
- id: model_behavior
title: Model Behavior
description: Lane desire and lead-vehicle awareness tuning
description: Lane desire, Blendful lane centering, and lead-vehicle awareness tuning
items:
- key: Blendful
widget: toggle
title: Blendful Mode
description: Nudges the car toward the lane center when the lane lines are clear. Drives laneless when they are not.
- key: LaneTurnDesire
widget: toggle
title: Use Lane Turn Desires