Handle echo pulse edges in correct order.
only emit a new distance value if echo pulse edges trigger in the right order. Fixes #1
This commit is contained in:
parent
94611f2381
commit
4811ab8239
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
build*/
|
build*/
|
||||||
*.user
|
*.user
|
||||||
|
.*.swp
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include <wiringPi.h>
|
#include <wiringPi.h>
|
||||||
|
|
||||||
volatile unsigned int timestampHigh = 0;
|
volatile unsigned int timestampHigh = 0;
|
||||||
volatile unsigned int timestampLow = 0;
|
volatile unsigned int pulseLength = 0;
|
||||||
|
|
||||||
DistanceThread::DistanceThread()
|
DistanceThread::DistanceThread()
|
||||||
{
|
{
|
||||||
@ -19,9 +19,10 @@ void DistanceThread::run()
|
|||||||
{
|
{
|
||||||
timestampHigh = micros();
|
timestampHigh = micros();
|
||||||
}
|
}
|
||||||
else
|
else if (timestampHigh != 0)
|
||||||
{
|
{
|
||||||
timestampLow = micros();
|
pulseLength = micros() - timestampHigh;
|
||||||
|
timestampHigh = 0;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -32,10 +33,9 @@ void DistanceThread::run()
|
|||||||
digitalWrite(m_triggerPin, LOW);
|
digitalWrite(m_triggerPin, LOW);
|
||||||
delayMicroseconds(40);
|
delayMicroseconds(40);
|
||||||
|
|
||||||
unsigned int delayUS = timestampLow - timestampHigh;
|
if (pulseLength < 25e3)
|
||||||
if (delayUS < 25e3)
|
|
||||||
{
|
{
|
||||||
emit distanceUpdated(delayUS / 0.58);
|
emit distanceUpdated(pulseLength / 0.58);
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(100);
|
delay(100);
|
||||||
|
Reference in New Issue
Block a user