diff options
| -rw-r--r-- | Main.qml | 37 | ||||
| -rw-r--r-- | main.cpp | 291 |
2 files changed, 210 insertions, 118 deletions
@@ -517,6 +517,11 @@ ApplicationWindow { onActivated: tabBar.currentIndex = 1 } + Shortcut { + sequence: "alt+3" + onActivated: tabBar.currentIndex = 2 + } + TabButton { text: qsTr("Image") } @@ -524,6 +529,10 @@ ApplicationWindow { TabButton { text: qsTr("Pixels") } + + TabButton { + text: qsTr("Profile") + } } SwipeView { @@ -540,9 +549,29 @@ ApplicationWindow { } QmlCustomPlot { - id: qmlPlot + id: pixelsQmlCustomPlot + + objectName: "pixelsCustomPlot" + + Label { + anchors { + top: parent.top + topMargin: 8 * 2 + horizontalCenter: parent.horizontalCenter + } + text: pixelsQmlCustomPlot.fps + color: Material.accent + } + + plot: pixelsPlot + // Component.onCompleted: setRange(Qt.rect(-640, 0, 1280, 800)) + // Component.onCompleted: initCustomPlot() + } + + QmlCustomPlot { + id: profileQmlCustomPlot - objectName: "qmlCustomPlot" + objectName: "profileCustomPlot" Label { anchors { @@ -550,11 +579,11 @@ ApplicationWindow { topMargin: 8 * 2 horizontalCenter: parent.horizontalCenter } - text: qmlPlot.fps + text: profileQmlCustomPlot.fps color: Material.accent } - plot: myPlot + plot: profilePlot // Component.onCompleted: setRange(Qt.rect(-640, 0, 1280, 800)) // Component.onCompleted: initCustomPlot() } @@ -10,7 +10,8 @@ // #include <execution> -int main(int argc, char *argv[]) +int main( + int argc, char *argv[]) { QApplication app(argc, argv); @@ -18,168 +19,230 @@ int main(int argc, char *argv[]) qmlRegisterType<QmlCustomPlot>("QmlCustomPlot", 1, 0, "QmlCustomPlot"); - auto plot = new QCustomPlot(); - plot->setOpenGl(true); - engine.rootContext()->setContextProperty("myPlot", plot); + auto pixelsPlot = new QCustomPlot(); + pixelsPlot->setOpenGl(true); + + auto profilePlot = new QCustomPlot(); + profilePlot->setOpenGl(true); + + engine.rootContext()->setContextProperty("pixelsPlot", pixelsPlot); + engine.rootContext()->setContextProperty("profilePlot", profilePlot); engine.rootContext()->setContextProperty("max_diff", 0.0); engine.rootContext()->setContextProperty("avg_diff", 0.0); engine.rootContext()->setContextProperty("median_diff", 0.0); - QVector<qreal> x_profile; - QVector<qreal> y_profile; + QVector<qreal> x_pixels; + QVector<qreal> y_pixels; + QVector<qreal> prev_y_profile; for (int x = -640; x < 640; ++x) { double y = double(QRandomGenerator::global()->bounded(0, 80000)) / 100.; - x_profile << x; - y_profile << y; + x_pixels << x; + y_pixels << y; } - prev_y_profile = y_profile; + prev_y_profile = y_pixels; - auto graph = plot->addGraph(); - graph->setPen(QPen(QBrush(Qt::red), 2)); - graph->setLineStyle(QCPGraph::lsNone); - graph->setScatterStyle(QCPScatterStyle::ssDot); - graph->setData(x_profile, y_profile); + auto pixelsGraph = pixelsPlot->addGraph(); + pixelsGraph->setPen(QPen(QBrush(Qt::red), 2)); + pixelsGraph->setLineStyle(QCPGraph::lsNone); + pixelsGraph->setScatterStyle(QCPScatterStyle::ssDot); + pixelsGraph->setData(x_pixels, y_pixels); - QNetworkRequest request(QUrl("http://rpi5:8081/v1/pixels")); + auto profileGraph = profilePlot->addGraph(); + profileGraph->setPen(QPen(QBrush(Qt::red), 2)); + profileGraph->setLineStyle(QCPGraph::lsNone); + profileGraph->setScatterStyle(QCPScatterStyle::ssDot); + profileGraph->setData(x_pixels, y_pixels); + + QNetworkRequest pixelsRequest(QUrl("http://rpi5:8081/v1/pixels")); + QNetworkRequest profileRequest(QUrl("http://rpi5:8081/v1/profile")); auto manager = new QNetworkAccessManager(&app); QTimer pixelsAutoRestartTimer(&app); pixelsAutoRestartTimer.setInterval(500); pixelsAutoRestartTimer.setSingleShot(false); - QObject::connect(&pixelsAutoRestartTimer, &QTimer::timeout, [&]() { manager->get(request); }); + QObject::connect(&pixelsAutoRestartTimer, &QTimer::timeout, [&]() { + manager->get(pixelsRequest); + }); - QObject::connect(manager, &QNetworkAccessManager::finished, [&](QNetworkReply *reply) { - // qDebug() << "replyFinished"; - if (reply->error()) { - qDebug() << "replyFinished:" << reply->errorString(); - reply->deleteLater(); + QTimer profileAutoRestartTimer(&app); + profileAutoRestartTimer.setInterval(500); + profileAutoRestartTimer.setSingleShot(false); + QObject::connect(&profileAutoRestartTimer, &QTimer::timeout, [&]() { + manager->get(profileRequest); + }); - return; - } + QObject::connect( + manager, &QNetworkAccessManager::finished, [&](QNetworkReply *reply) { + // qDebug() << "replyFinished"; + if (reply->error()) { + qDebug() << "replyFinished:" << reply->errorString(); + reply->deleteLater(); - auto json = QJsonDocument::fromJson(reply->readAll()).object(); - auto jsonPixels = json["pixels"].toArray(); - auto encoderPosition = json["encoderPosition"].toInteger(); - auto measurementCounter = json["measurementCounter"].toInteger(); - auto timestampUs = json["timestampUs"].toInteger(); + return; + } - // y_profile.clear(); + if (reply->url().toString().endsWith("pixels")) { + qDebug() << "got pixels"; + auto json = QJsonDocument::fromJson(reply->readAll()).object(); + auto jsonPixels = json["pixels"].toArray(); + auto encoderPosition = json["encoderPosition"].toInteger(); + auto measurementCounter = json["measurementCounter"].toInteger(); + auto timestampUs = json["timestampUs"].toInteger(); - for (int i = 0; i < jsonPixels.count(); ++i) { - y_profile[i] = jsonPixels.at(i).toDouble(); - } + // y_profile.clear(); - QVector<qreal> y_diff(y_profile.count()); - qreal max_diff{0.}; - qreal min_diff{1000000.}; - size_t max_diff_idx = 0; - size_t min_diff_idx = 0; - qreal avg_diff{0.}; + for (int i = 0; i < jsonPixels.count(); ++i) { + y_pixels[i] = jsonPixels.at(i).toDouble(); + } - for (size_t i = 0; i < y_profile.count(); ++i) { - y_diff[i] = y_profile.at(i) - prev_y_profile.at(i); + QVector<qreal> y_diff(y_pixels.count()); + qreal max_diff{0.}; + qreal min_diff{1000000.}; + size_t max_diff_idx = 0; + size_t min_diff_idx = 0; + qreal avg_diff{0.}; - if (y_diff.at(i) > max_diff) { - max_diff = y_diff.at(i); - max_diff_idx = i; - } + for (size_t i = 0; i < y_pixels.count(); ++i) { + y_diff[i] = y_pixels.at(i) - prev_y_profile.at(i); - if (y_diff.at(i) < min_diff) { - min_diff = y_diff.at(i); - min_diff_idx = i; - } + if (y_diff.at(i) > max_diff) { + max_diff = y_diff.at(i); + max_diff_idx = i; + } - avg_diff += fabs(y_diff.at(i)); - } + if (y_diff.at(i) < min_diff) { + min_diff = y_diff.at(i); + min_diff_idx = i; + } - avg_diff /= y_diff.size(); + avg_diff += fabs(y_diff.at(i)); + } - // for (size_t i = 0; i < y_profile.size(); ++i) { - // if - // } + avg_diff /= y_diff.size(); - // // const auto max_diff = std::max_element(y_diff.begin(), y_diff.end()); - // const auto avg_diff = std::accumulate(y_diff.begin(), y_diff.end(), 0.0) / y_diff.size(); - std::sort(y_diff.begin(), y_diff.end()); - const auto median_diff = y_diff.at(y_diff.size() * 3 / 4); + std::sort(y_diff.begin(), y_diff.end()); + const auto median_diff = y_diff.at(y_diff.size() * 3 / 4); - // if (qFuzzyIsNull(max_diff)) { - // qDebug() << "max_diff is null"; + pixelsGraph->setData(x_pixels, y_pixels); - // static bool done = [&]() -> bool { - // for (const auto & y : y_profile) { - // qDebug() << y; - // } - // return true; - // }(); + auto jsonLines = json["lines"].toArray(); - // manager->get(request); + QList<QLineF> lines(jsonLines.count()); - // return; - // } + // FIXME: validate + // WARNING + // AHTUNG + // DANGER + std::transform( + // std::execution::par_unseq + jsonLines.cbegin(), + jsonLines.cend(), + lines.begin(), + [](const auto &jsonLine) -> QLineF { + const auto jsonPoints = jsonLine.toArray(); + const auto jsonPoint0 = jsonPoints.at(0).toArray(); + const auto jsonPoint1 = jsonPoints.at(1).toArray(); + return {{jsonPoint0.at(0).toDouble(), + jsonPoint0.at(1).toDouble()}, + {jsonPoint1.at(0).toDouble(), + jsonPoint1.at(1).toDouble()}}; + }); - graph->setData(x_profile, y_profile); + // lines.append({{6,780},{38,787}}); - auto jsonLines = json["lines"].toArray(); + auto pixelsCustomPlot + = engine.rootObjects().first()->findChild<QmlCustomPlot *>( + "pixelsCustomPlot"); - QList<QLineF> lines(jsonLines.count()); + if (!pixelsCustomPlot) { + qWarning() << "no pixelsCustomPlot"; + } else { + // qDebug() << "set lines. count is" << lines.count(); + pixelsCustomPlot->setLines(lines); + } - // FIXME: validate - // WARNING - // AHTUNG - // DANGER - std::transform( - // std::execution::par_unseq - jsonLines.cbegin(), - jsonLines.cend(), - lines.begin(), - [](const auto &jsonLine) -> QLineF { - const auto jsonPoints = jsonLine.toArray(); - const auto jsonPoint0 = jsonPoints.at(0).toArray(); - const auto jsonPoint1 = jsonPoints.at(1).toArray(); - return {{jsonPoint0.at(0).toDouble(), jsonPoint0.at(1).toDouble()}, - {jsonPoint1.at(0).toDouble(), jsonPoint1.at(1).toDouble()}}; - }); + // 6 780 + // 38 787 - // lines.append({{6,780},{38,787}}); + engine.rootContext()->setContextProperty("max_diff", max_diff); + engine.rootContext()->setContextProperty("max_diff_idx", + int(max_diff_idx)); + engine.rootContext()->setContextProperty("min_diff", min_diff); + engine.rootContext()->setContextProperty("min_diff_idx", + int(min_diff_idx)); + engine.rootContext()->setContextProperty("avg_diff", avg_diff); + engine.rootContext()->setContextProperty("median_diff", + median_diff); + engine.rootContext()->setContextProperty("encoderPosition", + encoderPosition); + engine.rootContext()->setContextProperty("measurementCounter", + measurementCounter); + engine.rootContext()->setContextProperty("timestampUs", + timestampUs); - auto qmlCustomPlot = engine.rootObjects().first()->findChild<QmlCustomPlot *>( - "qmlCustomPlot"); + manager->get(pixelsRequest); + pixelsAutoRestartTimer.start(); - if (!qmlCustomPlot) { - qWarning() << "no qmlCustomPlot"; - } else { - // qDebug() << "set lines. count is" << lines.count(); - qmlCustomPlot->setLines(lines); - } + prev_y_profile = y_pixels; + } else if (reply->url().toString().endsWith("profile")) { + qDebug() << "got profile"; + auto json = QJsonDocument::fromJson(reply->readAll()).object(); + auto jsonProfile = json["profile"].toObject(); + auto jsonPointsMm = jsonProfile["pointsMm"].toArray(); - // 6 780 - // 38 787 + QList<qreal> x_profile(size_t(1280), 0.); + QList<qreal> y_profile(size_t(1280), 0.); - engine.rootContext()->setContextProperty("max_diff", max_diff); - engine.rootContext()->setContextProperty("max_diff_idx", int(max_diff_idx)); - engine.rootContext()->setContextProperty("min_diff", min_diff); - engine.rootContext()->setContextProperty("min_diff_idx", int(min_diff_idx)); - engine.rootContext()->setContextProperty("avg_diff", avg_diff); - engine.rootContext()->setContextProperty("median_diff", median_diff); - engine.rootContext()->setContextProperty("encoderPosition", encoderPosition); - engine.rootContext()->setContextProperty("measurementCounter", measurementCounter); - engine.rootContext()->setContextProperty("timestampUs", timestampUs); + static bool done{false}; - manager->get(request); - pixelsAutoRestartTimer.start(); + if (!done) { + qDebug() << "got profile result:"; + qDebug() << jsonProfile; + } - prev_y_profile = y_profile; + for (int i = 0; i < jsonPointsMm.count(); ++i) { + // qDebug() << jsonPointsMm.at(i); + // continue; + const auto x = jsonPointsMm.at(i).toArray()[0].toDouble(); + const auto z = jsonPointsMm.at(i).toArray()[1].toDouble(); - reply->deleteLater(); - }); + if (qFuzzyIsNull(z) || std::isnan(z)) { + x_profile[i] = 0; + y_profile[i] = 0; + continue; + } + + x_profile[i] = x; + y_profile[i] = z; + + if (!done) { + qDebug() << '\t' << x_profile[i] << y_profile[i]; + } + } + + done = true; + + profileGraph->setData(x_profile, y_profile); - manager->get(request); + // pixelsGraph->setData(x_profile, y_profile); + manager->get(profileRequest); + profileAutoRestartTimer.start(); + } else { + qDebug() << "unknown reply type"; + } + + reply->deleteLater(); + }); + + manager->get(pixelsRequest); pixelsAutoRestartTimer.start(); + manager->get(profileRequest); + profileAutoRestartTimer.start(); + // auto pixelsRequestFuture = QtConcurrent::run([&](){ // QThread::sleep(1); // // x_profile.clear(); |
