Separated Qt and WiringPi code in distance thread
This commit is contained in:
parent
388bc43b8b
commit
886451cc92
4 changed files with 13 additions and 74 deletions
|
@ -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
|
||||
)
|
||||
|
||||
|
|
|
@ -1,76 +1,16 @@
|
|||
#include "distancethread.h"
|
||||
#include <wiringPi.h>
|
||||
|
||||
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<double> 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<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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "hcsr04.h"
|
||||
#include <QThread>
|
||||
|
||||
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<typename VALUE_TYPE>
|
||||
double median(std::vector<VALUE_TYPE> values) const;
|
||||
|
||||
signals:
|
||||
void distanceUpdated(double distMeters);
|
||||
};
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue