#include "Perception.h" #include "Angle.h" #include "DistanceSensor.h" #include "NetworkSync.h" #include "Switch.h" #include Perception::Perception() { for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) this->trackedObjects[objIx] = nullptr; } Perception::Perception(Placement *sensors, unsigned int sensorCount) : Perception() { this->sensorCount = sensorCount; this->sensorPlacements = (Placement *)sensors; } unsigned int Perception::GetSensorCount() { return this->sensorCount; } Sensor *Perception::GetSensor(unsigned int sensorId) { if (sensorId >= this->sensorCount) return nullptr; Thing *thing = this->sensorPlacements[sensorId].thing; if (thing->IsSensor()) return (Sensor *)thing; return nullptr; } Sensor *Perception::FindSensorOfType(unsigned int sensorType) { for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) { Sensor *sensor = (Sensor *)this->sensorPlacements[sensorIx].thing; if (sensor->type == sensorType) return sensor; } return nullptr; } float Perception::GetDistance(float direction, float range) { float minDistance = INFINITY; if (range < 0) range = -range; for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) { TrackedObject *obj = trackedObjects[objIx]; if (obj == nullptr) continue; if (obj->position.angle > direction - range && obj->position.angle < direction + range) { minDistance = fminf(minDistance, obj->position.distance); } } return minDistance; } float Perception::GetDistance(float horizontalDirection, float verticalDirection, float range) { float minDistance = INFINITY; if (range < 0) range = -range; for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) { TrackedObject *obj = trackedObjects[objIx]; if (obj == nullptr) continue; if (obj->position.angle > horizontalDirection - range && obj->position.angle < horizontalDirection + range) { minDistance = fminf(minDistance, obj->position.distance); } } return minDistance; } bool Perception::ObjectNearby(float direction, float range) { if (range < 0) range = -range; for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) { TrackedObject *obj = trackedObjects[objIx]; if (obj == nullptr) continue; if (obj->position.angle > direction - range && obj->position.angle < direction + range) { if (obj->position.distance <= nearbyDistance) return true; } } return false; } void Perception::AddTrackedObject(Sensor *sensor, Polar position) { TrackedObject *obj = new TrackedObject(sensor, position); unsigned char farthestObjIx = 0; unsigned char availableSlotIx = 0; for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) { if (this->trackedObjects[objIx] == nullptr) { availableSlotIx = objIx; } // Do we see the same object? else { if (obj->IsTheSameAs(this->trackedObjects[objIx])) { this->trackedObjects[objIx]->Refresh(obj->position); return; } // Is this the fartest object we see? else if (this->trackedObjects[farthestObjIx] == nullptr || (this->trackedObjects[objIx]->position.distance > this->trackedObjects[farthestObjIx]->position.distance)) { farthestObjIx = objIx; } } } // Check if an perception slot is available (we currently see less than the // max number of objects) if (availableSlotIx < maxObjectCount) { // a slot is available this->trackedObjects[availableSlotIx] = obj; } // If this object is closer than the farthest object, then replace it else if (obj->position.distance < this->trackedObjects[farthestObjIx]->position.distance) { this->trackedObjects[farthestObjIx] = obj; // we may want to destroy the fartest object, but if it is created // externally, other links may still exist... } } unsigned char Perception::TrackedObjectCount() { unsigned char objectCount = 0; for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) { if (this->trackedObjects[objIx] != nullptr) objectCount++; } return objectCount; } TrackedObject **Perception::GetTrackedObjects() { return this->trackedObjects; } void Perception::Update(float currentTimeMs) { float deltaTime = currentTimeMs - lastUpdateTimeMs; if (deltaTime <= 0) return; 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; float distance = distanceSensor->GetDistance(); float angle = thingPlacement.horizontalDirection; Polar position = Polar(angle, distance); AddTrackedObject(distanceSensor, position); } else if (thing->type == Thing::SwitchType) { Switch *switchSensor = (Switch *)thing; if (switchSensor != nullptr && switchSensor->IsOn()) { Polar position = Polar(thingPlacement.horizontalDirection, nearbyDistance); AddTrackedObject(switchSensor, position); } } } for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) { TrackedObject *obj = trackedObjects[objIx]; if (obj == nullptr) continue; if (obj->DegradeConfidence(deltaTime) == false) { // delete obj if (roboid != nullptr && roboid->networkSync != nullptr) roboid->networkSync->DestroyObject(obj); this->trackedObjects[objIx] = nullptr; } } if (this->trackedObjects[0] != nullptr) { } } void Perception::UpdatePose(Polar translation) { for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) { TrackedObject *obj = trackedObjects[objIx]; if (obj == nullptr) continue; Polar newPosition = obj->position - translation; obj->position = newPosition; } } void Perception::UpdatePose(Quaternion rotation) { // only rotation around vertical axis is supported for now float rotationAngle; Vector3 rotationAxis; rotation.ToAngleAxis(&rotationAngle, &rotationAxis); // Make sure rotation axis is positive if (rotationAxis.y < 0) rotationAngle = -rotationAngle; for (unsigned char objIx = 0; objIx < maxObjectCount; objIx++) { TrackedObject *obj = trackedObjects[objIx]; if (obj == nullptr) continue; float updatedAngle = Angle::Normalize(obj->position.angle - rotationAngle); obj->position.angle = updatedAngle; } }