From 990805b9c9be38ba9f3e6479fae8877d44bea641 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Sat, 12 Oct 2024 20:24:09 -0400 Subject: [PATCH 1/4] Added erase_port and append functions to MotorGroup --- include/pros/motor_group.hpp | 22 ++++++++++++++++++++++ src/devices/vdml_motorgroup.cpp | 24 ++++++++++++++++++++++++ src/main.cpp | 6 ++++++ 3 files changed, 52 insertions(+) diff --git a/include/pros/motor_group.hpp b/include/pros/motor_group.hpp index 33edd0f5..6a2c31af 100644 --- a/include/pros/motor_group.hpp +++ b/include/pros/motor_group.hpp @@ -2357,6 +2357,15 @@ class MotorGroup : public virtual AbstractMotor { */ void append(AbstractMotor&); + + /** + * Appends all the motors on the port to this motor group + * + * \param port The port to append to the motor group + * + */ + void append(std::int8_t port); + /** * Removes the all motors on the port (regardless of reversal) from the motor group * @@ -2365,6 +2374,19 @@ class MotorGroup : public virtual AbstractMotor { */ void erase_port(std::int8_t port); + /** + * Removes all the motors in the other motor group reference from this motor group + * + * Maintains the order of the other motor group + * + */ + void operator-=(AbstractMotor&); + + /** + * Removes all the motors in the other motor group reference from this motor group + */ + void erase_port(AbstractMotor&); + ///@} private: /** diff --git a/src/devices/vdml_motorgroup.cpp b/src/devices/vdml_motorgroup.cpp index ab63102e..d7485716 100644 --- a/src/devices/vdml_motorgroup.cpp +++ b/src/devices/vdml_motorgroup.cpp @@ -676,5 +676,29 @@ void MotorGroup::erase_port(std::int8_t port) { } } } + +// Question: Do we need to deal with port is already part of motor group? +void MotorGroup::append(std::int8_t port) { + _ports.push_back(port); +} + +void MotorGroup::operator-=(AbstractMotor& other) { + auto ports = other.get_port_all(); + for (auto it = ports.begin(); it < ports.end(); it++) { + auto port = *it; + + // Locate port in _ports, remove if found + auto port_it = std::find(_ports.begin(), _ports.end(), port); + if (port_it != _ports.end()) { + _ports.erase(port_it); + } + } +} + +void MotorGroup::erase_port(AbstractMotor& other) { + (*this) -= other; +} + + } // namespace v5 } // namespace pros \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7fb87394..d7795d03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,6 +78,12 @@ void opcontrol() { pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 5 and reversed ports 4 & 6 + // Create test motor group + pros::MotorGroup test({7, 8}); + // Try to append port using new append function + left_mg.append(7); + // Try to erase ports using new erase function + left_mg.erase_port(test); while (true) { pros::lcd::print(0, "%d %d %d", (pros::lcd::read_buttons() & LCD_BTN_LEFT) >> 2, From 90e432d54f3819fc179105c5b1b18e39f114dcc3 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Tue, 22 Oct 2024 23:21:19 -0400 Subject: [PATCH 2/4] Updated append function --- src/devices/vdml_motorgroup.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/devices/vdml_motorgroup.cpp b/src/devices/vdml_motorgroup.cpp index d7485716..0c1ad533 100644 --- a/src/devices/vdml_motorgroup.cpp +++ b/src/devices/vdml_motorgroup.cpp @@ -677,7 +677,6 @@ void MotorGroup::erase_port(std::int8_t port) { } } -// Question: Do we need to deal with port is already part of motor group? void MotorGroup::append(std::int8_t port) { _ports.push_back(port); } From 16a0d9007e38f9fd0d5e7a17bbf0fe6f0bee6be3 Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Tue, 22 Oct 2024 23:22:30 -0400 Subject: [PATCH 3/4] Got rid of test code --- src/main.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d7795d03..3a30d1e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,13 +78,6 @@ void opcontrol() { pros::MotorGroup left_mg({1, -2, 3}); // Creates a motor group with forwards ports 1 & 3 and reversed port 2 pros::MotorGroup right_mg({-4, 5, -6}); // Creates a motor group with forwards port 5 and reversed ports 4 & 6 - // Create test motor group - pros::MotorGroup test({7, 8}); - // Try to append port using new append function - left_mg.append(7); - // Try to erase ports using new erase function - left_mg.erase_port(test); - while (true) { pros::lcd::print(0, "%d %d %d", (pros::lcd::read_buttons() & LCD_BTN_LEFT) >> 2, (pros::lcd::read_buttons() & LCD_BTN_CENTER) >> 1, From bf62f1d5819e0b4ddd3d65f897110d74ec733d6c Mon Sep 17 00:00:00 2001 From: Grace Lu Date: Thu, 24 Oct 2024 20:29:16 -0400 Subject: [PATCH 4/4] Updated append/erase functions --- src/devices/vdml_motorgroup.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/devices/vdml_motorgroup.cpp b/src/devices/vdml_motorgroup.cpp index 0c1ad533..14f96c4d 100644 --- a/src/devices/vdml_motorgroup.cpp +++ b/src/devices/vdml_motorgroup.cpp @@ -678,20 +678,24 @@ void MotorGroup::erase_port(std::int8_t port) { } void MotorGroup::append(std::int8_t port) { - _ports.push_back(port); + if (std::find_if(_ports.begin(), _ports.end(), + [port](std::int8_t p) { return std::abs(p) == std::abs(port); }) + == _ports.end()) { + // If no matching absolute value is found, append the port + _ports.push_back(port); + } } void MotorGroup::operator-=(AbstractMotor& other) { auto ports = other.get_port_all(); - for (auto it = ports.begin(); it < ports.end(); it++) { - auto port = *it; - // Locate port in _ports, remove if found - auto port_it = std::find(_ports.begin(), _ports.end(), port); - if (port_it != _ports.end()) { - _ports.erase(port_it); - } - } + // Iterate over the ports in 'other' + for (auto port : ports) { + // Remove ports that match the absolute value + _ports.erase(std::remove_if(_ports.begin(), _ports.end(), + [port](std::int8_t p) { return std::abs(p) == std::abs(port); }), + _ports.end()); + } } void MotorGroup::erase_port(AbstractMotor& other) {