thing tytpe enum->const

This commit is contained in:
Pascal Serrarens 2025-06-02 12:30:21 +02:00
parent c3ba44d47a
commit 2883f5e0fa
3 changed files with 293 additions and 247 deletions

View File

@ -16,7 +16,7 @@ void Participant::ReplaceLocalParticipant(Participant& newParticipant) {
}
Participant::Participant() {
std::cout << "P\n";
//std::cout << "P\n";
//this->root.name = "Isolated";
this->root = new Thing(this);
this->root->name = "Root";

260
Thing.cpp
View File

@ -16,19 +16,22 @@
#include <list>
#endif
namespace RoboidControl {
namespace RoboidControl
{
#pragma region Init
Thing* Thing::LocalRoot() {
Participant* p = Participant::LocalParticipant;
Thing* localRoot = p->root;
Thing *Thing::LocalRoot()
{
Participant *p = Participant::LocalParticipant;
Thing *localRoot = p->root;
return localRoot;
}
}
// Only use this for root things
Thing::Thing(Participant* owner) {
this->type = Type::Roboid; // should become root
// Only use this for root things
Thing::Thing(Participant *owner)
{
this->type = Type::Root; // should become root
this->position = Spherical::zero;
this->positionUpdated = true;
@ -40,11 +43,12 @@ Thing::Thing(Participant* owner) {
this->angularVelocity = Spherical::zero;
this->owner = owner;
//this->owner->Add(this, true);
std::cout << this->owner->name << ": New root thing " << std::endl;
}
// this->owner->Add(this, true);
//std::cout << this->owner->name << ": New root thing " << std::endl;
}
Thing::Thing(unsigned char thingType, Thing* parent) {
Thing::Thing(unsigned char thingType, Thing *parent)
{
this->type = thingType;
this->position = Spherical::zero;
@ -62,81 +66,94 @@ Thing::Thing(unsigned char thingType, Thing* parent) {
std::cout << this->owner->name << ": New thing for " << parent->name
<< std::endl;
}
}
Thing::~Thing() {
Thing::~Thing()
{
std::cout << "Destroy thing " << this->name << std::endl;
}
}
// Thing Thing::Reconstruct(Participant* owner, unsigned char thingType,
// unsigned char thingId) {
// Thing thing = Thing(owner, thingType);
// thing.id = thingId;
// return thing;
// }
Thing Thing::Reconstruct(Participant *owner, unsigned char thingType, unsigned char thingId)
{
Thing thing = Thing(thingType, owner->root);
thing.id = thingId;
return thing;
}
#pragma endregion Init
void Thing::SetName(const char* name) {
void Thing::SetName(const char *name)
{
this->name = name;
this->nameChanged = true;
}
}
const char* Thing::GetName() const {
const char *Thing::GetName() const
{
return this->name;
}
}
void Thing::SetModel(const char* url) {
void Thing::SetModel(const char *url)
{
this->modelUrl = url;
}
}
#pragma region Hierarchy
void Thing::SetParent(Thing* parent) {
if (parent == nullptr) {
Thing* parentThing = this->parent;
void Thing::SetParent(Thing *parent)
{
if (parent == nullptr)
{
Thing *parentThing = this->parent;
if (parentThing != nullptr)
parentThing->RemoveChild(this);
this->parent = nullptr;
} else
}
else
parent->AddChild(this);
this->hierarchyChanged = true;
}
}
// void Thing::SetParent(Thing* parent) {
// parent->AddChild(this);
// this->hierarchyChanged = true;
// }
// void Thing::SetParent(Thing* parent) {
// parent->AddChild(this);
// this->hierarchyChanged = true;
// }
// const Thing& Thing::GetParent() {
// return *this->parent;
// }
// const Thing& Thing::GetParent() {
// return *this->parent;
// }
bool Thing::IsRoot() const {
bool Thing::IsRoot() const
{
return this == LocalRoot() || this->parent == nullptr; //&Thing::Root;
}
}
// void Thing::SetParent(Thing* root, const char* name) {
// Thing* thing = root->FindChild(name);
// if (thing != nullptr)
// this->SetParent(thing);
// }
// void Thing::SetParent(Thing* root, const char* name) {
// Thing* thing = root->FindChild(name);
// if (thing != nullptr)
// this->SetParent(thing);
// }
Thing* Thing::GetParent() {
Thing *Thing::GetParent()
{
return this->parent;
}
}
Thing* Thing::GetChildByIndex(unsigned char ix) {
Thing *Thing::GetChildByIndex(unsigned char ix)
{
return this->children[ix];
}
}
void Thing::AddChild(Thing* child) {
void Thing::AddChild(Thing *child)
{
unsigned char newChildCount = this->childCount + 1;
Thing** newChildren = new Thing*[newChildCount];
Thing **newChildren = new Thing *[newChildCount];
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
{
newChildren[childIx] = this->children[childIx];
if (this->children[childIx] == child) {
if (this->children[childIx] == child)
{
// child is already present, stop copying do not update the children
delete[] newChildren;
return;
@ -151,20 +168,25 @@ void Thing::AddChild(Thing* child) {
this->children = newChildren;
this->childCount = newChildCount;
}
}
Thing* Thing::RemoveChild(Thing* child) {
Thing *Thing::RemoveChild(Thing *child)
{
unsigned char newChildCount = this->childCount - 1;
Thing** newChildren = new Thing*[newChildCount];
Thing **newChildren = new Thing *[newChildCount];
unsigned char newChildIx = 0;
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
if (this->children[childIx] != child) {
if (newChildIx == newChildCount) { // We did not find the child
for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
{
if (this->children[childIx] != child)
{
if (newChildIx == newChildCount)
{ // We did not find the child
// stop copying and return nothing
delete[] newChildren;
return nullptr;
} else
}
else
newChildren[newChildIx++] = this->children[childIx];
}
}
@ -176,89 +198,105 @@ Thing* Thing::RemoveChild(Thing* child) {
this->childCount = newChildCount;
return child;
}
}
Thing* Thing::GetChild(unsigned char id, bool recurse) {
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
Thing* child = this->children[childIx];
Thing *Thing::GetChild(unsigned char id, bool recurse)
{
for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
{
Thing *child = this->children[childIx];
if (child == nullptr)
continue;
if (child->id == id)
return child;
if (recurse) {
Thing* foundChild = child->GetChild(id, recurse);
if (recurse)
{
Thing *foundChild = child->GetChild(id, recurse);
if (foundChild != nullptr)
return foundChild;
}
}
return nullptr;
}
}
Thing* Thing::FindChild(const char* name, bool recurse) {
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
Thing* child = this->children[childIx];
Thing *Thing::FindChild(const char *name, bool recurse)
{
for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
{
Thing *child = this->children[childIx];
if (child == nullptr || child->name == nullptr)
continue;
if (strcmp(child->name, name) == 0)
return child;
Thing* foundChild = child->FindChild(name);
Thing *foundChild = child->FindChild(name);
if (foundChild != nullptr)
return foundChild;
}
return nullptr;
}
}
#pragma endregion Hierarchy
#pragma region Pose
void Thing::SetPosition(Spherical position) {
void Thing::SetPosition(Spherical position)
{
this->position = position;
this->positionUpdated = true;
}
Spherical Thing::GetPosition() {
}
Spherical Thing::GetPosition()
{
return this->position;
}
}
void Thing::SetOrientation(SwingTwist orientation) {
void Thing::SetOrientation(SwingTwist orientation)
{
this->orientation = orientation;
this->orientationUpdated = true;
}
}
SwingTwist Thing::GetOrientation() {
SwingTwist Thing::GetOrientation()
{
return this->orientation;
}
}
void Thing::SetLinearVelocity(Spherical linearVelocity) {
if (this->linearVelocity.distance != linearVelocity.distance) {
void Thing::SetLinearVelocity(Spherical linearVelocity)
{
if (this->linearVelocity.distance != linearVelocity.distance)
{
this->linearVelocity = linearVelocity;
this->linearVelocityUpdated = true;
}
}
}
Spherical Thing::GetLinearVelocity() {
Spherical Thing::GetLinearVelocity()
{
return this->linearVelocity;
}
}
void Thing::SetAngularVelocity(Spherical angularVelocity) {
if (this->angularVelocity.distance != angularVelocity.distance) {
void Thing::SetAngularVelocity(Spherical angularVelocity)
{
if (this->angularVelocity.distance != angularVelocity.distance)
{
this->angularVelocity = angularVelocity;
this->angularVelocityUpdated = true;
}
}
}
Spherical Thing::GetAngularVelocity() {
Spherical Thing::GetAngularVelocity()
{
return this->angularVelocity;
}
}
#pragma endregion Pose
#pragma region Update
unsigned long Thing::GetTimeMs() {
unsigned long Thing::GetTimeMs()
{
#if defined(ARDUINO)
unsigned long ms = millis();
return ms;
@ -268,15 +306,16 @@ unsigned long Thing::GetTimeMs() {
now.time_since_epoch());
return static_cast<unsigned long>(ms.count());
#endif
}
}
// void Thing::Update(bool recursive) {
// Update(GetTimeMs(), recursive);
// }
// void Thing::Update(bool recursive) {
// Update(GetTimeMs(), recursive);
// }
void Thing::PrepareForUpdate() {}
void Thing::PrepareForUpdate() {}
void Thing::Update(bool recursive) {
void Thing::Update(bool recursive)
{
// if (this->positionUpdated || this->orientationUpdated)
// OnPoseChanged callback
this->positionUpdated = false;
@ -286,30 +325,35 @@ void Thing::Update(bool recursive) {
this->hierarchyChanged = false;
this->nameChanged = false;
if (recursive) {
if (recursive)
{
// std::cout << "# children: " << (int)this->childCount << std::endl;
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
Thing* child = this->children[childIx];
for (unsigned char childIx = 0; childIx < this->childCount; childIx++)
{
Thing *child = this->children[childIx];
if (child == nullptr)
continue;
child->Update(recursive);
}
}
}
}
void Thing::UpdateThings() {
void Thing::UpdateThings()
{
IsolatedParticipant::Isolated()->Update();
}
}
#pragma endregion Update
int Thing::GenerateBinary(char* buffer, unsigned char* ix) {
int Thing::GenerateBinary(char *buffer, unsigned char *ix)
{
(void)buffer;
(void)ix;
return 0;
}
void Thing::ProcessBinary(char* bytes) {
}
void Thing::ProcessBinary(char *bytes)
{
(void)bytes;
};
};
} // namespace RoboidControl

36
Thing.h
View File

@ -20,24 +20,26 @@ class ParticipantUDP;
class Thing {
public:
/// @brief Predefined thing types
enum Type : unsigned char {
Undetermined,
// Sensor,
Switch,
DistanceSensor,
DirectionalSensor,
TemperatureSensor,
TouchSensor,
// Motor,
ControlledMotor,
UncontrolledMotor,
Servo,
IncrementalEncoder,
struct Type {
static const unsigned char Undetermined = 0x00;
// Sensor
static const unsigned char Switch = 0x01;
static const unsigned char DistanceSensor = 0x02;
static const unsigned char DirectionalSensor = 0x03;
static const unsigned char TemperatureSensor = 0x04;
static const unsigned char TouchSensor = 0x05;
// Motor
static const unsigned char ControlledMotor = 0x06;
static const unsigned char UncontrolledMotor = 0x07;
static const unsigned char Servo = 0x08;
static const unsigned char IncrementalEncoder = 0x19;
// Other
Roboid,
Humanoid,
ExternalSensor,
DifferentialDrive
static const unsigned char Root = 0x10;
static const unsigned char Roboid = 0x09;
static const unsigned char Humanoid = 0x0A;
static const unsigned char ExternalSensor = 0x08;
static const unsigned char Animator = 0x0C;
static const unsigned char DifferentialDrive = 0x0D;
};
#pragma region Init