[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

@@ -7,19 +7,25 @@
#include <functional>
#include <vector>
#include <wpi/deprecated.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include "frc/motorcontrol/MotorController.h"
WPI_IGNORE_DEPRECATED
namespace frc {
/**
* Allows multiple MotorController objects to be linked together.
*/
class MotorControllerGroup : public wpi::Sendable,
public MotorController,
public wpi::SendableHelper<MotorControllerGroup> {
class [[deprecated(
"Use CAN motor controller followers or "
"PWMMotorController::AddFollower()")]] MotorControllerGroup
: public wpi::Sendable,
public MotorController,
public wpi::SendableHelper<MotorControllerGroup> {
public:
template <class... MotorControllers>
explicit MotorControllerGroup(MotorController& motorController,
@@ -50,3 +56,5 @@ class MotorControllerGroup : public wpi::Sendable,
} // namespace frc
#include "frc/motorcontrol/MotorControllerGroup.inc"
WPI_UNIGNORE_DEPRECATED

View File

@@ -6,6 +6,7 @@
#include <string>
#include <wpi/deprecated.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -16,6 +17,8 @@
namespace frc {
WPI_IGNORE_DEPRECATED
/**
* Nidec Brushless Motor.
*/
@@ -95,4 +98,6 @@ class NidecBrushless : public MotorController,
double m_speed = 0.0;
};
WPI_UNIGNORE_DEPRECATED
} // namespace frc

View File

@@ -4,9 +4,16 @@
#pragma once
#include <concepts>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include <units/voltage.h>
#include <wpi/deprecated.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -17,6 +24,8 @@
namespace frc {
class DMA;
WPI_IGNORE_DEPRECATED
/**
* Common base class for all PWM Motor Controllers.
*/
@@ -40,6 +49,20 @@ class PWMMotorController : public MotorController,
*/
void Set(double value) override;
/**
* Sets the voltage output of the PWMMotorController. Compensates for
* the current bus voltage to ensure that the desired voltage is output even
* if the battery voltage is below 12V - highly useful when the voltage
* outputs are "meaningful" (e.g. they come from a feedforward calculation).
*
* <p>NOTE: This function *must* be called regularly in order for voltage
* compensation to work properly - unlike the ordinary set function, it is not
* "set it and forget it."
*
* @param output The voltage to output.
*/
void SetVoltage(units::volt_t output) override;
/**
* Get the recently set value of the PWM. This value is affected by the
* inversion property. If you want the value that is sent directly to the
@@ -71,6 +94,24 @@ class PWMMotorController : public MotorController,
*/
void EnableDeadbandElimination(bool eliminateDeadband);
/**
* Make the given PWM motor controller follow the output of this one.
*
* @param follower The motor controller follower.
*/
void AddFollower(PWMMotorController& follower);
/**
* Make the given PWM motor controller follow the output of this one.
*
* @param follower The motor controller follower.
*/
template <std::derived_from<PWMMotorController> T>
void AddFollower(T&& follower) {
m_owningFollowers.emplace_back(
std::make_unique<std::decay_t<T>>(std::forward<T>(follower)));
}
protected:
/**
* Constructor for a PWM Motor %Controller connected via PWM.
@@ -87,8 +128,12 @@ class PWMMotorController : public MotorController,
private:
bool m_isInverted = false;
std::vector<PWMMotorController*> m_nonowningFollowers;
std::vector<std::unique_ptr<PWMMotorController>> m_owningFollowers;
PWM* GetPwm() { return &m_pwm; }
};
WPI_UNIGNORE_DEPRECATED
} // namespace frc