More refactoring to get rid of pointers

This commit is contained in:
Pascal Serrarens 2025-05-19 15:58:35 +02:00
parent da8831a968
commit 1b5fef15e6
27 changed files with 181 additions and 266 deletions

View File

@ -7,41 +7,31 @@ namespace Arduino {
#pragma region DRV8833 #pragma region DRV8833
DRV8833::DRV8833(Configuration config, Participant* participant) DRV8833::DRV8833(Configuration config, Thing& parent) : Thing(parent) {
: Thing(participant) { this->pinStandby = config.standby;
this->pinStandby = pinStandby;
if (pinStandby != 255) if (pinStandby != 255)
pinMode(pinStandby, OUTPUT); pinMode(pinStandby, OUTPUT);
this->motorA = new DRV8833Motor(this, config.AIn1, config.AIn2); this->motorA = new DRV8833Motor(*this, config.AIn1, config.AIn2);
this->motorA->SetName("Motor A"); this->motorA->SetName("Motor A");
this->motorB = new DRV8833Motor(this, config.BIn1, config.BIn2); this->motorB = new DRV8833Motor(*this, config.BIn1, config.BIn2);
this->motorB->SetName("Motor B"); this->motorB->SetName("Motor B");
} }
DRV8833::DRV8833(Configuration config, Thing* parent)
: DRV8833(config, parent->owner) {
this->SetParent(parent);
}
#pragma endregion DRV8833 #pragma endregion DRV8833
#pragma region Differential drive #pragma region Differential drive
DRV8833::DifferentialDrive::DifferentialDrive(DRV8833::Configuration config, DRV8833::DifferentialDrive::DifferentialDrive(DRV8833::Configuration config,
Participant* owner) Thing& parent)
: RoboidControl::DifferentialDrive(owner), drv8833(config, owner) { : RoboidControl::DifferentialDrive(parent), drv8833(config) {
this->leftWheel = this->drv8833.motorA; this->leftWheel = this->drv8833.motorA;
this->rightWheel = this->drv8833.motorB; this->rightWheel = this->drv8833.motorB;
} }
DRV8833::DifferentialDrive::DifferentialDrive(DRV8833::Configuration config,
Thing& parent)
: RoboidControl::DifferentialDrive(parent), drv8833(config) {}
void DRV8833::DifferentialDrive::Update(bool recurse) { void DRV8833::DifferentialDrive::Update(bool recurse) {
RoboidControl::DifferentialDrive::Update(recurse); RoboidControl::DifferentialDrive::Update(recurse);
this->drv8833.Update(recurse); this->drv8833.Update(false);
} }
#pragma endregion Differential drive #pragma endregion Differential drive
@ -52,11 +42,11 @@ void DRV8833::DifferentialDrive::Update(bool recurse) {
uint8_t DRV8833Motor::nextAvailablePwmChannel = 0; uint8_t DRV8833Motor::nextAvailablePwmChannel = 0;
#endif #endif
DRV8833Motor::DRV8833Motor(DRV8833* driver, DRV8833Motor::DRV8833Motor(DRV8833& driver,
unsigned char pinIn1, unsigned char pinIn1,
unsigned char pinIn2, unsigned char pinIn2,
bool reverse) bool reverse)
: Motor(driver->owner) { : Motor() {
this->SetParent(driver); this->SetParent(driver);
this->pinIn1 = pinIn1; this->pinIn1 = pinIn1;

View File

@ -18,11 +18,12 @@ class DRV8833 : public Thing {
int AIn2; int AIn2;
int BIn1; int BIn1;
int BIn2; int BIn2;
int standby = 255;
}; };
/// @brief Setup a DRV8833 motor controller /// @brief Setup a DRV8833 motor controller
DRV8833(Configuration config, Participant* owner = nullptr); // DRV8833(Configuration config, Participant* owner = nullptr);
DRV8833(Configuration config, Thing* parent); DRV8833(Configuration config, Thing& parent = Thing::LocalRoot());
DRV8833Motor* motorA = nullptr; DRV8833Motor* motorA = nullptr;
DRV8833Motor* motorB = nullptr; DRV8833Motor* motorB = nullptr;
@ -38,9 +39,9 @@ class DRV8833 : public Thing {
class DRV8833::DifferentialDrive : public RoboidControl::DifferentialDrive { class DRV8833::DifferentialDrive : public RoboidControl::DifferentialDrive {
public: public:
DifferentialDrive(DRV8833::Configuration config, // DifferentialDrive(DRV8833::Configuration config,
Participant* participant = nullptr); // Participant* participant = nullptr);
DifferentialDrive(DRV8833::Configuration config, Thing& parent); DifferentialDrive(DRV8833::Configuration config, Thing& parent = Thing::LocalRoot());
virtual void Update(bool recurse = false) override; virtual void Update(bool recurse = false) override;
@ -59,7 +60,7 @@ class DRV8833Motor : public Motor {
/// @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(DRV8833* driver, DRV8833Motor(DRV8833& driver,
unsigned char pinIn1, unsigned char pinIn1,
unsigned char pinIn2, unsigned char pinIn2,
bool reverse = false); bool reverse = false);
@ -81,7 +82,7 @@ class DRV8833Motor : public Motor {
#endif #endif
}; };
#pragma endregion Moto #pragma endregion Motor
} // namespace Arduino } // namespace Arduino
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -7,28 +7,15 @@ namespace Arduino {
#pragma region Digital input #pragma region Digital input
DigitalInput::DigitalInput(Participant* participant, unsigned char pin)
: Thing(participant) {
this->pin = pin;
pinMode(this->pin, INPUT);
}
DigitalInput::DigitalInput(unsigned char pin, Thing& parent) : Thing(parent) { DigitalInput::DigitalInput(unsigned char pin, Thing& parent) : Thing(parent) {
this->pin = pin; this->pin = pin;
pinMode(this->pin, INPUT); pinMode(this->pin, INPUT);
std::cout << "digital input start\n";
} }
// DigitalInput::DigitalInput(Thing* parent, unsigned char pin) : Thing(parent) {
// this->pin = pin;
// pinMode(this->pin, INPUT);
// }
void DigitalInput::Update(bool recursive) { void DigitalInput::Update(bool recursive) {
this->isHigh = digitalRead(this->pin); this->isHigh = digitalRead(this->pin);
this->isLow = !this->isHigh; this->isLow = !this->isHigh;
// std::cout << "DigitalINput pin " << (int)this->pin << ": " <<
Thing::Update(recursive); Thing::Update(recursive);
} }
@ -55,13 +42,16 @@ DigitalInput::RelativeEncoder::RelativeEncoder(Configuration config,
Thing& parent) Thing& parent)
: RoboidControl::RelativeEncoder(parent), : RoboidControl::RelativeEncoder(parent),
digitalInput(config.pin, parent), digitalInput(config.pin, parent),
pulsesPerRevolution(config.pulsesPerRevolution) { pulsesPerRevolution(config.pulsesPerRevolution) {}
void DigitalInput::RelativeEncoder::Start() {
// We support up to 2 pulse counters // We support up to 2 pulse counters
if (interruptCount == 0) if (interruptCount == 0)
attachInterrupt(digitalPinToInterrupt(config.pin), PulseInterrupt0, RISING); attachInterrupt(digitalPinToInterrupt(digitalInput.pin), PulseInterrupt0,
RISING);
else if (interruptCount == 1) else if (interruptCount == 1)
attachInterrupt(digitalPinToInterrupt(config.pin), PulseInterrupt1, RISING); attachInterrupt(digitalPinToInterrupt(digitalInput.pin), PulseInterrupt1,
RISING);
else { else {
// maximum interrupt count reached // maximum interrupt count reached
std::cout << "DigitalInput::RelativeEncoder: max. # counters of 2 reached" std::cout << "DigitalInput::RelativeEncoder: max. # counters of 2 reached"
@ -70,6 +60,7 @@ DigitalInput::RelativeEncoder::RelativeEncoder(Configuration config,
} }
interruptIx = interruptCount; interruptIx = interruptCount;
interruptCount++; interruptCount++;
std::cout << "pin ints. " << interruptCount << std::endl;
} }
int DigitalInput::RelativeEncoder::GetPulseCount() { int DigitalInput::RelativeEncoder::GetPulseCount() {
@ -88,6 +79,7 @@ int DigitalInput::RelativeEncoder::GetPulseCount() {
void DigitalInput::RelativeEncoder::Update(bool recursive) { void DigitalInput::RelativeEncoder::Update(bool recursive) {
unsigned long currentTimeMs = GetTimeMs(); unsigned long currentTimeMs = GetTimeMs();
if (this->lastUpdateTime == 0) { if (this->lastUpdateTime == 0) {
this->Start();
this->lastUpdateTime = currentTimeMs; this->lastUpdateTime = currentTimeMs;
return; return;
} }
@ -99,8 +91,8 @@ void DigitalInput::RelativeEncoder::Update(bool recursive) {
this->pulseFrequency = pulseCount / timeStep; this->pulseFrequency = pulseCount / timeStep;
this->rotationSpeed = pulseFrequency / pulsesPerRevolution; this->rotationSpeed = pulseFrequency / pulsesPerRevolution;
std::cout << "pulses: " << pulseCount << " per second " << pulseFrequency // std::cout << "pulses: " << pulseCount << " per second " << pulseFrequency
<< " timestep " << timeStep << std::endl; // << " timestep " << timeStep << std::endl;
this->lastUpdateTime = currentTimeMs; this->lastUpdateTime = currentTimeMs;
} }

View File

@ -13,7 +13,7 @@ class DigitalInput : public Thing {
/// @param participant The participant to use /// @param participant The participant to use
/// @param pin The digital pin /// @param pin The digital pin
DigitalInput(Participant* participant, unsigned char pin); DigitalInput(Participant* participant, unsigned char pin);
//DigitalInput(Thing* parent, unsigned char pin); // DigitalInput(Thing* parent, unsigned char pin);
DigitalInput(unsigned char pin, Thing& parent); DigitalInput(unsigned char pin, Thing& parent);
bool isHigh = false; bool isHigh = false;
@ -55,7 +55,7 @@ class DigitalInput::RelativeEncoder : public RoboidControl::RelativeEncoder {
unsigned char pulsesPerRevolution; unsigned char pulsesPerRevolution;
}; };
RelativeEncoder(Configuration config, Thing& parent); RelativeEncoder(Configuration config, Thing& parent = Thing::LocalRoot());
unsigned char pulsesPerRevolution; unsigned char pulsesPerRevolution;
@ -81,6 +81,9 @@ class DigitalInput::RelativeEncoder : public RoboidControl::RelativeEncoder {
int GetPulseCount(); int GetPulseCount();
long netPulseCount = 0; long netPulseCount = 0;
unsigned long lastUpdateTime = 0; unsigned long lastUpdateTime = 0;
private:
void Start();
}; };
#pragma endregion Incremental encoder #pragma endregion Incremental encoder

View File

@ -6,16 +6,6 @@
namespace RoboidControl { namespace RoboidControl {
namespace Arduino { namespace Arduino {
UltrasonicSensor::UltrasonicSensor(Configuration config,
Participant* participant)
: Thing(participant) {
this->pinTrigger = config.triggerPin;
this->pinEcho = config.echoPin;
pinMode(pinTrigger, OUTPUT); // configure the trigger pin to output mode
pinMode(pinEcho, INPUT); // configure the echo pin to input mode
}
UltrasonicSensor::UltrasonicSensor(Configuration config, Thing& parent) UltrasonicSensor::UltrasonicSensor(Configuration config, Thing& parent)
: Thing(parent) { : Thing(parent) {
this->pinTrigger = config.triggerPin; this->pinTrigger = config.triggerPin;
@ -67,23 +57,16 @@ void UltrasonicSensor::Update(bool recursive) {
#pragma region Touch sensor #pragma region Touch sensor
// UltrasonicSensor::TouchSensor::TouchSensor(
// UltrasonicSensor::Configuration config,
// Thing& parent)
// : RoboidControl::TouchSensor(parent), ultrasonic(config, parent) {
// this->touchedSomething = false;
// }
UltrasonicSensor::TouchSensor::TouchSensor( UltrasonicSensor::TouchSensor::TouchSensor(
UltrasonicSensor::Configuration config, UltrasonicSensor::Configuration config,
Thing& parent) Thing& parent)
: RoboidControl::TouchSensor(parent), ultrasonic(config, parent) { : RoboidControl::TouchSensor(parent), ultrasonic(config) {}
this->touchedSomething = false;
}
void UltrasonicSensor::TouchSensor::Update(bool recursive) { void UltrasonicSensor::TouchSensor::Update(bool recursive) {
this->ultrasonic.Update(recursive); this->ultrasonic.Update(false);
this->touchedSomething = (this->ultrasonic.distance > 0 && this->touchedSomething = (this->ultrasonic.distance > 0 &&
this->ultrasonic.distance <= this->touchDistance); this->ultrasonic.distance <= this->touchDistance);
RoboidControl::TouchSensor::Update(recursive);
} }
#pragma region Touch sensor #pragma region Touch sensor

View File

@ -13,12 +13,7 @@ class UltrasonicSensor : Thing {
int echoPin; int echoPin;
}; };
/// @brief Setup an ultrasonic sensor UltrasonicSensor(Configuration config, Thing& parent = Thing::LocalRoot());
/// @param participant The participant to use
/// @param pinTrigger The pin number of the trigger signal
/// @param pinEcho The pin number of the echo signal
UltrasonicSensor(Configuration config, Participant* participant);
UltrasonicSensor(Configuration config, Thing& parent);
// parameters // parameters
@ -51,8 +46,8 @@ class UltrasonicSensor : Thing {
class UltrasonicSensor::TouchSensor : public RoboidControl::TouchSensor { class UltrasonicSensor::TouchSensor : public RoboidControl::TouchSensor {
public: public:
TouchSensor(UltrasonicSensor::Configuration config, Thing& parent); TouchSensor(UltrasonicSensor::Configuration config,
//TouchSensor(UltrasonicSensor::Configuration config, Thing* parent); Thing& parent = Thing::LocalRoot());
float touchDistance = 0.2f; float touchDistance = 0.2f;

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 && thing->GetParent() != nullptr)) { if (thing->positionUpdated || (force && thing->IsRoot())) {
this->position = thing->GetPosition(); this->position = thing->GetPosition();
this->poseType |= Pose_Position; this->poseType |= Pose_Position;
} }
if (thing->orientationUpdated || (force && thing->GetParent() != nullptr)) { if (thing->orientationUpdated || (force && thing->IsRoot())) {
this->orientation = thing->GetOrientation(); this->orientation = thing->GetOrientation();
this->poseType |= Pose_Orientation; this->poseType |= Pose_Orientation;
} }

View File

@ -14,11 +14,12 @@ ThingMsg::ThingMsg(unsigned char networkId, Thing* thing) {
this->networkId = networkId; this->networkId = networkId;
this->thingId = thing->id; this->thingId = thing->id;
this->thingType = thing->type; this->thingType = thing->type;
Thing* parent = thing->GetParent(); if (thing->IsRoot())
if (parent != nullptr)
this->parentId = parent->id;
else
this->parentId = 0; this->parentId = 0;
else {
Thing parent = thing->GetParent();
this->parentId = parent.id;
}
} }
// ThingMsg::ThingMsg(unsigned char networkId, unsigned char thingId, // ThingMsg::ThingMsg(unsigned char networkId, unsigned char thingId,
@ -33,8 +34,9 @@ ThingMsg::~ThingMsg() {}
unsigned char ThingMsg::Serialize(char* buffer) { unsigned char ThingMsg::Serialize(char* buffer) {
#if defined(DEBUG) #if defined(DEBUG)
std::cout << "Send ThingMsg [" << (int)this->networkId << "/" << (int)this->thingId std::cout << "Send ThingMsg [" << (int)this->networkId << "/"
<< "] " << (int)this->thingType << " " << (int)this->parentId << std::endl; << (int)this->thingId << "] " << (int)this->thingType << " "
<< (int)this->parentId << std::endl;
#endif #endif
unsigned char ix = 0; unsigned char ix = 0;
buffer[ix++] = this->id; buffer[ix++] = this->id;

View File

@ -8,7 +8,12 @@ namespace RoboidControl {
ParticipantRegistry Participant::registry; ParticipantRegistry Participant::registry;
Participant LocalParticipant = Participant("0.0.0.0", 0); //Participant Participant::LocalParticipant = Participant("0.0.0.0", 0);
Participant& Participant::LocalParticipant() {
static Participant localParticipant("0.0.0.0", 0);
return localParticipant;
}
Participant::Participant(const char* ipAddress, int port) { Participant::Participant(const char* ipAddress, int port) {
// make a copy of the ip address string // make a copy of the ip address string
@ -51,7 +56,9 @@ Thing* Participant::Get(unsigned char thingId) {
} }
void Participant::Add(Thing* thing, bool checkId) { void Participant::Add(Thing* thing, bool checkId) {
if (checkId && thing->id == 0) { if (checkId && thing->id == 0) {
// allocate a new thing ID // allocate a new thing ID
#if defined(NO_STD) #if defined(NO_STD)
thing->id = this->thingCount + 1; thing->id = this->thingCount + 1;

View File

@ -72,7 +72,7 @@ class Participant {
/// @brief Destructor for the participant /// @brief Destructor for the participant
~Participant(); ~Participant();
static Participant LocalParticipant; static Participant& LocalParticipant();
public: public:
#if defined(NO_STD) #if defined(NO_STD)

View File

@ -27,7 +27,7 @@ void SiteServer::UpdateMyThings() {
if (thing == nullptr) if (thing == nullptr)
continue; continue;
thing->Update( true); thing->Update(true);
if (this->isIsolated == false) { if (this->isIsolated == false) {
// Send to all other participants // Send to all other participants
@ -76,8 +76,9 @@ void SiteServer::Process(Participant* sender, ThingMsg* msg) {
Thing::Reconstruct(sender, msg->thingType, msg->thingId); Thing::Reconstruct(sender, msg->thingType, msg->thingId);
if (msg->parentId != 0) { if (msg->parentId != 0) {
thing->SetParent(Get(msg->parentId)); thing->SetParent(*Get(msg->parentId));
if (thing->GetParent() != nullptr) if (thing->IsRoot())
// if (thing->GetParent() != nullptr)
#if defined(NO_STD) #if defined(NO_STD)
; ;
#else #else
@ -85,7 +86,7 @@ void SiteServer::Process(Participant* sender, ThingMsg* msg) {
<< (int)msg->parentId << "]\n"; << (int)msg->parentId << "]\n";
#endif #endif
} else } else
thing->SetParent(nullptr); thing->SetParent(Thing::LocalRoot());
} }
#pragma endregion Receive #pragma endregion Receive

View File

@ -20,7 +20,10 @@ namespace RoboidControl {
#pragma region Init #pragma region Init
Thing Thing::Root = Thing(Thing::Type::Undetermined, Root); Thing& Thing::LocalRoot() {
static Thing localRoot(Thing::Type::Undetermined, localRoot);
return localRoot;
}
Thing::Thing(unsigned char thingType, Thing& parent) { Thing::Thing(unsigned char thingType, Thing& parent) {
this->type = thingType; this->type = thingType;
@ -34,47 +37,18 @@ Thing::Thing(unsigned char thingType, Thing& parent) {
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 " this->owner = &Participant::LocalParticipant();
// << this->owner->ipAddress << ":" << this->owner->port <<
// std::endl;
this->owner->Add(this, true); this->owner->Add(this, true);
this->SetParent(&parent); this->SetParent(parent);
} }
Thing::Thing(Participant* owner, unsigned char thingType) { // Thing Thing::Reconstruct(Participant* owner, unsigned char thingType,
// unsigned char thingId) { // unsigned char thingId) {
if (owner == nullptr) // Thing thing = Thing(owner, thingType);
owner = IsolatedParticipant::Isolated(); // thing.id = thingId;
this->owner = owner; // return thing;
// this->id = thingId;
this->type = thingType;
this->position = Spherical::zero;
this->positionUpdated = true;
this->orientation = SwingTwist::identity;
this->orientationUpdated = true;
this->hierarchyChanged = true;
this->linearVelocity = Spherical::zero;
this->angularVelocity = Spherical::zero;
// std::cout << "add thing [" << (int)this->id << "] to owner "
// << this->owner->ipAddress << ":" << this->owner->port <<
// std::endl;
this->owner->Add(this, true);
}
// Thing::Thing(Thing* parent, unsigned char thingType) //, unsigned char thingId)
// : Thing(parent->owner, thingType) { //}, thingId) {
// this->SetParent(parent);
// } // }
Thing Thing::Reconstruct(Participant* owner, unsigned char thingType, unsigned char thingId) {
Thing thing = Thing(owner, thingType);
thing.id = thingId;
return thing;
}
#pragma endregion Init #pragma endregion Init
void Thing::SetName(const char* name) { void Thing::SetName(const char* name) {
@ -92,26 +66,39 @@ void Thing::SetModel(const char* url) {
#pragma region Hierarchy #pragma region Hierarchy
void Thing::SetParent(Thing* parent) { // void Thing::SetParent(Thing* parent) {
if (parent == nullptr) { // if (parent == nullptr) {
Thing* parentThing = this->parent; // Thing* parentThing = this->parent;
if (parentThing != nullptr) // if (parentThing != nullptr)
parentThing->RemoveChild(this); // parentThing->RemoveChild(this);
this->parent = nullptr; // this->parent = nullptr;
} else // } else
parent->AddChild(this); // parent->AddChild(this);
// this->hierarchyChanged = true;
// }
void Thing::SetParent(Thing& parent) {
parent.AddChild(this);
this->hierarchyChanged = true; this->hierarchyChanged = true;
} }
const Thing& Thing::GetParent() {
return *this->parent;
}
bool Thing::IsRoot() const {
return this == &LocalRoot(); //&Thing::Root;
}
// void Thing::SetParent(Thing* root, const char* name) { // void Thing::SetParent(Thing* root, const char* name) {
// Thing* thing = root->FindChild(name); // Thing* thing = root->FindChild(name);
// if (thing != nullptr) // if (thing != nullptr)
// this->SetParent(thing); // this->SetParent(thing);
// } // }
Thing* Thing::GetParent() { // Thing* Thing::GetParent() {
return this->parent; // return this->parent;
} // }
Thing* Thing::GetChildByIndex(unsigned char ix) { Thing* Thing::GetChildByIndex(unsigned char ix) {
return this->children[ix]; return this->children[ix];
@ -123,11 +110,11 @@ void Thing::AddChild(Thing* child) {
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
newChildren[childIx] = this->children[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 // // child is already present, stop copying do not update the children
delete[] newChildren; // delete[] newChildren;
return; // return;
} // }
} }
newChildren[this->childCount] = child; newChildren[this->childCount] = child;
@ -156,7 +143,7 @@ Thing* Thing::RemoveChild(Thing* child) {
} }
} }
child->parent = nullptr; child->parent = &Thing::LocalRoot();
delete[] this->children; delete[] this->children;
this->children = newChildren; this->children = newChildren;

25
Thing.h
View File

@ -41,17 +41,16 @@ class Thing {
}; };
#pragma region Init #pragma region Init
static Thing Root; static Thing& LocalRoot();
Thing(unsigned char thingType = Thing::Type::Undetermined, Thing& parent = Root); /// @brief Create a new thing
/// @brief Create a new thing for a participant
/// @param owner The owning participant
/// @param thingType The type of thing (can use Thing::Type) /// @param thingType The type of thing (can use Thing::Type)
/// @param thingId The ID of the thing, leave out or set to zero to generate /// @param parent (optional) The parent thing
/// an ID /// The owner will be the same as the owner of the parent thing, it will
Thing(Participant* owner, /// be Participant::LocalParticipant if the parent is not specified. A thing
unsigned char thingType = Thing::Type::Undetermined); /// without a parent will be a root thing.
Thing(unsigned char thingType = Thing::Type::Undetermined,
Thing& parent = LocalRoot());
/// @brief Create a new child thing /// @brief Create a new child thing
/// @param parent The parent thing /// @param parent The parent thing
@ -108,10 +107,14 @@ class Thing {
/// @brief Sets the parent of this Thing /// @brief Sets the parent of this Thing
/// @param parent The Thing which should become the parent /// @param parent The Thing which should become the parent
virtual void SetParent(Thing* parent); // virtual void SetParent(Thing* parent);
void SetParent(Thing& parent);
/// @brief Gets the parent of this Thing /// @brief Gets the parent of this Thing
/// @return The parent Thing /// @return The parent Thing
Thing* GetParent(); // Thing* GetParent();
const Thing& GetParent();
bool IsRoot() const;
/// @brief The number of children /// @brief The number of children
unsigned char childCount = 0; unsigned char childCount = 0;

View File

@ -2,11 +2,14 @@
namespace RoboidControl { namespace RoboidControl {
ControlledMotor::ControlledMotor(Motor* motor, RelativeEncoder* encoder) ControlledMotor::ControlledMotor(Motor& motor,
: Motor(), motor(motor), encoder(encoder) { RelativeEncoder& encoder,
Thing& parent)
: Motor(parent), motor(motor), encoder(encoder) {
this->type = Type::ControlledMotor; this->type = Type::ControlledMotor;
Thing* parent = motor->GetParent(); // encoder.SetParent(*this);
this->SetParent(parent); // Thing parent = motor.GetParent();
// this->SetParent(parent);
} }
void ControlledMotor::SetTargetVelocity(float velocity) { void ControlledMotor::SetTargetVelocity(float velocity) {
@ -16,9 +19,9 @@ void ControlledMotor::SetTargetVelocity(float velocity) {
} }
void ControlledMotor::Update(bool recurse) { void ControlledMotor::Update(bool recurse) {
encoder->Update(recurse); encoder.Update(false);
this->actualVelocity = (int)rotationDirection * encoder->rotationSpeed; this->actualVelocity = (int)rotationDirection * encoder.rotationSpeed;
unsigned long currentTimeMs = GetTimeMs(); unsigned long currentTimeMs = GetTimeMs();
float timeStep = (currentTimeMs - this->lastUpdateTime) / 1000.0f; float timeStep = (currentTimeMs - this->lastUpdateTime) / 1000.0f;
@ -27,10 +30,11 @@ void ControlledMotor::Update(bool recurse) {
float error = this->targetVelocity - this->actualVelocity; float error = this->targetVelocity - this->actualVelocity;
float acceleration = float acceleration =
error * timeStep * pidP; // Just P is used at this moment error * timeStep * pidP; // Just P is used at this moment
motor->SetTargetVelocity(targetVelocity + std::cout << "motor acc. " << acceleration << std::endl;
motor.SetTargetVelocity(targetVelocity +
acceleration); // or something like that acceleration); // or something like that
motor->Update(recurse); motor.Update(false);
} }
// float ControlledMotor::GetActualVelocity() { // float ControlledMotor::GetActualVelocity() {

View File

@ -10,12 +10,13 @@ namespace RoboidControl {
/// The speed is measured in revolutions per second. /// The speed is measured in revolutions per second.
class ControlledMotor : public Motor { class ControlledMotor : public Motor {
public: public:
ControlledMotor(Motor* motor, RelativeEncoder* encoder); ControlledMotor(Motor& motor, RelativeEncoder& encoder, Thing& parent = Thing::LocalRoot());
float pidP = 1; float pidP = 1;
float pidD = 0; float pidD = 0;
float pidI = 0; float pidI = 0;
/// @brief The actual velocity in revolutions per second
float actualVelocity; float actualVelocity;
enum Direction { Forward = 1, Reverse = -1 }; enum Direction { Forward = 1, Reverse = -1 };
@ -24,29 +25,14 @@ class ControlledMotor : public Motor {
virtual void Update(bool recurse = false) override; virtual void Update(bool recurse = false) override;
/// @brief Set the target verlocity for the motor controller /// @brief Set the target verlocity for the motor controller
/// @param speed the target velocity in revolutions per second. /// @param speed the target velocity in revolutions per second
virtual void SetTargetVelocity(float velocity) override; virtual void SetTargetVelocity(float velocity) override;
/// @brief Get the actual velocity from the encoder Motor& motor;
/// @return The velocity in revolutions per second RelativeEncoder& encoder;
// float GetActualVelocity();
// bool Drive(float distance);
Motor* motor;
RelativeEncoder* encoder;
protected: protected:
float lastUpdateTime; float lastUpdateTime;
//float targetVelocity;
// float netDistance = 0;
// float startDistance = 0;
// bool driving = false;
// float targetDistance = 0;
// float lastEncoderPosition = 0;
}; };
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -2,25 +2,8 @@
namespace RoboidControl { namespace RoboidControl {
// DifferentialDrive::DifferentialDrive(Participant* participant, DifferentialDrive::DifferentialDrive(Thing& parent)
// unsigned char thingId) : Thing(Type::DifferentialDrive, parent) {
// : Thing(participant, Thing::Type::DifferentialDrive, thingId) {
// this->leftWheel = new Motor();
// this->rightWheel = new Motor();
// }
// DifferentialDrive::DifferentialDrive(Thing* parent, unsigned char thingId)
// : Thing(parent, Thing::Type::DifferentialDrive, thingId) {}
DifferentialDrive::DifferentialDrive(Participant* owner) : Thing(owner, Type::DifferentialDrive) {
this->leftWheel = new Motor();
this->rightWheel = new Motor();
}
// DifferentialDrive::DifferentialDrive(Thing* parent) : Thing(parent, Type::DifferentialDrive) {
// this->leftWheel = new Motor();
// this->rightWheel = new Motor();
// }
DifferentialDrive::DifferentialDrive(Thing& parent) : Thing(Type::DifferentialDrive, parent) {
this->leftWheel = new Motor(); this->leftWheel = new Motor();
this->rightWheel = new Motor(); this->rightWheel = new Motor();
} }
@ -40,21 +23,28 @@ void DifferentialDrive::SetDriveDimensions(float wheelDiameter,
this->rightWheel->SetPosition(Spherical(distance, Direction::right)); this->rightWheel->SetPosition(Spherical(distance, Direction::right));
} }
// void DifferentialDrive::SetMotors(Thing* leftWheel, Thing* rightWheel) { Motor& DifferentialDrive::GetMotorLeft() {
// float distance = this->wheelSeparation / 2; return *this->leftWheel;
// this->leftWheel = leftWheel; }
// ;
// if (leftWheel != nullptr)
// this->leftWheel->SetPosition(Spherical(distance, Direction::left));
// this->rightWheel = rightWheel; Motor& DifferentialDrive::GetMotorRight() {
// if (rightWheel != nullptr) return *this->rightWheel;
// this->rightWheel->SetPosition(Spherical(distance, Direction::right)); }
// }
void DifferentialDrive::SetWheelVelocity(float velocityLeft, float velocityRight) { void DifferentialDrive::SetMotors(Motor& leftMotor, Motor& rightMotor) {
float distance = this->wheelSeparation / 2;
this->leftWheel = &leftMotor;
this->leftWheel->SetPosition(Spherical(distance, Direction::left));
this->rightWheel = &rightMotor;
this->rightWheel->SetPosition(Spherical(distance, Direction::right));
}
void DifferentialDrive::SetWheelVelocity(float velocityLeft,
float velocityRight) {
// if (this->leftWheel != nullptr) // if (this->leftWheel != nullptr)
// this->leftWheel->SetAngularVelocity(Spherical(velocityLeft, Direction::left)); // this->leftWheel->SetAngularVelocity(Spherical(velocityLeft,
// Direction::left));
// if (this->rightWheel != nullptr) // if (this->rightWheel != nullptr)
// this->rightWheel->SetAngularVelocity( // this->rightWheel->SetAngularVelocity(
// Spherical(velocityRight, Direction::right)); // Spherical(velocityRight, Direction::right));

View File

@ -10,20 +10,11 @@ namespace RoboidControl {
/// @sa @link https://en.wikipedia.org/wiki/Differential_wheeled_robot @endlink /// @sa @link https://en.wikipedia.org/wiki/Differential_wheeled_robot @endlink
class DifferentialDrive : public Thing { class DifferentialDrive : public Thing {
public: public:
// /// @brief Create a differential drive without networking support
// DifferentialDrive();
/// @brief Create a differential drive with networking support
/// @param participant The local participant
/// @param thingId The ID of the thing, leave out or set to zero to generate
/// an ID
DifferentialDrive(Participant* participant); //,
//unsigned char thingId = 0);
/// @brief Create a new child differential drive /// @brief Create a new child differential drive
/// @param parent The parent thing /// @param parent The parent thing
/// @param thingId The ID of the thing, leave out or set to zero to generate /// @param thingId The ID of the thing, leave out or set to zero to generate
/// an ID /// an ID
// DifferentialDrive(Thing* parent); //, unsigned char thingId = 0); DifferentialDrive(Thing& parent = Thing::LocalRoot());
DifferentialDrive(Thing& parent = Thing::Root);
/// @brief Configures the dimensions of the drive /// @brief Configures the dimensions of the drive
/// @param wheelDiameter The diameter of the wheels in meters /// @param wheelDiameter The diameter of the wheels in meters
@ -33,10 +24,13 @@ class DifferentialDrive : public Thing {
/// linear and angular velocity. /// linear and angular velocity.
/// @sa SetLinearVelocity SetAngularVelocity /// @sa SetLinearVelocity SetAngularVelocity
void SetDriveDimensions(float wheelDiameter, float wheelSeparation); void SetDriveDimensions(float wheelDiameter, float wheelSeparation);
Motor& GetMotorLeft();
Motor& GetMotorRight();
/// @brief Congures the motors for the wheels /// @brief Congures the motors for the wheels
/// @param leftWheel The motor for the left wheel /// @param leftWheel The motor for the left wheel
/// @param rightWheel The motor for the right wheel /// @param rightWheel The motor for the right wheel
void SetMotors(Thing* leftWheel, Thing* rightWheel); void SetMotors(Motor& leftMotor, Motor& rightMotor);
/// @brief Directly specify the speeds of the motors /// @brief Directly specify the speeds of the motors
/// @param speedLeft The speed of the left wheel in degrees per second. /// @param speedLeft The speed of the left wheel in degrees per second.
@ -48,10 +42,6 @@ class DifferentialDrive : public Thing {
/// @copydoc RoboidControl::Thing::Update(unsigned long) /// @copydoc RoboidControl::Thing::Update(unsigned long)
virtual void Update(bool recursive = true) override; virtual void Update(bool recursive = true) override;
/// @brief The left wheel
Motor* leftWheel = nullptr;
/// @brief The right wheel
Motor* rightWheel = nullptr;
protected: protected:
/// @brief The radius of a wheel in meters /// @brief The radius of a wheel in meters
@ -61,6 +51,11 @@ class DifferentialDrive : public Thing {
/// @brief Convert revolutions per second to meters per second /// @brief Convert revolutions per second to meters per second
float rpsToMs = 1.0f; float rpsToMs = 1.0f;
/// @brief The left wheel
Motor* leftWheel = nullptr;
/// @brief The right wheel
Motor* rightWheel = nullptr;
}; };
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -10,7 +10,7 @@ namespace RoboidControl {
// DigitalSensor::DigitalSensor(Thing* parent, unsigned char thingId) // DigitalSensor::DigitalSensor(Thing* parent, unsigned char thingId)
// : Thing(parent, Type::Switch) {} // : Thing(parent, Type::Switch) {}
DigitalSensor::DigitalSensor(Participant* owner) : Thing(owner, Type::Switch) {} // DigitalSensor::DigitalSensor(Participant* owner) : Thing(owner, Type::Switch) {}
// DigitalSensor::DigitalSensor(Thing* parent) : Thing(parent, Type::Switch) {} // DigitalSensor::DigitalSensor(Thing* parent) : Thing(parent, Type::Switch) {}
DigitalSensor::DigitalSensor(Thing& parent) : Thing(Type::Switch, parent) {} DigitalSensor::DigitalSensor(Thing& parent) : Thing(Type::Switch, parent) {}

View File

@ -19,7 +19,7 @@ class DigitalSensor : public Thing {
/// @param thingId The ID of the thing, leave out or set to zero to generate /// @param thingId The ID of the thing, leave out or set to zero to generate
/// an ID /// an ID
// DigitalSensor(Thing* parent); //, unsigned char thingId = 0); // DigitalSensor(Thing* parent); //, unsigned char thingId = 0);
DigitalSensor(Thing& parent = Thing::Root); DigitalSensor(Thing& parent = Thing::LocalRoot());
/// @brief The sigital state /// @brief The sigital state
bool state = 0; bool state = 0;

View File

@ -2,8 +2,8 @@
namespace RoboidControl { namespace RoboidControl {
RoboidControl::Motor::Motor(Participant* owner) // RoboidControl::Motor::Motor(Participant* owner)
: Thing(owner, Type::UncontrolledMotor) {} // : Thing(owner, Type::UncontrolledMotor) {}
// RoboidControl::Motor::Motor(Thing* parent) // RoboidControl::Motor::Motor(Thing* parent)
// : Thing(parent, Type::UncontrolledMotor) {} // : Thing(parent, Type::UncontrolledMotor) {}

View File

@ -8,7 +8,7 @@ class Motor : public Thing {
public: public:
Motor(Participant* owner); Motor(Participant* owner);
// Motor(Thing* parent); // Motor(Thing* parent);
Motor(Thing& parent = Thing::Root); Motor(Thing& parent = Thing::LocalRoot());
/// @brief Motor turning direction /// @brief Motor turning direction
enum class Direction { Clockwise = 1, CounterClockwise = -1 }; enum class Direction { Clockwise = 1, CounterClockwise = -1 };

View File

@ -2,8 +2,6 @@
namespace RoboidControl { namespace RoboidControl {
RelativeEncoder::RelativeEncoder(Participant* owner)
: Thing(owner, Type::IncrementalEncoder) {}
RelativeEncoder::RelativeEncoder(Thing& parent) RelativeEncoder::RelativeEncoder(Thing& parent)
: Thing(Type::IncrementalEncoder, parent) {} : Thing(Type::IncrementalEncoder, parent) {}

View File

@ -16,7 +16,7 @@ class RelativeEncoder : public Thing {
/// rotation /// rotation
RelativeEncoder(Participant* owner); RelativeEncoder(Participant* owner);
// RelativeEncoder(Thing* parent); // RelativeEncoder(Thing* parent);
RelativeEncoder(Thing& parent = Thing::Root); RelativeEncoder(Thing& parent = Thing::LocalRoot());
/// @brief Get the rotation speed /// @brief Get the rotation speed
/// @return The speed in revolutions per second /// @return The speed in revolutions per second

View File

@ -8,7 +8,7 @@ namespace RoboidControl {
// unsigned char thingId) // unsigned char thingId)
// : Thing(participant, Type::TemperatureSensor, thingId) {} // : Thing(participant, Type::TemperatureSensor, thingId) {}
TemperatureSensor::TemperatureSensor(Participant* owner) : Thing(owner, Type::TemperatureSensor) {} // TemperatureSensor::TemperatureSensor(Participant* owner) : Thing(owner, Type::TemperatureSensor) {}
TemperatureSensor::TemperatureSensor(Thing& parent) : Thing(Type::TemperatureSensor, parent) {} TemperatureSensor::TemperatureSensor(Thing& parent) : Thing(Type::TemperatureSensor, parent) {}

View File

@ -14,7 +14,7 @@ class TemperatureSensor : public Thing {
/// @param thingId The ID of the thing /// @param thingId The ID of the thing
TemperatureSensor(Participant* participant); //, unsigned char thingId); TemperatureSensor(Participant* participant); //, unsigned char thingId);
// TemperatureSensor(Thing* parent); // TemperatureSensor(Thing* parent);
TemperatureSensor(Thing& parent = Thing::Root); TemperatureSensor(Thing& parent = Thing::LocalRoot());
/// @brief The measured temperature /// @brief The measured temperature
float temperature = 0; float temperature = 0;

View File

@ -2,20 +2,6 @@
namespace RoboidControl { namespace RoboidControl {
// TouchSensor::TouchSensor() : Thing(Thing::Type::TouchSensor) {}
// TouchSensor::TouchSensor(Participant* owner, unsigned char thingId)
// : Thing(owner, Thing::Type::TouchSensor, thingId) {}
// TouchSensor::TouchSensor(Thing* parent, unsigned char thingId) :
// Thing(parent->owner, Thing::Type::TouchSensor, thingId) {
// this->SetParent(parent);
// }
TouchSensor::TouchSensor(Participant* owner)
: Thing(owner, Type::TouchSensor) {}
// TouchSensor::TouchSensor(Thing* parent) : Thing(parent, Type::TouchSensor) {}
TouchSensor::TouchSensor(Thing& parent) : Thing(Type::TouchSensor, parent) {} TouchSensor::TouchSensor(Thing& parent) : Thing(Type::TouchSensor, parent) {}
int TouchSensor::GenerateBinary(char* bytes, unsigned char* ix) { int TouchSensor::GenerateBinary(char* bytes, unsigned char* ix) {

View File

@ -8,19 +8,11 @@ namespace RoboidControl {
class TouchSensor : public Thing { class TouchSensor : public Thing {
// Why finishing this release (0.3), I notice that this is equivalent to a digital sensor // Why finishing this release (0.3), I notice that this is equivalent to a digital sensor
public: public:
/// @brief Create a touch sensor without communication abilities
//TouchSensor();
/// @brief Create a touch sensor for a participant
/// @param owner The owning participant
/// @param thingId The ID of the thing, leave out or set to zero to generate
/// an ID
TouchSensor(Participant* owner); //, unsigned char thingId = 0);
/// @brief Create a new child touch sensor /// @brief Create a new child touch sensor
/// @param parent The parent thing /// @param parent The parent thing
/// @param thingId The ID of the thing, leave out or set to zero to generate /// @param thingId The ID of the thing, leave out or set to zero to generate
/// an ID /// an ID
// TouchSensor(Thing* parent); //, unsigned char thingId = 0); TouchSensor(Thing& parent = Thing::LocalRoot());
TouchSensor(Thing& parent = Thing::Root);
/// @brief Value which is true when the sensor is touching something, false /// @brief Value which is true when the sensor is touching something, false
/// otherwise /// otherwise