Extended the ControlCore

This commit is contained in:
Pascal Serrarens 2024-12-11 18:01:06 +01:00
parent 14e3499742
commit 7e5876e2f8
9 changed files with 371 additions and 160 deletions

6
ControlCore/Client.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "Client.h"
bool Passer::Control::ControlClient::SendMsg(unsigned char *buffer,
unsigned char bufferSize) {
return false;
}

11
ControlCore/Client.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
namespace Passer::Control {
class ControlClient {
public:
unsigned char *buffer;
bool SendMsg(unsigned char *buffer, unsigned char bufferSize);
};
} // namespace Passer::Control

View File

@ -0,0 +1,12 @@
#include "LowLevelMessages.h"
#include "../float16/float16.h"
void LowLevelMessages::SendFloat16(unsigned char *buffer, unsigned char *ix,
float value) {
float16 value16 = float16(value);
short binary = value16.getBinary();
buffer[(*ix)++] = (binary >> 8) & 0xFF;
buffer[(*ix)++] = binary & 0xFF;
}

View File

@ -0,0 +1,12 @@
#include "../LinearAlgebra/Spherical.h"
namespace Passer::Control {
class LowLevelMessages {
public:
static void SendFloat16(unsigned char *buffer, unsigned char *ix,
float value);
};
} // namespace Passer::Control
using namespace Passer::Control;

View File

@ -1,39 +1,128 @@
#include "Messages.h" #include "Messages.h"
#include "LowLevelMessages.h"
#pragma region IMessage #pragma region IMessage
unsigned char *IMessage::Serialize() { return nullptr; } unsigned char IMessage::Serialize(unsigned char *buffer) { return 0; }
bool IMessage::SendMsg(IMessage msg, unsigned char bufferSize) { bool IMessage::SendMsg(ControlClient client, IMessage msg,
return SendMsg(msg.Serialize(), bufferSize); unsigned char bufferSize) {
return SendMsg(client, client.buffer, msg.Serialize(client.buffer));
} }
bool IMessage::SendMsg(unsigned char *buffer, unsigned char bufferSize) { bool IMessage::SendMsg(ControlClient client, unsigned char *buffer,
// SendBuffer(buffer, bufferSize); unsigned char bufferSize) {
return false; return false; // client.SendMsg(buffer, bufferSize);
} }
#pragma endregion #pragma endregion
#pragma region Investigate #pragma region Investigate
InvestigateMsg::InvestigateMsg(unsigned char thingId) { InvestigateMsg::InvestigateMsg(unsigned char networkId, unsigned char thingId) {
this->networkId = 0; this->networkId = networkId;
this->thingId = thingId; this->thingId = thingId;
} }
unsigned char *InvestigateMsg::Serialize() { unsigned char InvestigateMsg::Serialize(unsigned char *buffer) {
unsigned char *buffer = IMessage::buffer; unsigned char ix = 0;
buffer[0] = InvestigateMsg::id; buffer[ix++] = this->id;
buffer[1] = this->networkId; buffer[ix++] = this->networkId;
buffer[2] = this->thingId; buffer[ix++] = this->thingId;
return buffer; return ix;
} }
bool InvestigateMsg::Send(unsigned char thingId) { bool InvestigateMsg::Send(ControlClient client, unsigned char networkId,
InvestigateMsg msg = InvestigateMsg(thingId); unsigned char thingId) {
return SendMsg(msg, InvestigateMsg::length); InvestigateMsg msg = InvestigateMsg(networkId, thingId);
return SendMsg(client, msg, InvestigateMsg::length);
} }
// Investiaget // Investigate
#pragma endregion
#pragma region Thing
Passer::Control::ThingMsg::ThingMsg(unsigned char networkId,
unsigned char thingId,
unsigned char thingType,
unsigned char parentId) {
this->networkId = networkId;
this->thingId = thingId;
this->thingType = thingType;
this->parentId = parentId;
}
unsigned char ThingMsg::Serialize(unsigned char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
buffer[ix++] = this->thingType;
buffer[ix++] = this->parentId;
return ix;
}
bool Passer::Control::ThingMsg::Send(ControlClient client,
unsigned char networkId,
unsigned char thingId,
unsigned char thingType,
unsigned char parentId) {
ThingMsg msg = ThingMsg(networkId, thingId, thingType, parentId);
return SendMsg(client, msg, ThingMsg::length);
}
// Thing
#pragma endregion
#pragma region Name
NameMsg::NameMsg(unsigned char networkId, unsigned char thingId,
unsigned char nameLength, const char *name) {
this->networkId = networkId;
this->thingId = thingId;
this->nameLength = nameLength;
this->name = name;
}
unsigned char Passer::Control::NameMsg::Serialize(unsigned char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
buffer[ix++] = this->nameLength;
for (int nameIx = 0; nameIx < this->nameLength; nameIx++)
buffer[ix++] = name[nameIx];
return ix;
}
// Name
#pragma endregion
#pragma region ModelUrl
ModelUrlMsg::ModelUrlMsg(unsigned char networkId, unsigned char thingId,
unsigned char urlLength, const char *url,
float scale) {
this->networkId = networkId;
this->thingId = thingId;
this->urlLength = urlLength;
this->url = url;
this->scale = scale;
}
unsigned char ModelUrlMsg::Serialize(unsigned char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
LowLevelMessages::SendFloat16(buffer, &ix, this->scale);
buffer[ix++] = this->urlLength;
for (int urlIx = 0; urlIx < this->urlLength; urlIx++)
buffer[ix++] = url[urlIx];
return ix;
}
// Model Url
#pragma endregion #pragma endregion

View File

@ -1,13 +1,19 @@
#pragma once
#include "../float16/float16.h"
#include "Client.h"
namespace Passer::Control { namespace Passer::Control {
class IMessage { class IMessage {
public: public:
static unsigned char buffer[256]; // static unsigned char buffer[256];
virtual unsigned char *Serialize(); virtual unsigned char Serialize(unsigned char *buffer);
static bool SendMsg(IMessage msg, unsigned char bufferSize); static bool SendMsg(ControlClient client, IMessage msg,
static bool SendMsg(unsigned char *buffer, unsigned char bufferSize); unsigned char bufferSize);
static bool SendMsg(ControlClient client, unsigned char *buffer,
unsigned char bufferSize);
}; };
class InvestigateMsg : public IMessage { class InvestigateMsg : public IMessage {
@ -17,11 +23,61 @@ public:
unsigned char networkId; unsigned char networkId;
unsigned char thingId; unsigned char thingId;
InvestigateMsg(unsigned char thingId); InvestigateMsg(unsigned char networkId, unsigned char thingId);
virtual unsigned char *Serialize() override; virtual unsigned char Serialize(unsigned char *buffer) override;
static bool Send(unsigned char thingId); static bool Send(ControlClient client, unsigned char networkId,
unsigned char thingId);
};
class ThingMsg : public IMessage {
public:
static const unsigned char id = 0x80;
static const unsigned char length = 5;
unsigned char networkId;
unsigned char thingId;
unsigned char thingType;
unsigned char parentId;
ThingMsg(unsigned char networkId, unsigned char thingId,
unsigned char thingType, unsigned char parentId);
virtual unsigned char Serialize(unsigned char *buffer) override;
static bool Send(ControlClient client, unsigned char networkId,
unsigned char thingId, unsigned char thingType,
unsigned char parentId);
};
class NameMsg : public IMessage {
public:
static const unsigned char id = 0x91;
static const unsigned char length = 4;
unsigned char networkId;
unsigned char thingId;
unsigned char nameLength;
const char *name;
NameMsg(unsigned char networkId, unsigned char thingId,
unsigned char nameLength, const char *name);
virtual unsigned char Serialize(unsigned char *buffer) override;
};
class ModelUrlMsg : public IMessage {
public:
static const unsigned char id = 0x90;
unsigned char networkId;
unsigned char thingId;
float scale;
unsigned char urlLength;
const char *url;
ModelUrlMsg(unsigned char networkId, unsigned char thingId,
unsigned char urlLegth, const char *url, float scale = 1);
virtual unsigned char Serialize(unsigned char *buffer) override;
}; };
} // namespace Passer::Control } // namespace Passer::Control

View File

@ -1,5 +1,6 @@
#include "NetworkPerception.h" #include "NetworkPerception.h"
#include "ControlCore/Messages.h"
#include "NetworkSync.h" #include "NetworkSync.h"
#include "float16/float16.h" #include "float16/float16.h"
@ -174,118 +175,120 @@ void NetworkPerception::ReceivePoseMsg(unsigned char *data, Roboid *roboid) {
if (networkId == roboid->networkSync->networkId) if (networkId == roboid->networkSync->networkId)
networkId = 0x00; networkId = 0x00;
if (objectId == 0x80) // if (objectId == 0x80)
return ReceivePlane(data, roboid); // return ReceivePlane(data, roboid);
else if (objectId == 0x81) // else if (objectId == 0x81)
return ReceiveSphere(data, roboid); // return ReceiveSphere(data, roboid);
#if RC_DEBUG #if RC_DEBUG
// printf("Received PoseMsg [%d/%d]\n", networkId, objectId); // printf("Received PoseMsg [%d/%d]\n", networkId, objectId);
#endif #endif
InterestingThing *thing = // InterestingThing *thing =
roboid->perception->FindTrackedObject(networkId, objectId); // roboid->perception->FindTrackedObject(networkId, objectId);
if (thing == nullptr) { // if (thing == nullptr) {
thing = roboid->perception->AddTrackedObject(this, position, orientation, // thing = roboid->perception->AddTrackedObject(this, position, orientation,
0xFF, objectId, networkId); // 0xFF, objectId, networkId);
if (thing->networkId != 0x00) { // if (thing->networkId != 0x00) {
// Unknown thing // // Unknown thing
roboid->networkSync->SendInvestigate(thing); // InvestigateMsg::Send(thing);
} // // roboid->networkSync->SendInvestigate(thing);
} // }
// }
// SwingTwist16 roboidOrientation = roboid->GetOrientation(); // SwingTwist16 roboidOrientation = roboid->GetOrientation();
Spherical16 position = Spherical16::zero; // Spherical16 position = Spherical16::zero;
SwingTwist16 orientation = SwingTwist16::identity; // SwingTwist16 orientation = SwingTwist16::identity;
Vector3 worldAngles = ReceiveVector3(data, 16); // Vector3 worldAngles = ReceiveVector3(data, 16);
SwingTwist16 worldOrientation = SwingTwist16( // SwingTwist16 worldOrientation = SwingTwist16(
Angle16::Degrees(worldAngles.Up()), Angle16::Degrees(worldAngles.Right()), // Angle16::Degrees(worldAngles.Up()),
Angle16::Degrees( // Angle16::Degrees(worldAngles.Right()), Angle16::Degrees(
worldAngles.Forward())); // Quaternion::Euler(worldAngles); // worldAngles.Forward())); // Quaternion::Euler(worldAngles);
if ((poseType & NetworkSync::Pose_Orientation) != 0) { // if ((poseType & NetworkSync::Pose_Orientation) != 0) {
if (objectId == 0) { // if (objectId == 0) {
// roboid->SetOrientation(worldOrientation); // // roboid->SetOrientation(worldOrientation);
if (roboid->worldOrigin == nullptr) { // if (roboid->worldOrigin == nullptr) {
printf("creating new origin\n"); // printf("creating new origin\n");
roboid->worldOrigin = new Thing(0); // roboid->worldOrigin = new Thing(0);
printf("created\n"); // printf("created\n");
} // }
roboid->worldOrigin->orientation = // roboid->worldOrigin->orientation =
SwingTwist16::Inverse(worldOrientation); // SwingTwist16::Inverse(worldOrientation);
} else { // } else {
// orientation = SwingTwist16::Inverse(roboidOrientation) * // // orientation = SwingTwist16::Inverse(roboidOrientation) *
// worldOrientation; if (thing != nullptr) { // // worldOrientation; if (thing != nullptr) {
// thing->orientation = orientation; // // thing->orientation = orientation;
// } // // }
SwingTwist16 originOrientation; // SwingTwist16 originOrientation;
Spherical16 originPosition; // Spherical16 originPosition;
if (roboid->worldOrigin == nullptr) { // if (roboid->worldOrigin == nullptr) {
originOrientation = SwingTwist16::identity; // originOrientation = SwingTwist16::identity;
originPosition = Spherical16::zero; // originPosition = Spherical16::zero;
} else { // } else {
originOrientation = roboid->worldOrigin->orientation; // originOrientation = roboid->worldOrigin->orientation;
originPosition = roboid->worldOrigin->position; // originPosition = roboid->worldOrigin->position;
} // }
thing->orientation = originOrientation * worldOrientation; // thing->orientation = originOrientation * worldOrientation;
} // }
} // }
if ((poseType & NetworkSync::Pose_Position) != 0) { // if ((poseType & NetworkSync::Pose_Position) != 0) {
Vector3 worldVector3 = ReceiveVector3(data, 4); // Vector3 worldVector3 = ReceiveVector3(data, 4);
Spherical16 worldPosition = Spherical16::FromVector3(worldVector3); // Spherical16 worldPosition = Spherical16::FromVector3(worldVector3);
if (objectId == 0) { // if (objectId == 0) {
// roboid->SetPosition(worldPosition); // // roboid->SetPosition(worldPosition);
if (roboid->worldOrigin == nullptr) { // if (roboid->worldOrigin == nullptr) {
printf("creating new origin again\n"); // printf("creating new origin again\n");
roboid->worldOrigin = new Thing(0); // roboid->worldOrigin = new Thing(0);
} // }
roboid->worldOrigin->position = // roboid->worldOrigin->position =
roboid->worldOrigin->orientation * -worldPosition; // roboid->worldOrigin->orientation * -worldPosition;
} else { // } else {
SwingTwist16 originOrientation; // SwingTwist16 originOrientation;
Spherical16 originPosition; // Spherical16 originPosition;
if (roboid->worldOrigin == nullptr) { // if (roboid->worldOrigin == nullptr) {
originOrientation = SwingTwist16::identity; // originOrientation = SwingTwist16::identity;
originPosition = Spherical16::zero; // originPosition = Spherical16::zero;
} else { // } else {
originOrientation = roboid->worldOrigin->orientation; // originOrientation = roboid->worldOrigin->orientation;
originPosition = roboid->worldOrigin->position; // originPosition = roboid->worldOrigin->position;
} // }
// SwingTwist16 roboidLocalOrientation = // // SwingTwist16 roboidLocalOrientation =
// originOrientation * worldOrientation; // // originOrientation * worldOrientation;
thing->position = originPosition + originOrientation * worldPosition; // thing->position = originPosition + originOrientation * worldPosition;
/* // /*
Spherical16 roboidPosition = roboid->GetPosition(); // Spherical16 roboidPosition = roboid->GetPosition();
// 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;
// } // // }
Spherical16 localPosition = SwingTwist16::Inverse(roboidOrientation) * // Spherical16 localPosition = SwingTwist16::Inverse(roboidOrientation) *
(worldPosition - roboidPosition); // (worldPosition - roboidPosition);
Vector3 roboidVector3 = roboidPosition.ToVector3(); // Vector3 roboidVector3 = roboidPosition.ToVector3();
printf(" roboid position (%f %f %f)\n", roboidVector3.Right(), // printf(" roboid position (%f %f %f)\n", roboidVector3.Right(),
roboidVector3.Up(), roboidVector3.Forward()); // roboidVector3.Up(), roboidVector3.Forward());
printf(" [%d/%d] worldPosition (%f %f %f) localPosition %f (%f %f)\n ", // printf(" [%d/%d] worldPosition (%f %f %f) localPosition %f (%f %f)\n
networkId, objectId, worldVector3.Right(), worldVector3.Up(), // ",
worldVector3.Forward(), localPosition.distance, // networkId, objectId, worldVector3.Right(), worldVector3.Up(),
localPosition.direction.horizontal.InDegrees(), // worldVector3.Forward(), localPosition.distance,
localPosition.direction.vertical.InDegrees()); // localPosition.direction.horizontal.InDegrees(),
// localPosition.direction.vertical.InDegrees());
thing->position = localPosition; // thing->position = localPosition;
*/ // */
} // }
} // }
} }
void NetworkPerception::ReceiveTypedObject(unsigned char *data, void NetworkPerception::ReceiveTypedObject(unsigned char *data,

View File

@ -12,6 +12,7 @@
#endif #endif
#endif #endif
#include "ControlCore/Messages.h"
#include "LinearAlgebra/Angle.h" #include "LinearAlgebra/Angle.h"
#include "LinearAlgebra/AngleUsing.h" #include "LinearAlgebra/AngleUsing.h"
#include "LinearAlgebra/Spherical.h" #include "LinearAlgebra/Spherical.h"
@ -61,16 +62,16 @@ void NetworkSync::ReceiveNetworkId() {
printf("completed\n"); printf("completed\n");
} }
void NetworkSync::SendInvestigate(InterestingThing *thing) { // void NetworkSync::SendInvestigate(InterestingThing *thing) {
unsigned char ix = 0; // unsigned char ix = 0;
buffer[ix++] = InvestigateMsg; // buffer[ix++] = InvestigateMsg;
buffer[ix++] = thing->networkId; // buffer[ix++] = thing->networkId;
buffer[ix++] = thing->id; // buffer[ix++] = thing->id;
SendBuffer(ix); // SendBuffer(ix);
#ifdef RC_DEBUG // #ifdef RC_DEBUG
printf("Sent Investigate [%d/%d]\n", thing->networkId, thing->id); // printf("Sent Investigate [%d/%d]\n", thing->networkId, thing->id);
#endif // #endif
} // }
void NetworkSync::ReceiveCustom(unsigned char packetSize) { void NetworkSync::ReceiveCustom(unsigned char packetSize) {
unsigned char ix = 1; // first byte is the msgId unsigned char ix = 1; // first byte is the msgId
@ -101,6 +102,9 @@ void NetworkSync::PublishState(Roboid *roboid) {
} }
void NetworkSync::SendThingInfo(Thing *thing, bool recurse) { void NetworkSync::SendThingInfo(Thing *thing, bool recurse) {
if (thing == nullptr)
return;
SendThing(thing); SendThing(thing);
SendName(thing); SendName(thing);
SendModel(thing); SendModel(thing);
@ -120,17 +124,23 @@ void NetworkSync::SendThing(Thing *thing) {
if (thing == nullptr) if (thing == nullptr)
return; return;
unsigned char ix = 0; // unsigned char ix = 0;
buffer[ix++] = ThingMsg; // buffer[ix++] = ThingMsg;
buffer[ix++] = this->networkId; // buffer[ix++] = this->networkId;
buffer[ix++] = thing->id; // buffer[ix++] = thing->id;
buffer[ix++] = (unsigned char)thing->type; // buffer[ix++] = (unsigned char)thing->type;
Thing *parentThing = thing->GetParent(); // Thing *parentThing = thing->GetParent();
if (parentThing != nullptr) // if (parentThing != nullptr)
buffer[ix++] = parentThing->id; // buffer[ix++] = parentThing->id;
else // else
buffer[ix++] = 0x00; // buffer[ix++] = 0x00;
SendBuffer(ix); // SendBuffer(ix);
Thing *parent = thing->GetParent();
Passer::Control::ThingMsg msg =
Passer::Control::ThingMsg(this->networkId, thing->id, thing->type,
parent == nullptr ? 0 : parent->id);
SendBuffer(msg.Serialize(this->buffer));
#ifdef RC_DEBUG #ifdef RC_DEBUG
printf("Sent Thing [%d/%d] %d\n", networkId, thing->id, (byte)thing->type); printf("Sent Thing [%d/%d] %d\n", networkId, thing->id, (byte)thing->type);
@ -145,16 +155,19 @@ void NetworkSync::SendName(Thing *thing) {
if (len > 255) if (len > 255)
return; return;
unsigned char ix = 0; // unsigned char ix = 0;
buffer[ix++] = NameMsg; // buffer[ix++] = NameMsg;
buffer[ix++] = this->networkId; // buffer[ix++] = this->networkId;
buffer[ix++] = thing->id; // buffer[ix++] = thing->id;
buffer[ix++] = len; // buffer[ix++] = len;
for (unsigned char nameIx = 0; nameIx < len; nameIx++) // for (unsigned char nameIx = 0; nameIx < len; nameIx++)
buffer[ix++] = thing->name[nameIx]; // buffer[ix++] = thing->name[nameIx];
SendBuffer(ix); // SendBuffer(ix);
Passer::Control::NameMsg msg =
Passer::Control::NameMsg(this->networkId, thing->id, len, thing->name);
SendBuffer(msg.Serialize(this->buffer));
#ifdef RC_DEBUG #ifdef RC_DEBUG
SERIALPORT.printf("Sent Name [%d/%d] %s\n", networkId, buffer[1], SERIALPORT.printf("Sent Name [%d/%d] %s\n", networkId, buffer[1],
@ -170,19 +183,22 @@ void NetworkSync::SendModel(Thing *thing) {
if (len > 255) if (len > 255)
return; return;
unsigned char ix = 0; // unsigned char ix = 0;
buffer[ix++] = ModelMsg; // buffer[ix++] = ModelMsg;
buffer[ix++] = this->networkId; // buffer[ix++] = this->networkId;
buffer[ix++] = thing->id; // objectId // buffer[ix++] = thing->id; // objectId
// Spherical16 s = Spherical16::zero; // Spherical(thing->modelPosition); // // Spherical16 s = Spherical16::zero; // Spherical(thing->modelPosition);
// Messages::SendSpherical16(buffer, &ix, s); // // Messages::SendSpherical16(buffer, &ix, s);
Messages::SendFloat16(buffer, &ix, thing->modelScale); // Messages::SendFloat16(buffer, &ix, thing->modelScale);
buffer[ix++] = len; // buffer[ix++] = len;
for (int urlIx = 0; urlIx < len; urlIx++) // for (int urlIx = 0; urlIx < len; urlIx++)
buffer[ix++] = thing->modelUrl[urlIx]; // buffer[ix++] = thing->modelUrl[urlIx];
SendBuffer(ix); // SendBuffer(ix);
Passer::Control::ModelUrlMsg msg = Passer::Control::ModelUrlMsg(
this->networkId, thing->id, len, thing->modelUrl, thing->modelScale);
SendBuffer(msg.Serialize(this->buffer));
} }
void NetworkSync::SendCustom(Thing *thing) { void NetworkSync::SendCustom(Thing *thing) {
@ -393,3 +409,6 @@ void NetworkSync::SendInt(const int x) {
void NetworkSync::SendBuffer(unsigned char bufferSize) {} void NetworkSync::SendBuffer(unsigned char bufferSize) {}
void NetworkSync::PublishBuffer(unsigned char bufferSize) {} void NetworkSync::PublishBuffer(unsigned char bufferSize) {}
void NetworkSync::SendCBuffer(unsigned char bufferSize, unsigned char *buffer) {
}

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "ControlCore/Messages.h"
#include "NetworkPerception.h" #include "NetworkPerception.h"
#include "Perception.h" #include "Perception.h"
#include "Roboid.h" #include "Roboid.h"
@ -66,7 +67,7 @@ public:
void PublishState(Roboid *roboid); void PublishState(Roboid *roboid);
void SendInvestigate(InterestingThing *thing); // void SendInvestigate(InterestingThing *thing);
void PublishPerception(Roboid *roboid); void PublishPerception(Roboid *roboid);
void PublishTrackedObjects(Roboid *roboid, InterestingThing **objects); void PublishTrackedObjects(Roboid *roboid, InterestingThing **objects);
@ -100,6 +101,8 @@ protected:
virtual void SendBuffer(unsigned char bufferSize); virtual void SendBuffer(unsigned char bufferSize);
virtual void PublishBuffer(unsigned char bufferSize); virtual void PublishBuffer(unsigned char bufferSize);
virtual void SendCBuffer(unsigned char bufferSize, unsigned char *buffer);
void PublishClient(); void PublishClient();
}; };