From 7057c5741908df51079fefe23b7305109f4df8f7 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Fri, 5 Sep 2025 20:51:58 -0400 Subject: [PATCH] ui: cleanup cereal event params parsing (#1219) * Revert "bugfix: streamline LiveDelay parameter loading with safe handling (#1204)" This reverts commit 288a5e14daf5bdb3948cf4de8aab96c62f1b9cca. * ui: use AlignedBuffer for cereal data processing for Models panel * align * separate * split * event it * no more backup * Revert "no more backup" This reverts commit fa66ce5e774db6832a0cf3a32ff6fb6803cf0df1. --- .../qt/offroad/settings/models_panel.cc | 58 ++++++------------- .../qt/offroad/settings/models_panel.h | 1 + selfdrive/ui/sunnypilot/qt/util.cc | 13 +++++ selfdrive/ui/sunnypilot/qt/util.h | 3 + 4 files changed, 34 insertions(+), 41 deletions(-) diff --git a/selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.cc b/selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.cc index 02a01a4b6..c3f795e18 100644 --- a/selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.cc +++ b/selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.cc @@ -48,25 +48,6 @@ static const QString progressStyleError = progressStyleActive + " background-color: transparent;" "}"; -std::optional safeParamEventLoad(Params& params, const std::string& paramName) { - std::string raw = params.get(paramName); - if (raw.empty()) { - return std::nullopt; - } - - try { - AlignedBuffer alignedBuf; - auto buf = alignedBuf.align(raw.data(), raw.size()); - - capnp::FlatArrayMessageReader msg(kj::ArrayPtr(buf.begin(), buf.size())); - return msg.getRoot(); - } - catch (const kj::Exception& e) { - qInfo() << "Invalid param" << QString::fromStdString(paramName) << ":" << e.getDescription().cStr(); - return std::nullopt; - } -} - ModelsPanel::ModelsPanel(QWidget *parent) : QWidget(parent) { QVBoxLayout *main_layout = new QVBoxLayout(this); main_layout->setContentsMargins(50, 20, 50, 20); @@ -152,16 +133,10 @@ ModelsPanel::ModelsPanel(QWidget *parent) : QWidget(parent) { list->addItem(lagd_toggle_control); // Software delay control - int liveDelayMaxInt = 30; - if (const auto event = safeParamEventLoad(params, "LiveDelay"); event && event->hasLiveDelay()) { - auto liveDelay = event->getLiveDelay(); - float lateralDelay = liveDelay.getLateralDelay(); - liveDelayMaxInt = static_cast(lateralDelay * 100.0f) + 20; - } delay_control = new OptionControlSP("LagdToggleDelay", tr("Adjust Software Delay"), - tr("Adjust the software delay when Live Learning Steer Delay is toggled off." - "\nThe default software delay value is 0.2"), - "", {5, liveDelayMaxInt}, 1, false, nullptr, true, true); + tr("Adjust the software delay when Live Learning Steer Delay is toggled off." + "\nThe default software delay value is 0.2"), + "", {5, 50}, 1, false, nullptr, true, true); connect(delay_control, &OptionControlSP::updateLabels, [=]() { float value = QString::fromStdString(params.get("LagdToggleDelay")).toFloat(); @@ -449,27 +424,28 @@ void ModelsPanel::updateLabels() { "Disable to use a fixed steering response time. Keeping this on provides the stock openpilot experience."); bool lagdEnabled = params.getBool("LagdToggle"); if (lagdEnabled) { - if (const auto event = safeParamEventLoad(params, "LiveDelay"); event && event->hasLiveDelay()) { - auto liveDelay = event->getLiveDelay(); - float lateralDelay = liveDelay.getLateralDelay(); + auto liveDelayBytes = params.get("LiveDelay"); + if (!liveDelayBytes.empty()) { + auto LD = loadCerealEvent(params, "LiveDelay"); + float lateralDelay = LD->getLiveDelay().getLateralDelay(); desc += QString("

%1 %2 s") - .arg(tr("Live Steer Delay:")).arg(QString::number(lateralDelay, 'f', 3)); + .arg(tr("Live Steer Delay:")).arg(QString::number(lateralDelay, 'f', 3)); } } else { - std::string carParamsBytes = params.get("CarParamsPersistent"); + auto carParamsBytes = params.get("CarParamsPersistent"); if (!carParamsBytes.empty()) { - capnp::FlatArrayMessageReader msg(kj::ArrayPtr( - reinterpret_cast(carParamsBytes.data()), - carParamsBytes.size() / sizeof(capnp::word))); - auto carParams = msg.getRoot(); - float steerDelay = carParams.getSteerActuatorDelay(); + AlignedBuffer aligned_buf_cp; + capnp::FlatArrayMessageReader cmsg(aligned_buf_cp.align(carParamsBytes.data(), carParamsBytes.size())); + cereal::CarParams::Reader CP = cmsg.getRoot(); + + float steerDelay = CP.getSteerActuatorDelay(); float softwareDelay = QString::fromStdString(params.get("LagdToggleDelay")).toFloat(); float totalLag = steerDelay + softwareDelay; desc += QString("

" "%1 %2 s + %3 %4 s = %5 %6 s") - .arg(tr("Actuator Delay:"), QString::number(steerDelay, 'f', 2), - tr("Software Delay:"), QString::number(softwareDelay, 'f', 2), - tr("Total Delay:"), QString::number(totalLag, 'f', 2)); + .arg(tr("Actuator Delay:"), QString::number(steerDelay, 'f', 2), + tr("Software Delay:"), QString::number(softwareDelay, 'f', 2), + tr("Total Delay:"), QString::number(totalLag, 'f', 2)); } } lagd_toggle_control->setDescription(desc); diff --git a/selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.h b/selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.h index 1906ebd2a..1a39800dd 100644 --- a/selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.h +++ b/selfdrive/ui/sunnypilot/qt/offroad/settings/models_panel.h @@ -9,6 +9,7 @@ #include +#include "selfdrive/ui/sunnypilot/qt/util.h" #include "selfdrive/ui/sunnypilot/qt/offroad/settings/settings.h" class ModelsPanel : public QWidget { diff --git a/selfdrive/ui/sunnypilot/qt/util.cc b/selfdrive/ui/sunnypilot/qt/util.cc index ca85935d0..2e066e88b 100644 --- a/selfdrive/ui/sunnypilot/qt/util.cc +++ b/selfdrive/ui/sunnypilot/qt/util.cc @@ -110,3 +110,16 @@ QStringList searchFromList(const QString &query, const QStringList &list) { } return search_results; } + +std::optional loadCerealEvent(Params& params, const std::string& _param) { + std::string bytes = params.get(_param); + + try { + AlignedBuffer aligned_buf; + capnp::FlatArrayMessageReader cmsg(aligned_buf.align(bytes.data(), bytes.size())); + return cmsg.getRoot(); + } catch (kj::Exception& e) { + qInfo() << "invalid " << QString::fromStdString(_param) << ":" << e.getDescription().cStr(); + return std::nullopt; + } +} diff --git a/selfdrive/ui/sunnypilot/qt/util.h b/selfdrive/ui/sunnypilot/qt/util.h index 089b5370c..4b9d615ce 100644 --- a/selfdrive/ui/sunnypilot/qt/util.h +++ b/selfdrive/ui/sunnypilot/qt/util.h @@ -15,8 +15,11 @@ #include #include +#include "selfdrive/ui/sunnypilot/ui.h" + QString getUserAgent(bool sunnylink = false); std::optional getSunnylinkDongleId(); std::optional getParamIgnoringDefault(const std::string ¶m_name, const std::string &default_value); QMap loadPlatformList(); QStringList searchFromList(const QString &query, const QStringList &list); +std::optional loadCerealEvent(Params& params, const std::string& _param);