mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[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:
@@ -6,16 +6,13 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,16 +6,13 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "frc/NidecBrushless.h"
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "frc/smartdashboard/SendableBuilder.h"
|
||||
#include "frc/smartdashboard/SendableRegistry.h"
|
||||
|
||||
@@ -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); });
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -4,16 +4,18 @@
|
||||
|
||||
#include "frc/PWMSpeedController.h"
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#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); });
|
||||
}
|
||||
|
||||
@@ -6,16 +6,14 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,16 +6,14 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,16 +6,13 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,16 +6,14 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,17 +6,14 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,16 +6,13 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,16 +6,13 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,16 +6,13 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
@@ -6,16 +6,13 @@
|
||||
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
|
||||
#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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user