From a6a91798b24552522373414d43981b082025cfdd Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 6 Mar 2025 12:17:21 +0100 Subject: [PATCH] Small improvements --- Examples/BB2B.cpp | 12 ++++++------ Thing.cpp | 18 +++++++++++------- Thing.h | 5 ++--- Things/DifferentialDrive.cpp | 8 ++++---- Things/DifferentialDrive.h | 28 ++++++++++++++++++++++++++-- 5 files changed, 49 insertions(+), 22 deletions(-) diff --git a/Examples/BB2B.cpp b/Examples/BB2B.cpp index 98d74d7..0462ef1 100644 --- a/Examples/BB2B.cpp +++ b/Examples/BB2B.cpp @@ -6,6 +6,8 @@ #include using namespace RoboidControl; +using namespace std::this_thread; +using namespace std::chrono; int main() { // The robot's propulsion is a differential drive @@ -19,21 +21,19 @@ int main() { while (true) { // The left wheel turns forward when nothing is touched on the right side // and turn backward when the roboid hits something on the right - float leftWheelSpeed = (touchRight->touchedSomething) ? -0.5f : 0.5f; + float leftWheelSpeed = (touchRight->touchedSomething) ? -600.0f : 600.0f; // The right wheel does the same, but instead is controlled by // touches on the left side - float rightWheelSpeed = (touchLeft->touchedSomething) ? -0.5f : 0.5f; + float rightWheelSpeed = (touchLeft->touchedSomething) ? -600.0f : 600.0f; // When both sides are touching something, both wheels will turn backward // and the roboid will move backwards bb2b->SetWheelVelocity(leftWheelSpeed, rightWheelSpeed); // Update the roboid state - Thing::UpdateThings(Thing::GetTimeMs()); - // would like to do this: - // bb2b->Update(); + bb2b->Update(true); // and sleep for 100ms - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sleep_for(milliseconds(100)); } return 0; diff --git a/Thing.cpp b/Thing.cpp index f2e0856..6ed6acd 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -175,20 +175,24 @@ unsigned long Thing::GetTimeMs() { return static_cast(ms.count()); } -void Thing::Update() { +void Thing::Update(bool recursive) { #if defined(ARDUINO) Update(millis()); #else - Update(GetTimeMs()); + Update(GetTimeMs(), recursive); #endif } -void Thing::Update(unsigned long currentTimeMs) { +void Thing::Update(unsigned long currentTimeMs, bool recursive) { (void)currentTimeMs; - - // PoseMsg* poseMsg = new PoseMsg(this->networkId, this); - // participant->Send(remoteParticipant, poseMsg); - // delete poseMsg; + if (recursive) { + for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { + Thing* child = this->children[childIx]; + if (child == nullptr) + continue; + child->Update(currentTimeMs, recursive); + } + } } void Thing::UpdateThings(unsigned long currentTimeMs) { diff --git a/Thing.h b/Thing.h index 827b826..0ceb4a0 100644 --- a/Thing.h +++ b/Thing.h @@ -178,12 +178,11 @@ class Thing { static unsigned long GetTimeMs(); - void Update(); + void Update(bool recursive = false); /// @brief Updates the state of the thing /// @param currentTimeMs The current clock time in milliseconds - virtual void Update( - unsigned long currentTimeMs); // { (void)currentTimeMs; }; + virtual void Update(unsigned long currentTimeMs, bool recursive = false); static void UpdateThings(unsigned long currentTimeMs); diff --git a/Things/DifferentialDrive.cpp b/Things/DifferentialDrive.cpp index 029e95f..d5b5f84 100644 --- a/Things/DifferentialDrive.cpp +++ b/Things/DifferentialDrive.cpp @@ -6,8 +6,8 @@ DifferentialDrive::DifferentialDrive() : Thing() {} RoboidControl::DifferentialDrive::DifferentialDrive(Participant* participant) : Thing(participant) {} -void DifferentialDrive::SetDimensions(float wheelDiameter, - float wheelSeparation) { +void DifferentialDrive::SetDriveDimensions(float wheelDiameter, + float wheelSeparation) { this->wheelRadius = wheelDiameter > 0 ? wheelDiameter / 2 : -wheelDiameter / 2; this->wheelSeparation = @@ -40,7 +40,7 @@ void DifferentialDrive::SetWheelVelocity(float speedLeft, float speedRight) { Spherical(speedRight, Direction::right)); } -void DifferentialDrive::Update(unsigned long currentMs) { +void DifferentialDrive::Update(unsigned long currentMs, bool recursive) { if (this->linearVelocityUpdated == false) return; // this assumes forward velocity only.... @@ -61,7 +61,7 @@ void DifferentialDrive::Update(unsigned long currentMs) { this->wheelRadius * Rad2Deg; this->SetWheelVelocity(speedLeft, speedRight); - + Thing::Update(currentMs, recursive); // std::cout << "lin. speed " << linearVelocity << " ang. speed " << // angularVelocity.distance << " left wheel " // << speedLeft << " right wheel " << speedRight << "\n"; diff --git a/Things/DifferentialDrive.h b/Things/DifferentialDrive.h index 4e34bfa..03e6318 100644 --- a/Things/DifferentialDrive.h +++ b/Things/DifferentialDrive.h @@ -5,17 +5,39 @@ namespace RoboidControl { /// @brief A thing which can move itself using a differential drive system +/// +/// @sa @link https://en.wikipedia.org/wiki/Differential_wheeled_robot @endlink class DifferentialDrive : public Thing { public: + /// @brief Create a differential drive without networking support DifferentialDrive(); + /// @brief Create a differential drive with networking support + /// @param participant The local participant DifferentialDrive(Participant* participant); - void SetDimensions(float wheelDiameter, float wheelSeparation); + /// @brief Configures the dimensions of the drive + /// @param wheelDiameter The diameter of the wheels in meters + /// @param wheelSeparation The distance between the wheels in meters + /// + /// These values are used to compute the desired wheel speed from the set + /// linear and angular velocity. + /// @sa SetLinearVelocity SetAngularVelocity + void SetDriveDimensions(float wheelDiameter, float wheelSeparation); + /// @brief Congures the motors for the wheels + /// @param leftWheel The motor for the left wheel + /// @param rightWheel The motor for the right wheel void SetMotors(Thing* leftWheel, Thing* rightWheel); + /// @brief Directly specify the speeds of the motors + /// @param speedLeft The speed of the left wheel in degrees per second. + /// Positive moves the robot in the forward direction. + /// @param speedRight The speed of the right wheel in degrees per second. + /// Positive moves the robot in the forward direction. + void SetWheelVelocity(float speedLeft, float speedRight); - virtual void Update(unsigned long currentMs) override; + /// @copydoc RoboidControl::Thing::Update(unsigned long) + virtual void Update(unsigned long currentMs, bool recursive = true) override; protected: /// @brief The radius of a wheel in meters @@ -26,7 +48,9 @@ class DifferentialDrive : public Thing { /// @brief Convert revolutions per second to meters per second float rpsToMs = 1.0f; + /// @brief The left wheel Thing* leftWheel = nullptr; + /// @brief The right wheel Thing* rightWheel = nullptr; };