From 045826d3c2a9e5e892425b30fc90b624f5361cd9 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 18 Apr 2025 17:24:33 +0200 Subject: [PATCH] Fixed parenting --- Arduino/Things/DRV8833.cpp | 29 +++++++++++-- Arduino/Things/DRV8833.h | 65 ++++++++++++++++------------- Arduino/Things/UltrasonicSensor.cpp | 7 ++++ Arduino/Things/UltrasonicSensor.h | 6 +++ Participants/ParticipantUDP.cpp | 7 ++-- Thing.cpp | 10 ++++- Thing.h | 1 + Things/TouchSensor.h | 3 ++ 8 files changed, 91 insertions(+), 37 deletions(-) diff --git a/Arduino/Things/DRV8833.cpp b/Arduino/Things/DRV8833.cpp index f3fc46e..8e03f3c 100644 --- a/Arduino/Things/DRV8833.cpp +++ b/Arduino/Things/DRV8833.cpp @@ -9,11 +9,13 @@ namespace Arduino { uint8_t DRV8833Motor::nextAvailablePwmChannel = 0; #endif -DRV8833Motor::DRV8833Motor(Participant* participant, +DRV8833Motor::DRV8833Motor(DRV8833* driver, unsigned char pinIn1, unsigned char pinIn2, bool reverse) - : Thing(participant) { + : Thing(driver->owner) { + this->parent = driver; + this->pinIn1 = pinIn1; this->pinIn2 = pinIn2; @@ -120,11 +122,30 @@ DRV8833::DRV8833(Participant* participant, if (pinStandby != 255) 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->motorB = new DRV8833Motor(participant, pinBIn1, pinBIn2, reverseB); + this->motorB = new DRV8833Motor(this, pinBIn1, pinBIn2, reverseB); 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 RoboidControl \ No newline at end of file diff --git a/Arduino/Things/DRV8833.h b/Arduino/Things/DRV8833.h index fbd03e7..cbd63d6 100644 --- a/Arduino/Things/DRV8833.h +++ b/Arduino/Things/DRV8833.h @@ -7,6 +7,42 @@ namespace RoboidControl { 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 class DRV8833Motor : public Thing { public: @@ -17,7 +53,7 @@ class DRV8833Motor : public Thing { /// @param pinIn1 the pin number for the in1 signal /// @param pinIn2 the pin number for the in2 signal /// @param direction the forward turning direction of the motor - DRV8833Motor(Participant* participant, + DRV8833Motor(DRV8833* driver, unsigned char pinIn1, unsigned char pinIn2, bool reverse = false); @@ -39,32 +75,5 @@ class DRV8833Motor : public Thing { #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 RoboidControl \ No newline at end of file diff --git a/Arduino/Things/UltrasonicSensor.cpp b/Arduino/Things/UltrasonicSensor.cpp index d6deb64..0f89bd0 100644 --- a/Arduino/Things/UltrasonicSensor.cpp +++ b/Arduino/Things/UltrasonicSensor.cpp @@ -16,6 +16,13 @@ UltrasonicSensor::UltrasonicSensor(Participant* participant, 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() { // Start the ultrasonic 'ping' digitalWrite(pinTrigger, LOW); diff --git a/Arduino/Things/UltrasonicSensor.h b/Arduino/Things/UltrasonicSensor.h index a80be2b..3e9e815 100644 --- a/Arduino/Things/UltrasonicSensor.h +++ b/Arduino/Things/UltrasonicSensor.h @@ -8,6 +8,9 @@ namespace Arduino { /// @brief An HC-SR04 ultrasonic distance sensor class UltrasonicSensor : public TouchSensor { public: + // Inherit all constructors + using TouchSensor::TouchSensor; + /// @brief Setup an ultrasonic sensor /// @param participant The participant to use /// @param pinTrigger The pin number of the trigger signal @@ -15,6 +18,9 @@ class UltrasonicSensor : public TouchSensor { UltrasonicSensor(Participant* participant, unsigned char pinTrigger, unsigned char pinEcho); + UltrasonicSensor(Thing* parent, + unsigned char pinTrigger, + unsigned char pinEcho); // parameters diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index 65e28d9..b82df34 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -179,12 +179,13 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant, } 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); if (bufferSize <= 0) return true; + std::cout << "send msg " << (int)this->buffer[0] << " to " + << remoteParticipant->ipAddress << std::endl; + #if defined(_WIN32) || defined(_WIN64) Windows::ParticipantUDP* thisWindows = static_cast(this); @@ -227,7 +228,7 @@ void ParticipantUDP::PublishThingInfo(Thing* thing) { } bool ParticipantUDP::Publish(IMessage* msg) { - // std::cout << "publish msg\n"; + std::cout << "publish msg\n"; #if defined(_WIN32) || defined(_WIN64) Windows::ParticipantUDP* thisWindows = static_cast(this); diff --git a/Thing.cpp b/Thing.cpp index 86e4008..d1aeaa2 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -42,8 +42,14 @@ Thing::Thing(Participant* owner, int thingType, unsigned char thingId) { 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, false); + // << this->owner->ipAddress << ":" << this->owner->port << + // 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, diff --git a/Thing.h b/Thing.h index febe7a9..d08bd04 100644 --- a/Thing.h +++ b/Thing.h @@ -58,6 +58,7 @@ class Thing { // unsigned char networkId, // unsigned char thingId, // Type thingType = Type::Undetermined); + Thing(Thing* parent, int thingType = 0, unsigned char thingId = 0); /// @brief The participant managing this thing Participant* owner; diff --git a/Things/TouchSensor.h b/Things/TouchSensor.h index 39638e7..4e6d483 100644 --- a/Things/TouchSensor.h +++ b/Things/TouchSensor.h @@ -7,6 +7,9 @@ namespace RoboidControl { /// @brief A sensor which can detect touches class TouchSensor : public Thing { public: + // Inherit all constructors + using Thing::Thing; + /// @brief Create a touch sensor with isolated participant TouchSensor(); /// @brief Create a touch sensor