Separated Qt and WiringPi code in distance thread

This commit is contained in:
mandlm 2017-10-16 16:51:08 +02:00 committed by mandlm
parent 388bc43b8b
commit 886451cc92
4 changed files with 13 additions and 74 deletions

View File

@ -19,7 +19,10 @@ add_executable(${PROJECT_NAME}
led.h led.h
blinkthread.cpp blinkthread.cpp
blinkthread.h blinkthread.h
distancethread.cpp median.h
hcsr04.cpp
hcsr04.h
distancethread.cpp
distancethread.h distancethread.h
) )

View File

@ -1,76 +1,16 @@
#include "distancethread.h" #include "distancethread.h"
#include <wiringPi.h>
volatile unsigned int timestampHigh = 0; DistanceThread::DistanceThread(unsigned int triggerPin, unsigned int echoPin)
volatile unsigned int pulseLength = 0; : m_hcsr04(triggerPin, echoPin)
DistanceThread::DistanceThread()
{ {
wiringPiSetup();
pinMode(m_triggerPin, OUTPUT);
pinMode(m_echoPin, INPUT);
} }
void DistanceThread::run() 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) while (true)
{ {
std::vector<double> values; emit distanceUpdated(m_hcsr04.getDistance());
for (int i = 0; i < 15; ++i) msleep(100);
{
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);
} }
} }
template<typename VALUE_TYPE>
double DistanceThread::median(std::vector<VALUE_TYPE> 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;
}
}

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <vector> #include "hcsr04.h"
#include <QThread> #include <QThread>
class DistanceThread : public QThread class DistanceThread : public QThread
@ -8,19 +8,15 @@ class DistanceThread : public QThread
Q_OBJECT Q_OBJECT
private: private:
const int m_triggerPin = 28; HCSR04 m_hcsr04;
const int m_echoPin = 29;
public: public:
DistanceThread(); DistanceThread() = delete;
DistanceThread(unsigned int triggerPin, unsigned int echoPin);
protected: protected:
void run() override; void run() override;
private:
template<typename VALUE_TYPE>
double median(std::vector<VALUE_TYPE> values) const;
signals: signals:
void distanceUpdated(double distMeters); void distanceUpdated(double distMeters);
}; };

View File

@ -19,7 +19,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(blinkThread, &BlinkThread::ledOff, this, &MainWindow::ledOff); connect(blinkThread, &BlinkThread::ledOff, this, &MainWindow::ledOff);
blinkThread->start(); blinkThread->start();
DistanceThread *distanceThread = new DistanceThread(); DistanceThread *distanceThread = new DistanceThread(28, 29);
connect(distanceThread, &DistanceThread::distanceUpdated, this, &MainWindow::distanceUpdated); connect(distanceThread, &DistanceThread::distanceUpdated, this, &MainWindow::distanceUpdated);
distanceThread->start(); distanceThread->start();
} }