From 57fe8488ac8bf02ea7f2eb61be2e256ece773cd6 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Wed, 7 Apr 2021 06:02:40 +0800 Subject: [PATCH] Qt: show current calibration values (#20455) * show current calibration * read calibration from capnp * use CalStatus * cleanup calibrationd.py * remove import capnp * keep json writing,remove comment * fix test error * cleanup * remove test_read_saved_params * cleanup * write out capnp * restore test * clean up * get calibration from CalibrationParams * cleanup * update calibration when the description is visible * cleanup Co-authored-by: Adeeb Shihadeh Co-authored-by: Comma Device --- selfdrive/locationd/calibrationd.py | 7 ++----- selfdrive/ui/qt/offroad/settings.cc | 28 +++++++++++++++++++++++++--- selfdrive/ui/qt/widgets/controls.cc | 3 +++ selfdrive/ui/qt/widgets/controls.hpp | 8 ++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/selfdrive/locationd/calibrationd.py b/selfdrive/locationd/calibrationd.py index f9b857fbc4..b3254185ba 100755 --- a/selfdrive/locationd/calibrationd.py +++ b/selfdrive/locationd/calibrationd.py @@ -80,7 +80,7 @@ class Calibrator(): rpy_init = list(msg.liveCalibration.rpyCalib) valid_blocks = msg.liveCalibration.validBlocks except (ValueError, capnp.lib.capnp.KjException): - # TODO: remove this when offroad can read capnp + # TODO: remove this after next release calibration_params = json.loads(calibration_params) rpy_init = calibration_params["calib_radians"] valid_blocks = calibration_params['valid_blocks'] @@ -134,10 +134,7 @@ class Calibrator(): write_this_cycle = (self.idx == 0) and (self.block_idx % (INPUTS_WANTED//5) == 5) if self.param_put and write_this_cycle: - # TODO: change to raw bytes when offroad can read capnp - cal_params = {"calib_radians": list(self.rpy), - "valid_blocks": int(self.valid_blocks)} - put_nonblocking("CalibrationParams", json.dumps(cal_params).encode('utf8')) + put_nonblocking("CalibrationParams", self.get_msg().to_bytes()) def handle_v_ego(self, v_ego): self.v_ego = v_ego diff --git a/selfdrive/ui/qt/offroad/settings.cc b/selfdrive/ui/qt/offroad/settings.cc index 2b1763a45f..7c5537f618 100644 --- a/selfdrive/ui/qt/offroad/settings.cc +++ b/selfdrive/ui/qt/offroad/settings.cc @@ -93,12 +93,34 @@ DevicePanel::DevicePanel(QWidget* parent) : QWidget(parent) { GLWindow::ui_state.scene.driver_view = true; } )); - offroad_btns.append(new ButtonControl("Reset Calibration", "RESET", - "openpilot requires the device to be mounted within 4° left or right and within 5° up or down. openpilot is continuously calibrating, resetting is rarely required.", [=]() { + QString resetCalibDesc = "openpilot requires the device to be mounted within 4° left or right and within 5° up or down. openpilot is continuously calibrating, resetting is rarely required."; + ButtonControl *resetCalibBtn = new ButtonControl("Reset Calibration", "RESET", resetCalibDesc, [=]() { if (ConfirmationDialog::confirm("Are you sure you want to reset calibration?")) { Params().remove("CalibrationParams"); } - })); + }); + connect(resetCalibBtn, &ButtonControl::showDescription, [=]() { + QString desc = resetCalibDesc; + std::string calib_bytes = Params().get("CalibrationParams"); + if (!calib_bytes.empty()) { + try { + AlignedBuffer aligned_buf; + capnp::FlatArrayMessageReader cmsg(aligned_buf.align(calib_bytes.data(), calib_bytes.size())); + auto calib = cmsg.getRoot().getLiveCalibration(); + if (calib.getCalStatus() != 0) { + double pitch = calib.getRpyCalib()[1] * (180 / M_PI); + double yaw = calib.getRpyCalib()[2] * (180 / M_PI); + desc += QString(" Your device is pointed %1° %2 and %3° %4.") + .arg(QString::number(std::abs(pitch), 'g', 1), pitch > 0 ? "up" : "down", + QString::number(std::abs(yaw), 'g', 1), yaw > 0 ? "right" : "left"); + } + } catch (kj::Exception) { + qInfo() << "invalid CalibrationParams"; + } + } + resetCalibBtn->setDescription(desc); + }); + offroad_btns.append(resetCalibBtn); offroad_btns.append(new ButtonControl("Review Training Guide", "REVIEW", "Review the rules, features, and limitations of openpilot", [=]() { diff --git a/selfdrive/ui/qt/widgets/controls.cc b/selfdrive/ui/qt/widgets/controls.cc index d9c170e27e..37fa13e61c 100644 --- a/selfdrive/ui/qt/widgets/controls.cc +++ b/selfdrive/ui/qt/widgets/controls.cc @@ -48,6 +48,9 @@ AbstractControl::AbstractControl(const QString &title, const QString &desc, cons vlayout->addWidget(description); connect(title_label, &QPushButton::clicked, [=]() { + if (!description->isVisible()) { + emit showDescription(); + } description->setVisible(!description->isVisible()); }); } diff --git a/selfdrive/ui/qt/widgets/controls.hpp b/selfdrive/ui/qt/widgets/controls.hpp index b87299a59a..652749e9f2 100644 --- a/selfdrive/ui/qt/widgets/controls.hpp +++ b/selfdrive/ui/qt/widgets/controls.hpp @@ -14,6 +14,14 @@ QFrame *horizontal_line(QWidget *parent = nullptr); class AbstractControl : public QFrame { Q_OBJECT +public: + void setDescription(const QString &desc) { + if(description) description->setText(desc); + } + +signals: + void showDescription(); + protected: AbstractControl(const QString &title, const QString &desc = "", const QString &icon = "", QWidget *parent = nullptr);