Added temp sensor support

This commit is contained in:
Pascal Serrarens 2025-01-11 12:04:34 +01:00
parent f591d34d97
commit c092d029fc
12 changed files with 91 additions and 31 deletions

View File

@ -27,7 +27,7 @@ else()
. .
LinearAlgebra LinearAlgebra
) )
file(GLOB srcs *.cpp) file(GLOB srcs *.cpp Sensors/*.cpp)
add_library(ControlCore STATIC ${srcs}) add_library(ControlCore STATIC ${srcs})
enable_testing() enable_testing()

View File

@ -1,3 +1,5 @@
#pragma once
#include "Messages.h" #include "Messages.h"
namespace Passer { namespace Passer {

View File

@ -112,7 +112,7 @@ CustomMsg::CustomMsg(char *buffer) {
unsigned char ix = 1; unsigned char ix = 1;
this->networkId = buffer[ix++]; this->networkId = buffer[ix++];
this->thingId = buffer[ix++]; this->thingId = buffer[ix++];
this->data = this->bytes =
buffer + ix; // This is only valid because the code ensures the the msg buffer + ix; // This is only valid because the code ensures the the msg
// lifetime is shorter than the buffer lifetime... // lifetime is shorter than the buffer lifetime...
} }

View File

@ -71,8 +71,8 @@ public:
unsigned char thingId; unsigned char thingId;
Thing *thing; Thing *thing;
unsigned char dataSize; unsigned char bytesSize;
char *data; char *bytes;
CustomMsg(char *buffer); CustomMsg(char *buffer);
CustomMsg(unsigned char networkId, Thing *thing); CustomMsg(unsigned char networkId, Thing *thing);

View File

@ -174,20 +174,24 @@ void Participant::ReceiveData(unsigned char bufferSize,
delete msg; delete msg;
} break; } break;
case InvestigateMsg::id: { case InvestigateMsg::id: {
InvestigateMsg msg = InvestigateMsg(this->buffer); InvestigateMsg *msg = new InvestigateMsg(this->buffer);
ProcessInvestigateMsg(msg); Process(msg);
delete msg;
} break; } break;
case ThingMsg::id: { case ThingMsg::id: {
ThingMsg msg = ThingMsg(this->buffer); ThingMsg *msg = new ThingMsg(this->buffer);
ProcessThingMsg(msg); Process(msg);
delete msg;
} break; } break;
case PoseMsg::id: { case PoseMsg::id: {
PoseMsg msg = PoseMsg(this->buffer); PoseMsg *msg = new PoseMsg(this->buffer);
ProcessPoseMsg(msg); Process(msg);
delete msg;
} break; } break;
case CustomMsg::id: { case CustomMsg::id: {
CustomMsg msg = CustomMsg(this->buffer); CustomMsg *msg = new CustomMsg(this->buffer);
ProcessCustomMsg(msg); Process(msg);
delete msg;
} break; } break;
}; };
} }
@ -213,13 +217,17 @@ void Participant::Process(Participant *sender, NetworkIdMsg *msg) {
} }
} }
void Participant::ProcessInvestigateMsg(InvestigateMsg msg) {} void Participant::Process(InvestigateMsg *msg) {}
void Participant::ProcessThingMsg(ThingMsg msg) {} void Participant::Process(ThingMsg *msg) {}
void Passer::Control::Participant::ProcessPoseMsg(PoseMsg msg) {} void Participant::Process(PoseMsg *msg) {}
void Participant::ProcessCustomMsg(CustomMsg msg) {} void Participant::Process(CustomMsg *msg) {
Thing *thing = Thing::Get(msg->networkId, msg->thingId);
if (thing != nullptr)
thing->ProcessBytes(msg->bytes);
}
// Receive // Receive
#pragma endregion #pragma endregion

View File

@ -85,10 +85,10 @@ protected:
virtual void Process(Participant *sender, ClientMsg *msg); virtual void Process(Participant *sender, ClientMsg *msg);
virtual void Process(Participant *sender, NetworkIdMsg *msg); virtual void Process(Participant *sender, NetworkIdMsg *msg);
virtual void ProcessInvestigateMsg(InvestigateMsg msg); virtual void Process(InvestigateMsg *msg);
virtual void ProcessThingMsg(ThingMsg msg); virtual void Process(ThingMsg *msg);
virtual void ProcessPoseMsg(PoseMsg msg); virtual void Process(PoseMsg *msg);
virtual void ProcessCustomMsg(CustomMsg msg); virtual void Process(CustomMsg *msg);
}; };
} // namespace Control } // namespace Control

View File

@ -5,7 +5,11 @@
namespace Passer { namespace Passer {
namespace Control { namespace Control {
TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {} // TemperatureSensor::TemperatureSensor() : Thing(Type::TemperatureSensor) {}
TemperatureSensor::TemperatureSensor(unsigned char networkId,
unsigned char thingId)
: Thing(nullptr, networkId, thingId, Type::TemperatureSensor) {}
void TemperatureSensor::SetTemperature(float temp) { this->temp = temp; } void TemperatureSensor::SetTemperature(float temp) { this->temp = temp; }

View File

@ -7,7 +7,7 @@ namespace Control {
class TemperatureSensor : public Thing { class TemperatureSensor : public Thing {
public: public:
TemperatureSensor(); TemperatureSensor(unsigned char networkId, unsigned char thingId);
virtual void SetTemperature(float temp); virtual void SetTemperature(float temp);

View File

@ -1,6 +1,11 @@
#include "SiteServer.h" #include "SiteServer.h"
Passer::Control::SiteServer::SiteServer(int port) { #include "Sensors/TemperatureSensor.h"
namespace Passer {
namespace Control {
SiteServer::SiteServer(int port) {
this->name = "Site Server"; this->name = "Site Server";
this->ipAddress = "0.0.0.0"; this->ipAddress = "0.0.0.0";
@ -11,21 +16,37 @@ Passer::Control::SiteServer::SiteServer(int port) {
SetupUDP(port, ipAddress, 0); SetupUDP(port, ipAddress, 0);
} }
void Passer::Control::SiteServer::Update(unsigned long currentTimeMs) { void SiteServer::Update(unsigned long currentTimeMs) {
this->ReceiveUDP(); this->ReceiveUDP();
Thing::UpdateAll(currentTimeMs); Thing::UpdateAll(currentTimeMs);
} }
void Passer::Control::SiteServer::Process(Participant *sender, ClientMsg *msg) { void SiteServer::Process(Participant *sender, ClientMsg *msg) {
if (msg->networkId == 0) { if (msg->networkId == 0) {
std::cout << this->name << " received New Client -> " std::cout << this->name << " received New Client -> " << sender->ipAddress
<< sender->ipAddress << " " << " " << (int)sender->networkId << "\n";
<< (int)sender->networkId << "\n"; NetworkIdMsg *msg = new NetworkIdMsg(sender->networkId);
NetworkIdMsg* msg = new NetworkIdMsg(sender->networkId);
sender->Send(msg); sender->Send(msg);
delete msg; delete msg;
} }
} }
void Passer::Control::SiteServer::Process(Participant *sender, void SiteServer::Process(Participant *sender, NetworkIdMsg *msg) {}
NetworkIdMsg *msg) {}
void SiteServer::Process(ThingMsg *msg) {
Thing *thing = Thing::Get(msg->networkId, msg->thingId);
if (thing == nullptr) {
switch ((Thing::Type)msg->thingType) {
case Thing::Type::TemperatureSensor:
new TemperatureSensor(msg->networkId, msg->thingId);
break;
default:
new Thing(this, msg->networkId, msg->thingId,
(Thing::Type)msg->thingType);
break;
}
}
}
} // namespace Control
} // namespace Passer

View File

@ -17,6 +17,7 @@ protected:
virtual void Process(Participant *sender, ClientMsg *msg) override; virtual void Process(Participant *sender, ClientMsg *msg) override;
virtual void Process(Participant *sender, NetworkIdMsg *msg) override; virtual void Process(Participant *sender, NetworkIdMsg *msg) override;
virtual void Process(ThingMsg *msg) override;
}; };
} // namespace Control } // namespace Control

View File

@ -28,6 +28,26 @@ Thing::Thing(unsigned char thingType) {
this->angularVelocity = Spherical16::zero; this->angularVelocity = Spherical16::zero;
} }
Passer::Control::Thing::Thing(Participant *participant, unsigned char networkId,
unsigned char thingId, Type thingType) {
// no participant reference yet..
this->networkId = networkId;
this->id = thingId;
this->type = (unsigned char)thingType;
this->Init();
thingId = Thing::Add(this);
if (thingId < 0) {
std::cout << "ERROR: Thing store is full\n";
this->id = 0; // what to do when we cannot store any more things?
} else
this->id = thingId;
this->linearVelocity = Spherical16::zero;
this->angularVelocity = Spherical16::zero;
}
void Thing::Terminate() { Thing::Remove(this); } void Thing::Terminate() { Thing::Remove(this); }
void Thing::Init() {} void Thing::Init() {}

View File

@ -7,6 +7,8 @@
namespace Passer { namespace Passer {
namespace Control { namespace Control {
class Participant;
#define THING_STORE_SIZE 256 #define THING_STORE_SIZE 256
// IMPORTANT: values higher than 256 will need to change the Thing::id type // IMPORTANT: values higher than 256 will need to change the Thing::id type
// to 16-bit or higher, breaking the networking protocol! // to 16-bit or higher, breaking the networking protocol!
@ -39,6 +41,8 @@ public:
Thing(Type thingType = Type::Undetermined); Thing(Type thingType = Type::Undetermined);
Thing(unsigned char thingType); Thing(unsigned char thingType);
Thing(Participant *participant, unsigned char networkId,
unsigned char thingId, Type thingType = Type::Undetermined);
Thing *FindThing(const char *name); Thing *FindThing(const char *name);
// Thing *FindChild(unsigned char id); // Thing *FindChild(unsigned char id);