From 9b5213d3f6894d40a592ee2819ce355536fb6f51 Mon Sep 17 00:00:00 2001 From: Kenn Sebesta Date: Sat, 15 Jan 2022 07:32:38 -0500 Subject: [PATCH 1/5] [RTData] Make it pedantically clear that the `if()` is looking at a bool --- pages/pagertdata.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pages/pagertdata.cpp b/pages/pagertdata.cpp index ca3c10252..728914d0f 100644 --- a/pages/pagertdata.cpp +++ b/pages/pagertdata.cpp @@ -263,7 +263,7 @@ void PageRtData::timerSlot() } } - if (mUpdateValPlot) { + if (mUpdateValPlot == true) { int dataSize = mTempMosVec.size(); QVector xAxis(dataSize); @@ -346,7 +346,7 @@ void PageRtData::timerSlot() mUpdateValPlot = false; } - if (mUpdatePosPlot) { + if (mUpdatePosPlot == true) { QVector xAxis(mPositionVec.size()); for (int i = 0;i < mPositionVec.size();i++) { xAxis[i] = double(i); @@ -364,7 +364,7 @@ void PageRtData::timerSlot() mUpdatePosPlot = false; } - if (mExperimentReplot) { + if (mExperimentReplot == true) { ui->experimentPlot->clearGraphs(); for (int i = 0;i < mExperimentPlots.size();i++) { From 833d10814a0b53e9d58732e6e7d6d3c7c2e98c6d Mon Sep 17 00:00:00 2001 From: Kenn Sebesta Date: Sat, 29 Jan 2022 11:00:22 -0500 Subject: [PATCH 2/5] [RTData] Only rescale when graphs are visible --- pages/pageappadc.cpp | 4 ++-- pages/pageappbalance.cpp | 3 +-- pages/pageappimu.cpp | 6 +++--- pages/pageappnunchuk.cpp | 3 ++- pages/pageappppm.cpp | 4 +++- pages/pageexperiments.cpp | 2 +- pages/pageimu.cpp | 8 ++++---- pages/pagertdata.cpp | 26 ++++++++++++++------------ widgets/qcustomplot.h | 15 +++++++++++++++ 9 files changed, 45 insertions(+), 26 deletions(-) diff --git a/pages/pageappadc.cpp b/pages/pageappadc.cpp index 7eabc9803..638ee09c1 100644 --- a/pages/pageappadc.cpp +++ b/pages/pageappadc.cpp @@ -98,8 +98,8 @@ void PageAppAdc::paramChangedDouble(QObject *src, QString name, double newParam) y.append(val); } ui->throttlePlot->graph()->setData(x, y); - ui->throttlePlot->rescaleAxes(); - ui->throttlePlot->replot(); + ui->throttlePlot->rescaleAxesWhenVisible(); + ui->throttlePlot->replotWhenVisible(); } } diff --git a/pages/pageappbalance.cpp b/pages/pageappbalance.cpp index d497b9074..b217433b0 100644 --- a/pages/pageappbalance.cpp +++ b/pages/pageappbalance.cpp @@ -165,8 +165,7 @@ void PageAppBalance::timerSlot() } ui->balancePlot->graph(graphIndex++)->setData(xAxis, mAppDebug2); - ui->balancePlot->rescaleAxes(); - + ui->balancePlot->rescaleAxesWhenVisible(); ui->balancePlot->replotWhenVisible(); updateTextOutput(); diff --git a/pages/pageappimu.cpp b/pages/pageappimu.cpp index 1710ab578..878150018 100644 --- a/pages/pageappimu.cpp +++ b/pages/pageappimu.cpp @@ -186,9 +186,9 @@ void PageAppImu::timerSlot() ui->gyroPlot->graph(graphIndex++)->setData(xAxis, mGyroYVec); ui->gyroPlot->graph(graphIndex++)->setData(xAxis, mGyroZVec); - ui->rpyPlot->rescaleAxes(); - ui->accelPlot->rescaleAxes(); - ui->gyroPlot->rescaleAxes(); + ui->rpyPlot->rescaleAxesWhenVisible(); + ui->accelPlot->rescaleAxesWhenVisible(); + ui->gyroPlot->rescaleAxesWhenVisible(); ui->rpyPlot->replotWhenVisible(); ui->accelPlot->replotWhenVisible(); diff --git a/pages/pageappnunchuk.cpp b/pages/pageappnunchuk.cpp index 6cf392cb9..3e8be9dd6 100644 --- a/pages/pageappnunchuk.cpp +++ b/pages/pageappnunchuk.cpp @@ -107,7 +107,8 @@ void PageAppNunchuk::paramChangedDouble(QObject *src, QString name, double newPa y.append(val); } ui->throttlePlot->graph()->setData(x, y); - ui->throttlePlot->rescaleAxes(); + + ui->throttlePlot->rescaleAxesWhenVisible(); ui->throttlePlot->replotWhenVisible(); } } diff --git a/pages/pageappppm.cpp b/pages/pageappppm.cpp index df85cbdc4..cd15b7024 100644 --- a/pages/pageappppm.cpp +++ b/pages/pageappppm.cpp @@ -97,8 +97,10 @@ void PageAppPpm::paramChangedDouble(QObject *src, QString name, double newParam) double val = Utility::throttle_curve(i, val_acc, val_brake, mode); y.append(val); } + ui->throttlePlot->graph()->setData(x, y); - ui->throttlePlot->rescaleAxes(); + + ui->throttlePlot->rescaleAxesWhenVisible(); ui->throttlePlot->replotWhenVisible(); } } diff --git a/pages/pageexperiments.cpp b/pages/pageexperiments.cpp index ec325684b..1475c5323 100644 --- a/pages/pageexperiments.cpp +++ b/pages/pageexperiments.cpp @@ -594,7 +594,7 @@ void PageExperiments::plotSamples(bool exportFormat) } if (ui->autoscaleButton->isChecked()) { - ui->plot->rescaleAxes(); + ui->plot->rescaleAxesWhenVisible(); } ui->plot->replotWhenVisible(); diff --git a/pages/pageimu.cpp b/pages/pageimu.cpp index c4b76c763..fbdb0573e 100644 --- a/pages/pageimu.cpp +++ b/pages/pageimu.cpp @@ -201,10 +201,10 @@ void PageImu::timerSlot() ui->magPlot->graph(graphIndex++)->setData(xAxis, mMagYVec); ui->magPlot->graph(graphIndex++)->setData(xAxis, mMagZVec); - ui->rpyPlot->rescaleAxes(); - ui->accelPlot->rescaleAxes(); - ui->gyroPlot->rescaleAxes(); - ui->magPlot->rescaleAxes(); + ui->rpyPlot->rescaleAxesWhenVisible(); + ui->accelPlot->rescaleAxesWhenVisible(); + ui->gyroPlot->rescaleAxesWhenVisible(); + ui->magPlot->rescaleAxesWhenVisible(); ui->rpyPlot->replotWhenVisible(); ui->accelPlot->replotWhenVisible(); diff --git a/pages/pagertdata.cpp b/pages/pagertdata.cpp index 728914d0f..848d54be2 100644 --- a/pages/pagertdata.cpp +++ b/pages/pagertdata.cpp @@ -318,7 +318,6 @@ void PageRtData::timerSlot() ui->tempPlot->graph(graphIndex)->setName("Temperature Motor"); ui->tempPlot->graph(graphIndex)->setData(xAxis, mTempMotorVec); ui->tempPlot->graph(graphIndex)->setVisible(ui->tempShowMotorBox->isChecked()); - graphIndex++; // RPM plot graphIndex = 0; @@ -331,11 +330,13 @@ void PageRtData::timerSlot() ui->focPlot->graph(graphIndex++)->setData(xAxis, mVdVec); ui->focPlot->graph(graphIndex++)->setData(xAxis, mVqVec); + // Check if we should rescale the graphs if (ui->autoscaleButton->isChecked()) { - ui->currentPlot->rescaleAxes(); - ui->tempPlot->rescaleAxes(); - ui->rpmPlot->rescaleAxes(); - ui->focPlot->rescaleAxes(); + // Only rescale if visible + ui->currentPlot->rescaleAxesWhenVisible(); + ui->tempPlot->rescaleAxesWhenVisible(); + ui->rpmPlot->rescaleAxesWhenVisible(); + ui->focPlot->rescaleAxesWhenVisible(); } ui->currentPlot->replotWhenVisible(); @@ -356,7 +357,7 @@ void PageRtData::timerSlot() ui->posPlot->graph(0)->setData(xAxis, mPositionVec); if (ui->autoscaleButton->isChecked()) { - ui->posPlot->rescaleAxes(); + ui->posPlot->rescaleAxesWhenVisible(); } ui->posPlot->replotWhenVisible(); @@ -396,10 +397,11 @@ void PageRtData::timerSlot() ui->experimentPlot->legend->setVisible(mExperimentPlots.size() > 1); if (ui->experimentAutoScaleButton->isChecked()) { - ui->experimentPlot->rescaleAxes(); + ui->experimentPlot->rescaleAxesWhenVisible(); } ui->experimentPlot->replotWhenVisible(); + mExperimentReplot = false; } } @@ -540,11 +542,11 @@ void PageRtData::on_zoomVButton_toggled(bool checked) void PageRtData::on_rescaleButton_clicked() { - ui->currentPlot->rescaleAxes(); - ui->tempPlot->rescaleAxes(); - ui->rpmPlot->rescaleAxes(); - ui->focPlot->rescaleAxes(); - ui->posPlot->rescaleAxes(); + ui->currentPlot->rescaleAxesWhenVisible(); + ui->tempPlot->rescaleAxesWhenVisible(); + ui->rpmPlot->rescaleAxesWhenVisible(); + ui->focPlot->rescaleAxesWhenVisible(); + ui->posPlot->rescaleAxesWhenVisible(); ui->currentPlot->replotWhenVisible(); ui->tempPlot->replotWhenVisible(); diff --git a/widgets/qcustomplot.h b/widgets/qcustomplot.h index 53b225144..983a81ff1 100644 --- a/widgets/qcustomplot.h +++ b/widgets/qcustomplot.h @@ -3910,6 +3910,14 @@ class QCP_LIB_DECL QCustomPlot : public QWidget QCPAxis *xAxis, *yAxis, *xAxis2, *yAxis2; QCPLegend *legend; + void rescaleAxesWhenVisible() { + if (isVisible()) { + rescaleAxes(); + } else { + doRescaleOnShow = true; + } + } + void replotWhenVisible() { if (isVisible()) { replot(rpQueuedReplot); @@ -3940,11 +3948,18 @@ class QCP_LIB_DECL QCustomPlot : public QWidget void afterReplot(); protected: + bool doRescaleOnShow; bool doReplotOnShow; virtual void showEvent(QShowEvent *event) override { (void)event; + + // Catch up on any queued events + if (doRescaleOnShow) { + rescaleAxes(); + doRescaleOnShow = false; + } if (doReplotOnShow) { replot(rpQueuedReplot); doReplotOnShow = false; From 0feef3aae97d121f19688039500340aaf4dc2e20 Mon Sep 17 00:00:00 2001 From: Kenn Sebesta Date: Wed, 19 Jan 2022 15:08:07 -0500 Subject: [PATCH 3/5] [Pages][Welcome] Turn `updateData` off when this page is not visible --- mobile/StatPage.qml | 2 ++ pages/pagewelcome.cpp | 33 +++++++++++++++++++++++++++++++++ pages/pagewelcome.h | 3 +++ res/qml/WelcomeQmlPanel.qml | 4 ++++ 4 files changed, 42 insertions(+) diff --git a/mobile/StatPage.qml b/mobile/StatPage.qml index 5c583a2aa..34e6ff613 100644 --- a/mobile/StatPage.qml +++ b/mobile/StatPage.qml @@ -28,6 +28,7 @@ import Vedder.vesc.utility 1.0 Item { property Commands mCommands: VescIf.commands() property bool isHorizontal: width > height + property alias updateData: commandsUpdate.enabled Component.onCompleted: { mCommands.emitEmptyStats() @@ -87,6 +88,7 @@ Item { } Connections { + id: commandsUpdate target: mCommands onValuesSetupReceived: { diff --git a/pages/pagewelcome.cpp b/pages/pagewelcome.cpp index dc103941b..f8da1f3a4 100755 --- a/pages/pagewelcome.cpp +++ b/pages/pagewelcome.cpp @@ -29,6 +29,8 @@ #include #include #include +#include + PageWelcome::PageWelcome(QWidget *parent) : QWidget(parent), @@ -113,8 +115,39 @@ void PageWelcome::setVesc(VescInterface *vesc) ui->qmlWidget->engine()->rootContext()->setContextProperty("Utility", &mUtil); ui->qmlWidget->setSource(QUrl(QLatin1String("qrc:/res/qml/WelcomeQmlPanel.qml"))); + + // Install an event filter to monitor for when this QML widget becomes hidden + ui->qmlWidget->installEventFilter(this); +} + + +bool PageWelcome::eventFilter(QObject *obj, QEvent *event) +{ + switch(event->type()) { + case QEvent::Show: { + // Set the telemetry to active when this page is visible + bool ret = QQmlProperty::write(ui->qmlWidget->rootObject(), "shouldTelemetryBeActive", true); + if (ret == false) { + qDebug() << "[QML][Error] shouldTelemetryBeActive was not successfully written. Perhaps that variable no longer exists in the QML component file?"; + } + } break; + case QEvent::Hide: { + // Set the telemetry to inactive when this page is not visible + bool ret = QQmlProperty::write(ui->qmlWidget->rootObject(), "shouldTelemetryBeActive", false); + + if (ret == false) { + qDebug() << "[QML][Error] shouldTelemetryBeActive was not successfully written. Perhaps that variable no longer exists in the QML component file?"; + } + } break; + default: { + } break; + } + + return QObject::eventFilter(obj, event); } + + void PageWelcome::on_autoConnectButton_clicked() { Utility::autoconnectBlockingWithProgress(mVesc, this); diff --git a/pages/pagewelcome.h b/pages/pagewelcome.h index d6cfaf54a..aa5aae56f 100644 --- a/pages/pagewelcome.h +++ b/pages/pagewelcome.h @@ -56,6 +56,9 @@ private slots: QmlUi mQmlUi; Utility mUtil; +protected: + bool eventFilter(QObject *obj, QEvent *event); + }; #endif // PAGEWELCOME_H diff --git a/res/qml/WelcomeQmlPanel.qml b/res/qml/WelcomeQmlPanel.qml index b752d8aa6..3a4a28df8 100644 --- a/res/qml/WelcomeQmlPanel.qml +++ b/res/qml/WelcomeQmlPanel.qml @@ -37,6 +37,8 @@ Item { property int notchBot: 0 property int notchTop: 0 + property bool shouldTelemetryBeActive: true + function setupMotors() { if (!VescIf.isPortConnected()) { VescIf.emitMessageDialog("Setup motors FOC", @@ -213,6 +215,7 @@ Item { RtDataSetup { anchors.fill: parent dialogParent: container + updateData: shouldTelemetryBeActive } } @@ -220,6 +223,7 @@ Item { StatPage { anchors.fill: parent anchors.margins: 20 + updateData: shouldTelemetryBeActive } } } From e82b3fb3053f88be8ac0cc77b64517e07c65b6d5 Mon Sep 17 00:00:00 2001 From: Kenn Sebesta Date: Wed, 19 Jan 2022 15:32:40 -0500 Subject: [PATCH 4/5] [Realtime data][text readout] Repaint at a reduced frequency --- widgets/rtdatatext.cpp | 17 ++++++++++++++++- widgets/rtdatatext.h | 4 ++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/widgets/rtdatatext.cpp b/widgets/rtdatatext.cpp index da5518745..d5f2442a4 100644 --- a/widgets/rtdatatext.cpp +++ b/widgets/rtdatatext.cpp @@ -29,6 +29,7 @@ RtDataText::RtDataText(QWidget *parent) : QWidget(parent) mBoxH = 10; mBoxW = 10; mTxtOfs = 2; + shouldUpdate_flag = false; mValues.amp_hours = 0; mValues.amp_hours_charged = 0; @@ -47,13 +48,27 @@ RtDataText::RtDataText(QWidget *parent) : QWidget(parent) mValues.v_in = 0; mValues.watt_hours = 0; mValues.watt_hours_charged = 0; + + // Create a timer to control the text repaint rate + mTimer = new QTimer(this); + mTimer->setInterval(1000.0/30); // Set refresh rate to 30Hz + mTimer->start(); + + QObject::connect(mTimer, &QTimer::timeout, [&]() { + // Check if data should be repainted + if (shouldUpdate_flag == true){ + // Trigger the repaint + update(); + shouldUpdate_flag = false; + } + }); } void RtDataText::setValues(const MC_VALUES &values) { mValues = values; mValues.fault_str.remove(0, 11); - update(); + shouldUpdate_flag = true; } QSize RtDataText::sizeHint() const diff --git a/widgets/rtdatatext.h b/widgets/rtdatatext.h index 34f59e286..72d7e9441 100644 --- a/widgets/rtdatatext.h +++ b/widgets/rtdatatext.h @@ -21,6 +21,7 @@ #define RTDATATEXT_H #include +#include #include "datatypes.h" class RtDataText : public QWidget @@ -45,6 +46,9 @@ public slots: int mBoxH; int mBoxW; int mTxtOfs; + bool shouldUpdate_flag; + + QTimer *mTimer; }; From 0d4136fed77949eef1db8e93a4e5ee4dd50728aa Mon Sep 17 00:00:00 2001 From: Kenn Sebesta Date: Wed, 19 Jan 2022 15:48:00 -0500 Subject: [PATCH 5/5] [MainWindow] Repaint display bars at a reduced frequency Too high of a frequency can't be easily read and burns up lots of CPU in redraw events. --- mainwindow.cpp | 17 +++++++++++++++-- mainwindow.h | 4 ++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index ee3678acd..0501602e0 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -219,6 +219,8 @@ MainWindow::MainWindow(QWidget *parent) : mKeyLeft = false; mKeyRight = false; + shouldUpdateDisplayBars = false; + connect(mDebugTimer, SIGNAL(timeout()), this, SLOT(timerSlotDebugMsg())); connect(mTimer, SIGNAL(timeout()), @@ -481,6 +483,8 @@ MainWindow::MainWindow(QWidget *parent) : mPollImuTimer.start(int(1000.0 / mSettings.value("poll_rate_imu_data", 50).toDouble())); mPollBmsTimer.start(int(1000.0 / mSettings.value("poll_rate_bms_data", 10).toDouble())); + mRepaintDisplayBarTimer.start(1000.0/30); // Set the refresh rate to 30Hz + connect(&mPollRtTimer, &QTimer::timeout, [this]() { if (ui->actionRtData->isChecked()) { mVesc->commands()->getValues(); @@ -514,6 +518,15 @@ MainWindow::MainWindow(QWidget *parent) : } }); + connect(&mRepaintDisplayBarTimer, &QTimer::timeout, [this]() { + if (shouldUpdateDisplayBars) { + ui->dispCurrent->setVal(mMcValues.current_motor); + ui->dispDuty->setVal(mMcValues.duty_now * 100.0); + + shouldUpdateDisplayBars = false; + } + }); + // Restore size and position if (mSettings.contains("mainwindow/size")) { resize(mSettings.value("mainwindow/size").toSize()); @@ -951,8 +964,8 @@ void MainWindow::serialPortNotWritable(const QString &port) void MainWindow::valuesReceived(MC_VALUES values, unsigned int mask) { (void)mask; - ui->dispCurrent->setVal(values.current_motor); - ui->dispDuty->setVal(values.duty_now * 100.0); + mMcValues = values; + shouldUpdateDisplayBars = true; } void MainWindow::paramChangedDouble(QObject *src, QString name, double newParam) diff --git a/mainwindow.h b/mainwindow.h index 1106afe6e..7acbaf853 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -166,12 +166,16 @@ private slots: bool mKeyRight; bool mMcConfRead; bool mAppConfRead; + bool shouldUpdateDisplayBars; QMap mPageNameIdList; QTimer mPollRtTimer; QTimer mPollAppTimer; QTimer mPollImuTimer; QTimer mPollBmsTimer; + QTimer mRepaintDisplayBarTimer; + + MC_VALUES mMcValues; PageWelcome *mPageWelcome; PageConnection *mPageConnection;