[wpilib][hal] PWM Raw using microseconds (#5283)

Co-authored-by: Joe <sciencewhiz@users.noreply.github.com>
This commit is contained in:
Thad House
2023-06-22 19:43:16 -07:00
committed by GitHub
parent 1fca519fb4
commit c1a01569b4
56 changed files with 374 additions and 466 deletions

View File

@@ -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}); });
}

View File

@@ -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 {

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();
}

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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();

View File

@@ -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(

View File

@@ -7,6 +7,7 @@
#include <stdint.h>
#include <hal/Types.h>
#include <units/time.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -18,18 +19,9 @@ class DMA;
* Class implements the PWM generation in the FPGA.
*
* The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They
* are mapped to the hardware dependent values, in this case 0-2000 for the
* FPGA. Changes are immediately sent to the FPGA, and the update occurs at the
* next FPGA cycle (5.005ms). There is no delay.
*
* As of revision 0.1.10 of the FPGA, the FPGA interprets the 0-2000 values as
* follows:
* - 2000 = maximum pulse width
* - 1999 to 1001 = linear scaling from "full forward" to "center"
* - 1000 = center value
* - 999 to 2 = linear scaling from "center" to "full reverse"
* - 1 = minimum pulse width (currently 0.5ms)
* - 0 = disabled (i.e. PWM output is held low)
* are mapped to the microseconds to keep the pulse high, with a range of 0
* (off) to 4096. Changes are immediately sent to the FPGA, and the update
* occurs at the next FPGA cycle (5.05ms). There is no delay.
*/
class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
public:
@@ -40,15 +32,15 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
*/
enum PeriodMultiplier {
/**
* Don't skip pulses. PWM pulses occur every 5.005 ms
* Don't skip pulses. PWM pulses occur every 5.05 ms
*/
kPeriodMultiplier_1X = 1,
/**
* Skip every other pulse. PWM pulses occur every 10.010 ms
* Skip every other pulse. PWM pulses occur every 10.10 ms
*/
kPeriodMultiplier_2X = 2,
/**
* Skip three out of four pulses. PWM pulses occur every 20.020 ms
* Skip three out of four pulses. PWM pulses occur every 20.20 ms
*/
kPeriodMultiplier_4X = 4
};
@@ -78,30 +70,29 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
PWM& operator=(PWM&&) = default;
/**
* Set the PWM value directly to the hardware.
* Set the PWM pulse time directly to the hardware.
*
* Write a raw value to a PWM channel.
* Write a microsecond value to a PWM channel.
*
* @param value Raw PWM value.
* @param time Microsecond PWM value.
*/
virtual void SetRaw(uint16_t value);
virtual void SetPulseTime(units::microsecond_t time);
/**
* Get the PWM value directly from the hardware.
* Get the PWM pulse time directly from the hardware.
*
* Read a raw value from a PWM channel.
* Read a microsecond value from a PWM channel.
*
* @return Raw PWM control value.
* @return Microsecond PWM control value.
*/
virtual uint16_t GetRaw() const;
virtual units::microsecond_t GetPulseTime() const;
/**
* Set the PWM value based on a position.
*
* This is intended to be used by servos.
*
* @pre SetMaxPositivePwm() called.
* @pre SetMinNegativePwm() called.
* @pre SetBounds() called.
*
* @param pos The position to set the servo between 0.0 and 1.0.
*/
@@ -112,8 +103,7 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
*
* This is intended to be used by servos.
*
* @pre SetMaxPositivePwm() called.
* @pre SetMinNegativePwm() called.
* @pre SetBounds() called.
*
* @return The position the servo is set to between 0.0 and 1.0.
*/
@@ -124,11 +114,7 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
*
* This is intended to be used by motor controllers.
*
* @pre SetMaxPositivePwm() called.
* @pre SetMinPositivePwm() called.
* @pre SetCenterPwm() called.
* @pre SetMaxNegativePwm() called.
* @pre SetMinNegativePwm() called.
* @pre SetBounds() called.
*
* @param speed The speed to set the motor controller between -1.0 and 1.0.
*/
@@ -139,10 +125,7 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
*
* This is intended to be used by motor controllers.
*
* @pre SetMaxPositivePwm() called.
* @pre SetMinPositivePwm() called.
* @pre SetMaxNegativePwm() called.
* @pre SetMinNegativePwm() called.
* @pre SetBounds() called.
*
* @return The most recently set speed between -1.0 and 1.0.
*/
@@ -180,46 +163,38 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
* The values determine the upper and lower speeds as well as the deadband
* bracket.
*
* @param max The max PWM pulse width in ms
* @param deadbandMax The high end of the deadband range pulse width in ms
* @param center The center (off) pulse width in ms
* @param deadbandMin The low end of the deadband pulse width in ms
* @param min The minimum pulse width in ms
* @param max The max PWM pulse width in us
* @param deadbandMax The high end of the deadband range pulse width in us
* @param center The center (off) pulse width in us
* @param deadbandMin The low end of the deadband pulse width in us
* @param min The minimum pulse width in us
*/
void SetBounds(double max, double deadbandMax, double center,
double deadbandMin, double min);
/**
* Set the bounds on the PWM values.
*
* This sets the bounds on the PWM values for a particular each type of
* controller. The values determine the upper and lower speeds as well as the
* deadband bracket.
*
* @param max The Minimum pwm value
* @param deadbandMax The high end of the deadband range
* @param center The center speed (off)
* @param deadbandMin The low end of the deadband range
* @param min The minimum pwm value
*/
void SetRawBounds(int max, int deadbandMax, int center, int deadbandMin,
int min);
void SetBounds(units::microsecond_t max, units::microsecond_t deadbandMax,
units::microsecond_t center, units::microsecond_t deadbandMin,
units::microsecond_t min);
/**
* Get the bounds on the PWM values.
*
* This Gets the bounds on the PWM values for a particular each type of
* This gets the bounds on the PWM values for a particular each type of
* controller. The values determine the upper and lower speeds as well as the
* deadband bracket.
*
* @param max The Minimum pwm value
* @param max The maximum pwm value
* @param deadbandMax The high end of the deadband range
* @param center The center speed (off)
* @param deadbandMin The low end of the deadband range
* @param min The minimum pwm value
*/
void GetRawBounds(int32_t* max, int32_t* deadbandMax, int32_t* center,
int32_t* deadbandMin, int32_t* min);
void GetBounds(units::microsecond_t* max, units::microsecond_t* deadbandMax,
units::microsecond_t* center,
units::microsecond_t* deadbandMin, units::microsecond_t* min);
/**
* Sets the PWM output to be a continuous high signal while enabled.
*
*/
void SetAlwaysHighMode();
int GetChannel() const;

View File

@@ -4,6 +4,8 @@
#pragma once
#include <units/angle.h>
#include "frc/PWM.h"
namespace frc {
@@ -98,11 +100,11 @@ class Servo : public PWM {
private:
double GetServoAngleRange() const;
static constexpr double kMaxServoAngle = 180.0;
static constexpr double kMaxServoAngle = 180.;
static constexpr double kMinServoAngle = 0.0;
static constexpr double kDefaultMaxServoPWM = 2.4;
static constexpr double kDefaultMinServoPWM = 0.6;
static constexpr units::millisecond_t kDefaultMaxServoPWM = 2.4_ms;
static constexpr units::millisecond_t kDefaultMinServoPWM = 0.6_ms;
};
} // namespace frc

View File

@@ -67,29 +67,29 @@ class PWMSim {
void SetInitialized(bool initialized);
/**
* Register a callback to be run when the PWM raw value changes.
* Register a callback to be run when the PWM pulse microsecond value changes.
*
* @param callback the callback
* @param initialNotify whether to run the callback with the initial value
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
std::unique_ptr<CallbackStore> RegisterRawValueCallback(
std::unique_ptr<CallbackStore> RegisterPulseMicrosecondCallback(
NotifyCallback callback, bool initialNotify);
/**
* Get the PWM raw value.
* Get the PWM pulse microsecond value.
*
* @return the PWM raw value
* @return the PWM pulse microsecond value
*/
int GetRawValue() const;
int32_t GetPulseMicrosecond() const;
/**
* Set the PWM raw value.
* Set the PWM pulse microsecond value.
*
* @param rawValue the PWM raw value
* @param microsecondPulseTime the PWM pulse microsecond value
*/
void SetRawValue(int rawValue);
void SetPulseMicrosecond(int32_t microsecondPulseTime);
/**
* Register a callback to be run when the PWM speed changes.