diff --git a/wpilibc/src/main/native/cpp/DMC60.cpp b/wpilibc/src/main/native/cpp/DMC60.cpp index f84d839e62..001a51e5c8 100644 --- a/wpilibc/src/main/native/cpp/DMC60.cpp +++ b/wpilibc/src/main/native/cpp/DMC60.cpp @@ -6,16 +6,13 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -DMC60::DMC60(int channel) : PWMSpeedController(channel) { - SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +DMC60::DMC60(int channel) : PWMSpeedController("DMC60", channel) { + m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_DigilentDMC60, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "DMC60", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/Jaguar.cpp b/wpilibc/src/main/native/cpp/Jaguar.cpp index eded12f082..2cf93c1de2 100644 --- a/wpilibc/src/main/native/cpp/Jaguar.cpp +++ b/wpilibc/src/main/native/cpp/Jaguar.cpp @@ -6,16 +6,13 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -Jaguar::Jaguar(int channel) : PWMSpeedController(channel) { - SetBounds(2.31, 1.55, 1.507, 1.454, 0.697); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +Jaguar::Jaguar(int channel) : PWMSpeedController("Jaguar", channel) { + m_pwm.SetBounds(2.31, 1.55, 1.507, 1.454, 0.697); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_Jaguar, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "Jaguar", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/NidecBrushless.cpp b/wpilibc/src/main/native/cpp/NidecBrushless.cpp index 1ea2b95f9b..4909a01e31 100644 --- a/wpilibc/src/main/native/cpp/NidecBrushless.cpp +++ b/wpilibc/src/main/native/cpp/NidecBrushless.cpp @@ -5,6 +5,7 @@ #include "frc/NidecBrushless.h" #include +#include #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" diff --git a/wpilibc/src/main/native/cpp/PWM.cpp b/wpilibc/src/main/native/cpp/PWM.cpp index 086553b712..2f4e1fe17f 100644 --- a/wpilibc/src/main/native/cpp/PWM.cpp +++ b/wpilibc/src/main/native/cpp/PWM.cpp @@ -19,7 +19,7 @@ using namespace frc; -PWM::PWM(int channel) { +PWM::PWM(int channel, bool registerSendable) { if (!SensorUtil::CheckPWMChannel(channel)) { wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, "PWM Channel " + wpi::Twine(channel)); @@ -44,9 +44,9 @@ PWM::PWM(int channel) { wpi_setHALError(status); HAL_Report(HALUsageReporting::kResourceType_PWM, channel + 1); - SendableRegistry::GetInstance().AddLW(this, "PWM", channel); - - SetSafetyEnabled(false); + if (registerSendable) { + SendableRegistry::GetInstance().AddLW(this, "PWM", channel); + } } PWM::~PWM() { @@ -59,14 +59,6 @@ PWM::~PWM() { wpi_setHALError(status); } -void PWM::StopMotor() { - SetDisabled(); -} - -void PWM::GetDescription(wpi::raw_ostream& desc) const { - desc << "PWM " << GetChannel(); -} - void PWM::SetRaw(uint16_t value) { if (StatusIsFatal()) { return; @@ -115,8 +107,6 @@ void PWM::SetSpeed(double speed) { int32_t status = 0; HAL_SetPWMSpeed(m_handle, speed, &status); wpi_setHALError(status); - - Feed(); } double PWM::GetSpeed() const { @@ -223,8 +213,7 @@ int PWM::GetChannel() const { void PWM::InitSendable(SendableBuilder& builder) { builder.SetSmartDashboardType("PWM"); builder.SetActuator(true); - builder.SetSafeState([=]() { SetDisabled(); }); + builder.SetSafeState([=] { SetDisabled(); }); builder.AddDoubleProperty( - "Value", [=]() { return GetRaw(); }, - [=](double value) { SetRaw(value); }); + "Value", [=] { return GetRaw(); }, [=](double value) { SetRaw(value); }); } diff --git a/wpilibc/src/main/native/cpp/PWMSparkMax.cpp b/wpilibc/src/main/native/cpp/PWMSparkMax.cpp index 1b7915cb64..2e4a08e9be 100644 --- a/wpilibc/src/main/native/cpp/PWMSparkMax.cpp +++ b/wpilibc/src/main/native/cpp/PWMSparkMax.cpp @@ -10,12 +10,12 @@ using namespace frc; -PWMSparkMax::PWMSparkMax(int channel) : PWMSpeedController(channel) { - SetBounds(2.003, 1.55, 1.50, 1.46, 0.999); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +PWMSparkMax::PWMSparkMax(int channel) + : PWMSpeedController("PWMSparkMax", channel) { + m_pwm.SetBounds(2.003, 1.55, 1.50, 1.46, 0.999); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_RevSparkMaxPWM, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "PWMSparkMax", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/PWMSpeedController.cpp b/wpilibc/src/main/native/cpp/PWMSpeedController.cpp index 413bf97f7b..6012301874 100644 --- a/wpilibc/src/main/native/cpp/PWMSpeedController.cpp +++ b/wpilibc/src/main/native/cpp/PWMSpeedController.cpp @@ -4,16 +4,18 @@ #include "frc/PWMSpeedController.h" +#include + #include "frc/smartdashboard/SendableBuilder.h" using namespace frc; void PWMSpeedController::Set(double speed) { - SetSpeed(m_isInverted ? -speed : speed); + m_pwm.SetSpeed(m_isInverted ? -speed : speed); } double PWMSpeedController::Get() const { - return GetSpeed() * (m_isInverted ? -1.0 : 1.0); + return m_pwm.GetSpeed() * (m_isInverted ? -1.0 : 1.0); } void PWMSpeedController::SetInverted(bool isInverted) { @@ -25,24 +27,34 @@ bool PWMSpeedController::GetInverted() const { } void PWMSpeedController::Disable() { - SetDisabled(); + m_pwm.SetDisabled(); } void PWMSpeedController::StopMotor() { - PWM::StopMotor(); + Disable(); +} + +void PWMSpeedController::GetDescription(wpi::raw_ostream& desc) const { + desc << "PWM " << GetChannel(); +} + +int PWMSpeedController::GetChannel() const { + return m_pwm.GetChannel(); } void PWMSpeedController::PIDWrite(double output) { Set(output); } -PWMSpeedController::PWMSpeedController(int channel) : PWM(channel) {} +PWMSpeedController::PWMSpeedController(const wpi::Twine& name, int channel) + : m_pwm(channel, false) { + SendableRegistry::GetInstance().AddLW(this, name, channel); +} void PWMSpeedController::InitSendable(SendableBuilder& builder) { builder.SetSmartDashboardType("Speed Controller"); builder.SetActuator(true); - builder.SetSafeState([=]() { SetDisabled(); }); + builder.SetSafeState([=] { Disable(); }); builder.AddDoubleProperty( - "Value", [=]() { return GetSpeed(); }, - [=](double value) { SetSpeed(value); }); + "Value", [=] { return Get(); }, [=](double value) { Set(value); }); } diff --git a/wpilibc/src/main/native/cpp/PWMTalonFX.cpp b/wpilibc/src/main/native/cpp/PWMTalonFX.cpp index 3ab574da91..7b85cf8bd6 100644 --- a/wpilibc/src/main/native/cpp/PWMTalonFX.cpp +++ b/wpilibc/src/main/native/cpp/PWMTalonFX.cpp @@ -6,16 +6,14 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -PWMTalonFX::PWMTalonFX(int channel) : PWMSpeedController(channel) { - SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +PWMTalonFX::PWMTalonFX(int channel) + : PWMSpeedController("PWMTalonFX", channel) { + m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_TalonFX, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "PWMTalonFX", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/PWMTalonSRX.cpp b/wpilibc/src/main/native/cpp/PWMTalonSRX.cpp index e305015e39..b70c706760 100644 --- a/wpilibc/src/main/native/cpp/PWMTalonSRX.cpp +++ b/wpilibc/src/main/native/cpp/PWMTalonSRX.cpp @@ -6,16 +6,14 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -PWMTalonSRX::PWMTalonSRX(int channel) : PWMSpeedController(channel) { - SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +PWMTalonSRX::PWMTalonSRX(int channel) + : PWMSpeedController("PWMTalonSRX", channel) { + m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_PWMTalonSRX, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "PWMTalonSRX", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/PWMVenom.cpp b/wpilibc/src/main/native/cpp/PWMVenom.cpp index ec1c09bfac..75dc008862 100644 --- a/wpilibc/src/main/native/cpp/PWMVenom.cpp +++ b/wpilibc/src/main/native/cpp/PWMVenom.cpp @@ -6,16 +6,13 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -PWMVenom::PWMVenom(int channel) : PWMSpeedController(channel) { - SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +PWMVenom::PWMVenom(int channel) : PWMSpeedController("PWMVenom", channel) { + m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_FusionVenom, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "PWMVenom", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/PWMVictorSPX.cpp b/wpilibc/src/main/native/cpp/PWMVictorSPX.cpp index f34a045382..89cb207986 100644 --- a/wpilibc/src/main/native/cpp/PWMVictorSPX.cpp +++ b/wpilibc/src/main/native/cpp/PWMVictorSPX.cpp @@ -6,16 +6,14 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -PWMVictorSPX::PWMVictorSPX(int channel) : PWMSpeedController(channel) { - SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +PWMVictorSPX::PWMVictorSPX(int channel) + : PWMSpeedController("PWMVictorSPX", channel) { + m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_PWMVictorSPX, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "PWMVictorSPX", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/SD540.cpp b/wpilibc/src/main/native/cpp/SD540.cpp index 03e37df5b4..2ff10f5814 100644 --- a/wpilibc/src/main/native/cpp/SD540.cpp +++ b/wpilibc/src/main/native/cpp/SD540.cpp @@ -6,17 +6,14 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -SD540::SD540(int channel) : PWMSpeedController(channel) { - SetBounds(2.05, 1.55, 1.50, 1.44, 0.94); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +SD540::SD540(int channel) : PWMSpeedController("SD540", channel) { + m_pwm.SetBounds(2.05, 1.55, 1.50, 1.44, 0.94); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_MindsensorsSD540, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "SD540", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/Spark.cpp b/wpilibc/src/main/native/cpp/Spark.cpp index 2e0bcaa758..80db37d45f 100644 --- a/wpilibc/src/main/native/cpp/Spark.cpp +++ b/wpilibc/src/main/native/cpp/Spark.cpp @@ -6,16 +6,13 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -Spark::Spark(int channel) : PWMSpeedController(channel) { - SetBounds(2.003, 1.55, 1.50, 1.46, 0.999); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +Spark::Spark(int channel) : PWMSpeedController("Spark", channel) { + m_pwm.SetBounds(2.003, 1.55, 1.50, 1.46, 0.999); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_RevSPARK, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "Spark", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/Talon.cpp b/wpilibc/src/main/native/cpp/Talon.cpp index 2f9a51e8af..7424157053 100644 --- a/wpilibc/src/main/native/cpp/Talon.cpp +++ b/wpilibc/src/main/native/cpp/Talon.cpp @@ -6,16 +6,13 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -Talon::Talon(int channel) : PWMSpeedController(channel) { - SetBounds(2.037, 1.539, 1.513, 1.487, 0.989); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +Talon::Talon(int channel) : PWMSpeedController("Talon", channel) { + m_pwm.SetBounds(2.037, 1.539, 1.513, 1.487, 0.989); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_Talon, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "Talon", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/Victor.cpp b/wpilibc/src/main/native/cpp/Victor.cpp index a466add795..a6d4805022 100644 --- a/wpilibc/src/main/native/cpp/Victor.cpp +++ b/wpilibc/src/main/native/cpp/Victor.cpp @@ -6,16 +6,13 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -Victor::Victor(int channel) : PWMSpeedController(channel) { - SetBounds(2.027, 1.525, 1.507, 1.49, 1.026); - SetPeriodMultiplier(kPeriodMultiplier_2X); - SetSpeed(0.0); - SetZeroLatch(); +Victor::Victor(int channel) : PWMSpeedController("Victor", channel) { + m_pwm.SetBounds(2.027, 1.525, 1.507, 1.49, 1.026); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_2X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_Victor, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "Victor", GetChannel()); } diff --git a/wpilibc/src/main/native/cpp/VictorSP.cpp b/wpilibc/src/main/native/cpp/VictorSP.cpp index 10b7fdadcc..f4da973b59 100644 --- a/wpilibc/src/main/native/cpp/VictorSP.cpp +++ b/wpilibc/src/main/native/cpp/VictorSP.cpp @@ -6,16 +6,13 @@ #include -#include "frc/smartdashboard/SendableRegistry.h" - using namespace frc; -VictorSP::VictorSP(int channel) : PWMSpeedController(channel) { - SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); - SetPeriodMultiplier(kPeriodMultiplier_1X); - SetSpeed(0.0); - SetZeroLatch(); +VictorSP::VictorSP(int channel) : PWMSpeedController("VictorSP", channel) { + m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X); + m_pwm.SetSpeed(0.0); + m_pwm.SetZeroLatch(); HAL_Report(HALUsageReporting::kResourceType_VictorSP, GetChannel() + 1); - SendableRegistry::GetInstance().SetName(this, "VictorSP", GetChannel()); } diff --git a/wpilibc/src/main/native/include/frc/MotorSafety.h b/wpilibc/src/main/native/include/frc/MotorSafety.h index ee191b8459..b23a29c7ef 100644 --- a/wpilibc/src/main/native/include/frc/MotorSafety.h +++ b/wpilibc/src/main/native/include/frc/MotorSafety.h @@ -5,11 +5,14 @@ #pragma once #include -#include #include "frc/ErrorBase.h" #include "frc/Timer.h" +namespace wpi { +class raw_ostream; +} // namespace wpi + namespace frc { /** diff --git a/wpilibc/src/main/native/include/frc/PWM.h b/wpilibc/src/main/native/include/frc/PWM.h index 7f7cd912ae..869b6453b9 100644 --- a/wpilibc/src/main/native/include/frc/PWM.h +++ b/wpilibc/src/main/native/include/frc/PWM.h @@ -7,9 +7,8 @@ #include #include -#include -#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 { +class PWM : public ErrorBase, public Sendable, public SendableHelper { public: friend class AddressableLED; /** @@ -64,8 +63,10 @@ class PWM : public MotorSafety, public Sendable, public SendableHelper { * * @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&&) = 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. * diff --git a/wpilibc/src/main/native/include/frc/PWMSpeedController.h b/wpilibc/src/main/native/include/frc/PWMSpeedController.h index 3d12fb70c6..9d6f65af3c 100644 --- a/wpilibc/src/main/native/include/frc/PWMSpeedController.h +++ b/wpilibc/src/main/native/include/frc/PWMSpeedController.h @@ -4,15 +4,27 @@ #pragma once +#include + +#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 { 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; }; diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DMC60.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DMC60.java index dfe37bfb15..2f38230ea9 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DMC60.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DMC60.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * Digilent DMC 60 Speed Controller. @@ -33,14 +32,13 @@ public class DMC60 extends PWMSpeedController { * the MXP port */ public DMC60(final int channel) { - super(channel); + super("DMC60", channel); - setBounds(2.004, 1.52, 1.50, 1.48, 0.997); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_DigilentDMC60, getChannel() + 1); - SendableRegistry.setName(this, "DMC60", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Jaguar.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Jaguar.java index 388b286144..5e69c4a50a 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Jaguar.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Jaguar.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * Texas Instruments / Vex Robotics Jaguar Speed Controller as a PWM device. @@ -32,14 +31,13 @@ public class Jaguar extends PWMSpeedController { * the MXP port */ public Jaguar(final int channel) { - super(channel); + super("Jaguar", channel); - setBounds(2.31, 1.55, 1.507, 1.454, 0.697); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.31, 1.55, 1.507, 1.454, 0.697); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_Jaguar, getChannel() + 1); - SendableRegistry.setName(this, "Jaguar", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWM.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWM.java index ddfb39ec9a..f21ba025ea 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWM.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWM.java @@ -23,7 +23,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; * center value - 999 to 2 = linear scaling from "center" to "full reverse" - 1 = minimum pulse * width (currently .5ms) - 0 = disabled (i.e. PWM output is held low) */ -public class PWM extends MotorSafety implements Sendable, AutoCloseable { +public class PWM implements Sendable, AutoCloseable { /** Represents the amount to multiply the minimum servo-pulse pwm period by. */ public enum PeriodMultiplier { /** Period Multiplier: don't skip pulses. PWM pulses occur every 5.005 ms */ @@ -42,9 +42,24 @@ public class PWM extends MotorSafety implements Sendable, AutoCloseable { /** * Allocate a PWM given a channel. * + *

Checks channel value range and allocates the appropriate channel. The allocation is only + * done to help users ensure that they don't double assign channels. + * + *

By default, adds itself to SendableRegistry and LiveWindow. + * * @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port */ public PWM(final int channel) { + this(channel, true); + } + + /** + * Allocate a PWM given a channel. + * + * @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 + */ + public PWM(final int channel, final boolean registerSendable) { SensorUtil.checkPWMChannel(channel); m_channel = channel; @@ -55,9 +70,9 @@ public class PWM extends MotorSafety implements Sendable, AutoCloseable { PWMJNI.setPWMEliminateDeadband(m_handle, false); HAL.report(tResourceType.kResourceType_PWM, channel + 1); - SendableRegistry.addLW(this, "PWM", channel); - - setSafetyEnabled(false); + if (registerSendable) { + SendableRegistry.addLW(this, "PWM", channel); + } } /** Free the resource associated with the PWM channel and set the value to 0. */ @@ -72,16 +87,6 @@ public class PWM extends MotorSafety implements Sendable, AutoCloseable { m_handle = 0; } - @Override - public void stopMotor() { - setDisabled(); - } - - @Override - public String getDescription() { - return "PWM " + getChannel(); - } - /** * Optionally eliminate the deadband from a speed controller. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMSparkMax.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMSparkMax.java index 72a4303949..b57fd9ac35 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMSparkMax.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMSparkMax.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * REV Robotics SPARK MAX Speed Controller with PWM control. @@ -28,14 +27,13 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; public class PWMSparkMax extends PWMSpeedController { /** Common initialization code called by all constructors. */ public PWMSparkMax(final int channel) { - super(channel); + super("PWMSparkMax", channel); - setBounds(2.003, 1.55, 1.50, 1.46, 0.999); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.003, 1.55, 1.50, 1.46, 0.999); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_RevSparkMaxPWM, getChannel() + 1); - SendableRegistry.setName(this, "PWMSparkMax", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMSpeedController.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMSpeedController.java index 1c9801fc40..2db8baf5fe 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMSpeedController.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMSpeedController.java @@ -5,24 +5,31 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder; +import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** Common base class for all PWM Speed Controllers. */ -public abstract class PWMSpeedController extends PWM implements SpeedController { +public abstract class PWMSpeedController extends MotorSafety + implements SpeedController, Sendable, AutoCloseable { private boolean m_isInverted; + protected PWM m_pwm; /** * Constructor. * + * @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 */ - protected PWMSpeedController(int channel) { - super(channel); + protected PWMSpeedController(final String name, final int channel) { + m_pwm = new PWM(channel, false); + SendableRegistry.addLW(this, name, channel); } + /** Free the resource associated with the PWM channel and set the value to 0. */ @Override - public String getDescription() { - return "PWM " + getChannel(); + public void close() { + SendableRegistry.remove(this); + m_pwm.close(); } /** @@ -35,7 +42,7 @@ public abstract class PWMSpeedController extends PWM implements SpeedController */ @Override public void set(double speed) { - setSpeed(m_isInverted ? -speed : speed); + m_pwm.setSpeed(m_isInverted ? -speed : speed); feed(); } @@ -48,7 +55,7 @@ public abstract class PWMSpeedController extends PWM implements SpeedController */ @Override public double get() { - return getSpeed() * (m_isInverted ? -1.0 : 1.0); + return m_pwm.getSpeed() * (m_isInverted ? -1.0 : 1.0); } @Override @@ -63,7 +70,26 @@ public abstract class PWMSpeedController extends PWM implements SpeedController @Override public void disable() { - setDisabled(); + m_pwm.setDisabled(); + } + + @Override + public void stopMotor() { + disable(); + } + + @Override + public String getDescription() { + return "PWM " + getChannel(); + } + + /** + * Gets the PWM channel number. + * + * @return The channel number. + */ + public int getChannel() { + return m_pwm.getChannel(); } /** @@ -80,7 +106,7 @@ public abstract class PWMSpeedController extends PWM implements SpeedController public void initSendable(SendableBuilder builder) { builder.setSmartDashboardType("Speed Controller"); builder.setActuator(true); - builder.setSafeState(this::setDisabled); - builder.addDoubleProperty("Value", this::getSpeed, this::setSpeed); + builder.setSafeState(this::disable); + builder.addDoubleProperty("Value", this::get, this::set); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMTalonFX.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMTalonFX.java index d970f5d3e7..330c6abe30 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMTalonFX.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMTalonFX.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * Cross the Road Electronics (CTRE) Talon FX Speed Controller with PWM control. @@ -33,14 +32,13 @@ public class PWMTalonFX extends PWMSpeedController { * the MXP port */ public PWMTalonFX(final int channel) { - super(channel); + super("PWMTalonFX", channel); - setBounds(2.004, 1.52, 1.50, 1.48, 0.997); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_TalonFX, getChannel() + 1); - SendableRegistry.setName(this, "PWMTalonFX", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMTalonSRX.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMTalonSRX.java index 5bf5fc620c..77c7d37475 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMTalonSRX.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMTalonSRX.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * Cross the Road Electronics (CTRE) Talon SRX Speed Controller with PWM control. @@ -33,14 +32,13 @@ public class PWMTalonSRX extends PWMSpeedController { * on the MXP port */ public PWMTalonSRX(final int channel) { - super(channel); + super("PWMTalonSRX", channel); - setBounds(2.004, 1.52, 1.50, 1.48, 0.997); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_PWMTalonSRX, getChannel() + 1); - SendableRegistry.setName(this, "PWMTalonSRX", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMVenom.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMVenom.java index cdf07754ec..8a71842a14 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMVenom.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMVenom.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * Playing with Fusion Venom Smart Motor with PWM control. @@ -32,14 +31,13 @@ public class PWMVenom extends PWMSpeedController { * the MXP port */ public PWMVenom(final int channel) { - super(channel); + super("PWMVenom", channel); - setBounds(2.004, 1.52, 1.50, 1.48, 0.997); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_FusionVenom, getChannel() + 1); - SendableRegistry.setName(this, "PWMVenom", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMVictorSPX.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMVictorSPX.java index 976eb67fc3..62c86087ac 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMVictorSPX.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/PWMVictorSPX.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * Cross the Road Electronics (CTRE) Victor SPX Speed Controller with PWM control. @@ -33,14 +32,13 @@ public class PWMVictorSPX extends PWMSpeedController { * are on the MXP port */ public PWMVictorSPX(final int channel) { - super(channel); + super("PWMVictorSPX", channel); - setBounds(2.004, 1.52, 1.50, 1.48, 0.997); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_PWMVictorSPX, getChannel() + 1); - SendableRegistry.setName(this, "PWMVictorSPX", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SD540.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SD540.java index 58867334a5..8a14463964 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/SD540.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/SD540.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * Mindsensors SD540 Speed Controller. @@ -26,17 +25,6 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; * */ public class SD540 extends PWMSpeedController { - /** Common initialization code called by all constructors. */ - protected void initSD540() { - setBounds(2.05, 1.55, 1.50, 1.44, 0.94); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); - - HAL.report(tResourceType.kResourceType_MindsensorsSD540, getChannel() + 1); - SendableRegistry.setName(this, "SD540", getChannel()); - } - /** * Constructor. * @@ -44,7 +32,13 @@ public class SD540 extends PWMSpeedController { * the MXP port */ public SD540(final int channel) { - super(channel); - initSD540(); + super("SD540", channel); + + m_pwm.setBounds(2.05, 1.55, 1.50, 1.44, 0.94); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); + + HAL.report(tResourceType.kResourceType_MindsensorsSD540, getChannel() + 1); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Spark.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Spark.java index 1e6f55c697..cf3d8d29ba 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Spark.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Spark.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * REV Robotics SPARK Speed Controller. @@ -26,17 +25,6 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; * */ public class Spark extends PWMSpeedController { - /** Common initialization code called by all constructors. */ - protected void initSpark() { - setBounds(2.003, 1.55, 1.50, 1.46, 0.999); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); - - HAL.report(tResourceType.kResourceType_RevSPARK, getChannel() + 1); - SendableRegistry.setName(this, "Spark", getChannel()); - } - /** * Constructor. * @@ -44,7 +32,13 @@ public class Spark extends PWMSpeedController { * the MXP port */ public Spark(final int channel) { - super(channel); - initSpark(); + super("Spark", channel); + + m_pwm.setBounds(2.003, 1.55, 1.50, 1.46, 0.999); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); + + HAL.report(tResourceType.kResourceType_RevSPARK, getChannel() + 1); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Talon.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Talon.java index 355e7934a2..a0b3ac5694 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Talon.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Talon.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * Cross the Road Electronics (CTRE) Talon and Talon SR Speed Controller. @@ -32,14 +31,13 @@ public class Talon extends PWMSpeedController { * the MXP port */ public Talon(final int channel) { - super(channel); + super("Talon", channel); - setBounds(2.037, 1.539, 1.513, 1.487, 0.989); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.037, 1.539, 1.513, 1.487, 0.989); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_Talon, getChannel() + 1); - SendableRegistry.setName(this, "Talon", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Victor.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Victor.java index 0c4229cad7..bdd9ba7d2d 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Victor.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Victor.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * VEX Robotics Victor 888 Speed Controller The Vex Robotics Victor 884 Speed Controller can also be @@ -35,14 +34,13 @@ public class Victor extends PWMSpeedController { * the MXP port */ public Victor(final int channel) { - super(channel); + super("Victor", channel); - setBounds(2.027, 1.525, 1.507, 1.49, 1.026); - setPeriodMultiplier(PeriodMultiplier.k2X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.027, 1.525, 1.507, 1.49, 1.026); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k2X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_Victor, getChannel() + 1); - SendableRegistry.setName(this, "Victor", getChannel()); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/VictorSP.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/VictorSP.java index 2f04f79370..b588c5c9a2 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/VictorSP.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/VictorSP.java @@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; -import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry; /** * VEX Robotics Victor SP Speed Controller. @@ -33,14 +32,13 @@ public class VictorSP extends PWMSpeedController { * the MXP port */ public VictorSP(final int channel) { - super(channel); + super("VictorSP", channel); - setBounds(2.004, 1.52, 1.50, 1.48, 0.997); - setPeriodMultiplier(PeriodMultiplier.k1X); - setSpeed(0.0); - setZeroLatch(); + m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997); + m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X); + m_pwm.setSpeed(0.0); + m_pwm.setZeroLatch(); HAL.report(tResourceType.kResourceType_VictorSP, getChannel() + 1); - SendableRegistry.setName(this, "VictorSP", getChannel()); } }