Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Added Motor Group Append/Erase Functions #707

Open
wants to merge 4 commits into
base: develop-pros-4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions include/pros/motor_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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:
/**
Expand Down
27 changes: 27 additions & 0 deletions src/devices/vdml_motorgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,5 +676,32 @@ void MotorGroup::erase_port(std::int8_t port) {
}
}
}

void MotorGroup::append(std::int8_t 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();

// 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) {
(*this) -= other;
}


} // namespace v5
} // namespace pros
1 change: 0 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +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


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,
Expand Down