Compare commits

...

4 Commits

16 changed files with 301 additions and 145 deletions

View File

@ -9,11 +9,13 @@ namespace Arduino {
uint8_t DRV8833Motor::nextAvailablePwmChannel = 0; uint8_t DRV8833Motor::nextAvailablePwmChannel = 0;
#endif #endif
DRV8833Motor::DRV8833Motor(Participant* participant, DRV8833Motor::DRV8833Motor(DRV8833* driver,
unsigned char pinIn1, unsigned char pinIn1,
unsigned char pinIn2, unsigned char pinIn2,
bool reverse) bool reverse)
: Thing(participant) { : Thing(driver->owner) {
this->parent = driver;
this->pinIn1 = pinIn1; this->pinIn1 = pinIn1;
this->pinIn2 = pinIn2; this->pinIn2 = pinIn2;
@ -120,11 +122,30 @@ DRV8833::DRV8833(Participant* participant,
if (pinStandby != 255) if (pinStandby != 255)
pinMode(pinStandby, OUTPUT); pinMode(pinStandby, OUTPUT);
this->motorA = new DRV8833Motor(participant, pinAIn1, pinAIn2, reverseA); this->motorA = new DRV8833Motor(this, pinAIn1, pinAIn2, reverseA);
this->motorA->name = "Motor A"; this->motorA->name = "Motor A";
this->motorB = new DRV8833Motor(participant, pinBIn1, pinBIn2, reverseB); this->motorB = new DRV8833Motor(this, pinBIn1, pinBIn2, reverseB);
this->motorB->name = "Motor B"; this->motorB->name = "Motor B";
} }
DRV8833::DRV8833(Thing* parent,
unsigned char pinAIn1,
unsigned char pinAIn2,
unsigned char pinBIn1,
unsigned char pinBIn2,
unsigned char pinStandby,
bool reverseA,
bool reverseB)
: DRV8833(parent->owner,
pinAIn1,
pinAIn2,
pinBIn1,
pinBIn2,
pinStandby,
reverseA,
reverseB) {
this->parent = parent;
}
} // namespace Arduino } // namespace Arduino
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -7,6 +7,42 @@
namespace RoboidControl { namespace RoboidControl {
namespace Arduino { namespace Arduino {
class DRV8833Motor;
class DRV8833 : public Thing {
public:
/// @brief Setup a DRV8833 motor controller
/// @param pinAIn1 The pin number connected to the AIn1 port
/// @param pinAIn2 The pin number connected to the AIn2 port
/// @param pinBIn1 The pin number connected to the BIn1 port
/// @param pinBIn2 The pin number connceted to the BIn2 port
/// @param pinStandby The pin number connected to the standby port, 255
/// indicated that the port is not connected
/// @param reverseA The forward turning direction of motor A
/// @param reverseB The forward turning direction of motor B
DRV8833(Participant* participant,
unsigned char pinAIn1,
unsigned char pinAIn2,
unsigned char pinBIn1,
unsigned char pinBIn2,
unsigned char pinStandby = 255,
bool reverseA = false,
bool reverseB = false);
DRV8833(Thing* parent,
unsigned char pinAIn1,
unsigned char pinAIn2,
unsigned char pinBIn1,
unsigned char pinBIn2,
unsigned char pinStandby = 255,
bool reverseA = false,
bool reverseB = false);
DRV8833Motor* motorA = nullptr;
DRV8833Motor* motorB = nullptr;
protected:
unsigned char pinStandby = 255;
};
/// @brief Support for a DRV8833 motor controller /// @brief Support for a DRV8833 motor controller
class DRV8833Motor : public Thing { class DRV8833Motor : public Thing {
public: public:
@ -17,7 +53,7 @@ class DRV8833Motor : public Thing {
/// @param pinIn1 the pin number for the in1 signal /// @param pinIn1 the pin number for the in1 signal
/// @param pinIn2 the pin number for the in2 signal /// @param pinIn2 the pin number for the in2 signal
/// @param direction the forward turning direction of the motor /// @param direction the forward turning direction of the motor
DRV8833Motor(Participant* participant, DRV8833Motor(DRV8833* driver,
unsigned char pinIn1, unsigned char pinIn1,
unsigned char pinIn2, unsigned char pinIn2,
bool reverse = false); bool reverse = false);
@ -39,32 +75,5 @@ class DRV8833Motor : public Thing {
#endif #endif
}; };
class DRV8833 : public Thing {
public:
/// @brief Setup a DRV8833 motor controller
/// @param pinAIn1 The pin number connected to the AIn1 port
/// @param pinAIn2 The pin number connected to the AIn2 port
/// @param pinBIn1 The pin number connected to the BIn1 port
/// @param pinBIn2 The pin number connceted to the BIn2 port
/// @param pinStandby The pin number connected to the standby port, 255
/// indicated that the port is not connected
/// @param reverseA The forward turning direction of motor A
/// @param reverseB The forward turning direction of motor B
DRV8833(Participant* participant,
unsigned char pinAIn1,
unsigned char pinAIn2,
unsigned char pinBIn1,
unsigned char pinBIn2,
unsigned char pinStandby = 255,
bool reverseA = false,
bool reverseB = false);
DRV8833Motor* motorA = nullptr;
DRV8833Motor* motorB = nullptr;
protected:
unsigned char pinStandby = 255;
};
} // namespace Arduino } // namespace Arduino
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -16,6 +16,13 @@ UltrasonicSensor::UltrasonicSensor(Participant* participant,
pinMode(pinEcho, INPUT); // configure the echo pin to input mode pinMode(pinEcho, INPUT); // configure the echo pin to input mode
} }
UltrasonicSensor::UltrasonicSensor(Thing* parent,
unsigned char pinTrigger,
unsigned char pinEcho)
: UltrasonicSensor(parent->owner, pinTrigger, pinEcho) {
this->parent = parent;
}
float UltrasonicSensor::GetDistance() { float UltrasonicSensor::GetDistance() {
// Start the ultrasonic 'ping' // Start the ultrasonic 'ping'
digitalWrite(pinTrigger, LOW); digitalWrite(pinTrigger, LOW);
@ -44,10 +51,10 @@ float UltrasonicSensor::GetDistance() {
// if (distance > 30) // if (distance > 30)
// distance = 0; // distance = 0;
this->touchedSomething |= (this->distance <= this->touchDistance); this->touchedSomething |= (this->distance > 0 && this->distance <= this->touchDistance);
// std::cout << "Ultrasonic " << this->distance << " " << // std::cout << "Ultrasonic " << this->touchedSomething << " | " << (this->distance > 0) << " " <<
// this->touchedSomething << "\n"; // (this->distance <= this->touchDistance) << "\n";
return distance; return distance;
} }

View File

@ -8,6 +8,9 @@ namespace Arduino {
/// @brief An HC-SR04 ultrasonic distance sensor /// @brief An HC-SR04 ultrasonic distance sensor
class UltrasonicSensor : public TouchSensor { class UltrasonicSensor : public TouchSensor {
public: public:
// Inherit all constructors
//using TouchSensor::TouchSensor;
/// @brief Setup an ultrasonic sensor /// @brief Setup an ultrasonic sensor
/// @param participant The participant to use /// @param participant The participant to use
/// @param pinTrigger The pin number of the trigger signal /// @param pinTrigger The pin number of the trigger signal
@ -15,6 +18,9 @@ class UltrasonicSensor : public TouchSensor {
UltrasonicSensor(Participant* participant, UltrasonicSensor(Participant* participant,
unsigned char pinTrigger, unsigned char pinTrigger,
unsigned char pinEcho); unsigned char pinEcho);
UltrasonicSensor(Thing* parent,
unsigned char pinTrigger,
unsigned char pinEcho);
// parameters // parameters

View File

@ -2,20 +2,23 @@
namespace RoboidControl { namespace RoboidControl {
DestroyMsg::DestroyMsg(unsigned char networkId, Thing *thing) { DestroyMsg::DestroyMsg(unsigned char networkId, Thing* thing) {
this->networkId = networkId; this->networkId = networkId;
this->thingId = thing->id; this->thingId = thing->id;
} }
DestroyMsg::DestroyMsg(char* buffer) {} DestroyMsg::DestroyMsg(char* buffer) {
this->networkId = buffer[1];
this->thingId = buffer[2];
}
DestroyMsg::~DestroyMsg() {} DestroyMsg::~DestroyMsg() {}
unsigned char DestroyMsg::Serialize(char *buffer) { unsigned char DestroyMsg::Serialize(char* buffer) {
#if defined(DEBUG) //#if defined(DEBUG)
std::cout << "Send DestroyMsg [" << (int)this->networkId << "/" << (int)this->thingId std::cout << "Send DestroyMsg [" << (int)this->networkId << "/"
<< "] " << std::endl; << (int)this->thingId << "] " << std::endl;
#endif //#endif
unsigned char ix = 0; unsigned char ix = 0;
buffer[ix++] = this->id; buffer[ix++] = this->id;
buffer[ix++] = this->networkId; buffer[ix++] = this->networkId;
@ -23,4 +26,4 @@ unsigned char DestroyMsg::Serialize(char *buffer) {
return ix; return ix;
} }
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -8,11 +8,11 @@ PoseMsg::PoseMsg(unsigned char networkId, Thing* thing, bool force) {
this->thingId = thing->id; this->thingId = thing->id;
this->poseType = 0; this->poseType = 0;
if (thing->positionUpdated || force) { if (thing->positionUpdated || (force && thing->GetLinearVelocity().distance == 0)) {
this->position = thing->GetPosition(); this->position = thing->GetPosition();
this->poseType |= Pose_Position; this->poseType |= Pose_Position;
} }
if (thing->orientationUpdated || force) { if (thing->orientationUpdated || (force && thing->GetAngularVelocity().distance == 0)) {
this->orientation = thing->GetOrientation(); this->orientation = thing->GetOrientation();
this->poseType |= Pose_Orientation; this->poseType |= Pose_Orientation;
} }
@ -45,7 +45,7 @@ unsigned char PoseMsg::Serialize(char* buffer) {
if (this->poseType == 0) if (this->poseType == 0)
return 0; return 0;
#if defined(DEBUG) #if defined(DEBUG) && DEBUG > 1
std::cout << "Send PoseMsg [" << (int)this->networkId << "/" std::cout << "Send PoseMsg [" << (int)this->networkId << "/"
<< (int)this->thingId << "] " << (int)this->poseType << std::endl; << (int)this->thingId << "] " << (int)this->poseType << std::endl;
#endif #endif

View File

@ -4,6 +4,8 @@
namespace RoboidControl { namespace RoboidControl {
std::list<Participant*> Participant::participants;
Participant::Participant() {} Participant::Participant() {}
Participant::Participant(const char* ipAddress, int port) { Participant::Participant(const char* ipAddress, int port) {
@ -34,12 +36,63 @@ void Participant::Update(unsigned long currentTimeMs) {
} }
} }
Participant* Participant::GetParticipant(const char* ipAddress,
unsigned int port) {
for (Participant* participant : Participant::participants) {
if (participant == nullptr)
continue;
if (strcmp(participant->ipAddress, ipAddress) == 0 &&
participant->port == port)
return participant;
}
std::cout << "Could not find participant " << ipAddress << ":" << (int)port
<< std::endl;
return nullptr;
}
Participant* Participant::GetParticipant(unsigned char participantId) {
for (Participant* participant : Participant::participants) {
if (participant == nullptr)
continue;
if (participant->networkId == participantId)
return participant;
}
std::cout << "Could not find participant " << (int)participantId << std::endl;
return nullptr;
}
Participant* Participant::AddParticipant(const char* ipAddress, unsigned int port) {
Participant* participant = new Participant(ipAddress, port);
Participant::AddParticipant(participant);
return participant;
}
void Participant::AddParticipant(Participant* participant) {
Participant* foundParticipant =
Participant::GetParticipant(participant->ipAddress, participant->port);
if (foundParticipant == nullptr) {
#if defined(NO_STD)
this->things[this->thingCount++] = thing;
#else
Participant::participants.push_back(participant);
#endif
std::cout << "Add participant " << participant->ipAddress << ":"
<< participant->port << "[" << (int)participant->networkId
<< "]\n";
} else {
std::cout << "Did not add, existing participant " << participant->ipAddress << ":"
<< participant->port << "[" << (int)participant->networkId
<< "]\n";
}
}
Thing* Participant::Get(unsigned char thingId) { Thing* Participant::Get(unsigned char thingId) {
for (Thing* thing : this->things) { for (Thing* thing : this->things) {
if (thing->id == thingId) if (thing->id == thingId)
return thing; return thing;
} }
// std::cout << "Could not find thing " << this->ipAddress << ":" << this->port // std::cout << "Could not find thing " << this->ipAddress << ":" <<
// this->port
// << "[" << (int)thingId << "]\n"; // << "[" << (int)thingId << "]\n";
return nullptr; return nullptr;
} }
@ -64,7 +117,8 @@ void Participant::Add(Thing* thing, bool checkId) {
#else #else
this->things.push_back(thing); this->things.push_back(thing);
#endif #endif
// std::cout << "Add thing " << this->ipAddress << ":" << this->port << "[" // std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
// "["
// << (int)thing->id << "]\n"; // << (int)thing->id << "]\n";
} else { } else {
// std::cout << "Did not add, existing thing " << this->ipAddress << ":" // std::cout << "Did not add, existing thing " << this->ipAddress << ":"
@ -89,8 +143,8 @@ void Participant::Remove(Thing* thing) {
this->thingCount = lastThingIx; this->thingCount = lastThingIx;
#else #else
this->things.remove_if([thing](Thing* obj) { return obj == thing; }); this->things.remove_if([thing](Thing* obj) { return obj == thing; });
std::cout << "Removing " << thing->networkId << "/" << thing->id std::cout << "Removing [" << (int)thing->networkId << "/" << (int)thing->id
<< " list size = " << this->things.size() << "\n"; << "] list size = " << this->things.size() << "\n";
#endif #endif
} }

View File

@ -18,7 +18,7 @@ class Participant {
const char* ipAddress = "0.0.0.0"; const char* ipAddress = "0.0.0.0";
/// @brief The port number for UDP communication with the participant. This is /// @brief The port number for UDP communication with the participant. This is
/// 0 for isolated participants. /// 0 for isolated participants.
int port = 0; unsigned int port = 0;
/// @brief The network Id to identify the participant. /// @brief The network Id to identify the participant.
/// @note This field is likely to disappear in future versions /// @note This field is likely to disappear in future versions
@ -35,16 +35,24 @@ class Participant {
virtual void Update(unsigned long currentTimeMs = 0); virtual void Update(unsigned long currentTimeMs = 0);
protected: public:
#if defined(NO_STD) #if defined(NO_STD)
unsigned char thingCount = 0; unsigned char thingCount = 0;
Thing* things[MAX_THING_COUNT]; Thing* things[MAX_THING_COUNT];
#else #else
/// @brief The list of known participants
static std::list<Participant*> participants;
/// @brief The list of things managed by this participant /// @brief The list of things managed by this participant
std::list<Thing*> things; std::list<Thing*> things;
#endif #endif
public: public:
static Participant* GetParticipant(const char* ipAddress, unsigned int port);
static Participant* GetParticipant(unsigned char participantId);
static Participant* AddParticipant(const char* ipAddress, unsigned int port);
static void AddParticipant(Participant* participant);
/// @brief Find a thing managed by this participant /// @brief Find a thing managed by this participant
/// @param networkId The network ID for the thing /// @param networkId The network ID for the thing
/// @param thingId The ID of the thing /// @param thingId The ID of the thing

View File

@ -5,6 +5,7 @@
#include "Arduino/ArduinoParticipant.h" #include "Arduino/ArduinoParticipant.h"
#include "EspIdf/EspIdfParticipant.h" #include "EspIdf/EspIdfParticipant.h"
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
@ -31,6 +32,7 @@ ParticipantUDP::ParticipantUDP(int port) {
this->remoteSite = nullptr; this->remoteSite = nullptr;
if (this->port == 0) if (this->port == 0)
this->isIsolated = true; this->isIsolated = true;
Participant::AddParticipant(this);
} }
ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
@ -39,6 +41,7 @@ ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
this->isIsolated = true; this->isIsolated = true;
else else
this->remoteSite = new Participant(ipAddress, port); this->remoteSite = new Participant(ipAddress, port);
Participant::AddParticipant(this);
} }
static ParticipantUDP* isolatedParticipant = nullptr; static ParticipantUDP* isolatedParticipant = nullptr;
@ -100,18 +103,53 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) {
this->ReceiveUDP(); this->ReceiveUDP();
} }
UpdateMyThings(currentTimeMs);
UpdateOtherThings(currentTimeMs);
}
void ParticipantUDP::UpdateMyThings(unsigned long currentTimeMs = 0) {
for (Thing* thing : this->things) { for (Thing* thing : this->things) {
if (thing == nullptr) if (thing == nullptr || thing->GetParent() != nullptr)
continue; continue;
thing->Update(currentTimeMs, true); thing->Update(currentTimeMs, true);
if (this->isIsolated == false && thing->owner != this) { if (this->isIsolated || this->networkId == 0)
continue;
if (thing->isTerminated) {
DestroyMsg* destroyMsg = new DestroyMsg(this->networkId, thing);
this->Send(this->remoteSite, destroyMsg);
delete destroyMsg;
this->Remove(thing);
} else {
// Send to remote site
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); PoseMsg* poseMsg = new PoseMsg(this->networkId, thing);
this->Send(thing->owner, poseMsg); this->Send(this->remoteSite, poseMsg);
BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing);
this->Send(thing->owner, binaryMsg);
delete poseMsg; delete poseMsg;
BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing);
this->Send(this->remoteSite, binaryMsg);
delete binaryMsg;
}
}
}
void ParticipantUDP::UpdateOtherThings(unsigned long currentTimeMs = 0) {
for (Participant* participant : Participant::participants) {
if (participant == nullptr || participant == this)
continue;
participant->Update(currentTimeMs);
if (this->isIsolated)
continue;
for (Thing* thing : participant->things) {
PoseMsg* poseMsg = new PoseMsg(participant->networkId, thing);
this->Send(participant, poseMsg);
delete poseMsg;
BinaryMsg* binaryMsg = new BinaryMsg(participant->networkId, thing);
this->Send(participant, binaryMsg);
delete binaryMsg;
} }
} }
} }
@ -135,27 +173,6 @@ void ParticipantUDP::ReceiveUDP() {
#endif #endif
} }
Participant* ParticipantUDP::GetParticipant(const char* ipAddress, int port) {
for (Participant* sender : this->senders) {
if (strcmp(sender->ipAddress, ipAddress) == 0 && sender->port == port)
return sender;
}
return nullptr;
}
Participant* ParticipantUDP::AddParticipant(const char* ipAddress, int port) {
// std::cout << "New Participant " << ipAddress << ":" << port << "\n";
Participant* participant = new Participant(ipAddress, port);
#if defined(NO_STD)
participant->networkId = this->senderCount;
this->senders[this->senderCount++] = participant;
#else
participant->networkId = (unsigned char)this->senders.size();
this->senders.push_back(participant);
#endif
return participant;
}
#pragma region Send #pragma region Send
void ParticipantUDP::SendThingInfo(Participant* remoteParticipant, void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
@ -179,12 +196,13 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
} }
bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) { bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) {
// std::cout << "send msg " << (int)this->buffer[0] << " to "
// << remoteParticipant->ipAddress << std::endl;
int bufferSize = msg->Serialize(this->buffer); int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)
return true; return true;
// std::cout << "send msg " << (int)this->buffer[0] << " to "
// << remoteParticipant->ipAddress << std::endl;
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
Windows::ParticipantUDP* thisWindows = Windows::ParticipantUDP* thisWindows =
static_cast<Windows::ParticipantUDP*>(this); static_cast<Windows::ParticipantUDP*>(this);
@ -256,6 +274,8 @@ bool ParticipantUDP::Publish(IMessage* msg) {
void ParticipantUDP::ReceiveData(unsigned char packetSize, void ParticipantUDP::ReceiveData(unsigned char packetSize,
char* senderIpAddress, char* senderIpAddress,
unsigned int senderPort) { unsigned int senderPort) {
// std::cout << "Receive data from " << senderIpAddress << ":" << senderPort
// << std::endl;
Participant* sender = this->GetParticipant(senderIpAddress, senderPort); Participant* sender = this->GetParticipant(senderIpAddress, senderPort);
if (sender == nullptr) { if (sender == nullptr) {
sender = this->AddParticipant(senderIpAddress, senderPort); sender = this->AddParticipant(senderIpAddress, senderPort);
@ -343,12 +363,12 @@ void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) {
<< " -> " << (int)msg->networkId << "\n"; << " -> " << (int)msg->networkId << "\n";
#endif #endif
if (this->networkId != msg->networkId) { if (this->networkId != msg->networkId)
this->networkId = msg->networkId; this->networkId = msg->networkId;
// std::cout << this->things.size() << " things\n";
for (Thing* thing : this->things) // std::cout << this->things.size() << " things\n";
this->SendThingInfo(sender, thing); for (Thing* thing : this->things)
} this->SendThingInfo(sender, thing);
} }
void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) { void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {
@ -410,6 +430,17 @@ void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) {
std::cout << this->name << ": process PoseMsg [" << (int)this->networkId std::cout << this->name << ": process PoseMsg [" << (int)this->networkId
<< "/" << (int)msg->networkId << "]\n"; << "/" << (int)msg->networkId << "]\n";
#endif #endif
Participant* owner = Participant::GetParticipant(msg->networkId);
if (owner == nullptr)
return;
Thing* thing = owner->Get(msg->thingId);
if (thing == nullptr)
return;
if ((msg->poseType & PoseMsg::Pose_Position) != 0)
thing->SetPosition(msg->position);
std::cout << "update position for" << (int)thing->id << std::endl;
} }
void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) { void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
@ -418,16 +449,23 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
<< "/" << (int)msg->thingId << "] "; << "/" << (int)msg->thingId << "] ";
#endif #endif
Thing* thing = sender->Get(msg->thingId); Participant* owner = Participant::GetParticipant(msg->networkId);
if (thing != nullptr) if (owner != nullptr) {
thing->ProcessBinary(msg->data); Thing* thing = owner->Get(msg->thingId);
if (thing != nullptr)
thing->ProcessBinary(msg->data);
#if !defined(NO_STD) #if !defined(NO_STD)
else { else {
std::cout << " unknown thing [" << (int)msg->networkId << "/" #if defined(DEBUG)
<< (int)msg->thingId << "]"; std::cout << " unknown thing [" << (int)msg->networkId << "/"
} << (int)msg->thingId << "]";
std::cout << std::endl;
#endif #endif
}
#if defined(DEBUG)
std::cout << std::endl;
#endif
#endif
}
} }
// Receive // Receive

View File

@ -2,6 +2,7 @@
#include "Messages/BinaryMsg.h" #include "Messages/BinaryMsg.h"
#include "Messages/InvestigateMsg.h" #include "Messages/InvestigateMsg.h"
#include "Messages/DestroyMsg.h"
#include "Messages/ModelUrlMsg.h" #include "Messages/ModelUrlMsg.h"
#include "Messages/NameMsg.h" #include "Messages/NameMsg.h"
#include "Messages/ParticipantMsg.h" #include "Messages/ParticipantMsg.h"
@ -100,6 +101,8 @@ class ParticipantUDP : public Participant {
bool connected = false; bool connected = false;
virtual void Update(unsigned long currentTimeMs = 0) override; virtual void Update(unsigned long currentTimeMs = 0) override;
virtual void UpdateMyThings(unsigned long currentTimeMs);
virtual void UpdateOtherThings(unsigned long currentTimeMs);
void SendThingInfo(Participant* remoteParticipant, Thing* thing); void SendThingInfo(Participant* remoteParticipant, Thing* thing);
void PublishThingInfo(Thing* thing); void PublishThingInfo(Thing* thing);
@ -112,12 +115,12 @@ class ParticipantUDP : public Participant {
unsigned int senderPort); unsigned int senderPort);
void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant); void ReceiveData(unsigned char bufferSize, Participant* remoteParticipant);
#if defined(NO_STD) // #if defined(NO_STD)
unsigned char senderCount = 0; // unsigned char senderCount = 0;
Participant* senders[MAX_SENDER_COUNT]; // Participant* senders[MAX_SENDER_COUNT];
#else // #else
std::list<Participant*> senders; // std::list<Participant*> senders;
#endif // #endif
protected: protected:
unsigned long nextPublishMe = 0; unsigned long nextPublishMe = 0;
@ -126,8 +129,8 @@ class ParticipantUDP : public Participant {
void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort); void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort);
Participant* GetParticipant(const char* ipAddress, int port); //Participant* GetParticipant(const char* ipAddress, int port);
Participant* AddParticipant(const char* ipAddress, int port); // Participant* AddParticipant(const char* ipAddress, int port);
void ReceiveUDP(); void ReceiveUDP();

View File

@ -16,17 +16,31 @@ SiteServer::SiteServer(int port) {
this->ipAddress = "0.0.0.0"; this->ipAddress = "0.0.0.0";
this->port = port; this->port = port;
#if defined(NO_STD)
this->senders[this->senderCount++] = this;
#else
this->senders.push_back(this);
#endif
SetupUDP(port, ipAddress, 0); SetupUDP(port, ipAddress, 0);
}
#if !defined(NO_STD) void SiteServer::UpdateMyThings(unsigned long currentTimeMs) {
// Register<TemperatureSensor>((unsigned char)Thing::Type::TemperatureSensor); for (Thing* thing : this->things) {
#endif if (thing == nullptr)
continue;
thing->Update(currentTimeMs, true);
if (this->isIsolated == false) {
// Send to all other participants
for (Participant* participant : Participant::participants) {
if (participant == nullptr || participant == this)
continue;
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing);
this->Send(participant, poseMsg);
delete poseMsg;
BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing);
this->Send(participant, binaryMsg);
delete binaryMsg;
}
}
}
} }
void SiteServer::Process(Participant* sender, ParticipantMsg* msg) { void SiteServer::Process(Participant* sender, ParticipantMsg* msg) {
@ -46,8 +60,7 @@ void SiteServer::Process(Participant* sender, ThingMsg* msg) {
Thing* thing = sender->Get(msg->thingId); Thing* thing = sender->Get(msg->thingId);
if (thing == nullptr) { if (thing == nullptr) {
// #if defined(NO_STD) // #if defined(NO_STD)
new Thing(sender, (Thing::Type)msg->thingType, new Thing(sender, (Thing::Type)msg->thingType, msg->thingId);
msg->thingId);
// #else // #else
// auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType); // auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
// //Thing* newThing; // //Thing* newThing;

View File

@ -15,18 +15,7 @@ class SiteServer : public ParticipantUDP {
public: public:
SiteServer(int port = 7681); SiteServer(int port = 7681);
// virtual void Update(unsigned long currentTimeMs = 0) override; virtual void UpdateMyThings(unsigned long currentTimeMs) override;
// #if !defined(NO_STD)
// template <typename ThingClass>
// void Register(unsigned char thingType) {
// thingMsgProcessors[thingType] = [](Participant* participant,
// unsigned char networkId,
// unsigned char thingId) {
// return new ThingClass(participant, networkId, thingId);
// };
// };
// #endif
protected: protected:
unsigned long nextPublishMe = 0; unsigned long nextPublishMe = 0;
@ -34,13 +23,6 @@ class SiteServer : public ParticipantUDP {
virtual void Process(Participant* sender, ParticipantMsg* msg) override; virtual void Process(Participant* sender, ParticipantMsg* msg) override;
virtual void Process(Participant* sender, SiteMsg* msg) override; virtual void Process(Participant* sender, SiteMsg* msg) override;
virtual void Process(Participant* sender, ThingMsg* msg) override; virtual void Process(Participant* sender, ThingMsg* msg) override;
// #if !defined(NO_STD)
// using ThingConstructor = std::function<Thing*(Participant* participant,
// unsigned char networkId,
// unsigned char thingId)>;
// std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
// #endif
}; };
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -17,12 +17,6 @@
namespace RoboidControl { namespace RoboidControl {
// LocalParticipant* Thing::CheckHiddenParticipant() {
// if (isolatedParticipant == nullptr)
// isolatedParticipant = new LocalParticipant(0);
// return isolatedParticipant;
// }
Thing::Thing(int thingType) Thing::Thing(int thingType)
: Thing(IsolatedParticipant::Isolated(), thingType) {} : Thing(IsolatedParticipant::Isolated(), thingType) {}
@ -36,14 +30,22 @@ Thing::Thing(Participant* owner, int thingType, unsigned char thingId) {
this->networkId = 0; this->networkId = 0;
this->position = Spherical::zero; this->position = Spherical::zero;
this->positionUpdated = true;
this->orientation = SwingTwist::identity; this->orientation = SwingTwist::identity;
this->orientationUpdated = true;
this->linearVelocity = Spherical::zero; this->linearVelocity = Spherical::zero;
this->angularVelocity = Spherical::zero; this->angularVelocity = Spherical::zero;
// std::cout << "add thing [" << (int)this->id << "] to owner " // std::cout << "add thing [" << (int)this->id << "] to owner "
// << this->owner->ipAddress << ":" << this->owner->port << std::endl; // << this->owner->ipAddress << ":" << this->owner->port <<
this->owner->Add(this, false); // std::endl;
this->owner->Add(this, true);
}
Thing::Thing(Thing* parent, int thingType, unsigned char thingId)
: Thing(parent->owner, thingType, thingId) {
this->parent = parent;
} }
// Thing::Thing(Participant* owner, // Thing::Thing(Participant* owner,
@ -64,6 +66,7 @@ Thing::Thing(Participant* owner, int thingType, unsigned char thingId) {
// } // }
void Thing::Terminate() { void Thing::Terminate() {
this->isTerminated = true;
// Thing::Remove(this); // Thing::Remove(this);
} }
@ -238,8 +241,10 @@ SwingTwist Thing::GetOrientation() {
} }
void Thing::SetLinearVelocity(Spherical linearVelocity) { void Thing::SetLinearVelocity(Spherical linearVelocity) {
this->linearVelocity = linearVelocity; if (this->linearVelocity.distance != linearVelocity.distance) {
this->linearVelocityUpdated = true; this->linearVelocity = linearVelocity;
this->linearVelocityUpdated = true;
}
} }
Spherical Thing::GetLinearVelocity() { Spherical Thing::GetLinearVelocity() {
@ -247,8 +252,10 @@ Spherical Thing::GetLinearVelocity() {
} }
void Thing::SetAngularVelocity(Spherical angularVelocity) { void Thing::SetAngularVelocity(Spherical angularVelocity) {
this->angularVelocity = angularVelocity; if (this->angularVelocity.distance != angularVelocity.distance) {
this->angularVelocityUpdated = true; this->angularVelocity = angularVelocity;
this->angularVelocityUpdated = true;
}
} }
Spherical Thing::GetAngularVelocity() { Spherical Thing::GetAngularVelocity() {

View File

@ -58,6 +58,7 @@ class Thing {
// unsigned char networkId, // unsigned char networkId,
// unsigned char thingId, // unsigned char thingId,
// Type thingType = Type::Undetermined); // Type thingType = Type::Undetermined);
Thing(Thing* parent, int thingType = 0, unsigned char thingId = 0);
/// @brief The participant managing this thing /// @brief The participant managing this thing
Participant* owner; Participant* owner;
@ -173,6 +174,7 @@ class Thing {
public: public:
/// @brief Terminated things are no longer updated /// @brief Terminated things are no longer updated
void Terminate(); void Terminate();
bool isTerminated = false;
/// @brief Sets the location from where the 3D model of this Thing can be /// @brief Sets the location from where the 3D model of this Thing can be
/// loaded from /// loaded from

View File

@ -15,8 +15,8 @@ int TouchSensor::GenerateBinary(char* bytes, unsigned char* ix) {
} }
void TouchSensor::ProcessBinary(char* bytes) { void TouchSensor::ProcessBinary(char* bytes) {
// if (bytes[0] == 1) if (bytes[0] == 1)
// std::cout << this->name << " is Touching something!\n"; std::cout << this->name << " is Touching something!\n";
this->touchedSomething |= (bytes[0] == 1); this->touchedSomething |= (bytes[0] == 1);
} }

View File

@ -7,6 +7,9 @@ namespace RoboidControl {
/// @brief A sensor which can detect touches /// @brief A sensor which can detect touches
class TouchSensor : public Thing { class TouchSensor : public Thing {
public: public:
// Inherit all constructors
using Thing::Thing;
/// @brief Create a touch sensor with isolated participant /// @brief Create a touch sensor with isolated participant
TouchSensor(); TouchSensor();
/// @brief Create a touch sensor /// @brief Create a touch sensor