mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpilib][hal] PWM Raw using microseconds (#5283)
Co-authored-by: Joe <sciencewhiz@users.noreply.github.com>
This commit is contained in:
@@ -54,18 +54,18 @@ PWM::~PWM() {
|
||||
FRC_ReportError(status, "Channel {}", m_channel);
|
||||
}
|
||||
|
||||
void PWM::SetRaw(uint16_t value) {
|
||||
void PWM::SetPulseTime(units::microsecond_t time) {
|
||||
int32_t status = 0;
|
||||
HAL_SetPWMRaw(m_handle, value, &status);
|
||||
HAL_SetPWMPulseTimeMicroseconds(m_handle, time.value(), &status);
|
||||
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
||||
}
|
||||
|
||||
uint16_t PWM::GetRaw() const {
|
||||
units::microsecond_t PWM::GetPulseTime() const {
|
||||
int32_t status = 0;
|
||||
uint16_t value = HAL_GetPWMRaw(m_handle, &status);
|
||||
double value = HAL_GetPWMPulseTimeMicroseconds(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
||||
|
||||
return value;
|
||||
return units::microsecond_t{value};
|
||||
}
|
||||
|
||||
void PWM::SetPosition(double pos) {
|
||||
@@ -135,27 +135,37 @@ void PWM::EnableDeadbandElimination(bool eliminateDeadband) {
|
||||
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
||||
}
|
||||
|
||||
void PWM::SetBounds(double max, double deadbandMax, double center,
|
||||
double deadbandMin, double min) {
|
||||
void PWM::SetBounds(units::microsecond_t max, units::microsecond_t deadbandMax,
|
||||
units::microsecond_t center,
|
||||
units::microsecond_t deadbandMin,
|
||||
units::microsecond_t min) {
|
||||
int32_t status = 0;
|
||||
HAL_SetPWMConfig(m_handle, max, deadbandMax, center, deadbandMin, min,
|
||||
&status);
|
||||
HAL_SetPWMConfigMicroseconds(m_handle, max.value(), deadbandMax.value(),
|
||||
center.value(), deadbandMin.value(), min.value(),
|
||||
&status);
|
||||
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
||||
}
|
||||
|
||||
void PWM::SetRawBounds(int max, int deadbandMax, int center, int deadbandMin,
|
||||
int min) {
|
||||
void PWM::GetBounds(units::microsecond_t* max,
|
||||
units::microsecond_t* deadbandMax,
|
||||
units::microsecond_t* center,
|
||||
units::microsecond_t* deadbandMin,
|
||||
units::microsecond_t* min) {
|
||||
int32_t status = 0;
|
||||
HAL_SetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min,
|
||||
&status);
|
||||
int32_t rawMax, rawDeadbandMax, rawCenter, rawDeadbandMin, rawMin;
|
||||
HAL_GetPWMConfigMicroseconds(m_handle, &rawMax, &rawDeadbandMax, &rawCenter,
|
||||
&rawDeadbandMin, &rawMin, &status);
|
||||
*max = units::microsecond_t{static_cast<double>(rawMax)};
|
||||
*deadbandMax = units::microsecond_t{static_cast<double>(rawDeadbandMax)};
|
||||
*center = units::microsecond_t{static_cast<double>(rawCenter)};
|
||||
*deadbandMin = units::microsecond_t{static_cast<double>(rawDeadbandMin)};
|
||||
*min = units::microsecond_t{static_cast<double>(rawMin)};
|
||||
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
||||
}
|
||||
|
||||
void PWM::GetRawBounds(int* max, int* deadbandMax, int* center,
|
||||
int* deadbandMin, int* min) {
|
||||
void PWM::SetAlwaysHighMode() {
|
||||
int32_t status = 0;
|
||||
HAL_GetPWMConfigRaw(m_handle, max, deadbandMax, center, deadbandMin, min,
|
||||
&status);
|
||||
HAL_SetPWMAlwaysHighMode(m_handle, &status);
|
||||
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
|
||||
}
|
||||
|
||||
@@ -168,6 +178,6 @@ void PWM::InitSendable(wpi::SendableBuilder& builder) {
|
||||
builder.SetActuator(true);
|
||||
builder.SetSafeState([=, this] { SetDisabled(); });
|
||||
builder.AddDoubleProperty(
|
||||
"Value", [=, this] { return GetRaw(); },
|
||||
[=, this](double value) { SetRaw(value); });
|
||||
"Value", [=, this] { return GetPulseTime().value(); },
|
||||
[=, this](double value) { SetPulseTime(units::millisecond_t{value}); });
|
||||
}
|
||||
|
||||
@@ -13,12 +13,12 @@ using namespace frc;
|
||||
constexpr double Servo::kMaxServoAngle;
|
||||
constexpr double Servo::kMinServoAngle;
|
||||
|
||||
constexpr double Servo::kDefaultMaxServoPWM;
|
||||
constexpr double Servo::kDefaultMinServoPWM;
|
||||
constexpr units::millisecond_t Servo::kDefaultMaxServoPWM;
|
||||
constexpr units::millisecond_t Servo::kDefaultMinServoPWM;
|
||||
|
||||
Servo::Servo(int channel) : PWM(channel) {
|
||||
// Set minimum and maximum PWM values supported by the servo
|
||||
SetBounds(kDefaultMaxServoPWM, 0.0, 0.0, 0.0, kDefaultMinServoPWM);
|
||||
SetBounds(kDefaultMaxServoPWM, 0.0_ms, 0.0_ms, 0.0_ms, kDefaultMinServoPWM);
|
||||
|
||||
// Assign defaults for period multiplier for the servo PWM control signal
|
||||
SetPeriodMultiplier(kPeriodMultiplier_4X);
|
||||
@@ -32,7 +32,7 @@ void Servo::Set(double value) {
|
||||
}
|
||||
|
||||
void Servo::SetOffline() {
|
||||
SetRaw(0);
|
||||
SetDisabled();
|
||||
}
|
||||
|
||||
double Servo::Get() const {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
using namespace frc;
|
||||
|
||||
DMC60::DMC60(int channel) : PWMMotorController("DMC60", channel) {
|
||||
m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997);
|
||||
m_pwm.SetBounds(2.004_ms, 1.52_ms, 1.50_ms, 1.48_ms, 0.997_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
using namespace frc;
|
||||
|
||||
Jaguar::Jaguar(int channel) : PWMMotorController("Jaguar", channel) {
|
||||
m_pwm.SetBounds(2.31, 1.55, 1.507, 1.454, 0.697);
|
||||
m_pwm.SetBounds(2.31_ms, 1.55_ms, 1.507_ms, 1.454_ms, 0.697_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -30,7 +30,7 @@ void NidecBrushless::Set(double speed) {
|
||||
if (!m_disabled) {
|
||||
m_speed = speed;
|
||||
m_dio.UpdateDutyCycle(0.5 + 0.5 * (m_isInverted ? -speed : speed));
|
||||
m_pwm.SetRaw(0xffff);
|
||||
m_pwm.SetAlwaysHighMode();
|
||||
}
|
||||
Feed();
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ using namespace frc;
|
||||
|
||||
PWMSparkMax::PWMSparkMax(int channel)
|
||||
: PWMMotorController("PWMSparkMax", channel) {
|
||||
m_pwm.SetBounds(2.003, 1.55, 1.50, 1.46, 0.999);
|
||||
m_pwm.SetBounds(2.003_ms, 1.55_ms, 1.50_ms, 1.46_ms, 0.999_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -10,7 +10,7 @@ using namespace frc;
|
||||
|
||||
PWMTalonFX::PWMTalonFX(int channel)
|
||||
: PWMMotorController("PWMTalonFX", channel) {
|
||||
m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997);
|
||||
m_pwm.SetBounds(2.004_ms, 1.52_ms, 1.50_ms, 1.48_ms, 0.997_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -10,7 +10,7 @@ using namespace frc;
|
||||
|
||||
PWMTalonSRX::PWMTalonSRX(int channel)
|
||||
: PWMMotorController("PWMTalonSRX", channel) {
|
||||
m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997);
|
||||
m_pwm.SetBounds(2.004_ms, 1.52_ms, 1.50_ms, 1.48_ms, 0.997_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
using namespace frc;
|
||||
|
||||
PWMVenom::PWMVenom(int channel) : PWMMotorController("PWMVenom", channel) {
|
||||
m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997);
|
||||
m_pwm.SetBounds(2.004_ms, 1.52_ms, 1.50_ms, 1.48_ms, 0.997_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -10,7 +10,7 @@ using namespace frc;
|
||||
|
||||
PWMVictorSPX::PWMVictorSPX(int channel)
|
||||
: PWMMotorController("PWMVictorSPX", channel) {
|
||||
m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997);
|
||||
m_pwm.SetBounds(2.004_ms, 1.52_ms, 1.50_ms, 1.48_ms, 0.997_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
using namespace frc;
|
||||
|
||||
SD540::SD540(int channel) : PWMMotorController("SD540", channel) {
|
||||
m_pwm.SetBounds(2.05, 1.55, 1.50, 1.44, 0.94);
|
||||
m_pwm.SetBounds(2.05_ms, 1.55_ms, 1.50_ms, 1.44_ms, 0.94_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
using namespace frc;
|
||||
|
||||
Spark::Spark(int channel) : PWMMotorController("Spark", channel) {
|
||||
m_pwm.SetBounds(2.003, 1.55, 1.50, 1.46, 0.999);
|
||||
m_pwm.SetBounds(2.003_ms, 1.55_ms, 1.50_ms, 1.46_ms, 0.999_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
using namespace frc;
|
||||
|
||||
Talon::Talon(int channel) : PWMMotorController("Talon", channel) {
|
||||
m_pwm.SetBounds(2.037, 1.539, 1.513, 1.487, 0.989);
|
||||
m_pwm.SetBounds(2.037_ms, 1.539_ms, 1.513_ms, 1.487_ms, 0.989_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
using namespace frc;
|
||||
|
||||
Victor::Victor(int channel) : PWMMotorController("Victor", channel) {
|
||||
m_pwm.SetBounds(2.027, 1.525, 1.507, 1.49, 1.026);
|
||||
m_pwm.SetBounds(2.027_ms, 1.525_ms, 1.507_ms, 1.49_ms, 1.026_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_2X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
using namespace frc;
|
||||
|
||||
VictorSP::VictorSP(int channel) : PWMMotorController("VictorSP", channel) {
|
||||
m_pwm.SetBounds(2.004, 1.52, 1.50, 1.48, 0.997);
|
||||
m_pwm.SetBounds(2.004_ms, 1.52_ms, 1.50_ms, 1.48_ms, 0.997_ms);
|
||||
m_pwm.SetPeriodMultiplier(PWM::kPeriodMultiplier_1X);
|
||||
m_pwm.SetSpeed(0.0);
|
||||
m_pwm.SetZeroLatch();
|
||||
|
||||
@@ -39,21 +39,21 @@ void PWMSim::SetInitialized(bool initialized) {
|
||||
HALSIM_SetPWMInitialized(m_index, initialized);
|
||||
}
|
||||
|
||||
std::unique_ptr<CallbackStore> PWMSim::RegisterRawValueCallback(
|
||||
std::unique_ptr<CallbackStore> PWMSim::RegisterPulseMicrosecondCallback(
|
||||
NotifyCallback callback, bool initialNotify) {
|
||||
auto store = std::make_unique<CallbackStore>(
|
||||
m_index, -1, callback, &HALSIM_CancelPWMRawValueCallback);
|
||||
store->SetUid(HALSIM_RegisterPWMRawValueCallback(m_index, &CallbackStoreThunk,
|
||||
store.get(), initialNotify));
|
||||
m_index, -1, callback, &HALSIM_CancelPWMPulseMicrosecondCallback);
|
||||
store->SetUid(HALSIM_RegisterPWMPulseMicrosecondCallback(
|
||||
m_index, &CallbackStoreThunk, store.get(), initialNotify));
|
||||
return store;
|
||||
}
|
||||
|
||||
int PWMSim::GetRawValue() const {
|
||||
return HALSIM_GetPWMRawValue(m_index);
|
||||
int32_t PWMSim::GetPulseMicrosecond() const {
|
||||
return HALSIM_GetPWMPulseMicrosecond(m_index);
|
||||
}
|
||||
|
||||
void PWMSim::SetRawValue(int rawValue) {
|
||||
HALSIM_SetPWMRawValue(m_index, rawValue);
|
||||
void PWMSim::SetPulseMicrosecond(int32_t microsecondPulseTime) {
|
||||
HALSIM_SetPWMPulseMicrosecond(m_index, microsecondPulseTime);
|
||||
}
|
||||
|
||||
std::unique_ptr<CallbackStore> PWMSim::RegisterSpeedCallback(
|
||||
|
||||
Reference in New Issue
Block a user