mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
[wpilib][hal] PWM Raw using microseconds (#5283)
Co-authored-by: Joe <sciencewhiz@users.noreply.github.com>
This commit is contained in:
@@ -39,14 +39,6 @@ constexpr int32_t kExpectedLoopTiming = 40;
|
||||
* devices.
|
||||
*/
|
||||
constexpr float kDefaultPwmPeriod = 5.05f;
|
||||
/**
|
||||
* kDefaultPwmCenter is the PWM range center in ms
|
||||
*/
|
||||
constexpr float kDefaultPwmCenter = 1.5f;
|
||||
/**
|
||||
* kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
|
||||
*/
|
||||
constexpr int32_t kDefaultPwmStepsDown = 1000;
|
||||
constexpr int32_t kPwmDisabled = 0;
|
||||
|
||||
struct DigitalPort {
|
||||
|
||||
@@ -62,7 +62,7 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
|
||||
SimPWMData[origChannel].initialized = true;
|
||||
|
||||
// Defaults to allow an always valid config.
|
||||
HAL_SetPWMConfig(handle, 2.0, 1.501, 1.5, 1.499, 1.0, status);
|
||||
HAL_SetPWMConfigMicroseconds(handle, 2000, 1501, 1500, 1499, 1000, status);
|
||||
|
||||
port->previousAllocation = allocationLocation ? allocationLocation : "";
|
||||
|
||||
@@ -84,62 +84,28 @@ HAL_Bool HAL_CheckPWMChannel(int32_t channel) {
|
||||
return channel < kNumPWMChannels && channel >= 0;
|
||||
}
|
||||
|
||||
void HAL_SetPWMConfig(HAL_DigitalHandle pwmPortHandle, double max,
|
||||
double deadbandMax, double center, double deadbandMin,
|
||||
double min, int32_t* status) {
|
||||
void HAL_SetPWMConfigMicroseconds(HAL_DigitalHandle pwmPortHandle, int32_t max,
|
||||
int32_t deadbandMax, int32_t center,
|
||||
int32_t deadbandMin, int32_t min,
|
||||
int32_t* status) {
|
||||
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
||||
if (port == nullptr) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
// calculate the loop time in milliseconds
|
||||
double loopTime =
|
||||
HAL_GetPWMLoopTiming(status) / (kSystemClockTicksPerMicrosecond * 1e3);
|
||||
if (*status != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t maxPwm = static_cast<int32_t>((max - kDefaultPwmCenter) / loopTime +
|
||||
kDefaultPwmStepsDown - 1);
|
||||
int32_t deadbandMaxPwm = static_cast<int32_t>(
|
||||
(deadbandMax - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
|
||||
int32_t centerPwm = static_cast<int32_t>(
|
||||
(center - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
|
||||
int32_t deadbandMinPwm = static_cast<int32_t>(
|
||||
(deadbandMin - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1);
|
||||
int32_t minPwm = static_cast<int32_t>((min - kDefaultPwmCenter) / loopTime +
|
||||
kDefaultPwmStepsDown - 1);
|
||||
|
||||
port->maxPwm = maxPwm;
|
||||
port->deadbandMaxPwm = deadbandMaxPwm;
|
||||
port->deadbandMinPwm = deadbandMinPwm;
|
||||
port->centerPwm = centerPwm;
|
||||
port->minPwm = minPwm;
|
||||
port->maxPwm = max;
|
||||
port->deadbandMaxPwm = deadbandMax;
|
||||
port->deadbandMinPwm = deadbandMin;
|
||||
port->centerPwm = center;
|
||||
port->minPwm = min;
|
||||
port->configSet = true;
|
||||
}
|
||||
|
||||
void HAL_SetPWMConfigRaw(HAL_DigitalHandle pwmPortHandle, int32_t maxPwm,
|
||||
int32_t deadbandMaxPwm, int32_t centerPwm,
|
||||
int32_t deadbandMinPwm, int32_t minPwm,
|
||||
int32_t* status) {
|
||||
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
||||
if (port == nullptr) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
port->maxPwm = maxPwm;
|
||||
port->deadbandMaxPwm = deadbandMaxPwm;
|
||||
port->deadbandMinPwm = deadbandMinPwm;
|
||||
port->centerPwm = centerPwm;
|
||||
port->minPwm = minPwm;
|
||||
}
|
||||
|
||||
void HAL_GetPWMConfigRaw(HAL_DigitalHandle pwmPortHandle, int32_t* maxPwm,
|
||||
int32_t* deadbandMaxPwm, int32_t* centerPwm,
|
||||
int32_t* deadbandMinPwm, int32_t* minPwm,
|
||||
int32_t* status) {
|
||||
void HAL_GetPWMConfigMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t* maxPwm, int32_t* deadbandMaxPwm,
|
||||
int32_t* centerPwm, int32_t* deadbandMinPwm,
|
||||
int32_t* minPwm, int32_t* status) {
|
||||
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
||||
if (port == nullptr) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
@@ -172,15 +138,15 @@ HAL_Bool HAL_GetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
|
||||
return port->eliminateDeadband;
|
||||
}
|
||||
|
||||
void HAL_SetPWMRaw(HAL_DigitalHandle pwmPortHandle, int32_t value,
|
||||
int32_t* status) {
|
||||
void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t value, int32_t* status) {
|
||||
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
||||
if (port == nullptr) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
SimPWMData[port->channel].rawValue = value;
|
||||
SimPWMData[port->channel].pulseMicrosecond = value;
|
||||
}
|
||||
|
||||
void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed,
|
||||
@@ -231,19 +197,20 @@ void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
SimPWMData[port->channel].rawValue = 0;
|
||||
SimPWMData[port->channel].pulseMicrosecond = 0;
|
||||
SimPWMData[port->channel].position = 0;
|
||||
SimPWMData[port->channel].speed = 0;
|
||||
}
|
||||
|
||||
int32_t HAL_GetPWMRaw(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t* status) {
|
||||
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
||||
if (port == nullptr) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SimPWMData[port->channel].rawValue;
|
||||
return SimPWMData[port->channel].pulseMicrosecond;
|
||||
}
|
||||
|
||||
double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
@@ -299,6 +266,19 @@ void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
SimPWMData[port->channel].zeroLatch = false;
|
||||
}
|
||||
|
||||
void HAL_SetPWMAlwaysHighMode(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t* status) {
|
||||
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
||||
if (port == nullptr) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
SimPWMData[port->channel].pulseMicrosecond = 0xFFFF;
|
||||
SimPWMData[port->channel].position = 0xFFFF;
|
||||
SimPWMData[port->channel].speed = 0xFFFF;
|
||||
}
|
||||
|
||||
void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
|
||||
int32_t* status) {
|
||||
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
||||
|
||||
@@ -17,7 +17,7 @@ void InitializePWMData() {
|
||||
PWMData* hal::SimPWMData;
|
||||
void PWMData::ResetData() {
|
||||
initialized.Reset(false);
|
||||
rawValue.Reset(0);
|
||||
pulseMicrosecond.Reset(0);
|
||||
speed.Reset(0);
|
||||
position.Reset(0);
|
||||
periodScale.Reset(0);
|
||||
@@ -34,7 +34,7 @@ void HALSIM_ResetPWMData(int32_t index) {
|
||||
LOWERNAME)
|
||||
|
||||
DEFINE_CAPI(HAL_Bool, Initialized, initialized)
|
||||
DEFINE_CAPI(int32_t, RawValue, rawValue)
|
||||
DEFINE_CAPI(int32_t, PulseMicrosecond, pulseMicrosecond)
|
||||
DEFINE_CAPI(double, Speed, speed)
|
||||
DEFINE_CAPI(double, Position, position)
|
||||
DEFINE_CAPI(int32_t, PeriodScale, periodScale)
|
||||
@@ -46,7 +46,7 @@ DEFINE_CAPI(HAL_Bool, ZeroLatch, zeroLatch)
|
||||
void HALSIM_RegisterPWMAllCallbacks(int32_t index, HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify) {
|
||||
REGISTER(initialized);
|
||||
REGISTER(rawValue);
|
||||
REGISTER(pulseMicrosecond);
|
||||
REGISTER(speed);
|
||||
REGISTER(position);
|
||||
REGISTER(periodScale);
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
namespace hal {
|
||||
class PWMData {
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Initialized)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(RawValue)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(PulseMicrosecond)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Speed)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(Position)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(PeriodScale)
|
||||
@@ -19,7 +19,8 @@ class PWMData {
|
||||
public:
|
||||
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetInitializedName> initialized{
|
||||
false};
|
||||
SimDataValue<int32_t, HAL_MakeInt, GetRawValueName> rawValue{0};
|
||||
SimDataValue<int32_t, HAL_MakeInt, GetPulseMicrosecondName> pulseMicrosecond{
|
||||
0};
|
||||
SimDataValue<double, HAL_MakeDouble, GetSpeedName> speed{0};
|
||||
SimDataValue<double, HAL_MakeDouble, GetPositionName> position{0};
|
||||
SimDataValue<int32_t, HAL_MakeInt, GetPeriodScaleName> periodScale{0};
|
||||
|
||||
Reference in New Issue
Block a user