Separated out perceived object

This commit is contained in:
Pascal Serrarens 2023-12-31 17:07:09 +01:00
parent 5e09a4d454
commit 63726b0663
6 changed files with 84 additions and 74 deletions

View File

@ -43,6 +43,7 @@ include_directories(
add_library(RoboidControl STATIC add_library(RoboidControl STATIC
"Roboid.cpp" "Roboid.cpp"
"Perception.cpp" "Perception.cpp"
"PerceivedObject.cpp"
"Propulsion.cpp" "Propulsion.cpp"
"Motor.cpp" "Motor.cpp"
"DifferentialDrive.cpp" "DifferentialDrive.cpp"

View File

@ -55,8 +55,8 @@ Polar DifferentialDrive::GetVelocity() {
float leftSpeed = leftMotor->GetSpeed(); float leftSpeed = leftMotor->GetSpeed();
float rightSpeed = rightMotor->GetSpeed(); float rightSpeed = rightMotor->GetSpeed();
float speed = (leftSpeed + rightSpeed) / 2; float speed = (leftSpeed + rightSpeed) / 2;
float direction = speed >= 0 ? 0 : 180; float direction = speed >= 0 ? 0.0F : 180.0F;
float distance = abs(speed); float distance = fabsf(speed);
Polar velocity = Polar(direction, distance); Polar velocity = Polar(direction, distance);
return velocity; return velocity;
} }

54
PerceivedObject.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "PerceivedObject.h"
#include <math.h>
/***
* Oject perception
***/
PerceivedObject::PerceivedObject() {
this->id = 0;
this->confidence = maxConfidence;
this->position = Polar(0, INFINITY);
this->radius = INFINITY;
}
PerceivedObject::PerceivedObject(Sensor *sensor, Polar position, float radius)
: PerceivedObject() {
this->sensor = sensor;
this->position = position;
this->radius = radius;
}
bool PerceivedObject::IsTheSameAs(PerceivedObject *otherObj) {
if (id != 0 && id == otherObj->id)
return true;
if (fabsf(position.distance - otherObj->position.distance) > equalityDistance)
return false;
if (fabsf(position.angle - otherObj->position.angle) > equalityAngle)
return false;
return true;
}
bool PerceivedObject::DegradeConfidence(float deltaTime) {
unsigned char confidenceDrop =
(unsigned char)((float)confidenceDropSpeed * deltaTime);
// Make sure the confidence always drops
if (confidenceDrop == 0)
confidenceDrop = 1;
if (confidence <= confidenceDrop) {
// object is dead
confidence = 0;
return false;
} else {
confidence -= confidenceDrop;
return true;
}
}
void PerceivedObject::Refresh(Polar position, float radius) {
this->position = position;
this->radius = radius;
this->confidence = maxConfidence;
}

26
PerceivedObject.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "Polar.h"
#include "Sensor.h"
class PerceivedObject {
public:
PerceivedObject();
PerceivedObject(Sensor *sensor, Polar position, float radius = 0.1F);
static constexpr float equalityDistance = 0.3F;
static constexpr float equalityAngle = 5.0F;
bool IsTheSameAs(PerceivedObject *otherObj);
char id;
Polar position = Polar::zero;
float radius;
Sensor *sensor = nullptr;
static constexpr char maxConfidence = 255;
static constexpr char confidenceDropSpeed = 2;
char confidence;
bool DegradeConfidence(float deltaTime);
void Refresh(Polar position, float radius);
};

View File

@ -95,56 +95,6 @@ bool Perception::ObjectNearby(float direction, float range) {
return false; return false;
} }
/***
* Oject perception
***/
PerceivedObject::PerceivedObject() {
this->id = 0;
this->confidence = maxConfidence;
this->position = Polar(0, INFINITY);
this->radius = INFINITY;
}
PerceivedObject::PerceivedObject(Sensor *sensor, Polar position, float radius)
: PerceivedObject() {
this->sensor = sensor;
this->position = position;
this->radius = radius;
}
bool PerceivedObject::IsTheSameAs(PerceivedObject *otherObj) {
if (id != 0 && id == otherObj->id)
return true;
if (abs(position.distance - otherObj->position.distance) > equalityDistance)
return false;
if (abs(position.angle - otherObj->position.angle) > equalityAngle)
return false;
return true;
}
bool PerceivedObject::DegradeConfidence(float deltaTime) {
unsigned char confidenceDrop =
(unsigned char)((float)confidenceDropSpeed * deltaTime);
// Make sure the confidence always drops
if (confidenceDrop == 0)
confidenceDrop = 1;
if (confidence <= confidenceDrop) {
// object is dead
confidence = 0;
return false;
} else {
confidence -= confidenceDrop;
return true;
}
}
void PerceivedObject::Refresh(Polar position, float radius) {
this->position = position;
this->radius = radius;
this->confidence = maxConfidence;
}
void Perception::AddPerceivedObject(Sensor *sensor, Polar position) { void Perception::AddPerceivedObject(Sensor *sensor, Polar position) {
// int objCount = PerceivedObjectCount(); // int objCount = PerceivedObjectCount();

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "PerceivedObject.h"
#include "Placement.h" #include "Placement.h"
#include "Polar.h" #include "Polar.h"
#include "Quaternion.h" #include "Quaternion.h"
@ -10,28 +11,6 @@ namespace RoboidControl {
class Roboid; class Roboid;
class PerceivedObject {
public:
PerceivedObject();
PerceivedObject(Sensor *sensor, Polar position, float radius = 0.1F);
static constexpr float equalityDistance = 0.3F;
static constexpr float equalityAngle = 5.0F;
bool IsTheSameAs(PerceivedObject *otherObj);
char id;
Polar position = Polar::zero;
float radius;
Sensor *sensor = nullptr;
static constexpr char maxConfidence = 255;
static constexpr char confidenceDropSpeed = 2;
char confidence;
bool DegradeConfidence(float deltaTime);
void Refresh(Polar position, float radius);
};
/// @brief Module to which keeps track of objects around the roboid /// @brief Module to which keeps track of objects around the roboid
class Perception { class Perception {
public: public: