mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[wpilib][hal] PWM Raw using microseconds (#5283)
Co-authored-by: Joe <sciencewhiz@users.noreply.github.com>
This commit is contained in:
@@ -112,9 +112,7 @@ void initializeDigital(int32_t* status) {
|
||||
|
||||
pwmSystem->writeConfig_Period(std::lround(kDefaultPwmPeriod / loopTime),
|
||||
status);
|
||||
uint16_t minHigh = std::lround(
|
||||
(kDefaultPwmCenter - kDefaultPwmStepsDown * loopTime) / loopTime);
|
||||
pwmSystem->writeConfig_MinHigh(minHigh, status);
|
||||
pwmSystem->writeConfig_MinHigh(0, status);
|
||||
// Ensure that PWM output values are set to OFF
|
||||
for (uint8_t pwmIndex = 0; pwmIndex < kNumPWMChannels; pwmIndex++) {
|
||||
// Copy of SetPWM
|
||||
|
||||
@@ -47,15 +47,8 @@ constexpr int32_t kExpectedLoopTiming = 40;
|
||||
* devices.
|
||||
*/
|
||||
constexpr double kDefaultPwmPeriod = 5.05;
|
||||
/**
|
||||
* kDefaultPwmCenter is the PWM range center in ms
|
||||
*/
|
||||
constexpr double kDefaultPwmCenter = 1.5;
|
||||
/**
|
||||
* kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
|
||||
*/
|
||||
constexpr int32_t kDefaultPwmStepsDown = 1000;
|
||||
constexpr int32_t kPwmDisabled = 0;
|
||||
constexpr int32_t kPwmAlwaysHigh = 0xFFFF;
|
||||
|
||||
extern std::unique_ptr<tDIO> digitalSystem;
|
||||
extern std::unique_ptr<tRelay> relaySystem;
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "ConstantsInternal.h"
|
||||
#include "DigitalInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
@@ -118,7 +120,7 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
|
||||
}
|
||||
|
||||
// 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 : "";
|
||||
|
||||
@@ -158,62 +160,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;
|
||||
@@ -246,18 +214,30 @@ 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 microsecondPulseTime,
|
||||
int32_t* status) {
|
||||
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
||||
if (port == nullptr) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
|
||||
if (microsecondPulseTime < 0 ||
|
||||
(microsecondPulseTime != 0xFFFF && microsecondPulseTime >= 4096)) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
hal::SetLastError(
|
||||
status,
|
||||
fmt::format("Pulse time {} out of range. Expect [0-4096) or 0xFFFF",
|
||||
microsecondPulseTime));
|
||||
return;
|
||||
}
|
||||
|
||||
if (port->channel < tPWM::kNumHdrRegisters) {
|
||||
pwmSystem->writeHdr(port->channel, value, status);
|
||||
pwmSystem->writeHdr(port->channel, microsecondPulseTime, status);
|
||||
} else {
|
||||
pwmSystem->writeMXP(port->channel - tPWM::kNumHdrRegisters, value, status);
|
||||
pwmSystem->writeMXP(port->channel - tPWM::kNumHdrRegisters,
|
||||
microsecondPulseTime, status);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +282,7 @@ void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed,
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_SetPWMRaw(pwmPortHandle, rawValue, status);
|
||||
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, rawValue, status);
|
||||
}
|
||||
|
||||
void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double pos,
|
||||
@@ -335,14 +315,15 @@ void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double pos,
|
||||
return;
|
||||
}
|
||||
|
||||
HAL_SetPWMRaw(pwmPortHandle, rawValue, status);
|
||||
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, rawValue, status);
|
||||
}
|
||||
|
||||
void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
HAL_SetPWMRaw(pwmPortHandle, kPwmDisabled, status);
|
||||
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, kPwmDisabled, status);
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -367,12 +348,11 @@ double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t value = HAL_GetPWMRaw(pwmPortHandle, status);
|
||||
int32_t value = HAL_GetPWMPulseTimeMicroseconds(pwmPortHandle, status);
|
||||
if (*status != 0) {
|
||||
return 0;
|
||||
}
|
||||
DigitalPort* dPort = port.get();
|
||||
|
||||
if (value == kPwmDisabled) {
|
||||
return 0.0;
|
||||
} else if (value > GetMaxPositivePwm(dPort)) {
|
||||
@@ -401,7 +381,7 @@ double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t value = HAL_GetPWMRaw(pwmPortHandle, status);
|
||||
int32_t value = HAL_GetPWMPulseTimeMicroseconds(pwmPortHandle, status);
|
||||
if (*status != 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -444,6 +424,11 @@ void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_SetPWMAlwaysHighMode(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t* status) {
|
||||
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, kPwmAlwaysHigh, status);
|
||||
}
|
||||
|
||||
int32_t HAL_GetPWMLoopTiming(int32_t* status) {
|
||||
initializeDigital(status);
|
||||
if (*status != 0) {
|
||||
|
||||
@@ -13,7 +13,7 @@ void HALSIM_ResetPWMData(int32_t index) {}
|
||||
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, PWM##CAPINAME, RETURN)
|
||||
|
||||
DEFINE_CAPI(HAL_Bool, Initialized, false)
|
||||
DEFINE_CAPI(int32_t, RawValue, 0)
|
||||
DEFINE_CAPI(int32_t, PulseMicrosecond, 0)
|
||||
DEFINE_CAPI(double, Speed, 0)
|
||||
DEFINE_CAPI(double, Position, 0)
|
||||
DEFINE_CAPI(int32_t, PeriodScale, 0)
|
||||
|
||||
@@ -63,43 +63,27 @@ Java_edu_wpi_first_hal_PWMJNI_freePWMPort
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_PWMJNI
|
||||
* Method: setPWMConfigRaw
|
||||
* Method: setPWMConfigMicroseconds
|
||||
* Signature: (IIIIII)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_PWMJNI_setPWMConfigRaw
|
||||
Java_edu_wpi_first_hal_PWMJNI_setPWMConfigMicroseconds
|
||||
(JNIEnv* env, jclass, jint id, jint maxPwm, jint deadbandMaxPwm,
|
||||
jint centerPwm, jint deadbandMinPwm, jint minPwm)
|
||||
{
|
||||
int32_t status = 0;
|
||||
HAL_SetPWMConfigRaw((HAL_DigitalHandle)id, maxPwm, deadbandMaxPwm, centerPwm,
|
||||
deadbandMinPwm, minPwm, &status);
|
||||
HAL_SetPWMConfigMicroseconds((HAL_DigitalHandle)id, maxPwm, deadbandMaxPwm,
|
||||
centerPwm, deadbandMinPwm, minPwm, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_PWMJNI
|
||||
* Method: setPWMConfig
|
||||
* Signature: (IDDDDD)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_PWMJNI_setPWMConfig
|
||||
(JNIEnv* env, jclass, jint id, jdouble maxPwm, jdouble deadbandMaxPwm,
|
||||
jdouble centerPwm, jdouble deadbandMinPwm, jdouble minPwm)
|
||||
{
|
||||
int32_t status = 0;
|
||||
HAL_SetPWMConfig((HAL_DigitalHandle)id, maxPwm, deadbandMaxPwm, centerPwm,
|
||||
deadbandMinPwm, minPwm, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_PWMJNI
|
||||
* Method: getPWMConfigRaw
|
||||
* Method: getPWMConfigMicroseconds
|
||||
* Signature: (I)Ljava/lang/Object;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_edu_wpi_first_hal_PWMJNI_getPWMConfigRaw
|
||||
Java_edu_wpi_first_hal_PWMJNI_getPWMConfigMicroseconds
|
||||
(JNIEnv* env, jclass, jint id)
|
||||
{
|
||||
int32_t status = 0;
|
||||
@@ -108,8 +92,8 @@ Java_edu_wpi_first_hal_PWMJNI_getPWMConfigRaw
|
||||
int32_t centerPwm = 0;
|
||||
int32_t deadbandMinPwm = 0;
|
||||
int32_t minPwm = 0;
|
||||
HAL_GetPWMConfigRaw((HAL_DigitalHandle)id, &maxPwm, &deadbandMaxPwm,
|
||||
¢erPwm, &deadbandMinPwm, &minPwm, &status);
|
||||
HAL_GetPWMConfigMicroseconds((HAL_DigitalHandle)id, &maxPwm, &deadbandMaxPwm,
|
||||
¢erPwm, &deadbandMinPwm, &minPwm, &status);
|
||||
CheckStatus(env, status);
|
||||
return CreatePWMConfigDataResult(env, maxPwm, deadbandMaxPwm, centerPwm,
|
||||
deadbandMinPwm, minPwm);
|
||||
@@ -146,15 +130,15 @@ Java_edu_wpi_first_hal_PWMJNI_getPWMEliminateDeadband
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_PWMJNI
|
||||
* Method: setPWMRaw
|
||||
* Signature: (IS)V
|
||||
* Method: setPulseTimeMicroseconds
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_PWMJNI_setPWMRaw
|
||||
(JNIEnv* env, jclass, jint id, jshort value)
|
||||
Java_edu_wpi_first_hal_PWMJNI_setPulseTimeMicroseconds
|
||||
(JNIEnv* env, jclass, jint id, jint value)
|
||||
{
|
||||
int32_t status = 0;
|
||||
HAL_SetPWMRaw((HAL_DigitalHandle)id, value, &status);
|
||||
HAL_SetPWMPulseTimeMicroseconds((HAL_DigitalHandle)id, value, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
@@ -188,15 +172,16 @@ Java_edu_wpi_first_hal_PWMJNI_setPWMPosition
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_PWMJNI
|
||||
* Method: getPWMRaw
|
||||
* Signature: (I)S
|
||||
* Method: getPulseTimeMicroseconds
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jshort JNICALL
|
||||
Java_edu_wpi_first_hal_PWMJNI_getPWMRaw
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_PWMJNI_getPulseTimeMicroseconds
|
||||
(JNIEnv* env, jclass, jint id)
|
||||
{
|
||||
int32_t status = 0;
|
||||
jshort returnValue = HAL_GetPWMRaw((HAL_DigitalHandle)id, &status);
|
||||
int32_t returnValue =
|
||||
HAL_GetPWMPulseTimeMicroseconds((HAL_DigitalHandle)id, &status);
|
||||
CheckStatus(env, status);
|
||||
return returnValue;
|
||||
}
|
||||
@@ -259,6 +244,20 @@ Java_edu_wpi_first_hal_PWMJNI_latchPWMZero
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_PWMJNI
|
||||
* Method: setAlwaysHighMode
|
||||
* Signature: (I)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_PWMJNI_setAlwaysHighMode
|
||||
(JNIEnv* env, jclass, jint id)
|
||||
{
|
||||
int32_t status = 0;
|
||||
HAL_SetPWMAlwaysHighMode((HAL_DigitalHandle)id, &status);
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_PWMJNI
|
||||
* Method: setPWMPeriodScale
|
||||
|
||||
@@ -64,52 +64,52 @@ Java_edu_wpi_first_hal_simulation_PWMDataJNI_setInitialized
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
|
||||
* Method: registerRawValueCallback
|
||||
* Method: registerPulseMicrosecondCallback
|
||||
* Signature: (ILjava/lang/Object;Z)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_PWMDataJNI_registerRawValueCallback
|
||||
Java_edu_wpi_first_hal_simulation_PWMDataJNI_registerPulseMicrosecondCallback
|
||||
(JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify)
|
||||
{
|
||||
return sim::AllocateCallback(env, index, callback, initialNotify,
|
||||
&HALSIM_RegisterPWMRawValueCallback);
|
||||
&HALSIM_RegisterPWMPulseMicrosecondCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
|
||||
* Method: cancelRawValueCallback
|
||||
* Method: cancelPulseMicrosecondCallback
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_PWMDataJNI_cancelRawValueCallback
|
||||
Java_edu_wpi_first_hal_simulation_PWMDataJNI_cancelPulseMicrosecondCallback
|
||||
(JNIEnv* env, jclass, jint index, jint handle)
|
||||
{
|
||||
return sim::FreeCallback(env, handle, index,
|
||||
&HALSIM_CancelPWMRawValueCallback);
|
||||
&HALSIM_CancelPWMPulseMicrosecondCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
|
||||
* Method: getRawValue
|
||||
* Method: getPulseMicrosecond
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_PWMDataJNI_getRawValue
|
||||
Java_edu_wpi_first_hal_simulation_PWMDataJNI_getPulseMicrosecond
|
||||
(JNIEnv*, jclass, jint index)
|
||||
{
|
||||
return HALSIM_GetPWMRawValue(index);
|
||||
return HALSIM_GetPWMPulseMicrosecond(index);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
|
||||
* Method: setRawValue
|
||||
* Method: setPulseMicrosecond
|
||||
* Signature: (II)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_PWMDataJNI_setRawValue
|
||||
Java_edu_wpi_first_hal_simulation_PWMDataJNI_setPulseMicrosecond
|
||||
(JNIEnv*, jclass, jint index, jint value)
|
||||
{
|
||||
HALSIM_SetPWMRawValue(index, value);
|
||||
HALSIM_SetPWMPulseMicrosecond(index, value);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -50,7 +50,7 @@ HAL_Bool HAL_CheckPWMChannel(int32_t channel);
|
||||
/**
|
||||
* Sets the configuration settings for the PWM channel.
|
||||
*
|
||||
* All values are in milliseconds.
|
||||
* All values are in microseconds.
|
||||
*
|
||||
* @param[in] pwmPortHandle the PWM handle
|
||||
* @param[in] maxPwm the maximum PWM value
|
||||
@@ -60,17 +60,15 @@ HAL_Bool HAL_CheckPWMChannel(int32_t channel);
|
||||
* @param[in] minPwm the minimum PWM value
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
*/
|
||||
void HAL_SetPWMConfig(HAL_DigitalHandle pwmPortHandle, double maxPwm,
|
||||
double deadbandMaxPwm, double centerPwm,
|
||||
double deadbandMinPwm, double minPwm, int32_t* status);
|
||||
void HAL_SetPWMConfigMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t maxPwm, int32_t deadbandMaxPwm,
|
||||
int32_t centerPwm, int32_t deadbandMinPwm,
|
||||
int32_t minPwm, int32_t* status);
|
||||
|
||||
/**
|
||||
* Sets the raw configuration settings for the PWM channel.
|
||||
* Gets the pwm configuration settings for the PWM channel.
|
||||
*
|
||||
* We recommend using HAL_SetPWMConfig() instead, as those values are properly
|
||||
* scaled. Usually used for values grabbed by HAL_GetPWMConfigRaw().
|
||||
*
|
||||
* Values are in raw FPGA units.
|
||||
* Values are in microseconds.
|
||||
*
|
||||
* @param[in] pwmPortHandle the PWM handle
|
||||
* @param[in] maxPwm the maximum PWM value
|
||||
@@ -80,29 +78,10 @@ void HAL_SetPWMConfig(HAL_DigitalHandle pwmPortHandle, double maxPwm,
|
||||
* @param[in] minPwm the minimum PWM value
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
*/
|
||||
void HAL_SetPWMConfigRaw(HAL_DigitalHandle pwmPortHandle, int32_t maxPwm,
|
||||
int32_t deadbandMaxPwm, int32_t centerPwm,
|
||||
int32_t deadbandMinPwm, int32_t minPwm,
|
||||
int32_t* status);
|
||||
|
||||
/**
|
||||
* Gets the raw pwm configuration settings for the PWM channel.
|
||||
*
|
||||
* Values are in raw FPGA units. These units have the potential to change for
|
||||
* any FPGA release.
|
||||
*
|
||||
* @param[in] pwmPortHandle the PWM handle
|
||||
* @param[in] maxPwm the maximum PWM value
|
||||
* @param[in] deadbandMaxPwm the high range of the center deadband
|
||||
* @param[in] centerPwm the center PWM value
|
||||
* @param[in] deadbandMinPwm the low range of the center deadband
|
||||
* @param[in] minPwm the minimum PWM value
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* Sets if the FPGA should output the center value if the input value is within
|
||||
@@ -126,17 +105,16 @@ HAL_Bool HAL_GetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t* status);
|
||||
|
||||
/**
|
||||
* Sets a PWM channel to the desired value.
|
||||
* Sets a PWM channel to the desired pulse width in microseconds.
|
||||
*
|
||||
* The values are in raw FPGA units, and have the potential to change with any
|
||||
* FPGA release.
|
||||
*
|
||||
* @param[in] pwmPortHandle the PWM handle
|
||||
* @param[in] value the PWM value to set
|
||||
* @param[in] microsecondPulseTime the PWM value to set
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
*/
|
||||
void HAL_SetPWMRaw(HAL_DigitalHandle pwmPortHandle, int32_t value,
|
||||
int32_t* status);
|
||||
void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t microsecondPulseTime,
|
||||
int32_t* status);
|
||||
|
||||
/**
|
||||
* Sets a PWM channel to the desired scaled value.
|
||||
@@ -177,16 +155,14 @@ void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double position,
|
||||
void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t* status);
|
||||
|
||||
/**
|
||||
* Gets a value from a PWM channel.
|
||||
*
|
||||
* The values are in raw FPGA units, and have the potential to change with any
|
||||
* FPGA release.
|
||||
* Gets the current microsecond pulse time from a PWM channel.
|
||||
*
|
||||
* @param[in] pwmPortHandle the PWM handle
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
* @return the current raw PWM value
|
||||
* @return the current PWM microsecond pulse time
|
||||
*/
|
||||
int32_t HAL_GetPWMRaw(HAL_DigitalHandle pwmPortHandle, int32_t* status);
|
||||
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
||||
int32_t* status);
|
||||
|
||||
/**
|
||||
* Gets a scaled value from a PWM channel.
|
||||
@@ -228,6 +204,14 @@ void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status);
|
||||
void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
|
||||
int32_t* status);
|
||||
|
||||
/**
|
||||
* Sets the PWM output to be a continous high signal while enabled.
|
||||
*
|
||||
* @param[in] pwmPortHandle the PWM handle.
|
||||
* @param[out] status Error status variable. 0 on success.
|
||||
*/
|
||||
void HAL_SetPWMAlwaysHighMode(HAL_DigitalHandle pwmPortHandle, int32_t* status);
|
||||
|
||||
/**
|
||||
* Gets the loop timing of the PWM system.
|
||||
*
|
||||
|
||||
@@ -20,12 +20,13 @@ void HALSIM_CancelPWMInitializedCallback(int32_t index, int32_t uid);
|
||||
HAL_Bool HALSIM_GetPWMInitialized(int32_t index);
|
||||
void HALSIM_SetPWMInitialized(int32_t index, HAL_Bool initialized);
|
||||
|
||||
int32_t HALSIM_RegisterPWMRawValueCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
void HALSIM_CancelPWMRawValueCallback(int32_t index, int32_t uid);
|
||||
int32_t HALSIM_GetPWMRawValue(int32_t index);
|
||||
void HALSIM_SetPWMRawValue(int32_t index, int32_t rawValue);
|
||||
int32_t HALSIM_RegisterPWMPulseMicrosecondCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void HALSIM_CancelPWMPulseMicrosecondCallback(int32_t index, int32_t uid);
|
||||
int32_t HALSIM_GetPWMPulseMicrosecond(int32_t index);
|
||||
void HALSIM_SetPWMPulseMicrosecond(int32_t index, int32_t microsecondPulseTime);
|
||||
|
||||
int32_t HALSIM_RegisterPWMSpeedCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
|
||||
@@ -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