Added perception

This commit is contained in:
Pascal Serrarens 2023-11-21 17:38:48 +01:00
parent bcbec5da32
commit 66c60b66ed
4 changed files with 55 additions and 9 deletions

3
Perception.h Normal file
View File

@ -0,0 +1,3 @@
#include "Sensing.h"
using Perception = Sensing;

View File

@ -1,9 +1,9 @@
#pragma once
#include "Activation.h"
#include "Perception.h"
#include "Placement.h"
#include "Propulsion.h"
#include "Sensing.h"
class Roboid {
public:

View File

@ -166,14 +166,48 @@ bool Sensing::SwitchOn(float fromAngle, float toAngle) {
return false;
}
unsigned int Sensing::ToDepthMapIndex(float angle) {
unsigned int depthMapIx = (unsigned int)(((angle - rangeMinimum) / (rangeMaximum - rangeMinimum)) * (float)resolution);
return depthMapIx;
}
float Sensing::GetDistance(float angle) {
for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) {
Placement placement = sensorPlacements[sensorIx];
float placementAngle = placement.direction.x;
if (placementAngle == angle) {
DistanceSensor* distanceSensor = (DistanceSensor*)placement.thing;
return distanceSensor->GetDistance();
if (depthMap != nullptr) {
if (angle < rangeMinimum || angle > rangeMaximum)
return INFINITY;
unsigned int depthMapIx = ToDepthMapIndex(angle);
return depthMap[depthMapIx];
} else {
for (unsigned int sensorIx = 0; sensorIx < this->sensorCount; sensorIx++) {
Placement placement = sensorPlacements[sensorIx];
float placementAngle = placement.direction.x;
if (placementAngle == angle) {
DistanceSensor* distanceSensor = (DistanceSensor*)placement.thing;
return distanceSensor->GetDistance();
}
}
}
return INFINITY;
}
void Sensing::SetResolution(unsigned int resolution) {
this->resolution = resolution;
this->depthMap = new float[this->resolution];
}
void Sensing::SetRange(float min, float max) {
this->rangeMinimum = min;
this->rangeMaximum = max;
}
float* Sensing::GetDepthMap() {
return this->depthMap;
}
void Sensing::SetDepthMap(float angle, float distance) {
if (angle < rangeMinimum || angle > rangeMaximum)
return;
unsigned int depthMapIx = ToDepthMapIndex(angle);
depthMap[depthMapIx] = distance;
}

View File

@ -72,10 +72,19 @@ class Sensing {
bool SwitchOn(float fromAngle, float toAngle);
void SetResolution(unsigned int resolution);
void SetRange(float min, float max);
float* GetDepthMap();
unsigned int ToDepthMapIndex(float angle);
void SetDepthMap(float angle, float distance);
protected:
// SensorPlacement* sensors = nullptr;
Placement* sensorPlacements = nullptr;
unsigned int sensorCount = 0;
};
using Perception = Sensing;
unsigned int resolution;
float rangeMinimum;
float rangeMaximum;
float* depthMap = nullptr;
};