mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[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:
@@ -16,11 +16,21 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
WPI_IGNORE_DEPRECATED
|
||||
|
||||
DifferentialDrive::DifferentialDrive(MotorController& leftMotor,
|
||||
MotorController& rightMotor)
|
||||
: m_leftMotor(&leftMotor), m_rightMotor(&rightMotor) {
|
||||
wpi::SendableRegistry::AddChild(this, m_leftMotor);
|
||||
wpi::SendableRegistry::AddChild(this, m_rightMotor);
|
||||
: DifferentialDrive{[&](double output) { leftMotor.Set(output); },
|
||||
[&](double output) { rightMotor.Set(output); }} {
|
||||
wpi::SendableRegistry::AddChild(this, &leftMotor);
|
||||
wpi::SendableRegistry::AddChild(this, &rightMotor);
|
||||
}
|
||||
|
||||
WPI_UNIGNORE_DEPRECATED
|
||||
|
||||
DifferentialDrive::DifferentialDrive(std::function<void(double)> leftMotor,
|
||||
std::function<void(double)> rightMotor)
|
||||
: m_leftMotor{std::move(leftMotor)}, m_rightMotor{std::move(rightMotor)} {
|
||||
static int instances = 0;
|
||||
++instances;
|
||||
wpi::SendableRegistry::AddLW(this, "DifferentialDrive", instances);
|
||||
@@ -40,8 +50,11 @@ void DifferentialDrive::ArcadeDrive(double xSpeed, double zRotation,
|
||||
|
||||
auto [left, right] = ArcadeDriveIK(xSpeed, zRotation, squareInputs);
|
||||
|
||||
m_leftMotor->Set(left * m_maxOutput);
|
||||
m_rightMotor->Set(right * m_maxOutput);
|
||||
m_leftOutput = left * m_maxOutput;
|
||||
m_rightOutput = right * m_maxOutput;
|
||||
|
||||
m_leftMotor(m_leftOutput);
|
||||
m_rightMotor(m_rightOutput);
|
||||
|
||||
Feed();
|
||||
}
|
||||
@@ -60,8 +73,11 @@ void DifferentialDrive::CurvatureDrive(double xSpeed, double zRotation,
|
||||
|
||||
auto [left, right] = CurvatureDriveIK(xSpeed, zRotation, allowTurnInPlace);
|
||||
|
||||
m_leftMotor->Set(left * m_maxOutput);
|
||||
m_rightMotor->Set(right * m_maxOutput);
|
||||
m_leftOutput = left * m_maxOutput;
|
||||
m_rightOutput = right * m_maxOutput;
|
||||
|
||||
m_leftMotor(m_leftOutput);
|
||||
m_rightMotor(m_rightOutput);
|
||||
|
||||
Feed();
|
||||
}
|
||||
@@ -80,8 +96,11 @@ void DifferentialDrive::TankDrive(double leftSpeed, double rightSpeed,
|
||||
|
||||
auto [left, right] = TankDriveIK(leftSpeed, rightSpeed, squareInputs);
|
||||
|
||||
m_leftMotor->Set(left * m_maxOutput);
|
||||
m_rightMotor->Set(right * m_maxOutput);
|
||||
m_leftOutput = left * m_maxOutput;
|
||||
m_rightOutput = right * m_maxOutput;
|
||||
|
||||
m_leftMotor(m_leftOutput);
|
||||
m_rightMotor(m_rightOutput);
|
||||
|
||||
Feed();
|
||||
}
|
||||
@@ -157,8 +176,12 @@ DifferentialDrive::WheelSpeeds DifferentialDrive::TankDriveIK(
|
||||
}
|
||||
|
||||
void DifferentialDrive::StopMotor() {
|
||||
m_leftMotor->StopMotor();
|
||||
m_rightMotor->StopMotor();
|
||||
m_leftOutput = 0.0;
|
||||
m_rightOutput = 0.0;
|
||||
|
||||
m_leftMotor(0.0);
|
||||
m_rightMotor(0.0);
|
||||
|
||||
Feed();
|
||||
}
|
||||
|
||||
@@ -171,9 +194,7 @@ void DifferentialDrive::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetActuator(true);
|
||||
builder.SetSafeState([=, this] { StopMotor(); });
|
||||
builder.AddDoubleProperty(
|
||||
"Left Motor Speed", [=, this] { return m_leftMotor->Get(); },
|
||||
[=, this](double value) { m_leftMotor->Set(value); });
|
||||
"Left Motor Speed", [&] { return m_leftOutput; }, m_leftMotor);
|
||||
builder.AddDoubleProperty(
|
||||
"Right Motor Speed", [=, this] { return m_rightMotor->Get(); },
|
||||
[=, this](double value) { m_rightMotor->Set(value); });
|
||||
"Right Motor Speed", [&] { return m_rightOutput; }, m_rightMotor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user