From 9f27087d2d50f0abea1b5405cbbe1e7f8884a7dd Mon Sep 17 00:00:00 2001 From: Rocky14683 Date: Tue, 17 Dec 2024 08:47:17 +0800 Subject: [PATCH 1/4] change task_running to atomic bool and add wait_until_settled helper function --- include/VOSS/chassis/AbstractChassis.hpp | 4 +++- src/VOSS/chassis/AbstractChassis.cpp | 27 +++++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/VOSS/chassis/AbstractChassis.hpp b/include/VOSS/chassis/AbstractChassis.hpp index 22c43818..3830dc5e 100644 --- a/include/VOSS/chassis/AbstractChassis.hpp +++ b/include/VOSS/chassis/AbstractChassis.hpp @@ -21,7 +21,7 @@ class AbstractChassis { controller_ptr default_controller; ec_ptr default_ec; std::unique_ptr task = nullptr; - bool task_running = false; + std::atomic_bool task_running = false; pros::motor_brake_mode_e brakeMode; void move_task(controller_ptr controller, ec_ptr ec, double max, @@ -39,6 +39,8 @@ class AbstractChassis { virtual bool execute(DiffChassisCommand cmd, double max) = 0; virtual void set_brake_mode(pros::motor_brake_mode_e mode) = 0; + void wait_until_settled(); + void move(double distance, double max = 100.0, voss::Flags flags = voss::Flags::NONE); diff --git a/src/VOSS/chassis/AbstractChassis.cpp b/src/VOSS/chassis/AbstractChassis.cpp index 6774622a..4060194c 100644 --- a/src/VOSS/chassis/AbstractChassis.cpp +++ b/src/VOSS/chassis/AbstractChassis.cpp @@ -25,13 +25,13 @@ void AbstractChassis::move_task(controller_ptr controller, ec_ptr ec, flags & voss::Flags::THRU, ec), max)) { if (pros::competition::is_disabled()) { - this->task_running = false; + this->task_running.store(false); return; } pros::delay(constants::MOTOR_UPDATE_DELAY); } - this->task_running = false; + this->task_running.store(false); }); // Early exit for async movement @@ -55,13 +55,13 @@ void AbstractChassis::turn_task(controller_ptr controller, ec_ptr ec, flags & voss::Flags::THRU, direction, ec), max)) { if (pros::competition::is_disabled()) { - this->task_running = false; + this->task_running.store(false); return; } pros::delay(constants::MOTOR_UPDATE_DELAY); } - this->task_running = false; + this->task_running.store(false); }); // Early exit for async movement @@ -99,10 +99,10 @@ void AbstractChassis::move(Pose target, controller_ptr controller, double max, void AbstractChassis::move(Pose target, controller_ptr controller, ec_ptr ec, double max, voss::Flags flags) { - while (this->task_running) { + while (this->task_running.load()) { pros::delay(constants::MOTOR_UPDATE_DELAY); } - this->task_running = true; + this->task_running.store(true); controller->set_target(target, flags & voss::Flags::RELATIVE, ec); this->move_task(std::move(controller), std::move(ec), max, flags); @@ -124,10 +124,10 @@ void AbstractChassis::turn(double target, controller_ptr controller, double max, void AbstractChassis::turn(double target, controller_ptr controller, ec_ptr ec, double max, voss::Flags flags, voss::AngularDirection direction) { - while (this->task_running) { + while (this->task_running.load()) { pros::delay(constants::MOTOR_UPDATE_DELAY); } - this->task_running = true; + this->task_running.store(true); controller->set_target({NAN, NAN, target}, flags & voss::Flags::RELATIVE, ec); @@ -153,10 +153,10 @@ void AbstractChassis::turn_to(Point target, controller_ptr controller, void AbstractChassis::turn_to(Point target, controller_ptr controller, ec_ptr ec, double max, voss::Flags flags, voss::AngularDirection direction) { - while (this->task_running) { + while (this->task_running.load()) { pros::delay(10); } - this->task_running = true; + this->task_running.store(true); controller->set_target({target.x, target.y, std::nullopt}, flags & voss::Flags::RELATIVE, ec); @@ -165,4 +165,11 @@ void AbstractChassis::turn_to(Point target, controller_ptr controller, direction); } + +void AbstractChassis::wait_until_settled() { + while(this->task_running.load()) { + pros::delay(constants::MOTOR_UPDATE_DELAY); + } +} + } // namespace voss::chassis \ No newline at end of file From 4b261c9a6c3c681dbacd0f1421fd98aa8886fbdc Mon Sep 17 00:00:00 2001 From: Rocky14683 Date: Tue, 17 Dec 2024 09:19:26 +0800 Subject: [PATCH 2/4] add is_settled get function --- include/VOSS/chassis/AbstractChassis.hpp | 1 + src/VOSS/chassis/AbstractChassis.cpp | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/VOSS/chassis/AbstractChassis.hpp b/include/VOSS/chassis/AbstractChassis.hpp index 3830dc5e..ce783022 100644 --- a/include/VOSS/chassis/AbstractChassis.hpp +++ b/include/VOSS/chassis/AbstractChassis.hpp @@ -40,6 +40,7 @@ class AbstractChassis { virtual void set_brake_mode(pros::motor_brake_mode_e mode) = 0; void wait_until_settled(); + bool is_settled(); void move(double distance, double max = 100.0, voss::Flags flags = voss::Flags::NONE); diff --git a/src/VOSS/chassis/AbstractChassis.cpp b/src/VOSS/chassis/AbstractChassis.cpp index 4060194c..48f90d06 100644 --- a/src/VOSS/chassis/AbstractChassis.cpp +++ b/src/VOSS/chassis/AbstractChassis.cpp @@ -165,11 +165,14 @@ void AbstractChassis::turn_to(Point target, controller_ptr controller, direction); } - void AbstractChassis::wait_until_settled() { - while(this->task_running.load()) { + while (this->task_running.load()) { pros::delay(constants::MOTOR_UPDATE_DELAY); } } +bool AbstractChassis::is_settled() { + return this->task_running.load(); +} + } // namespace voss::chassis \ No newline at end of file From 4b1d4dc480a13813479997f538702f6f9791ab70 Mon Sep 17 00:00:00 2001 From: Rocky14683 Date: Tue, 17 Dec 2024 17:09:52 +0800 Subject: [PATCH 3/4] according to chatgpt, using implicit load and store is better --- src/VOSS/chassis/AbstractChassis.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/VOSS/chassis/AbstractChassis.cpp b/src/VOSS/chassis/AbstractChassis.cpp index 48f90d06..f0f32cb8 100644 --- a/src/VOSS/chassis/AbstractChassis.cpp +++ b/src/VOSS/chassis/AbstractChassis.cpp @@ -25,13 +25,13 @@ void AbstractChassis::move_task(controller_ptr controller, ec_ptr ec, flags & voss::Flags::THRU, ec), max)) { if (pros::competition::is_disabled()) { - this->task_running.store(false); + this->task_running = false; return; } pros::delay(constants::MOTOR_UPDATE_DELAY); } - this->task_running.store(false); + this->task_running = false; }); // Early exit for async movement @@ -55,13 +55,13 @@ void AbstractChassis::turn_task(controller_ptr controller, ec_ptr ec, flags & voss::Flags::THRU, direction, ec), max)) { if (pros::competition::is_disabled()) { - this->task_running.store(false); + this->task_running = false; return; } pros::delay(constants::MOTOR_UPDATE_DELAY); } - this->task_running.store(false); + this->task_running = false; }); // Early exit for async movement @@ -99,10 +99,10 @@ void AbstractChassis::move(Pose target, controller_ptr controller, double max, void AbstractChassis::move(Pose target, controller_ptr controller, ec_ptr ec, double max, voss::Flags flags) { - while (this->task_running.load()) { + while (this->task_running) { pros::delay(constants::MOTOR_UPDATE_DELAY); } - this->task_running.store(true); + this->task_running = true; controller->set_target(target, flags & voss::Flags::RELATIVE, ec); this->move_task(std::move(controller), std::move(ec), max, flags); @@ -124,10 +124,10 @@ void AbstractChassis::turn(double target, controller_ptr controller, double max, void AbstractChassis::turn(double target, controller_ptr controller, ec_ptr ec, double max, voss::Flags flags, voss::AngularDirection direction) { - while (this->task_running.load()) { + while (this->task_running) { pros::delay(constants::MOTOR_UPDATE_DELAY); } - this->task_running.store(true); + this->task_running = true; controller->set_target({NAN, NAN, target}, flags & voss::Flags::RELATIVE, ec); @@ -153,10 +153,10 @@ void AbstractChassis::turn_to(Point target, controller_ptr controller, void AbstractChassis::turn_to(Point target, controller_ptr controller, ec_ptr ec, double max, voss::Flags flags, voss::AngularDirection direction) { - while (this->task_running.load()) { + while (this->task_running) { pros::delay(10); } - this->task_running.store(true); + this->task_running = true; controller->set_target({target.x, target.y, std::nullopt}, flags & voss::Flags::RELATIVE, ec); @@ -166,13 +166,13 @@ void AbstractChassis::turn_to(Point target, controller_ptr controller, } void AbstractChassis::wait_until_settled() { - while (this->task_running.load()) { + while (this->task_running) { pros::delay(constants::MOTOR_UPDATE_DELAY); } } bool AbstractChassis::is_settled() { - return this->task_running.load(); + return this->task_running; } } // namespace voss::chassis \ No newline at end of file From e3b5cfb0e9f3eac02c9394f100ee9a961892f215 Mon Sep 17 00:00:00 2001 From: Rocky14683 Date: Mon, 23 Dec 2024 18:39:16 +0800 Subject: [PATCH 4/4] make some minor changes --- include/VOSS/chassis/AbstractChassis.hpp | 4 ++-- src/VOSS/chassis/AbstractChassis.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/VOSS/chassis/AbstractChassis.hpp b/include/VOSS/chassis/AbstractChassis.hpp index ce783022..94c46f57 100644 --- a/include/VOSS/chassis/AbstractChassis.hpp +++ b/include/VOSS/chassis/AbstractChassis.hpp @@ -39,8 +39,8 @@ class AbstractChassis { virtual bool execute(DiffChassisCommand cmd, double max) = 0; virtual void set_brake_mode(pros::motor_brake_mode_e mode) = 0; - void wait_until_settled(); - bool is_settled(); + void wait_until_settled() const; + bool is_settled() const; void move(double distance, double max = 100.0, voss::Flags flags = voss::Flags::NONE); diff --git a/src/VOSS/chassis/AbstractChassis.cpp b/src/VOSS/chassis/AbstractChassis.cpp index f0f32cb8..5bf971ad 100644 --- a/src/VOSS/chassis/AbstractChassis.cpp +++ b/src/VOSS/chassis/AbstractChassis.cpp @@ -154,7 +154,7 @@ void AbstractChassis::turn_to(Point target, controller_ptr controller, ec_ptr ec, double max, voss::Flags flags, voss::AngularDirection direction) { while (this->task_running) { - pros::delay(10); + pros::delay(constants::MOTOR_UPDATE_DELAY); } this->task_running = true; @@ -165,13 +165,13 @@ void AbstractChassis::turn_to(Point target, controller_ptr controller, direction); } -void AbstractChassis::wait_until_settled() { +void AbstractChassis::wait_until_settled() const { while (this->task_running) { pros::delay(constants::MOTOR_UPDATE_DELAY); } } -bool AbstractChassis::is_settled() { +bool AbstractChassis::is_settled() const { return this->task_running; }