29 lines
624 B
C++
29 lines
624 B
C++
#include "DistanceSensor.h"
|
|
|
|
#include <math.h>
|
|
|
|
DistanceSensor::DistanceSensor() {
|
|
this->type = Thing::DistanceSensorType;
|
|
this->distance = INFINITY;
|
|
this->triggerDistance = 1.0F;
|
|
}
|
|
|
|
DistanceSensor::DistanceSensor(float triggerDistance) : DistanceSensor() {
|
|
this->triggerDistance = triggerDistance;
|
|
}
|
|
|
|
float DistanceSensor::GetDistance() {
|
|
if (distance < minRange || distance > maxRange)
|
|
return -1; // invalid distance
|
|
|
|
return distance;
|
|
};
|
|
|
|
bool DistanceSensor::ObjectNearby() {
|
|
if (distance < minRange || distance > maxRange)
|
|
return false;
|
|
|
|
bool isOn = distance <= triggerDistance;
|
|
return isOn;
|
|
}
|