mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-12-02 05:23:43 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 41e044d654 | |||
| 1d8788e9d5 | |||
| 5966b94504 | |||
| 7fde95c2e4 |
@@ -52,7 +52,13 @@ std::vector<std::string> PandaSafety::fetchCarParams() {
|
||||
if (!params_.getBool("ControlsReady")) {
|
||||
return {};
|
||||
}
|
||||
return {params_.get("CarParams"), params_.get("CarParamsSP")};
|
||||
|
||||
std::string car_params = params_.get("CarParams");
|
||||
std::string car_params_sp = params_.get("CarParamsSP");
|
||||
if (car_params.empty() || car_params_sp.empty()) {
|
||||
return {};
|
||||
}
|
||||
return {car_params, car_params_sp};
|
||||
}
|
||||
|
||||
// TODO-SP: Use structs instead of vector
|
||||
|
||||
@@ -8,10 +8,10 @@ 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, Plan
|
||||
from openpilot.sunnypilot.modeld_v2.constants import ModelConstants
|
||||
|
||||
LANE_WIDTH_DEFAULT = 3.6 # m
|
||||
LANE_WIDTH_CLIP = (2.4, 4.5) # 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
|
||||
@@ -19,11 +19,6 @@ LINE_STD = (0.2, 0.6)
|
||||
POINT_STD = 0.1
|
||||
YIELD = (0.2, 0.45)
|
||||
X_IDXS = np.array(ModelConstants.X_IDXS)
|
||||
JUMP = 0.5
|
||||
JUMP_TAU = 0.5
|
||||
JUMP_ACCEPT = 3.0
|
||||
WIDEN = 0.4
|
||||
YAW_RATE = Plan.ORIENTATION_RATE.start + 2 # matched .991 correlation to actual car
|
||||
|
||||
|
||||
class Blendful:
|
||||
@@ -41,8 +36,7 @@ class Blendful:
|
||||
self.l_prob = 0.0
|
||||
self.r_prob = 0.0
|
||||
self.model_curvature = 0.0
|
||||
self.ref = None
|
||||
self.jumped = np.zeros(2)
|
||||
self.smooth = 0.0 # smooths the correction target 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, enabled: bool = True) -> float:
|
||||
@@ -58,8 +52,8 @@ class Blendful:
|
||||
self.usual_offset.initialized = False
|
||||
self.lane_width.initialized = False
|
||||
self.going_around = False
|
||||
self.ref = None
|
||||
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
|
||||
@@ -69,10 +63,6 @@ class Blendful:
|
||||
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]
|
||||
plan = model_output['plan'][0]
|
||||
self._jumps(lines[1:3], v_ego, plan[0, YAW_RATE] if plan.shape[-1] > YAW_RATE else model_curvature * v_ego,
|
||||
np.interp(abs(model_curvature) * v_ego ** 2, [0.18, 0.53], [JUMP_TAU, 0.0]), lookahead)
|
||||
held = bool(np.any((self.jumped > 0) & (self.jumped < JUMP_ACCEPT)))
|
||||
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
|
||||
@@ -93,8 +83,6 @@ class Blendful:
|
||||
lane_trust = min(l_prob, r_prob)
|
||||
else:
|
||||
lane_trust = l_prob + r_prob - l_prob * r_prob
|
||||
if held or (l_prob * r_prob > 0.5 and np.interp(lookahead, X_IDXS, lines[2] - lines[1]) > self.lane_width.x + WIDEN):
|
||||
lane_trust = 0.0
|
||||
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
|
||||
@@ -108,7 +96,7 @@ class Blendful:
|
||||
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 and not held:
|
||||
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
|
||||
@@ -155,14 +143,3 @@ class Blendful:
|
||||
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)
|
||||
|
||||
def _jumps(self, raw: np.ndarray, v_ego: float, yaw_rate: float, tau: float, lookahead: float) -> None:
|
||||
if self.ref is None:
|
||||
self.ref, self.jumped = raw.copy(), np.zeros(2)
|
||||
return
|
||||
ref = np.array([np.interp(X_IDXS + v_ego * DT_MDL, X_IDXS, line) for line in self.ref]) - yaw_rate * DT_MDL * X_IDXS
|
||||
off = np.abs([np.interp(lookahead, X_IDXS, d) for d in raw - ref]) > JUMP
|
||||
self.jumped = np.where(off & ~off[::-1], self.jumped + DT_MDL, 0.0)
|
||||
self.ref = ref + (raw - ref) * DT_MDL / (DT_MDL + tau)
|
||||
accept = (self.jumped >= JUMP_ACCEPT) | off.all()
|
||||
self.ref[accept] = raw[accept]
|
||||
|
||||
@@ -8,6 +8,8 @@ import numpy as np
|
||||
|
||||
from openpilot.common.transformations.camera import DEVICE_CAMERAS
|
||||
|
||||
MAX_CAMERA_OFFSET = 0.35 # m, the settings' range
|
||||
|
||||
|
||||
class CameraOffsetHelper:
|
||||
def __init__(self):
|
||||
@@ -34,6 +36,13 @@ class CameraOffsetHelper:
|
||||
def set_offset(self, offset):
|
||||
self.camera_offset = offset
|
||||
|
||||
def load_offset(self, params):
|
||||
offset = params.get("CameraOffset", return_default=True)
|
||||
if offset is None or not np.isfinite(offset) or abs(offset) > MAX_CAMERA_OFFSET + 1e-6: # older sunnypilot stored whole centimeters
|
||||
offset = 0.0
|
||||
params.put("CameraOffset", offset)
|
||||
self.set_offset(offset)
|
||||
|
||||
def update(self, model_transform_main, model_transform_extra, sm, main_wide_camera):
|
||||
self.actual_camera_offset = (0.9 * self.actual_camera_offset) + (0.1 * self.camera_offset)
|
||||
dc = DEVICE_CAMERAS[(str(sm['deviceState'].deviceType), str(sm['narrowRoadCameraState'].sensor))]
|
||||
|
||||
@@ -424,7 +424,7 @@ def main(demo=False):
|
||||
if sm.frame % 60 == 0:
|
||||
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))
|
||||
camera_offset_helper.load_offset(params)
|
||||
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']:
|
||||
|
||||
@@ -27,6 +27,18 @@ class TestCameraOffset(OpenpilotTestCase):
|
||||
self.camera_offset = CameraOffsetHelper()
|
||||
self.dc = DEVICE_CAMERAS[('mici', 'os04c10')]
|
||||
|
||||
def test_load_offset_resets_out_of_range(self):
|
||||
class Params:
|
||||
def __init__(self, v): self.v = v
|
||||
def get(self, key, return_default=False): return self.v
|
||||
def put(self, key, v): self.v = v
|
||||
|
||||
for stored, used in ((0.2, 0.2), (-0.35, -0.35), (4.0, 0.0), (-35.0, 0.0), (float('nan'), 0.0), (None, 0.0)):
|
||||
params = Params(stored)
|
||||
self.camera_offset.load_offset(params)
|
||||
assert self.camera_offset.camera_offset == used
|
||||
assert params.v == (stored if used == stored else 0.0)
|
||||
|
||||
def test_smoothing(self):
|
||||
self.camera_offset.set_offset(0.2)
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ class LatControlTorqueExt(NeuralNetworkLateralControl, LatControlTorqueExtOverri
|
||||
desired_lateral_accel, actual_lateral_accel, lateral_accel_deadzone, gravity_adjusted_lateral_accel,
|
||||
desired_curvature, actual_curvature, steer_limited_by_safety, output_torque):
|
||||
self._ff = ff
|
||||
self._pid = pid
|
||||
self._pid_log = pid_log
|
||||
self._setpoint = setpoint
|
||||
self._measurement = measurement
|
||||
|
||||
@@ -63,7 +63,7 @@ class LatControlTorqueExtBase:
|
||||
self.torque_params = lac_torque.torque_params
|
||||
|
||||
self._ff = 0.0
|
||||
self._pid = PIDController(KP, KI)
|
||||
self._pid = PIDController(KP, KI, pos_limit=lac_torque.steer_max, neg_limit=-lac_torque.steer_max, rate=1 / lac_torque.dt)
|
||||
self._pid_log = log.ControlsState.LateralTorqueState.new_message()
|
||||
self._setpoint = 0.0
|
||||
self._measurement = 0.0
|
||||
|
||||
@@ -4,6 +4,7 @@ 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
|
||||
|
||||
from openpilot.cereal import log, messaging
|
||||
@@ -59,9 +60,9 @@ def _make_model_v2():
|
||||
return model
|
||||
|
||||
|
||||
def _run_update(controller, VM):
|
||||
def _run_update(controller, VM, v_ego=30, desired_curvature=0.5):
|
||||
CS = car.CarState.new_message()
|
||||
CS.vEgo = 30
|
||||
CS.vEgo = v_ego
|
||||
CS.steeringPressed = False
|
||||
lp = generate_deviceMotion()
|
||||
pose = Pose.from_device_motion(lp.deviceMotion)
|
||||
@@ -69,7 +70,7 @@ def _run_update(controller, VM):
|
||||
model_v2 = _make_model_v2().modelV2
|
||||
controller.extension.update_model_v2(model_v2)
|
||||
controller.extension.update_lateral_lag(0.2)
|
||||
return controller.update(True, CS, VM, params, False, 0.5, pose, False, 0.2)
|
||||
return controller.update(True, CS, VM, params, False, desired_curvature, pose, False, 0.2)
|
||||
|
||||
|
||||
class TestLatControlTorqueExt(OpenpilotTestCase):
|
||||
@@ -107,3 +108,12 @@ class TestLatControlTorqueExt(OpenpilotTestCase):
|
||||
controller, VM, _ = _make_controller(enhanced=True, nnlc=True)
|
||||
output_torque, _, pid_log = _run_update(controller, VM)
|
||||
assert pid_log.active
|
||||
|
||||
def test_low_speed_gain_bounded(self):
|
||||
for kwargs in ({"nnlc": True}, {"enhanced": True}):
|
||||
controller, VM, _ = _make_controller(**kwargs)
|
||||
one_degree = -VM.calc_curvature(math.radians(1.0), 2.0, 0.0)
|
||||
output_torque, _, _ = _run_update(controller, VM, v_ego=2.0, desired_curvature=one_degree)
|
||||
assert controller.extension._nnlc_enabled or controller.extension._jerk_aware_enabled
|
||||
assert controller.extension._pid is not controller.pid
|
||||
assert abs(output_torque) < 0.3, kwargs
|
||||
|
||||
Reference in New Issue
Block a user