[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

@@ -12,16 +12,14 @@ DriveSubsystem::DriveSubsystem()
m_rightLeader{kRightMotor1Port},
m_rightFollower{kRightMotor2Port},
m_feedforward{ks, kv, ka} {
wpi::SendableRegistry::AddChild(&m_drive, &m_leftLeader);
wpi::SendableRegistry::AddChild(&m_drive, &m_rightLeader);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightLeader.SetInverted(true);
// You might need to not do this depending on the specific motor controller
// that you are using -- contact the respective vendor's documentation for
// more details.
m_rightFollower.SetInverted(true);
m_leftFollower.Follow(m_leftLeader);
m_rightFollower.Follow(m_rightLeader);

View File

@@ -4,15 +4,13 @@
#pragma once
#include <frc/motorcontrol/MotorController.h>
/**
* A simplified stub class that simulates the API of a common "smart" motor
* controller.
*
* <p>Has no actual functionality.
*/
class ExampleSmartMotorController : public frc::MotorController {
class ExampleSmartMotorController {
public:
enum PIDMode { kPosition, kVelocity, kMovementWitchcraft };
@@ -68,17 +66,17 @@ class ExampleSmartMotorController : public frc::MotorController {
*/
void ResetEncoder() {}
void Set(double speed) override { m_value = speed; }
void Set(double speed) { m_value = speed; }
double Get() const override { return m_value; }
double Get() const { return m_value; }
void SetInverted(bool isInverted) override {}
void SetInverted(bool isInverted) {}
bool GetInverted() const override { return false; }
bool GetInverted() const { return false; }
void Disable() override {}
void Disable() {}
void StopMotor() override {}
void StopMotor() {}
private:
double m_value = 0.0;

View File

@@ -83,5 +83,7 @@ class DriveSubsystem : public frc2::SubsystemBase {
frc::SimpleMotorFeedforward<units::meters> m_feedforward;
// The robot's drive
frc::DifferentialDrive m_drive{m_leftLeader, m_rightLeader};
frc::DifferentialDrive m_drive{
[&](double output) { m_leftLeader.Set(output); },
[&](double output) { m_rightLeader.Set(output); }};
};