[wpilib] PWMSpeedController: Use PWM by composition (#3248)

This cleans up the user experience by removing lower-level functions from the
interface.

Also remove MotorSafety from "raw" PWM.
This commit is contained in:
Peter Johnson
2021-03-21 11:12:49 -07:00
committed by GitHub
parent 160fb740f4
commit 9550777b9d
32 changed files with 242 additions and 252 deletions

View File

@@ -5,11 +5,14 @@
#pragma once
#include <wpi/mutex.h>
#include <wpi/raw_ostream.h>
#include "frc/ErrorBase.h"
#include "frc/Timer.h"
namespace wpi {
class raw_ostream;
} // namespace wpi
namespace frc {
/**

View File

@@ -7,9 +7,8 @@
#include <stdint.h>
#include <hal/Types.h>
#include <wpi/raw_ostream.h>
#include "frc/MotorSafety.h"
#include "frc/ErrorBase.h"
#include "frc/smartdashboard/Sendable.h"
#include "frc/smartdashboard/SendableHelper.h"
@@ -34,7 +33,7 @@ class SendableBuilder;
* - 1 = minimum pulse width (currently 0.5ms)
* - 0 = disabled (i.e. PWM output is held low)
*/
class PWM : public MotorSafety, public Sendable, public SendableHelper<PWM> {
class PWM : public ErrorBase, public Sendable, public SendableHelper<PWM> {
public:
friend class AddressableLED;
/**
@@ -64,8 +63,10 @@ class PWM : public MotorSafety, public Sendable, public SendableHelper<PWM> {
*
* @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the
* MXP port
* @param registerSendable If true, adds this instance to SendableRegistry
* and LiveWindow
*/
explicit PWM(int channel);
explicit PWM(int channel, bool registerSendable = true);
/**
* Free the PWM channel.
@@ -77,10 +78,6 @@ class PWM : public MotorSafety, public Sendable, public SendableHelper<PWM> {
PWM(PWM&&) = default;
PWM& operator=(PWM&&) = default;
// MotorSafety interface
void StopMotor() override;
void GetDescription(wpi::raw_ostream& desc) const override;
/**
* Set the PWM value directly to the hardware.
*

View File

@@ -4,15 +4,27 @@
#pragma once
#include <wpi/Twine.h>
#include "frc/MotorSafety.h"
#include "frc/PWM.h"
#include "frc/SpeedController.h"
#include "frc/smartdashboard/Sendable.h"
#include "frc/smartdashboard/SendableHelper.h"
namespace wpi {
class raw_ostream;
} // namespace wpi
namespace frc {
/**
* Common base class for all PWM Speed Controllers.
*/
class PWMSpeedController : public PWM, public SpeedController {
class PWMSpeedController : public SpeedController,
public MotorSafety,
public Sendable,
public SendableHelper<PWMSpeedController> {
public:
PWMSpeedController(PWMSpeedController&&) = default;
PWMSpeedController& operator=(PWMSpeedController&&) = default;
@@ -42,7 +54,11 @@ class PWMSpeedController : public PWM, public SpeedController {
void Disable() override;
// MotorSafety interface
void StopMotor() override;
void GetDescription(wpi::raw_ostream& desc) const override;
int GetChannel() const;
/**
* Write out the PID value as seen in the PIDOutput base object.
@@ -55,13 +71,16 @@ class PWMSpeedController : public PWM, public SpeedController {
/**
* Constructor for a PWM Speed Controller connected via PWM.
*
* @param name Name to use for SendableRegistry
* @param channel The PWM channel that the controller is attached to. 0-9 are
* on-board, 10-19 are on the MXP port
*/
explicit PWMSpeedController(int channel);
PWMSpeedController(const wpi::Twine& name, int channel);
void InitSendable(SendableBuilder& builder) override;
PWM m_pwm;
private:
bool m_isInverted = false;
};