[wpilib] Add functional interface equivalents to MotorController (#6053)

This does not deprecate any current functionality, but prepares the way for future deprecation.

The drive classes now accept void(double) functions, which makes them more flexible.

The C++ API ended up a bit more verbose, but the Java API is really concise with method references, which is >80% of our userbase. For example:

`DifferentialDrive drive = new DifferentialDrive(m_leftMotor::set, m_rightMotor::set);`

Lambdas can be passed to interoperate with vendor motor controller APIs that don't have e.g., set(double), so CTRE doesn't have to maintain their WPI_ classes anymore.

MotorControllerGroup was replaced with PWMMotorController.addFollower() for PWM motor controllers. Users of CAN motor controllers should use their vendor's follower functionality.
This commit is contained in:
Tyler Veness
2024-01-01 13:37:51 -08:00
committed by GitHub
parent 8aca706217
commit e7c9f27683
132 changed files with 1159 additions and 697 deletions

View File

@@ -5,7 +5,7 @@
#include <gtest/gtest.h>
#include "frc/drive/DifferentialDrive.h"
#include "motorcontrol/MockMotorController.h"
#include "motorcontrol/MockPWMMotorController.h"
TEST(DifferentialDriveTest, ArcadeDriveIK) {
// Forward
@@ -240,9 +240,10 @@ TEST(DifferentialDriveTest, TankDriveIKSquared) {
}
TEST(DifferentialDriveTest, ArcadeDrive) {
frc::MockMotorController left;
frc::MockMotorController right;
frc::DifferentialDrive drive{left, right};
frc::MockPWMMotorController left;
frc::MockPWMMotorController right;
frc::DifferentialDrive drive{[&](double output) { left.Set(output); },
[&](double output) { right.Set(output); }};
drive.SetDeadband(0.0);
// Forward
@@ -277,9 +278,10 @@ TEST(DifferentialDriveTest, ArcadeDrive) {
}
TEST(DifferentialDriveTest, ArcadeDriveSquared) {
frc::MockMotorController left;
frc::MockMotorController right;
frc::DifferentialDrive drive{left, right};
frc::MockPWMMotorController left;
frc::MockPWMMotorController right;
frc::DifferentialDrive drive{[&](double output) { left.Set(output); },
[&](double output) { right.Set(output); }};
drive.SetDeadband(0.0);
// Forward
@@ -314,9 +316,10 @@ TEST(DifferentialDriveTest, ArcadeDriveSquared) {
}
TEST(DifferentialDriveTest, CurvatureDrive) {
frc::MockMotorController left;
frc::MockMotorController right;
frc::DifferentialDrive drive{left, right};
frc::MockPWMMotorController left;
frc::MockPWMMotorController right;
frc::DifferentialDrive drive{[&](double output) { left.Set(output); },
[&](double output) { right.Set(output); }};
drive.SetDeadband(0.0);
// Forward
@@ -351,9 +354,10 @@ TEST(DifferentialDriveTest, CurvatureDrive) {
}
TEST(DifferentialDriveTest, CurvatureDriveTurnInPlace) {
frc::MockMotorController left;
frc::MockMotorController right;
frc::DifferentialDrive drive{left, right};
frc::MockPWMMotorController left;
frc::MockPWMMotorController right;
frc::DifferentialDrive drive{[&](double output) { left.Set(output); },
[&](double output) { right.Set(output); }};
drive.SetDeadband(0.0);
// Forward
@@ -388,9 +392,10 @@ TEST(DifferentialDriveTest, CurvatureDriveTurnInPlace) {
}
TEST(DifferentialDriveTest, TankDrive) {
frc::MockMotorController left;
frc::MockMotorController right;
frc::DifferentialDrive drive{left, right};
frc::MockPWMMotorController left;
frc::MockPWMMotorController right;
frc::DifferentialDrive drive{[&](double output) { left.Set(output); },
[&](double output) { right.Set(output); }};
drive.SetDeadband(0.0);
// Forward
@@ -425,9 +430,10 @@ TEST(DifferentialDriveTest, TankDrive) {
}
TEST(DifferentialDriveTest, TankDriveSquared) {
frc::MockMotorController left;
frc::MockMotorController right;
frc::DifferentialDrive drive{left, right};
frc::MockPWMMotorController left;
frc::MockPWMMotorController right;
frc::DifferentialDrive drive{[&](double output) { left.Set(output); },
[&](double output) { right.Set(output); }};
drive.SetDeadband(0.0);
// Forward

View File

@@ -5,7 +5,7 @@
#include <gtest/gtest.h>
#include "frc/drive/MecanumDrive.h"
#include "motorcontrol/MockMotorController.h"
#include "motorcontrol/MockPWMMotorController.h"
TEST(MecanumDriveTest, CartesianIK) {
// Forward
@@ -82,11 +82,14 @@ TEST(MecanumDriveTest, CartesianIKGyro90CW) {
}
TEST(MecanumDriveTest, Cartesian) {
frc::MockMotorController fl;
frc::MockMotorController rl;
frc::MockMotorController fr;
frc::MockMotorController rr;
frc::MecanumDrive drive{fl, rl, fr, rr};
frc::MockPWMMotorController fl;
frc::MockPWMMotorController rl;
frc::MockPWMMotorController fr;
frc::MockPWMMotorController rr;
frc::MecanumDrive drive{[&](double output) { fl.Set(output); },
[&](double output) { rl.Set(output); },
[&](double output) { fr.Set(output); },
[&](double output) { rr.Set(output); }};
drive.SetDeadband(0.0);
// Forward
@@ -126,11 +129,14 @@ TEST(MecanumDriveTest, Cartesian) {
}
TEST(MecanumDriveTest, CartesianGyro90CW) {
frc::MockMotorController fl;
frc::MockMotorController fr;
frc::MockMotorController rl;
frc::MockMotorController rr;
frc::MecanumDrive drive{fl, rl, fr, rr};
frc::MockPWMMotorController fl;
frc::MockPWMMotorController rl;
frc::MockPWMMotorController fr;
frc::MockPWMMotorController rr;
frc::MecanumDrive drive{[&](double output) { fl.Set(output); },
[&](double output) { rl.Set(output); },
[&](double output) { fr.Set(output); },
[&](double output) { rr.Set(output); }};
drive.SetDeadband(0.0);
// Forward in global frame; left in robot frame
@@ -170,11 +176,14 @@ TEST(MecanumDriveTest, CartesianGyro90CW) {
}
TEST(MecanumDriveTest, Polar) {
frc::MockMotorController fl;
frc::MockMotorController fr;
frc::MockMotorController rl;
frc::MockMotorController rr;
frc::MecanumDrive drive{fl, rl, fr, rr};
frc::MockPWMMotorController fl;
frc::MockPWMMotorController rl;
frc::MockPWMMotorController fr;
frc::MockPWMMotorController rr;
frc::MecanumDrive drive{[&](double output) { fl.Set(output); },
[&](double output) { rl.Set(output); },
[&](double output) { fr.Set(output); },
[&](double output) { rr.Set(output); }};
drive.SetDeadband(0.0);
// Forward