Performance improvement

This commit is contained in:
Pascal Serrarens 2024-05-10 11:17:44 +02:00
parent 2b392ef045
commit ba18f5763f
5 changed files with 46 additions and 31 deletions

View File

@ -47,7 +47,6 @@ void DifferentialDrive::SetMotorTargetSpeeds(float leftSpeed,
}; };
} }
#include <Arduino.h>
void DifferentialDrive::SetTwistSpeed(float forward, float yaw) { void DifferentialDrive::SetTwistSpeed(float forward, float yaw) {
float leftSpeed = float leftSpeed =
Float::Clamp(forward + yaw, -1, 1); // revolutions per second Float::Clamp(forward + yaw, -1, 1); // revolutions per second

View File

@ -20,7 +20,6 @@ void NetworkPerception::ProcessPacket(Roboid *roboid, unsigned char *buffer,
} }
} }
#include <Arduino.h>
void NetworkPerception::ReceiveCreateMsg(unsigned char *data, Roboid *roboid) { void NetworkPerception::ReceiveCreateMsg(unsigned char *data, Roboid *roboid) {
unsigned char networkId = data[1]; unsigned char networkId = data[1];
unsigned char objectId = data[2]; unsigned char objectId = data[2];
@ -37,6 +36,7 @@ void NetworkPerception::ReceiveCreateMsg(unsigned char *data, Roboid *roboid) {
} }
void NetworkPerception::ReceivePlane(unsigned char *data, Roboid *roboid) { void NetworkPerception::ReceivePlane(unsigned char *data, Roboid *roboid) {
unsigned char networkId = data[1];
unsigned char poseType = data[3]; unsigned char poseType = data[3];
if ((poseType & NetworkSync::Pose_Position) == 0 | if ((poseType & NetworkSync::Pose_Position) == 0 |
(poseType & NetworkSync::Pose_Orientation) == 0) (poseType & NetworkSync::Pose_Orientation) == 0)
@ -50,7 +50,7 @@ void NetworkPerception::ReceivePlane(unsigned char *data, Roboid *roboid) {
float distance = deltaPosition.magnitude(); float distance = deltaPosition.magnitude();
if (roboid->perception->IsInteresting(distance) == false) { if (roboid->perception->IsInteresting(distance) == false) {
printf(" plane not interesting\n"); // printf(" plane not interesting\n");
return; return;
} }
@ -67,9 +67,9 @@ void NetworkPerception::ReceivePlane(unsigned char *data, Roboid *roboid) {
// worldPosition.y, worldPosition.z, roboidPosition.x, // worldPosition.y, worldPosition.z, roboidPosition.x,
// roboidPosition.y, roboidPosition.z, position.distance, // roboidPosition.y, roboidPosition.z, position.distance,
// (float)position.horizontalAngle, (float)position.verticalAngle); // (float)position.horizontalAngle, (float)position.verticalAngle);
printf("Received plane %f (%f %f)\n", position.distance, // printf("Received plane %f (%f %f)\n", position.distance,
(float)position.horizontalAngle, (float)position.verticalAngle); // (float)position.horizontalAngle, (float)position.verticalAngle);
roboid->perception->AddTrackedObject(this, position, 0x80); roboid->perception->AddTrackedObject(this, position, 0x80, networkId);
} }
void NetworkPerception::ReceivePoseMsg(unsigned char *data, Roboid *roboid) { void NetworkPerception::ReceivePoseMsg(unsigned char *data, Roboid *roboid) {
@ -93,7 +93,7 @@ void NetworkPerception::ReceivePoseMsg(unsigned char *data, Roboid *roboid) {
float distance = Vector3::Distance(roboidPosition, worldPosition); float distance = Vector3::Distance(roboidPosition, worldPosition);
if (roboid->perception->IsInteresting(distance) == false) { if (roboid->perception->IsInteresting(distance) == false) {
printf(" not interesting\n"); // printf(" not interesting\n");
return; return;
} }

View File

@ -6,7 +6,7 @@
#include <math.h> #include <math.h>
#define RC_DEBUG2 // #define RC_DEBUG2
#ifdef RC_DEBUG2 #ifdef RC_DEBUG2
#include <Arduino.h> #include <Arduino.h>
#endif #endif
@ -76,27 +76,31 @@ float GetPlaneDistance(InterestingThing *plane, float horizontalAngle,
horizontalAngle); horizontalAngle);
if (abs(deltaAngle) < abs(range)) { if (abs(deltaAngle) < abs(range)) {
// distance = distance // distance = distance
printf(" plane distance = %f (%f-%f)+%f=%f", distance, // printf(" plane distance = %f (%f-%f)+%f=%f", distance,
(float)plane->position.horizontalAngle, horizontalAngle, range, // (float)plane->position.horizontalAngle, horizontalAngle, range,
deltaAngle); // deltaAngle);
} else if (deltaAngle < -range) { } else if (deltaAngle < -range) {
float angle = deltaAngle + range; float angle = deltaAngle + range;
printf(" plane distance < %f (%f-%f)+%f=%f %f", distance, // printf(" plane distance < %f (%f-%f)+%f=%f %f", distance,
(float)plane->position.horizontalAngle, horizontalAngle, range, // (float)plane->position.horizontalAngle, horizontalAngle, range,
angle, cosf(angle * Angle::Deg2Rad)); // angle, cosf(angle * Angle::Deg2Rad));
if (angle > -90) if (angle > -90)
distance = distance / cosf(angle * Angle::Deg2Rad); distance = distance / cosf(angle * Angle::Deg2Rad);
else
distance = 9999; // infinity?
} else if (deltaAngle > range) { } else if (deltaAngle > range) {
float angle = deltaAngle - range; float angle = deltaAngle - range;
printf(" plane distance > %f (%f-%f)-%f=%f %f", distance, // printf(" plane distance > %f (%f-%f)-%f=%f %f", distance,
(float)plane->position.horizontalAngle, horizontalAngle, range, // (float)plane->position.horizontalAngle, horizontalAngle, range,
angle, cosf(angle * Angle::Deg2Rad)); // angle, cosf(angle * Angle::Deg2Rad));
if (angle < 90) if (angle < 90)
distance = distance / cosf(angle * Angle::Deg2Rad); distance = distance / cosf(angle * Angle::Deg2Rad);
else
distance = 9999; // infinity?
} }
printf(" distance = %f\n", distance); // printf(" distance = %f\n", distance);
return distance; return distance;
} }
@ -186,7 +190,8 @@ bool Perception::ObjectNearby(float direction, float range) {
#include <WifiSync.h> #include <WifiSync.h>
void Perception::AddTrackedObject(Sensor *sensor, Polar position, void Perception::AddTrackedObject(Sensor *sensor, Polar position,
unsigned char thingType) { unsigned char thingType,
unsigned char networkId) {
InterestingThing *obj = new InterestingThing(sensor, position); InterestingThing *obj = new InterestingThing(sensor, position);
obj->type = thingType; obj->type = thingType;
@ -224,10 +229,11 @@ void Perception::AddTrackedObject(Sensor *sensor, Polar position,
if (availableSlotIx >= 0) { //< maxObjectCount) { if (availableSlotIx >= 0) { //< maxObjectCount) {
// a slot is available // a slot is available
this->trackedObjects[availableSlotIx] = obj; this->trackedObjects[availableSlotIx] = obj;
obj->networkId = 0x00; obj->networkId = networkId;
obj->id = lastObjectId++; // availableSlotIx + 1; obj->id = lastObjectId++; // availableSlotIx + 1;
#ifdef RC_DEBUG2 #ifdef RC_DEBUG2
printf("%d: new tracked object [/%d]\n", availableSlotIx, obj->id); printf("%d: new tracked object [%d/%d]\n", availableSlotIx, obj->networkId,
obj->id);
#endif #endif
roboid->networkSync->NewObject(obj); roboid->networkSync->NewObject(obj);
((WifiSync *)roboid->networkSync)->PublishTrackedObject(roboid, obj); ((WifiSync *)roboid->networkSync)->PublishTrackedObject(roboid, obj);
@ -237,7 +243,7 @@ void Perception::AddTrackedObject(Sensor *sensor, Polar position,
this->trackedObjects[farthestObjIx]->position.distance) { this->trackedObjects[farthestObjIx]->position.distance) {
delete this->trackedObjects[farthestObjIx]; delete this->trackedObjects[farthestObjIx];
this->trackedObjects[farthestObjIx] = obj; this->trackedObjects[farthestObjIx] = obj;
obj->networkId = 0x00; obj->networkId = networkId;
obj->id = lastObjectId++; // availableSlotIx + 1; obj->id = lastObjectId++; // availableSlotIx + 1;
#ifdef RC_DEBUG2 #ifdef RC_DEBUG2
// Serial.print((int)obj->id); // Serial.print((int)obj->id);

View File

@ -88,7 +88,8 @@ public:
/// @param position The position of the sensor in polar coordinates local to /// @param position The position of the sensor in polar coordinates local to
/// the roboid /// the roboid
void AddTrackedObject(Sensor *sensor, Polar position, void AddTrackedObject(Sensor *sensor, Polar position,
unsigned char objectType = 0x00); unsigned char objectType = 0x00,
unsigned char networkId = 0x00);
InterestingThing * InterestingThing *
AddTrackedObject(Sensor *sensor, Spherical position, AddTrackedObject(Sensor *sensor, Spherical position,
Quaternion orientation = Quaternion::identity, Quaternion orientation = Quaternion::identity,

View File

@ -20,24 +20,33 @@ InterestingThing::InterestingThing(Sensor *sensor, Spherical position,
this->updated = true; this->updated = true;
} }
#include <Arduino.h> // #include <Arduino.h>
bool InterestingThing::IsTheSameAs(InterestingThing *otherObj) { bool InterestingThing::IsTheSameAs(InterestingThing *otherObj) {
if (id != 0 && id == otherObj->id) if (id != 0 && id == otherObj->id)
return true; return true;
if (type != otherObj->type) if (type != otherObj->type)
return false; return false;
printf(" %f (%f %f) is same as %f (%f %f)\n", otherObj->position.distance,
(float)otherObj->position.horizontalAngle, float equalDistance = equalityDistance;
(float)otherObj->position.verticalAngle, position.distance, float equalAngle = equalityAngle;
(float)position.horizontalAngle, (float)position.verticalAngle); if (otherObj->type == 0x80 || otherObj->id == 0x80) {
if (fabsf(position.distance - otherObj->position.distance) > equalityDistance) equalDistance = 0.1;
equalAngle = 10;
}
// printf(" %d %f (%f %f) is same as %f (%f %f)\n", otherObj->type,
// otherObj->position.distance,
// (float)otherObj->position.horizontalAngle,
// (float)otherObj->position.verticalAngle, position.distance,
// (float)position.horizontalAngle, (float)position.verticalAngle);
if (fabsf(position.distance - otherObj->position.distance) > equalDistance)
return false; return false;
if (fabsf(position.horizontalAngle - otherObj->position.horizontalAngle) > if (fabsf(position.horizontalAngle - otherObj->position.horizontalAngle) >
equalityAngle) equalAngle)
return false; return false;
if (fabsf(position.verticalAngle - otherObj->position.verticalAngle) > if (fabsf(position.verticalAngle - otherObj->position.verticalAngle) >
equalityAngle) equalAngle)
return false; return false;
// printf(" -> yes ");
return true; return true;
} }