Steps towards using ContolCore

This commit is contained in:
Pascal Serrarens 2024-12-13 16:58:03 +01:00
parent 7e5876e2f8
commit 55b63f7c3e
14 changed files with 167 additions and 67 deletions

View File

@ -1,6 +0,0 @@
#include "Client.h"
bool Passer::Control::ControlClient::SendMsg(unsigned char *buffer,
unsigned char bufferSize) {
return false;
}

View File

@ -1,11 +0,0 @@
#pragma once
namespace Passer::Control {
class ControlClient {
public:
unsigned char *buffer;
bool SendMsg(unsigned char *buffer, unsigned char bufferSize);
};
} // namespace Passer::Control

38
ControlCore/CoreThing.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "CoreThing.h"
CoreThing::CoreThing(
// Participant *client,
unsigned char networkId, unsigned char thingId, unsigned char thingType) {
// this->client = client;
this->id = thingId;
this->type = thingType;
this->networkId = networkId;
this->Init();
// CoreThings::Add(this);
}
void CoreThing::Init() {}
// CoreThing *CoreThings::allThings[256] = {nullptr};
// CoreThing *CoreThings::Get(unsigned char networkId, unsigned char thingId) {
// for (unsigned char ix = 0; ix < 256; ix++) {
// CoreThing *thing = allThings[ix];
// if (thing == nullptr)
// continue;
// if (thing->networkId == networkId && thing->id == thingId)
// return thing;
// }
// return nullptr;
// }
// bool CoreThings::Add(CoreThing *newThing) {
// for (unsigned char ix = 0; ix < 256; ix++) {
// CoreThing *thing = allThings[ix];
// if (thing == nullptr) {
// allThings[ix] = newThing;
// return true;
// }
// }
// return false;
// }

41
ControlCore/CoreThing.h Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include "Participant.h"
namespace Passer::Control {
class CoreThing {
public:
// Participant *client;
unsigned char networkId;
unsigned char id;
// CoreThing *parent;
unsigned char type;
const char *name;
const char *modelUrl;
// protected Sensor sensor;
public:
CoreThing(
// Participant *client,
unsigned char networkId, unsigned char thingId,
unsigned char thingType = 0);
protected:
virtual void Init();
// Things
};
// class CoreThings {
// friend class CoreThing;
// public:
// static CoreThing *allThings[];
// public:
// static CoreThing *Get(unsigned char networkId, unsigned char thingId);
// static bool Add(CoreThing *thing);
// };
} // namespace Passer::Control

View File

@ -1,19 +1,15 @@
#include "Messages.h"
#include "LowLevelMessages.h"
#include "string.h"
#pragma region IMessage
unsigned char IMessage::Serialize(unsigned char *buffer) { return 0; }
bool IMessage::SendMsg(ControlClient client, IMessage msg,
unsigned char bufferSize) {
return SendMsg(client, client.buffer, msg.Serialize(client.buffer));
}
bool IMessage::SendMsg(ControlClient client, unsigned char *buffer,
unsigned char bufferSize) {
return false; // client.SendMsg(buffer, bufferSize);
bool IMessage::SendMsg(Participant *client, IMessage msg) {
// return SendMsg(client, client.buffer, );nameLength
return client->SendBuffer(msg.Serialize(client->buffer));
}
#pragma endregion
@ -33,10 +29,10 @@ unsigned char InvestigateMsg::Serialize(unsigned char *buffer) {
return ix;
}
bool InvestigateMsg::Send(ControlClient client, unsigned char networkId,
bool InvestigateMsg::Send(Participant *client, unsigned char networkId,
unsigned char thingId) {
InvestigateMsg msg = InvestigateMsg(networkId, thingId);
return SendMsg(client, msg, InvestigateMsg::length);
return SendMsg(client, msg);
}
// Investigate
@ -44,10 +40,8 @@ bool InvestigateMsg::Send(ControlClient client, unsigned char networkId,
#pragma region Thing
Passer::Control::ThingMsg::ThingMsg(unsigned char networkId,
unsigned char thingId,
unsigned char thingType,
unsigned char parentId) {
ThingMsg::ThingMsg(unsigned char networkId, unsigned char thingId,
unsigned char thingType, unsigned char parentId) {
this->networkId = networkId;
this->thingId = thingId;
this->thingType = thingType;
@ -64,13 +58,11 @@ unsigned char ThingMsg::Serialize(unsigned char *buffer) {
return ix;
}
bool Passer::Control::ThingMsg::Send(ControlClient client,
unsigned char networkId,
unsigned char thingId,
unsigned char thingType,
unsigned char parentId) {
bool ThingMsg::Send(Participant *client, unsigned char networkId,
unsigned char thingId, unsigned char thingType,
unsigned char parentId) {
ThingMsg msg = ThingMsg(networkId, thingId, thingType, parentId);
return SendMsg(client, msg, ThingMsg::length);
return SendMsg(client, msg);
}
// Thing
@ -79,24 +71,37 @@ bool Passer::Control::ThingMsg::Send(ControlClient client,
#pragma region Name
NameMsg::NameMsg(unsigned char networkId, unsigned char thingId,
unsigned char nameLength, const char *name) {
const char *name, unsigned char nameLength) {
this->networkId = networkId;
this->thingId = thingId;
this->nameLength = nameLength;
this->name = name;
this->nameLength = nameLength;
}
unsigned char Passer::Control::NameMsg::Serialize(unsigned char *buffer) {
unsigned char NameMsg::Serialize(unsigned char *buffer) {
unsigned char ix = 0;
buffer[ix++] = this->id;
buffer[ix++] = this->networkId;
buffer[ix++] = this->thingId;
buffer[ix++] = this->nameLength;
for (int nameIx = 0; nameIx < this->nameLength; nameIx++)
buffer[ix++] = name[nameIx];
buffer[ix++] = this->name[nameIx];
return ix;
}
bool NameMsg::Send(Participant *client, CoreThing *thing,
unsigned char nameLength) {
if (thing->name == nullptr)
return true; // nothing sent, but still a success!
if (strlen(thing->name) == 0)
return true;
NameMsg msg = NameMsg(thing->networkId, thing->id, thing->name, nameLength);
return SendMsg(client, msg);
}
// Name
#pragma endregion

View File

@ -1,19 +1,15 @@
#pragma once
#include "../float16/float16.h"
#include "Client.h"
#include "CoreThing.h"
#include "Participant.h"
namespace Passer::Control {
class IMessage {
public:
// static unsigned char buffer[256];
virtual unsigned char Serialize(unsigned char *buffer);
static bool SendMsg(ControlClient client, IMessage msg,
unsigned char bufferSize);
static bool SendMsg(ControlClient client, unsigned char *buffer,
unsigned char bufferSize);
static bool SendMsg(Participant *client, IMessage msg);
};
class InvestigateMsg : public IMessage {
@ -27,7 +23,7 @@ public:
virtual unsigned char Serialize(unsigned char *buffer) override;
static bool Send(ControlClient client, unsigned char networkId,
static bool Send(Participant *client, unsigned char networkId,
unsigned char thingId);
};
@ -45,7 +41,7 @@ public:
virtual unsigned char Serialize(unsigned char *buffer) override;
static bool Send(ControlClient client, unsigned char networkId,
static bool Send(Participant *client, unsigned char networkId,
unsigned char thingId, unsigned char thingType,
unsigned char parentId);
};
@ -59,10 +55,13 @@ public:
unsigned char nameLength;
const char *name;
NameMsg(unsigned char networkId, unsigned char thingId,
unsigned char nameLength, const char *name);
NameMsg(unsigned char networkId, unsigned char thingId, const char *name,
unsigned char nameLength);
virtual unsigned char Serialize(unsigned char *buffer) override;
static bool Send(Participant *client, CoreThing *thing,
unsigned char nameLength);
};
class ModelUrlMsg : public IMessage {

View File

@ -0,0 +1,10 @@
#include "Participant.h"
// bool Passer::Control::ControlClient::SendMsg(unsigned char *buffer,
// unsigned char bufferSize) {
// return false;
// }
bool Participant::SendBuffer(unsigned char bufferSize) { return false; }
bool Participant::PublishBuffer(unsigned char bufferSize) { return false; }

16
ControlCore/Participant.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
namespace Passer::Control {
class Participant {
public:
// unsigned char *buffer;
// bool SendMsg(unsigned char *buffer, unsigned char bufferSize);
unsigned char buffer[1024];
virtual bool SendBuffer(unsigned char bufferSize);
virtual bool PublishBuffer(unsigned char bufferSize);
};
} // namespace Passer::Control
using namespace Passer::Control;

View File

@ -166,9 +166,11 @@ void NetworkSync::SendName(Thing *thing) {
// SendBuffer(ix);
Passer::Control::NameMsg msg =
Passer::Control::NameMsg(this->networkId, thing->id, len, thing->name);
Passer::Control::NameMsg(this->networkId, thing->id, thing->name, len);
SendBuffer(msg.Serialize(this->buffer));
// Passer::Control::NameMsg::Send(this, thing);
#ifdef RC_DEBUG
SERIALPORT.printf("Sent Name [%d/%d] %s\n", networkId, buffer[1],
thing->name);
@ -406,9 +408,10 @@ void NetworkSync::SendInt(const int x) {
// Low-level functions
void NetworkSync::SendBuffer(unsigned char bufferSize) {}
// bool NetworkSync::SendBuffer(unsigned char bufferSize) { return false; }
void NetworkSync::PublishBuffer(unsigned char bufferSize) {}
// bool NetworkSync::PublishBuffer(unsigned char bufferSize) { return false; }
void NetworkSync::SendCBuffer(unsigned char bufferSize, unsigned char *buffer) {
}
// void NetworkSync::SendCBuffer(unsigned char bufferSize, unsigned char
// *buffer) {
//}

View File

@ -1,6 +1,7 @@
#pragma once
#include "ControlCore/Messages.h"
#include "ControlCore/Participant.h"
#include "NetworkPerception.h"
#include "Perception.h"
#include "Roboid.h"
@ -10,7 +11,7 @@ namespace Passer {
namespace RoboidControl {
/// @brief Interface for synchronizaing state between clients across a network
class NetworkSync {
class NetworkSync : public Participant {
public:
NetworkSync() {};
NetworkSync(Roboid *roboid);
@ -97,11 +98,11 @@ protected:
// UInt8 parentId,
// InterestingThing* object);
unsigned char buffer[256];
virtual void SendBuffer(unsigned char bufferSize);
virtual void PublishBuffer(unsigned char bufferSize);
// unsigned char buffer[256];
// virtual bool SendBuffer(unsigned char bufferSize);
// virtual bool PublishBuffer(unsigned char bufferSize);
virtual void SendCBuffer(unsigned char bufferSize, unsigned char *buffer);
// virtual void SendCBuffer(unsigned char bufferSize, unsigned char *buffer);
void PublishClient();
};

View File

@ -15,7 +15,7 @@ Roboid::Roboid() : Thing(0) {
#ifdef RC_DEBUG
Serial.begin(115200);
#endif
this->type = RoboidType;
this->type = (unsigned char)RoboidType;
this->perception = new Perception();
this->perception->roboid = this;
this->propulsion = new Propulsion();

View File

@ -3,7 +3,7 @@
#include "Roboid.h"
Sensor::Sensor() : Thing() { // for now, id should be set properly later
this->type = Thing::SensorType;
this->type = (unsigned char)Thing::SensorType;
}
void Sensor::SetParent(Thing *parent) {

View File

@ -7,6 +7,7 @@ using namespace Passer::RoboidControl;
int Thing::lastThingId = 1;
Thing::Thing() {
//: CoreThing(0, lastThingId++, (unsigned char)Type::Undetermined) {
this->id = lastThingId++;
this->type = (unsigned int)Type::Undetermined;
this->childCount = 0;
@ -14,8 +15,10 @@ Thing::Thing() {
this->children = nullptr;
}
Thing::Thing(unsigned char id) : id(id) {
// this->position = SphericalOf<signed short>::zero;
Thing::Thing(unsigned char id) {
//: CoreThing(0, id, (unsigned char)Type::Undetermined) {
this->id = id;
// this->position = SphericalOf<signed short>::zero;
this->type = (unsigned int)Type::Undetermined;
this->childCount = 0;
this->parent = nullptr;

View File

@ -1,5 +1,6 @@
#pragma once
#include "ControlCore/CoreThing.h"
#include "LinearAlgebra/AngleAxis.h"
#include "LinearAlgebra/Quaternion.h"
#include "LinearAlgebra/Spherical.h"
@ -9,7 +10,7 @@ namespace Passer {
namespace RoboidControl {
/// @brief A thing is a functional component on a robot
class Thing {
class Thing { //: public CoreThing {
public:
Thing();
/// @brief Default constructor for a Thing