Object creation/destruction works

This commit is contained in:
Pascal Serrarens 2025-04-23 15:05:38 +02:00
parent a292179f87
commit 693c9b8b33
8 changed files with 91 additions and 67 deletions

View File

@ -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,

View File

@ -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;
}

View File

@ -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

View File

@ -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,11 +114,16 @@ 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;
if (thing->nameChanged) {
NameMsg* nameMsg = new NameMsg(this->networkId, thing);
this->Send(this->remoteSite, nameMsg);
delete nameMsg;
}
}
}
@ -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<Windows::ParticipantUDP*>(this);
thisWindows->Receive();
#elif defined(__unix__) || defined(__APPLE__)
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this);
thisPosix->Receive();
#elif defined(ARDUINO)
Arduino::ParticipantUDP* thisArduino =
static_cast<Arduino::ParticipantUDP*>(this);
thisArduino->Receive();
#elif defined(IDF_VER)
EspIdf::ParticipantUDP* thisEspIdf =
static_cast<EspIdf::ParticipantUDP*>(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<Windows::ParticipantUDP*>(this);
thisWindows->Receive();
#elif defined(__unix__) || defined(__APPLE__)
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this);
thisPosix->Receive();
#elif defined(ARDUINO)
Arduino::ParticipantUDP* thisArduino =
static_cast<Arduino::ParticipantUDP*>(this);
thisArduino->Receive();
#elif defined(IDF_VER)
EspIdf::ParticipantUDP* thisEspIdf =
static_cast<EspIdf::ParticipantUDP*>(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)

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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);
}