thing tytpe enum->const

This commit is contained in:
Pascal Serrarens 2025-06-02 12:30:21 +02:00
parent c3ba44d47a
commit 2883f5e0fa
3 changed files with 293 additions and 247 deletions

View File

@ -16,7 +16,7 @@ void Participant::ReplaceLocalParticipant(Participant& newParticipant) {
} }
Participant::Participant() { Participant::Participant() {
std::cout << "P\n"; //std::cout << "P\n";
//this->root.name = "Isolated"; //this->root.name = "Isolated";
this->root = new Thing(this); this->root = new Thing(this);
this->root->name = "Root"; this->root->name = "Root";

260
Thing.cpp
View File

@ -16,19 +16,22 @@
#include <list> #include <list>
#endif #endif
namespace RoboidControl { namespace RoboidControl
{
#pragma region Init #pragma region Init
Thing* Thing::LocalRoot() { Thing *Thing::LocalRoot()
Participant* p = Participant::LocalParticipant; {
Thing* localRoot = p->root; Participant *p = Participant::LocalParticipant;
Thing *localRoot = p->root;
return localRoot; return localRoot;
} }
// Only use this for root things // Only use this for root things
Thing::Thing(Participant* owner) { Thing::Thing(Participant *owner)
this->type = Type::Roboid; // should become root {
this->type = Type::Root; // should become root
this->position = Spherical::zero; this->position = Spherical::zero;
this->positionUpdated = true; this->positionUpdated = true;
@ -40,11 +43,12 @@ Thing::Thing(Participant* owner) {
this->angularVelocity = Spherical::zero; this->angularVelocity = Spherical::zero;
this->owner = owner; this->owner = owner;
//this->owner->Add(this, true); // this->owner->Add(this, true);
std::cout << this->owner->name << ": New root thing " << std::endl; //std::cout << this->owner->name << ": New root thing " << std::endl;
} }
Thing::Thing(unsigned char thingType, Thing* parent) { Thing::Thing(unsigned char thingType, Thing *parent)
{
this->type = thingType; this->type = thingType;
this->position = Spherical::zero; this->position = Spherical::zero;
@ -62,81 +66,94 @@ Thing::Thing(unsigned char thingType, Thing* parent) {
std::cout << this->owner->name << ": New thing for " << parent->name std::cout << this->owner->name << ": New thing for " << parent->name
<< std::endl; << std::endl;
} }
Thing::~Thing() { Thing::~Thing()
{
std::cout << "Destroy thing " << this->name << std::endl; std::cout << "Destroy thing " << this->name << std::endl;
} }
// Thing Thing::Reconstruct(Participant* owner, unsigned char thingType, Thing Thing::Reconstruct(Participant *owner, unsigned char thingType, unsigned char thingId)
// unsigned char thingId) { {
// Thing thing = Thing(owner, thingType); Thing thing = Thing(thingType, owner->root);
// thing.id = thingId; thing.id = thingId;
// return thing; return thing;
// } }
#pragma endregion Init #pragma endregion Init
void Thing::SetName(const char* name) { void Thing::SetName(const char *name)
{
this->name = name; this->name = name;
this->nameChanged = true; this->nameChanged = true;
} }
const char* Thing::GetName() const { const char *Thing::GetName() const
{
return this->name; return this->name;
} }
void Thing::SetModel(const char* url) { void Thing::SetModel(const char *url)
{
this->modelUrl = url; this->modelUrl = url;
} }
#pragma region Hierarchy #pragma region Hierarchy
void Thing::SetParent(Thing* parent) { void Thing::SetParent(Thing *parent)
if (parent == nullptr) { {
Thing* parentThing = this->parent; if (parent == nullptr)
{
Thing *parentThing = this->parent;
if (parentThing != nullptr) if (parentThing != nullptr)
parentThing->RemoveChild(this); parentThing->RemoveChild(this);
this->parent = nullptr; this->parent = nullptr;
} else }
else
parent->AddChild(this); parent->AddChild(this);
this->hierarchyChanged = true; this->hierarchyChanged = true;
} }
// void Thing::SetParent(Thing* parent) { // void Thing::SetParent(Thing* parent) {
// parent->AddChild(this); // parent->AddChild(this);
// this->hierarchyChanged = true; // this->hierarchyChanged = true;
// } // }
// const Thing& Thing::GetParent() { // const Thing& Thing::GetParent() {
// return *this->parent; // return *this->parent;
// } // }
bool Thing::IsRoot() const { bool Thing::IsRoot() const
{
return this == LocalRoot() || this->parent == nullptr; //&Thing::Root; return this == LocalRoot() || this->parent == nullptr; //&Thing::Root;
} }
// void Thing::SetParent(Thing* root, const char* name) { // void Thing::SetParent(Thing* root, const char* name) {
// Thing* thing = root->FindChild(name); // Thing* thing = root->FindChild(name);
// if (thing != nullptr) // if (thing != nullptr)
// this->SetParent(thing); // this->SetParent(thing);
// } // }
Thing* Thing::GetParent() { Thing *Thing::GetParent()
{
return this->parent; return this->parent;
} }
Thing* Thing::GetChildByIndex(unsigned char ix) { Thing *Thing::GetChildByIndex(unsigned char ix)
{
return this->children[ix]; return this->children[ix];
} }
void Thing::AddChild(Thing* child) { void Thing::AddChild(Thing *child)
{
unsigned char newChildCount = this->childCount + 1; unsigned char newChildCount = this->childCount + 1;
Thing** newChildren = new Thing*[newChildCount]; Thing **newChildren = new Thing *[newChildCount];
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
{
newChildren[childIx] = this->children[childIx]; newChildren[childIx] = this->children[childIx];
if (this->children[childIx] == child) { if (this->children[childIx] == child)
{
// child is already present, stop copying do not update the children // child is already present, stop copying do not update the children
delete[] newChildren; delete[] newChildren;
return; return;
@ -151,20 +168,25 @@ void Thing::AddChild(Thing* child) {
this->children = newChildren; this->children = newChildren;
this->childCount = newChildCount; this->childCount = newChildCount;
} }
Thing* Thing::RemoveChild(Thing* child) { Thing *Thing::RemoveChild(Thing *child)
{
unsigned char newChildCount = this->childCount - 1; unsigned char newChildCount = this->childCount - 1;
Thing** newChildren = new Thing*[newChildCount]; Thing **newChildren = new Thing *[newChildCount];
unsigned char newChildIx = 0; unsigned char newChildIx = 0;
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
if (this->children[childIx] != child) { {
if (newChildIx == newChildCount) { // We did not find the child if (this->children[childIx] != child)
{
if (newChildIx == newChildCount)
{ // We did not find the child
// stop copying and return nothing // stop copying and return nothing
delete[] newChildren; delete[] newChildren;
return nullptr; return nullptr;
} else }
else
newChildren[newChildIx++] = this->children[childIx]; newChildren[newChildIx++] = this->children[childIx];
} }
} }
@ -176,89 +198,105 @@ Thing* Thing::RemoveChild(Thing* child) {
this->childCount = newChildCount; this->childCount = newChildCount;
return child; return child;
} }
Thing* Thing::GetChild(unsigned char id, bool recurse) { Thing *Thing::GetChild(unsigned char id, bool recurse)
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { {
Thing* child = this->children[childIx]; for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
{
Thing *child = this->children[childIx];
if (child == nullptr) if (child == nullptr)
continue; continue;
if (child->id == id) if (child->id == id)
return child; return child;
if (recurse) { if (recurse)
Thing* foundChild = child->GetChild(id, recurse); {
Thing *foundChild = child->GetChild(id, recurse);
if (foundChild != nullptr) if (foundChild != nullptr)
return foundChild; return foundChild;
} }
} }
return nullptr; return nullptr;
} }
Thing* Thing::FindChild(const char* name, bool recurse) { Thing *Thing::FindChild(const char *name, bool recurse)
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { {
Thing* child = this->children[childIx]; for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
{
Thing *child = this->children[childIx];
if (child == nullptr || child->name == nullptr) if (child == nullptr || child->name == nullptr)
continue; continue;
if (strcmp(child->name, name) == 0) if (strcmp(child->name, name) == 0)
return child; return child;
Thing* foundChild = child->FindChild(name); Thing *foundChild = child->FindChild(name);
if (foundChild != nullptr) if (foundChild != nullptr)
return foundChild; return foundChild;
} }
return nullptr; return nullptr;
} }
#pragma endregion Hierarchy #pragma endregion Hierarchy
#pragma region Pose #pragma region Pose
void Thing::SetPosition(Spherical position) { void Thing::SetPosition(Spherical position)
{
this->position = position; this->position = position;
this->positionUpdated = true; this->positionUpdated = true;
} }
Spherical Thing::GetPosition() { Spherical Thing::GetPosition()
{
return this->position; return this->position;
} }
void Thing::SetOrientation(SwingTwist orientation) { void Thing::SetOrientation(SwingTwist orientation)
{
this->orientation = orientation; this->orientation = orientation;
this->orientationUpdated = true; this->orientationUpdated = true;
} }
SwingTwist Thing::GetOrientation() { SwingTwist Thing::GetOrientation()
{
return this->orientation; return this->orientation;
} }
void Thing::SetLinearVelocity(Spherical linearVelocity) { void Thing::SetLinearVelocity(Spherical linearVelocity)
if (this->linearVelocity.distance != linearVelocity.distance) { {
if (this->linearVelocity.distance != linearVelocity.distance)
{
this->linearVelocity = linearVelocity; this->linearVelocity = linearVelocity;
this->linearVelocityUpdated = true; this->linearVelocityUpdated = true;
} }
} }
Spherical Thing::GetLinearVelocity() { Spherical Thing::GetLinearVelocity()
{
return this->linearVelocity; return this->linearVelocity;
} }
void Thing::SetAngularVelocity(Spherical angularVelocity) { void Thing::SetAngularVelocity(Spherical angularVelocity)
if (this->angularVelocity.distance != angularVelocity.distance) { {
if (this->angularVelocity.distance != angularVelocity.distance)
{
this->angularVelocity = angularVelocity; this->angularVelocity = angularVelocity;
this->angularVelocityUpdated = true; this->angularVelocityUpdated = true;
} }
} }
Spherical Thing::GetAngularVelocity() { Spherical Thing::GetAngularVelocity()
{
return this->angularVelocity; return this->angularVelocity;
} }
#pragma endregion Pose #pragma endregion Pose
#pragma region Update #pragma region Update
unsigned long Thing::GetTimeMs() { unsigned long Thing::GetTimeMs()
{
#if defined(ARDUINO) #if defined(ARDUINO)
unsigned long ms = millis(); unsigned long ms = millis();
return ms; return ms;
@ -268,15 +306,16 @@ unsigned long Thing::GetTimeMs() {
now.time_since_epoch()); now.time_since_epoch());
return static_cast<unsigned long>(ms.count()); return static_cast<unsigned long>(ms.count());
#endif #endif
} }
// void Thing::Update(bool recursive) { // void Thing::Update(bool recursive) {
// Update(GetTimeMs(), recursive); // Update(GetTimeMs(), recursive);
// } // }
void Thing::PrepareForUpdate() {} void Thing::PrepareForUpdate() {}
void Thing::Update(bool recursive) { void Thing::Update(bool recursive)
{
// if (this->positionUpdated || this->orientationUpdated) // if (this->positionUpdated || this->orientationUpdated)
// OnPoseChanged callback // OnPoseChanged callback
this->positionUpdated = false; this->positionUpdated = false;
@ -286,30 +325,35 @@ void Thing::Update(bool recursive) {
this->hierarchyChanged = false; this->hierarchyChanged = false;
this->nameChanged = false; this->nameChanged = false;
if (recursive) { if (recursive)
{
// std::cout << "# children: " << (int)this->childCount << std::endl; // std::cout << "# children: " << (int)this->childCount << std::endl;
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
Thing* child = this->children[childIx]; {
Thing *child = this->children[childIx];
if (child == nullptr) if (child == nullptr)
continue; continue;
child->Update(recursive); child->Update(recursive);
} }
} }
} }
void Thing::UpdateThings() { void Thing::UpdateThings()
{
IsolatedParticipant::Isolated()->Update(); IsolatedParticipant::Isolated()->Update();
} }
#pragma endregion Update #pragma endregion Update
int Thing::GenerateBinary(char* buffer, unsigned char* ix) { int Thing::GenerateBinary(char *buffer, unsigned char *ix)
{
(void)buffer; (void)buffer;
(void)ix; (void)ix;
return 0; return 0;
} }
void Thing::ProcessBinary(char* bytes) { void Thing::ProcessBinary(char *bytes)
{
(void)bytes; (void)bytes;
}; };
} // namespace RoboidControl } // namespace RoboidControl

36
Thing.h
View File

@ -20,24 +20,26 @@ class ParticipantUDP;
class Thing { class Thing {
public: public:
/// @brief Predefined thing types /// @brief Predefined thing types
enum Type : unsigned char { struct Type {
Undetermined, static const unsigned char Undetermined = 0x00;
// Sensor, // Sensor
Switch, static const unsigned char Switch = 0x01;
DistanceSensor, static const unsigned char DistanceSensor = 0x02;
DirectionalSensor, static const unsigned char DirectionalSensor = 0x03;
TemperatureSensor, static const unsigned char TemperatureSensor = 0x04;
TouchSensor, static const unsigned char TouchSensor = 0x05;
// Motor, // Motor
ControlledMotor, static const unsigned char ControlledMotor = 0x06;
UncontrolledMotor, static const unsigned char UncontrolledMotor = 0x07;
Servo, static const unsigned char Servo = 0x08;
IncrementalEncoder, static const unsigned char IncrementalEncoder = 0x19;
// Other // Other
Roboid, static const unsigned char Root = 0x10;
Humanoid, static const unsigned char Roboid = 0x09;
ExternalSensor, static const unsigned char Humanoid = 0x0A;
DifferentialDrive static const unsigned char ExternalSensor = 0x08;
static const unsigned char Animator = 0x0C;
static const unsigned char DifferentialDrive = 0x0D;
}; };
#pragma region Init #pragma region Init