From 886451cc929e7ca8263b82047c1af89dd5750149 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Mon, 16 Oct 2017 16:51:08 +0200 Subject: [PATCH] Separated Qt and WiringPi code in distance thread --- source/CMakeLists.txt | 5 ++- source/distancethread.cpp | 68 +++------------------------------------ source/distancethread.h | 12 +++---- source/mainwindow.cpp | 2 +- 4 files changed, 13 insertions(+), 74 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 83bbd0f..dc0efd5 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -19,7 +19,10 @@ add_executable(${PROJECT_NAME} led.h blinkthread.cpp blinkthread.h - distancethread.cpp + median.h + hcsr04.cpp + hcsr04.h + distancethread.cpp distancethread.h ) diff --git a/source/distancethread.cpp b/source/distancethread.cpp index 0df5e3d..37867b4 100644 --- a/source/distancethread.cpp +++ b/source/distancethread.cpp @@ -1,76 +1,16 @@ #include "distancethread.h" -#include -volatile unsigned int timestampHigh = 0; -volatile unsigned int pulseLength = 0; - -DistanceThread::DistanceThread() +DistanceThread::DistanceThread(unsigned int triggerPin, unsigned int echoPin) + : m_hcsr04(triggerPin, echoPin) { - wiringPiSetup(); - pinMode(m_triggerPin, OUTPUT); - pinMode(m_echoPin, INPUT); } void DistanceThread::run() { - wiringPiISR(m_echoPin, INT_EDGE_BOTH, [] - { - if (digitalRead(29) == HIGH) - { - timestampHigh = micros(); - } - else if (timestampHigh != 0) - { - pulseLength = micros() - timestampHigh; - timestampHigh = 0; - } - }); - while (true) { - std::vector values; - for (int i = 0; i < 15; ++i) - { - digitalWrite(m_triggerPin, HIGH); - delayMicroseconds(10); - digitalWrite(m_triggerPin, LOW); - delayMicroseconds(40); - - if (pulseLength < 25e3) - { - values.push_back(pulseLength); - } - - delay(10); - } - - if (!values.empty()) - { - emit distanceUpdated(median(values) / 0.58); - } - - delay(100); + emit distanceUpdated(m_hcsr04.getDistance()); + msleep(100); } } -template -double DistanceThread::median(std::vector values) const -{ - if (values.size() == 0) - { - return 0.0; - } - - size_t centerIndex = values.size() / 2; - std::nth_element(values.begin(), values.begin() + centerIndex, values.end()); - - if (values.size() % 2 == 1) - { - return values[centerIndex]; - } - else - { - std::nth_element(values.begin(), values.begin() + centerIndex + 1, values.end()); - return (values[centerIndex] + values[centerIndex + 1]) / 2.0; - } -} diff --git a/source/distancethread.h b/source/distancethread.h index f70bf0b..c73b55d 100644 --- a/source/distancethread.h +++ b/source/distancethread.h @@ -1,6 +1,6 @@ #pragma once -#include +#include "hcsr04.h" #include class DistanceThread : public QThread @@ -8,19 +8,15 @@ class DistanceThread : public QThread Q_OBJECT private: - const int m_triggerPin = 28; - const int m_echoPin = 29; + HCSR04 m_hcsr04; public: - DistanceThread(); + DistanceThread() = delete; + DistanceThread(unsigned int triggerPin, unsigned int echoPin); protected: void run() override; -private: - template - double median(std::vector values) const; - signals: void distanceUpdated(double distMeters); }; diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 5584ab8..31aa075 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -19,7 +19,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(blinkThread, &BlinkThread::ledOff, this, &MainWindow::ledOff); blinkThread->start(); - DistanceThread *distanceThread = new DistanceThread(); + DistanceThread *distanceThread = new DistanceThread(28, 29); connect(distanceThread, &DistanceThread::distanceUpdated, this, &MainWindow::distanceUpdated); distanceThread->start(); }