From c2ac149df69dec193d6089ec2f39cb28081e35aa Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 6 Jun 2025 09:37:30 +0200 Subject: [PATCH] Optimise sending of messages --- Participant.cpp | 2 +- Participants/ParticipantUDP.cpp | 98 +++++++++++++-------------------- Participants/ParticipantUDP.h | 3 +- Things/ControlledMotor.cpp | 4 +- Things/Motor.cpp | 16 ++++++ Things/Motor.h | 4 +- 6 files changed, 59 insertions(+), 68 deletions(-) diff --git a/Participant.cpp b/Participant.cpp index bd03908..c3cf0ad 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -62,7 +62,7 @@ bool Participant::Send(IMessage* msg) { return true; // std::cout << "send msg " << (static_cast(this->buffer[0]) & 0xff) - // << " to " << remoteParticipant->ipAddress << std::endl; + // << " to " << this->ipAddress << std::endl; #if defined(_WIN32) || defined(_WIN64) Windows::ParticipantUDP* thisWindows = diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index 89682ea..fc99aae 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -97,7 +97,7 @@ void ParticipantUDP::Update() { if (this->remoteSite == nullptr) this->Publish(msg); else - this->remoteSite->Send(msg); + this->Send(msg); delete msg; @@ -127,19 +127,19 @@ void ParticipantUDP::UpdateMyThings() { continue; // std::cout << thing->name << "\n"; - if (thing->hierarchyChanged) { - if (!(this->isIsolated || this->networkId == 0)) { - ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); - this->remoteSite->Send(thingMsg); - delete thingMsg; + // if (thing->hierarchyChanged) { + // if (!(this->isIsolated || this->networkId == 0)) { + // ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); + // this->remoteSite->Send(thingMsg); + // delete thingMsg; - if (thing->nameChanged) { - NameMsg* nameMsg = new NameMsg(this->networkId, thing); - this->remoteSite->Send(nameMsg); - delete nameMsg; - } - } - } + // if (thing->nameChanged) { + // NameMsg* nameMsg = new NameMsg(this->networkId, thing); + // this->remoteSite->Send(nameMsg); + // delete nameMsg; + // } + // } + // } // std::cout << "B\n"; // Why don't we do recursive? @@ -149,26 +149,26 @@ void ParticipantUDP::UpdateMyThings() { thing->Update(false); // std::cout << "C\n"; - if (!(this->isIsolated || this->networkId == 0)) { - if (thing->terminate) { - DestroyMsg* destroyMsg = new DestroyMsg(this->networkId, thing); - this->remoteSite->Send(destroyMsg); - delete destroyMsg; - } else { - // Send to remote site - if (thing->nameChanged) { - NameMsg* nameMsg = new NameMsg(this->networkId, thing); - this->remoteSite->Send(nameMsg); - delete nameMsg; - } - PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); - this->remoteSite->Send(poseMsg); - delete poseMsg; - BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing); - this->remoteSite->Send(binaryMsg); - delete binaryMsg; - } - } + // if (!(this->isIsolated || this->networkId == 0)) { + // if (thing->terminate) { + // DestroyMsg* destroyMsg = new DestroyMsg(this->networkId, thing); + // this->remoteSite->Send(destroyMsg); + // delete destroyMsg; + // } else { + // // Send to remote site + // if (thing->nameChanged) { + // NameMsg* nameMsg = new NameMsg(this->networkId, thing); + // this->remoteSite->Send(nameMsg); + // delete nameMsg; + // } + // PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); + // this->remoteSite->Send(poseMsg); + // delete poseMsg; + // BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing); + // this->remoteSite->Send(binaryMsg); + // delete binaryMsg; + // } + // } // std::cout << "D\n"; if (thing->terminate) this->Remove(thing); @@ -230,34 +230,10 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant, delete binaryMsg; } -// bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) { -// int bufferSize = msg->Serialize(this->buffer); -// if (bufferSize <= 0) -// return true; - -// // std::cout << "send msg " << (static_cast(this->buffer[0]) & 0xff) -// // << " to " << remoteParticipant->ipAddress << std::endl; - -// #if defined(_WIN32) || defined(_WIN64) -// Windows::ParticipantUDP* thisWindows = -// static_cast(this); -// return thisWindows->Send(remoteParticipant, bufferSize); -// #elif defined(__unix__) || defined(__APPLE__) -// Posix::ParticipantUDP* thisPosix = -// static_cast(this); return -// thisPosix->Send(remoteParticipant, bufferSize); -// #elif defined(ARDUINO) -// Arduino::ParticipantUDP* thisArduino = -// static_cast(this); -// return thisArduino->Send(remoteParticipant, bufferSize); -// #elif defined(IDF_VER) -// EspIdf::ParticipantUDP* thisEspIdf = -// static_cast(this); -// return thisEspIdf->Send(remoteParticipant, bufferSize); -// #else -// return false; -// #endif -// } +bool ParticipantUDP::Send(IMessage* msg) { + if (this->remoteSite != nullptr) + this->remoteSite->Send(msg); +} void ParticipantUDP::PublishThingInfo(Thing* thing) { // std::cout << "Publish thing info" << thing->networkId << "\n"; diff --git a/Participants/ParticipantUDP.h b/Participants/ParticipantUDP.h index 9d92251..8b4b769 100644 --- a/Participants/ParticipantUDP.h +++ b/Participants/ParticipantUDP.h @@ -77,7 +77,6 @@ public: long publishInterval = 3000; // 3 seconds protected: - char buffer[1024]; #if !defined(ARDUINO) #if defined(__unix__) || defined(__APPLE__) @@ -114,7 +113,7 @@ public: void SendThingInfo(Participant* remoteParticipant, Thing* thing); void PublishThingInfo(Thing* thing); - //bool Send(Participant* remoteParticipant, IMessage* msg); + virtual bool Send(IMessage* msg) override; bool Publish(IMessage* msg); #pragma endregion Send diff --git a/Things/ControlledMotor.cpp b/Things/ControlledMotor.cpp index 26b16bc..9bc5d88 100644 --- a/Things/ControlledMotor.cpp +++ b/Things/ControlledMotor.cpp @@ -39,8 +39,8 @@ void ControlledMotor::Update(bool recurse) { this->lastError = error; float output = p_term + i_term + d_term; - std::cout << "target " << this->targetVelocity << " actual " - << this->actualVelocity << " output = " << output << std::endl; + // std::cout << "target " << this->targetVelocity << " actual " + // << this->actualVelocity << " output = " << output << std::endl; // float acceleration = // error * timeStep * pidP; // Just P is used at this moment // std::cout << "motor acc. " << acceleration << std::endl; diff --git a/Things/Motor.cpp b/Things/Motor.cpp index 936d4ec..c80e82e 100644 --- a/Things/Motor.cpp +++ b/Things/Motor.cpp @@ -1,5 +1,8 @@ #include "Motor.h" +#include "Messages/BinaryMsg.h" +#include "Participant.h" + namespace RoboidControl { Motor::Motor(Thing* parent) : Thing(parent) { @@ -7,7 +10,20 @@ Motor::Motor(Thing* parent) : Thing(parent) { } void Motor::SetTargetVelocity(float targetSpeed) { + if (targetSpeed != this->targetVelocity) { this->targetVelocity = targetSpeed; + + if (this->owner->networkId != 0) { + // in other word: if we are connected... + BinaryMsg* binaryMsg = new BinaryMsg(this->owner->networkId, this); + this->owner->Send(binaryMsg); + delete binaryMsg; + } + } +} + +float Motor::GetTargetVelocity() { + return this->targetVelocity; } int Motor::GenerateBinary(char* data, unsigned char* ix) { diff --git a/Things/Motor.h b/Things/Motor.h index 38ac149..0332289 100644 --- a/Things/Motor.h +++ b/Things/Motor.h @@ -14,11 +14,11 @@ class Motor : public Thing { Direction direction; virtual void SetTargetVelocity(float velocity); // -1..0..1 + virtual float GetTargetVelocity(); int GenerateBinary(char* bytes, unsigned char* ix) override; - // virtual void ProcessBinary(char* bytes) override; - //protected: + protected: float targetVelocity = 0; };