[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

@@ -4,8 +4,10 @@
#pragma once
#include <functional>
#include <string>
#include <wpi/deprecated.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -20,43 +22,9 @@ class MotorController;
* the Kit of Parts drive base, "tank drive", or West Coast Drive.
*
* These drive bases typically have drop-center / skid-steer with two or more
* wheels per side (e.g., 6WD or 8WD). This class takes a MotorController per
* side. For four and six motor drivetrains, construct and pass in
* MotorControllerGroup instances as follows.
*
* Four motor drivetrain:
* @code{.cpp}
* class Robot {
* public:
* frc::PWMVictorSPX m_frontLeft{1};
* frc::PWMVictorSPX m_rearLeft{2};
* frc::MotorControllerGroup m_left{m_frontLeft, m_rearLeft};
*
* frc::PWMVictorSPX m_frontRight{3};
* frc::PWMVictorSPX m_rearRight{4};
* frc::MotorControllerGroup m_right{m_frontRight, m_rearRight};
*
* frc::DifferentialDrive m_drive{m_left, m_right};
* };
* @endcode
*
* Six motor drivetrain:
* @code{.cpp}
* class Robot {
* public:
* frc::PWMVictorSPX m_frontLeft{1};
* frc::PWMVictorSPX m_midLeft{2};
* frc::PWMVictorSPX m_rearLeft{3};
* frc::MotorControllerGroup m_left{m_frontLeft, m_midLeft, m_rearLeft};
*
* frc::PWMVictorSPX m_frontRight{4};
* frc::PWMVictorSPX m_midRight{5};
* frc::PWMVictorSPX m_rearRight{6};
* frc::MotorControllerGroup m_right{m_frontRight, m_midRight, m_rearRight};
*
* frc::DifferentialDrive m_drive{m_left, m_right};
* };
* @endcode
* wheels per side (e.g., 6WD or 8WD). This class takes a setter per side. For
* four and six motor drivetrains, use CAN motor controller followers or
* PWMMotorController::AddFollower().
*
* A differential drive robot has left and right wheels separated by an
* arbitrary width.
@@ -101,13 +69,33 @@ class DifferentialDrive : public RobotDriveBase,
double right = 0.0;
};
WPI_IGNORE_DEPRECATED
/**
* Construct a DifferentialDrive.
*
* To pass multiple motors per side, use CAN motor controller followers or
* PWMSpeedController::AddFollower(). If a motor needs to be inverted, do so
* before passing it in.
*
* @param leftMotor Left motor.
* @param rightMotor Right motor.
*/
DifferentialDrive(MotorController& leftMotor, MotorController& rightMotor);
WPI_UNIGNORE_DEPRECATED
/**
* Construct a DifferentialDrive.
*
* To pass multiple motors per side, use a MotorControllerGroup. If a motor
* needs to be inverted, do so before passing it in.
*
* @param leftMotor Left motor setter.
* @param rightMotor Right motor setter.
*/
DifferentialDrive(MotorController& leftMotor, MotorController& rightMotor);
DifferentialDrive(std::function<void(double)> leftMotor,
std::function<void(double)> rightMotor);
~DifferentialDrive() override = default;
@@ -210,8 +198,12 @@ class DifferentialDrive : public RobotDriveBase,
void InitSendable(wpi::SendableBuilder& builder) override;
private:
MotorController* m_leftMotor;
MotorController* m_rightMotor;
std::function<void(double)> m_leftMotor;
std::function<void(double)> m_rightMotor;
// Used for Sendable property getters
double m_leftOutput = 0.0;
double m_rightOutput = 0.0;
};
} // namespace frc

View File

@@ -4,10 +4,12 @@
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <units/angle.h>
#include <wpi/deprecated.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -69,15 +71,39 @@ class MecanumDrive : public RobotDriveBase,
double rearRight = 0.0;
};
WPI_IGNORE_DEPRECATED
/**
* Construct a MecanumDrive.
*
* If a motor needs to be inverted, do so before passing it in.
*
* @param frontLeftMotor Front-left motor.
* @param rearLeftMotor Rear-left motor.
* @param frontRightMotor Front-right motor.
* @param rearRightMotor Rear-right motor.
*/
MecanumDrive(MotorController& frontLeftMotor, MotorController& rearLeftMotor,
MotorController& frontRightMotor,
MotorController& rearRightMotor);
WPI_UNIGNORE_DEPRECATED
/**
* Construct a MecanumDrive.
*
* If a motor needs to be inverted, do so before passing it in.
*
* @param frontLeftMotor Front-left motor setter.
* @param rearLeftMotor Rear-left motor setter.
* @param frontRightMotor Front-right motor setter.
* @param rearRightMotor Rear-right motor setter.
*/
MecanumDrive(std::function<void(double)> frontLeftMotor,
std::function<void(double)> rearLeftMotor,
std::function<void(double)> frontRightMotor,
std::function<void(double)> rearRightMotor);
~MecanumDrive() override = default;
MecanumDrive(MecanumDrive&&) = default;
@@ -141,10 +167,16 @@ class MecanumDrive : public RobotDriveBase,
void InitSendable(wpi::SendableBuilder& builder) override;
private:
MotorController* m_frontLeftMotor;
MotorController* m_rearLeftMotor;
MotorController* m_frontRightMotor;
MotorController* m_rearRightMotor;
std::function<void(double)> m_frontLeftMotor;
std::function<void(double)> m_rearLeftMotor;
std::function<void(double)> m_frontRightMotor;
std::function<void(double)> m_rearRightMotor;
// Used for Sendable property getters
double m_frontLeftOutput = 0.0;
double m_rearLeftOutput = 0.0;
double m_frontRightOutput = 0.0;
double m_rearRightOutput = 0.0;
bool reported = false;
};