From 693c9b8b33baaccdf3d3af4bdf5cf1c73a3a15b2 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 23 Apr 2025 15:05:38 +0200 Subject: [PATCH] Object creation/destruction works --- Arduino/Things/DRV8833.cpp | 4 +- Messages/NameMsg.cpp | 7 ++-- Participant.cpp | 17 +------- Participants/ParticipantUDP.cpp | 69 ++++++++++++++++++++------------- Participants/SiteServer.cpp | 35 ++++++++++------- Thing.cpp | 15 +++++-- Thing.h | 9 ++++- Things/TouchSensor.cpp | 2 +- 8 files changed, 91 insertions(+), 67 deletions(-) diff --git a/Arduino/Things/DRV8833.cpp b/Arduino/Things/DRV8833.cpp index 17579d8..dfd659c 100644 --- a/Arduino/Things/DRV8833.cpp +++ b/Arduino/Things/DRV8833.cpp @@ -123,9 +123,9 @@ DRV8833::DRV8833(Participant* participant, pinMode(pinStandby, OUTPUT); this->motorA = new DRV8833Motor(this, pinAIn1, pinAIn2, reverseA); - this->motorA->name = "Motor A"; + this->motorA->SetName("Motor A"); this->motorB = new DRV8833Motor(this, pinBIn1, pinBIn2, reverseB); - this->motorB->name = "Motor B"; + this->motorB->SetName("Motor B"); } DRV8833::DRV8833(Thing* parent, diff --git a/Messages/NameMsg.cpp b/Messages/NameMsg.cpp index da91c79..84aba94 100644 --- a/Messages/NameMsg.cpp +++ b/Messages/NameMsg.cpp @@ -7,15 +7,16 @@ namespace RoboidControl { NameMsg::NameMsg(unsigned char networkId, Thing* thing) { this->networkId = networkId; this->thingId = thing->id; - if (thing->name == nullptr) + const char* thingName = thing->GetName(); + if (thingName == nullptr) this->nameLength = 0; else - this->nameLength = (unsigned char)strlen(thing->name); + this->nameLength = (unsigned char)strlen(thingName); // the name string in the buffer is not \0 terminated! char* name = new char[this->nameLength + 1]; for (int i = 0; i < this->nameLength; i++) - name[i] = thing->name[i]; + name[i] = thingName[i]; name[this->nameLength] = '\0'; this->name = name; } diff --git a/Participant.cpp b/Participant.cpp index bd38901..53ee6d4 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -151,22 +151,9 @@ void Participant::Remove(Thing* thing) { this->thingCount = lastThingIx; #else this->things.remove_if([thing](Thing* obj) { return obj == thing; }); - std::cout << "Removing [" << (int)thing->networkId << "/" << (int)thing->id - << "] list size = " << this->things.size() << "\n"; + // std::cout << "Removing [" << (int)thing->networkId << "/" << (int)thing->id + // << "] list size = " << this->things.size() << "\n"; #endif } -// void Participant::UpdateAll(unsigned long currentTimeMs) { -// // Not very efficient, but it works for now. - -// for (Thing* thing : this->things) { -// if (thing != nullptr && thing->GetParent() == nullptr) { // update all -// root things -// // std::cout << " update " << (int)ix << " thingid " << (int)thing->id -// // << "\n"; -// thing->Update(currentTimeMs); -// } -// } -// } - } // namespace RoboidControl diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index 780701a..efe27d2 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -80,6 +80,8 @@ void ParticipantUDP::SetupUDP(int localPort, this->connected = true; } +#pragma region Update + void ParticipantUDP::Update(unsigned long currentTimeMs) { if (currentTimeMs == 0) currentTimeMs = Thing::GetTimeMs(); @@ -112,12 +114,17 @@ void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) { continue; if (thing->hierarchyChanged) { - std::cout << "thing hierarchy changed " << (int)thing->id << std::endl; if (!(this->isIsolated || this->networkId == 0)) { - ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); - this->Send(this->remoteSite, thingMsg); - delete thingMsg; + ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); + this->Send(this->remoteSite, thingMsg); + delete thingMsg; + + if (thing->nameChanged) { + NameMsg* nameMsg = new NameMsg(this->networkId, thing); + this->Send(this->remoteSite, nameMsg); + delete nameMsg; } + } } // Why don't we do recursive? @@ -127,12 +134,17 @@ void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) { thing->Update(currentTimeMs, false); if (!(this->isIsolated || this->networkId == 0)) { - if (thing->isTerminated) { + if (thing->terminate) { DestroyMsg* destroyMsg = new DestroyMsg(this->networkId, thing); this->Send(this->remoteSite, destroyMsg); delete destroyMsg; } else { // Send to remote site + if (thing->nameChanged) { + NameMsg* nameMsg = new NameMsg(this->networkId, thing); + this->Send(this->remoteSite, nameMsg); + delete nameMsg; + } PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); this->Send(this->remoteSite, poseMsg); delete poseMsg; @@ -141,7 +153,7 @@ void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) { delete binaryMsg; } } - if (thing->isTerminated) + if (thing->terminate) this->Remove(thing); } } @@ -151,7 +163,7 @@ void ParticipantUDP::UpdateOtherThings(unsigned long currentTimeMs = 0) { if (participant == nullptr || participant == this) continue; - //participant->Update(currentTimeMs); + participant->Update(currentTimeMs); if (this->isIsolated) continue; @@ -166,24 +178,8 @@ void ParticipantUDP::UpdateOtherThings(unsigned long currentTimeMs = 0) { } } -void ParticipantUDP::ReceiveUDP() { -#if defined(_WIN32) || defined(_WIN64) - Windows::ParticipantUDP* thisWindows = - static_cast(this); - thisWindows->Receive(); -#elif defined(__unix__) || defined(__APPLE__) - Posix::ParticipantUDP* thisPosix = static_cast(this); - thisPosix->Receive(); -#elif defined(ARDUINO) - Arduino::ParticipantUDP* thisArduino = - static_cast(this); - thisArduino->Receive(); -#elif defined(IDF_VER) - EspIdf::ParticipantUDP* thisEspIdf = - static_cast(this); - thisEspIdf->Receive(); -#endif -} +// Update +#pragma endregion #pragma region Send @@ -283,6 +279,25 @@ bool ParticipantUDP::Publish(IMessage* msg) { #pragma region Receive +void ParticipantUDP::ReceiveUDP() { +#if defined(_WIN32) || defined(_WIN64) + Windows::ParticipantUDP* thisWindows = + static_cast(this); + thisWindows->Receive(); +#elif defined(__unix__) || defined(__APPLE__) + Posix::ParticipantUDP* thisPosix = static_cast(this); + thisPosix->Receive(); +#elif defined(ARDUINO) + Arduino::ParticipantUDP* thisArduino = + static_cast(this); + thisArduino->Receive(); +#elif defined(IDF_VER) + EspIdf::ParticipantUDP* thisEspIdf = + static_cast(this); + thisEspIdf->Receive(); +#endif +} + void ParticipantUDP::ReceiveData(unsigned char packetSize, char* senderIpAddress, unsigned int senderPort) { @@ -420,10 +435,10 @@ void ParticipantUDP::Process(Participant* sender, NameMsg* msg) { nameLength); // Leave space for null terminator #endif thingName[nameLength] = '\0'; - thing->name = thingName; + thing->SetName(thingName); #if !defined(NO_STD) - std::cout << thing->name; + std::cout << thing->GetName(); #endif } #if !defined(NO_STD) diff --git a/Participants/SiteServer.cpp b/Participants/SiteServer.cpp index 7484409..39a859b 100644 --- a/Participants/SiteServer.cpp +++ b/Participants/SiteServer.cpp @@ -9,6 +9,8 @@ namespace RoboidControl { +#pragma region Init + SiteServer::SiteServer(int port) { this->name = "Site Server"; this->publishInterval = 0; @@ -19,6 +21,10 @@ SiteServer::SiteServer(int port) { SetupUDP(port, ipAddress, 0); } +#pragma endregion Init + +#pragma region Update + void SiteServer::UpdateMyThings(unsigned long currentTimeMs) { for (Thing* thing : this->things) { if (thing == nullptr) @@ -43,8 +49,12 @@ void SiteServer::UpdateMyThings(unsigned long currentTimeMs) { } } +#pragma endregion Update + +#pragma region Receive + void SiteServer::Process(Participant* sender, ParticipantMsg* msg) { - if (msg->networkId == 0) { + if (msg->networkId != sender->networkId) { // std::cout << this->name << " received New Client -> " << // sender->ipAddress // << ":" << (int)sender->port << "\n"; @@ -59,21 +69,18 @@ void SiteServer::Process(Participant* sender, SiteMsg* msg) {} void SiteServer::Process(Participant* sender, ThingMsg* msg) { Thing* thing = sender->Get(msg->thingId); if (thing == nullptr) { - // #if defined(NO_STD) new Thing(sender, (Thing::Type)msg->thingType, msg->thingId); - // #else - // auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); - // //Thing* newThing; - // if (thingMsgProcessor != thingMsgProcessors.end()) // found item - // //newThing = - // thingMsgProcessor->second(sender, msg->networkId, - // msg->thingId); - // else - // //newThing = - // new Thing(sender, msg->networkId, msg->thingId, - // (Thing::Type)msg->thingType); - // #endif + + if (msg->parentId != 0) { + thing->SetParent(Get(msg->parentId)); + if (thing->GetParent() != nullptr) + std::cout << "Could not find parent [" << (int)msg->networkId << "/" + << (int)msg->parentId << "]\n"; + } else + thing->SetParent(nullptr); } } +#pragma endregion Receive + } // namespace RoboidControl diff --git a/Thing.cpp b/Thing.cpp index ca3a212..65579f6 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -49,8 +49,13 @@ Thing::Thing(Thing* parent, int thingType, unsigned char thingId) this->SetParent(parent); } -void Thing::Terminate() { - this->isTerminated = true; +void Thing::SetName(const char* name) { + this->name = name; + this->nameChanged = true; +} + +const char* Thing::GetName() const { + return this->name; } Thing* Thing::FindThing(const char* name) { @@ -77,7 +82,6 @@ void Thing::SetParent(Thing* parent) { this->parent = nullptr; } else parent->AddChild(this); - std::cout << "setting parent for " << (int) this->id << std::endl; this->hierarchyChanged = true; } @@ -164,6 +168,8 @@ void Thing::SetModel(const char* url) { this->modelUrl = url; } +#pragma region Update + unsigned long Thing::GetTimeMs() { #if defined(ARDUINO) return millis(); @@ -187,6 +193,7 @@ void Thing::Update(unsigned long currentTimeMs, bool recursive) { // this->linearVelocityUpdated = false; // this->angularVelocityUpdated = false; this->hierarchyChanged = false; + this->nameChanged = false; if (recursive) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { @@ -202,6 +209,8 @@ void Thing::UpdateThings(unsigned long currentTimeMs) { IsolatedParticipant::Isolated()->Update(currentTimeMs); } +#pragma endregion Update + int Thing::GenerateBinary(char* buffer, unsigned char* ix) { (void)buffer; (void)ix; diff --git a/Thing.h b/Thing.h index 0d45d65..21c557b 100644 --- a/Thing.h +++ b/Thing.h @@ -114,9 +114,14 @@ class Thing { Thing* parent = nullptr; Thing** children = nullptr; - public: /// @brief The name of the thing const char* name = nullptr; + + public: + void SetName(const char* name); + const char* GetName() const; + bool nameChanged = false; + /// @brief An URL pointing to the location where a model of the thing can be /// found const char* modelUrl = nullptr; @@ -177,7 +182,7 @@ class Thing { public: /// @brief Terminated things are no longer updated void Terminate(); - bool isTerminated = false; + bool terminate = false; /// @brief Sets the location from where the 3D model of this Thing can be /// loaded from diff --git a/Things/TouchSensor.cpp b/Things/TouchSensor.cpp index e068142..be98aeb 100644 --- a/Things/TouchSensor.cpp +++ b/Things/TouchSensor.cpp @@ -16,7 +16,7 @@ int TouchSensor::GenerateBinary(char* bytes, unsigned char* ix) { void TouchSensor::ProcessBinary(char* bytes) { if (bytes[0] == 1) - std::cout << this->name << " is Touching something!\n"; + std::cout << this->GetName() << " is Touching something!\n"; this->touchedSomething |= (bytes[0] == 1); }