Object perception with distance sensors
This commit is contained in:
parent
e51f5a38e0
commit
4f7e911be1
@ -1,16 +1,12 @@
|
|||||||
#include "DistanceSensor.h"
|
#include "DistanceSensor.h"
|
||||||
|
|
||||||
DistanceSensor::DistanceSensor() {
|
DistanceSensor::DistanceSensor() { this->type = Thing::DistanceSensorType; }
|
||||||
this->type = Thing::DistanceSensorType;
|
|
||||||
}
|
|
||||||
|
|
||||||
DistanceSensor::DistanceSensor(float triggerDistance) {
|
DistanceSensor::DistanceSensor(float triggerDistance) {
|
||||||
this->triggerDistance = triggerDistance;
|
this->triggerDistance = triggerDistance;
|
||||||
}
|
}
|
||||||
|
|
||||||
float DistanceSensor::GetDistance() {
|
float DistanceSensor::GetDistance() { return distance; };
|
||||||
return distance;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool DistanceSensor::ObjectNearby() {
|
bool DistanceSensor::ObjectNearby() {
|
||||||
bool isOn = GetDistance() <= triggerDistance;
|
bool isOn = GetDistance() <= triggerDistance;
|
||||||
|
@ -84,25 +84,25 @@ bool Perception::ObjectNearby(float direction, float range) {
|
|||||||
if (range < 0)
|
if (range < 0)
|
||||||
range = -range;
|
range = -range;
|
||||||
|
|
||||||
for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) {
|
// for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) {
|
||||||
Placement placement = sensorPlacements[sensorIx];
|
// Placement placement = sensorPlacements[sensorIx];
|
||||||
if (placement.horizontalDirection > direction - range &&
|
// if (placement.horizontalDirection > direction - range &&
|
||||||
placement.horizontalDirection < direction + range) {
|
// placement.horizontalDirection < direction + range) {
|
||||||
Thing *thing = placement.thing;
|
// Thing *thing = placement.thing;
|
||||||
if (thing == nullptr)
|
// if (thing == nullptr)
|
||||||
continue;
|
// continue;
|
||||||
|
|
||||||
if (thing->type == Thing::DistanceSensorType) {
|
// if (thing->type == Thing::DistanceSensorType) {
|
||||||
DistanceSensor *distanceSensor = (DistanceSensor *)thing;
|
// DistanceSensor *distanceSensor = (DistanceSensor *)thing;
|
||||||
if (distanceSensor != nullptr && distanceSensor->ObjectNearby())
|
// if (distanceSensor != nullptr && distanceSensor->ObjectNearby())
|
||||||
return true;
|
// return true;
|
||||||
} else if (thing->type == Thing::SwitchType) {
|
// } else if (thing->type == Thing::SwitchType) {
|
||||||
Switch *switchSensor = (Switch *)thing;
|
// Switch *switchSensor = (Switch *)thing;
|
||||||
if (switchSensor != nullptr && switchSensor->IsOn())
|
// if (switchSensor != nullptr && switchSensor->IsOn())
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) {
|
for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) {
|
||||||
PerceivedObject *obj = perceivedObjects[objIx];
|
PerceivedObject *obj = perceivedObjects[objIx];
|
||||||
@ -243,6 +243,32 @@ void Perception::Update(float currentTimeMs) {
|
|||||||
|
|
||||||
lastUpdateTimeMs = currentTimeMs;
|
lastUpdateTimeMs = currentTimeMs;
|
||||||
|
|
||||||
|
// Update sensing
|
||||||
|
for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) {
|
||||||
|
Placement thingPlacement = sensorPlacements[sensorIx];
|
||||||
|
Thing *thing = thingPlacement.thing;
|
||||||
|
if (thing == nullptr)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (thing->type == Thing::DistanceSensorType) {
|
||||||
|
DistanceSensor *distanceSensor = (DistanceSensor *)thing;
|
||||||
|
printf("S%d: %d %f\n", sensorIx, (int)distanceSensor,
|
||||||
|
distanceSensor->GetDistance());
|
||||||
|
if (distanceSensor != nullptr && distanceSensor->ObjectNearby()) {
|
||||||
|
Polar position = Polar(thingPlacement.horizontalDirection,
|
||||||
|
distanceSensor->GetDistance());
|
||||||
|
AddPerceivedObject(position);
|
||||||
|
}
|
||||||
|
} else if (thing->type == Thing::SwitchType) {
|
||||||
|
Switch *switchSensor = (Switch *)thing;
|
||||||
|
if (switchSensor != nullptr && switchSensor->IsOn()) {
|
||||||
|
Polar position =
|
||||||
|
Polar(thingPlacement.horizontalDirection, nearbyDistance);
|
||||||
|
AddPerceivedObject(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) {
|
for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) {
|
||||||
PerceivedObject *obj = perceivedObjects[objIx];
|
PerceivedObject *obj = perceivedObjects[objIx];
|
||||||
if (obj == nullptr)
|
if (obj == nullptr)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user