[hal, wpilib] PWM Rewrite (#7845)

The HAL will only contain the output period and the raw microseconds. Higher level things such as SimDevice can handle everything else.
This commit is contained in:
Thad House
2025-03-20 19:23:22 -07:00
committed by GitHub
parent 2e21a41f87
commit e2cc9e0059
96 changed files with 1037 additions and 2453 deletions

View File

@@ -123,13 +123,6 @@ public class DIOJNI extends JNIWrapper {
*/
public static native boolean isAnyPulsing();
/**
* Gets the loop timing of the PWM system.
*
* @return the loop time in clock ticks
*/
public static native short getLoopTiming();
/**
* Allocates a DO PWM Generator.
*

View File

@@ -1,32 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.hal;
/** Structure for holding the config data result for PWM. */
@SuppressWarnings("MemberName")
public class PWMConfigDataResult {
PWMConfigDataResult(int max, int deadbandMax, int center, int deadbandMin, int min) {
this.max = max;
this.deadbandMax = deadbandMax;
this.center = center;
this.deadbandMin = deadbandMin;
this.min = min;
}
/** The maximum PWM value in microseconds. */
public int max;
/** The deadband maximum PWM value in microseconds. */
public int deadbandMax;
/** The center PWM value in microseconds. */
public int center;
/** The deadband minimum PWM value in microseconds. */
public int deadbandMin;
/** The minimum PWM value in microseconds. */
public int min;
}

View File

@@ -34,50 +34,13 @@ public class PWMJNI extends DIOJNI {
public static native void freePWMPort(int pwmPortHandle);
/**
* Sets the configuration settings for the PWM channel.
* Indicates the pwm is used by a simulated device.
*
* <p>All values are in microseconds.
*
* @param pwmPortHandle the PWM handle
* @param maxPwm the maximum PWM value
* @param deadbandMaxPwm the high range of the center deadband
* @param centerPwm the center PWM value
* @param deadbandMinPwm the low range of the center deadband
* @param minPwm the minimum PWM value
* @param handle the pwm handle
* @param device simulated device handle
* @see "HAL_SetPWMSimDevice"
*/
public static native void setPWMConfigMicroseconds(
int pwmPortHandle,
int maxPwm,
int deadbandMaxPwm,
int centerPwm,
int deadbandMinPwm,
int minPwm);
/**
* Gets the pwm configuration settings for the PWM channel.
*
* <p>Values are in microseconds.
*
* @param pwmPortHandle the PWM handle
* @return the pwm configuration settings
*/
public static native PWMConfigDataResult getPWMConfigMicroseconds(int pwmPortHandle);
/**
* Sets if the FPGA should output the center value if the input value is within the deadband.
*
* @param pwmPortHandle the PWM handle
* @param eliminateDeadband true to eliminate deadband, otherwise false
*/
public static native void setPWMEliminateDeadband(int pwmPortHandle, boolean eliminateDeadband);
/**
* Gets the current eliminate deadband value.
*
* @param pwmPortHandle the PWM handle
* @return true if set, otherwise false
*/
public static native boolean getPWMEliminateDeadband(int pwmPortHandle);
public static native void setPWMSimDevice(int handle, int device);
/**
* Sets a PWM channel to the desired pulse width in microseconds.
@@ -87,28 +50,6 @@ public class PWMJNI extends DIOJNI {
*/
public static native void setPulseTimeMicroseconds(int pwmPortHandle, int microsecondPulseTime);
/**
* Sets a PWM channel to the desired scaled value.
*
* <p>The values range from -1 to 1 and the period is controlled by the PWM Period and MinHigh
* registers.
*
* @param pwmPortHandle the PWM handle
* @param speed the scaled PWM value to set
*/
public static native void setPWMSpeed(int pwmPortHandle, double speed);
/**
* Sets a PWM channel to the desired position value.
*
* <p>The values range from 0 to 1 and the period is controlled by the PWM Period and MinHigh
* registers.
*
* @param pwmPortHandle the PWM handle
* @param position the positional PWM value to set
*/
public static native void setPWMPosition(int pwmPortHandle, double position);
/**
* Gets the current microsecond pulse time from a PWM channel.
*
@@ -118,56 +59,12 @@ public class PWMJNI extends DIOJNI {
public static native int getPulseTimeMicroseconds(int pwmPortHandle);
/**
* Gets a scaled value from a PWM channel.
*
* <p>The values range from -1 to 1.
*
* @param pwmPortHandle the PWM handle
* @return the current speed PWM value
*/
public static native double getPWMSpeed(int pwmPortHandle);
/**
* Gets a position value from a PWM channel.
*
* <p>The values range from 0 to 1.
*
* @param pwmPortHandle the PWM handle
* @return the current positional PWM value
*/
public static native double getPWMPosition(int pwmPortHandle);
/**
* Sets a PWM channel to be disabled.
*
* <p>The channel is disabled until the next time it is set. Note this is different from just
* setting a 0 speed, as this will actively stop all signaling on the channel.
* Sets the PWM output period.
*
* @param pwmPortHandle the PWM handle.
* @param period 0 for 5ms, 1 or 2 for 10ms, 3 for 20ms
*/
public static native void setPWMDisabled(int pwmPortHandle);
/**
* Forces a PWM signal to go to 0 temporarily.
*
* @param pwmPortHandle the PWM handle.
*/
public static native void latchPWMZero(int pwmPortHandle);
/**
* Sets the PWM output to be a continuous high signal while enabled.
*
* @param pwmPortHandle the PWM handle.
*/
public static native void setAlwaysHighMode(int pwmPortHandle);
/**
* Sets how how often the PWM signal is squelched, thus scaling the period.
*
* @param pwmPortHandle the PWM handle.
* @param squelchMask the 2-bit mask of outputs to squelch
*/
public static native void setPWMPeriodScale(int pwmPortHandle, int squelchMask);
public static native void setPWMOutputPeriod(int pwmPortHandle, int period);
/** Utility class. */
private PWMJNI() {}

View File

@@ -26,41 +26,14 @@ public class PWMDataJNI extends JNIWrapper {
public static native void setPulseMicrosecond(int index, int microsecondPulseTime);
public static native int registerSpeedCallback(
public static native int registerOutputPeriodCallback(
int index, NotifyCallback callback, boolean initialNotify);
public static native void cancelSpeedCallback(int index, int uid);
public static native void cancelOutputPeriodCallback(int index, int uid);
public static native double getSpeed(int index);
public static native int getOutputPeriod(int index);
public static native void setSpeed(int index, double speed);
public static native int registerPositionCallback(
int index, NotifyCallback callback, boolean initialNotify);
public static native void cancelPositionCallback(int index, int uid);
public static native double getPosition(int index);
public static native void setPosition(int index, double position);
public static native int registerPeriodScaleCallback(
int index, NotifyCallback callback, boolean initialNotify);
public static native void cancelPeriodScaleCallback(int index, int uid);
public static native int getPeriodScale(int index);
public static native void setPeriodScale(int index, int periodScale);
public static native int registerZeroLatchCallback(
int index, NotifyCallback callback, boolean initialNotify);
public static native void cancelZeroLatchCallback(int index, int uid);
public static native boolean getZeroLatch(int index);
public static native void setZeroLatch(int index, boolean zeroLatch);
public static native void setOutputPeriod(int index, int period);
public static native void resetData(int index);

View File

@@ -190,21 +190,6 @@ Java_edu_wpi_first_hal_DIOJNI_isAnyPulsing
return returnValue;
}
/*
* Class: edu_wpi_first_hal_DIOJNI
* Method: getLoopTiming
* Signature: ()S
*/
JNIEXPORT jshort JNICALL
Java_edu_wpi_first_hal_DIOJNI_getLoopTiming
(JNIEnv* env, jclass)
{
int32_t status = 0;
jshort returnValue = HAL_GetPWMLoopTiming(&status);
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_hal_DIOJNI
* Method: allocateDigitalPWM

View File

@@ -44,7 +44,6 @@ static JException halHandleExCls;
static JException uncleanStatusExCls;
static JException nullPointerEx;
static JClass powerDistributionVersionCls;
static JClass pwmConfigDataResultCls;
static JClass canStatusCls;
static JClass matchInfoDataCls;
static JClass canReceiveMessageCls;
@@ -56,7 +55,6 @@ static JClass canStreamOverflowExCls;
static const JClassInit classes[] = {
{"edu/wpi/first/hal/PowerDistributionVersion",
&powerDistributionVersionCls},
{"edu/wpi/first/hal/PWMConfigDataResult", &pwmConfigDataResultCls},
{"edu/wpi/first/hal/can/CANStatus", &canStatusCls},
{"edu/wpi/first/hal/MatchInfoData", &matchInfoDataCls},
{"edu/wpi/first/hal/can/CANReceiveMessage", &canReceiveMessageCls},
@@ -180,17 +178,6 @@ void ThrowBoundaryException(JNIEnv* env, double value, double lower,
env->Throw(static_cast<jthrowable>(ex));
}
jobject CreatePWMConfigDataResult(JNIEnv* env, int32_t maxPwm,
int32_t deadbandMaxPwm, int32_t centerPwm,
int32_t deadbandMinPwm, int32_t minPwm) {
static jmethodID constructor =
env->GetMethodID(pwmConfigDataResultCls, "<init>", "(IIIII)V");
return env->NewObject(
pwmConfigDataResultCls, constructor, static_cast<jint>(maxPwm),
static_cast<jint>(deadbandMaxPwm), static_cast<jint>(centerPwm),
static_cast<jint>(deadbandMinPwm), static_cast<jint>(minPwm));
}
jobject CreateREVPHVersion(JNIEnv* env, uint32_t firmwareMajor,
uint32_t firmwareMinor, uint32_t firmwareFix,
uint32_t hardwareMinor, uint32_t hardwareMajor,

View File

@@ -49,10 +49,6 @@ void ThrowIllegalArgumentException(JNIEnv* env, std::string_view msg);
void ThrowBoundaryException(JNIEnv* env, double value, double lower,
double upper);
jobject CreatePWMConfigDataResult(JNIEnv* env, int32_t maxPwm,
int32_t deadbandMaxPwm, int32_t centerPwm,
int32_t deadbandMinPwm, int32_t minPwm);
jobject CreateREVPHVersion(JNIEnv* env, uint32_t firmwareMajor,
uint32_t firmwareMinor, uint32_t firmwareFix,
uint32_t hardwareMinor, uint32_t hardwareMajor,

View File

@@ -63,69 +63,14 @@ Java_edu_wpi_first_hal_PWMJNI_freePWMPort
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: setPWMConfigMicroseconds
* Signature: (IIIIII)V
* Method: setPWMSimDevice
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_PWMJNI_setPWMConfigMicroseconds
(JNIEnv* env, jclass, jint id, jint maxPwm, jint deadbandMaxPwm,
jint centerPwm, jint deadbandMinPwm, jint minPwm)
Java_edu_wpi_first_hal_PWMJNI_setPWMSimDevice
(JNIEnv* env, jclass, jint handle, jint device)
{
int32_t status = 0;
HAL_SetPWMConfigMicroseconds((HAL_DigitalHandle)id, maxPwm, deadbandMaxPwm,
centerPwm, deadbandMinPwm, minPwm, &status);
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: getPWMConfigMicroseconds
* Signature: (I)Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL
Java_edu_wpi_first_hal_PWMJNI_getPWMConfigMicroseconds
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
int32_t maxPwm = 0;
int32_t deadbandMaxPwm = 0;
int32_t centerPwm = 0;
int32_t deadbandMinPwm = 0;
int32_t minPwm = 0;
HAL_GetPWMConfigMicroseconds((HAL_DigitalHandle)id, &maxPwm, &deadbandMaxPwm,
&centerPwm, &deadbandMinPwm, &minPwm, &status);
CheckStatus(env, status);
return CreatePWMConfigDataResult(env, maxPwm, deadbandMaxPwm, centerPwm,
deadbandMinPwm, minPwm);
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: setPWMEliminateDeadband
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_PWMJNI_setPWMEliminateDeadband
(JNIEnv* env, jclass, jint id, jboolean value)
{
int32_t status = 0;
HAL_SetPWMEliminateDeadband((HAL_DigitalHandle)id, value, &status);
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: getPWMEliminateDeadband
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL
Java_edu_wpi_first_hal_PWMJNI_getPWMEliminateDeadband
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
auto val = HAL_GetPWMEliminateDeadband((HAL_DigitalHandle)id, &status);
CheckStatus(env, status);
return (jboolean)val;
HAL_SetPWMSimDevice((HAL_DigitalHandle)handle, (HAL_SimDeviceHandle)device);
}
/*
@@ -142,34 +87,6 @@ Java_edu_wpi_first_hal_PWMJNI_setPulseTimeMicroseconds
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: setPWMSpeed
* Signature: (ID)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_PWMJNI_setPWMSpeed
(JNIEnv* env, jclass, jint id, jdouble value)
{
int32_t status = 0;
HAL_SetPWMSpeed((HAL_DigitalHandle)id, value, &status);
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: setPWMPosition
* Signature: (ID)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_PWMJNI_setPWMPosition
(JNIEnv* env, jclass, jint id, jdouble value)
{
int32_t status = 0;
HAL_SetPWMPosition((HAL_DigitalHandle)id, value, &status);
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: getPulseTimeMicroseconds
@@ -188,87 +105,15 @@ Java_edu_wpi_first_hal_PWMJNI_getPulseTimeMicroseconds
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: getPWMSpeed
* Signature: (I)D
*/
JNIEXPORT jdouble JNICALL
Java_edu_wpi_first_hal_PWMJNI_getPWMSpeed
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
jdouble returnValue = HAL_GetPWMSpeed((HAL_DigitalHandle)id, &status);
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: getPWMPosition
* Signature: (I)D
*/
JNIEXPORT jdouble JNICALL
Java_edu_wpi_first_hal_PWMJNI_getPWMPosition
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
jdouble returnValue = HAL_GetPWMPosition((HAL_DigitalHandle)id, &status);
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: setPWMDisabled
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_PWMJNI_setPWMDisabled
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_SetPWMDisabled((HAL_DigitalHandle)id, &status);
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_hal_PWMJNI
* Method: latchPWMZero
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_PWMJNI_latchPWMZero
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_LatchPWMZero((HAL_DigitalHandle)id, &status);
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
* Method: setPWMOutputPeriod
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_PWMJNI_setPWMPeriodScale
Java_edu_wpi_first_hal_PWMJNI_setPWMOutputPeriod
(JNIEnv* env, jclass, jint id, jint value)
{
int32_t status = 0;
HAL_SetPWMPeriodScale((HAL_DigitalHandle)id, value, &status);
HAL_SetPWMOutputPeriod((HAL_DigitalHandle)id, value, &status);
CheckStatus(env, status);
}

View File

@@ -114,201 +114,52 @@ Java_edu_wpi_first_hal_simulation_PWMDataJNI_setPulseMicrosecond
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: registerSpeedCallback
* Method: registerOutputPeriodCallback
* Signature: (ILjava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_registerSpeedCallback
Java_edu_wpi_first_hal_simulation_PWMDataJNI_registerOutputPeriodCallback
(JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify)
{
return sim::AllocateCallback(env, index, callback, initialNotify,
&HALSIM_RegisterPWMSpeedCallback);
&HALSIM_RegisterPWMOutputPeriodCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: cancelSpeedCallback
* Method: cancelOutputPeriodCallback
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_cancelSpeedCallback
(JNIEnv* env, jclass, jint index, jint handle)
{
return sim::FreeCallback(env, handle, index, &HALSIM_CancelPWMSpeedCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: getSpeed
* Signature: (I)D
*/
JNIEXPORT jdouble JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_getSpeed
(JNIEnv*, jclass, jint index)
{
return HALSIM_GetPWMSpeed(index);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: setSpeed
* Signature: (ID)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_setSpeed
(JNIEnv*, jclass, jint index, jdouble value)
{
HALSIM_SetPWMSpeed(index, value);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: registerPositionCallback
* Signature: (ILjava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_registerPositionCallback
(JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify)
{
return sim::AllocateCallback(env, index, callback, initialNotify,
&HALSIM_RegisterPWMPositionCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: cancelPositionCallback
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_cancelPositionCallback
Java_edu_wpi_first_hal_simulation_PWMDataJNI_cancelOutputPeriodCallback
(JNIEnv* env, jclass, jint index, jint handle)
{
return sim::FreeCallback(env, handle, index,
&HALSIM_CancelPWMPositionCallback);
&HALSIM_CancelPWMOutputPeriodCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: getPosition
* Signature: (I)D
*/
JNIEXPORT jdouble JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_getPosition
(JNIEnv*, jclass, jint index)
{
return HALSIM_GetPWMPosition(index);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: setPosition
* Signature: (ID)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_setPosition
(JNIEnv*, jclass, jint index, jdouble value)
{
HALSIM_SetPWMPosition(index, value);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: registerPeriodScaleCallback
* Signature: (ILjava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_registerPeriodScaleCallback
(JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify)
{
return sim::AllocateCallback(env, index, callback, initialNotify,
&HALSIM_RegisterPWMPeriodScaleCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: cancelPeriodScaleCallback
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_cancelPeriodScaleCallback
(JNIEnv* env, jclass, jint index, jint handle)
{
return sim::FreeCallback(env, handle, index,
&HALSIM_CancelPWMPeriodScaleCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: getPeriodScale
* Method: getOutputPeriod
* Signature: (I)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_getPeriodScale
Java_edu_wpi_first_hal_simulation_PWMDataJNI_getOutputPeriod
(JNIEnv*, jclass, jint index)
{
return HALSIM_GetPWMPeriodScale(index);
return HALSIM_GetPWMOutputPeriod(index);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: setPeriodScale
* Method: setOutputPeriod
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_setPeriodScale
Java_edu_wpi_first_hal_simulation_PWMDataJNI_setOutputPeriod
(JNIEnv*, jclass, jint index, jint value)
{
HALSIM_SetPWMPeriodScale(index, value);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: registerZeroLatchCallback
* Signature: (ILjava/lang/Object;Z)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_registerZeroLatchCallback
(JNIEnv* env, jclass, jint index, jobject callback, jboolean initialNotify)
{
return sim::AllocateCallback(env, index, callback, initialNotify,
&HALSIM_RegisterPWMZeroLatchCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: cancelZeroLatchCallback
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_cancelZeroLatchCallback
(JNIEnv* env, jclass, jint index, jint handle)
{
return sim::FreeCallback(env, handle, index,
&HALSIM_CancelPWMZeroLatchCallback);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: getZeroLatch
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_getZeroLatch
(JNIEnv*, jclass, jint index)
{
return HALSIM_GetPWMZeroLatch(index);
}
/*
* Class: edu_wpi_first_hal_simulation_PWMDataJNI
* Method: setZeroLatch
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_PWMDataJNI_setZeroLatch
(JNIEnv*, jclass, jint index, jboolean value)
{
HALSIM_SetPWMZeroLatch(index, value);
HALSIM_SetPWMOutputPeriod(index, value);
}
/*

View File

@@ -47,61 +47,12 @@ void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle);
HAL_Bool HAL_CheckPWMChannel(int32_t channel);
/**
* Sets the configuration settings for the PWM channel.
* Indicates the pwm is used by a simulated device.
*
* All values are in microseconds.
*
* @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.
* @param handle the pwm handle
* @param device simulated device handle
*/
void HAL_SetPWMConfigMicroseconds(HAL_DigitalHandle pwmPortHandle,
int32_t maxPwm, int32_t deadbandMaxPwm,
int32_t centerPwm, int32_t deadbandMinPwm,
int32_t minPwm, int32_t* status);
/**
* Gets the pwm configuration settings for the PWM channel.
*
* Values are in microseconds.
*
* @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_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
* the deadband.
*
* @param[in] pwmPortHandle the PWM handle
* @param[in] eliminateDeadband true to eliminate deadband, otherwise false
* @param[out] status Error status variable. 0 on success.
*/
void HAL_SetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
HAL_Bool eliminateDeadband, int32_t* status);
/**
* Gets the current eliminate deadband value.
*
* @param[in] pwmPortHandle the PWM handle
* @param[out] status Error status variable. 0 on success.
* @return true if set, otherwise false
*/
HAL_Bool HAL_GetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
int32_t* status);
void HAL_SetPWMSimDevice(HAL_DigitalHandle handle, HAL_SimDeviceHandle device);
/**
* Sets a PWM channel to the desired pulse width in microseconds.
@@ -115,44 +66,6 @@ void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
int32_t microsecondPulseTime,
int32_t* status);
/**
* Sets a PWM channel to the desired scaled value.
*
* The values range from -1 to 1 and the period is controlled by the PWM Period
* and MinHigh registers.
*
* @param[in] pwmPortHandle the PWM handle
* @param[in] speed the scaled PWM value to set
* @param[out] status Error status variable. 0 on success.
*/
void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed,
int32_t* status);
/**
* Sets a PWM channel to the desired position value.
*
* The values range from 0 to 1 and the period is controlled by the PWM Period
* and MinHigh registers.
*
* @param[in] pwmPortHandle the PWM handle
* @param[in] position the positional PWM value to set
* @param[out] status Error status variable. 0 on success.
*/
void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double position,
int32_t* status);
/**
* Sets a PWM channel to be disabled.
*
* The channel is disabled until the next time it is set. Note this is different
* from just setting a 0 speed, as this will actively stop all signaling on the
* channel.
*
* @param[in] pwmPortHandle the PWM handle.
* @param[out] status Error status variable. 0 on success.
*/
void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t* status);
/**
* Gets the current microsecond pulse time from a PWM channel.
*
@@ -164,70 +77,14 @@ int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
int32_t* status);
/**
* Gets a scaled value from a PWM channel.
*
* The values range from -1 to 1.
*
* @param[in] pwmPortHandle the PWM handle
* @param[out] status Error status variable. 0 on success.
* @return the current speed PWM value
*/
double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status);
/**
* Gets a position value from a PWM channel.
*
* The values range from 0 to 1.
*
* @param[in] pwmPortHandle the PWM handle
* @param[out] status Error status variable. 0 on success.
* @return the current positional PWM value
*/
double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status);
/**
* Forces a PWM signal to go to 0 temporarily.
* Sets the PWM output period.
*
* @param[in] pwmPortHandle the PWM handle.
* @param[in] period 0 for 5ms, 1 or 2 for 10ms, 3 for 20ms
* @param[out] status Error status variable. 0 on success.
*/
void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status);
/**
* Sets how how often the PWM signal is squelched, thus scaling the period.
*
* @param[in] pwmPortHandle the PWM handle.
* @param[in] squelchMask the 2-bit mask of outputs to squelch
* @param[out] status Error status variable. 0 on success.
*/
void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
int32_t* status);
/**
* Sets the PWM output to be a continuous 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.
*
* @param[out] status Error status variable. 0 on success.
* @return the loop time in clock ticks
*/
int32_t HAL_GetPWMLoopTiming(int32_t* status);
/**
* Gets the pwm starting cycle time.
*
* This time is relative to the FPGA time.
*
* @param[out] status Error status variable. 0 on success.
* @return the pwm cycle start time
*/
uint64_t HAL_GetPWMCycleStartTime(int32_t* status);
void HAL_SetPWMOutputPeriod(HAL_DigitalHandle pwmPortHandle, int32_t period,
int32_t* status);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -20,6 +20,8 @@ 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);
HAL_SimDeviceHandle HALSIM_GetPWMSimDevice(int32_t index);
int32_t HALSIM_RegisterPWMPulseMicrosecondCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
@@ -28,35 +30,13 @@ 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,
void* param, HAL_Bool initialNotify);
void HALSIM_CancelPWMSpeedCallback(int32_t index, int32_t uid);
double HALSIM_GetPWMSpeed(int32_t index);
void HALSIM_SetPWMSpeed(int32_t index, double speed);
int32_t HALSIM_RegisterPWMPositionCallback(int32_t index,
HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);
void HALSIM_CancelPWMPositionCallback(int32_t index, int32_t uid);
double HALSIM_GetPWMPosition(int32_t index);
void HALSIM_SetPWMPosition(int32_t index, double position);
int32_t HALSIM_RegisterPWMPeriodScaleCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void HALSIM_CancelPWMPeriodScaleCallback(int32_t index, int32_t uid);
int32_t HALSIM_GetPWMPeriodScale(int32_t index);
void HALSIM_SetPWMPeriodScale(int32_t index, int32_t periodScale);
int32_t HALSIM_RegisterPWMZeroLatchCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void HALSIM_CancelPWMZeroLatchCallback(int32_t index, int32_t uid);
HAL_Bool HALSIM_GetPWMZeroLatch(int32_t index);
void HALSIM_SetPWMZeroLatch(int32_t index, HAL_Bool zeroLatch);
int32_t HALSIM_RegisterPWMOutputPeriodCallback(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify);
void HALSIM_CancelPWMOutputPeriodCallback(int32_t index, int32_t uid);
int32_t HALSIM_GetPWMOutputPeriod(int32_t index);
void HALSIM_SetPWMOutputPeriod(int32_t index, int32_t periodScale);
void HALSIM_RegisterPWMAllCallbacks(int32_t index, HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify);

View File

@@ -42,13 +42,6 @@ constexpr int32_t kPwmDisabled = 0;
struct DigitalPort {
uint8_t channel;
bool configSet = false;
bool eliminateDeadband = false;
int32_t maxPwm = 0;
int32_t deadbandMaxPwm = 0;
int32_t centerPwm = 0;
int32_t deadbandMinPwm = 0;
int32_t minPwm = 0;
int32_t filterIndex = 0;
std::string previousAllocation;
};

View File

@@ -21,46 +21,6 @@ namespace hal::init {
void InitializePWM() {}
} // namespace hal::init
static inline int32_t GetMaxPositivePwm(DigitalPort* port) {
return port->maxPwm;
}
static inline int32_t GetMinPositivePwm(DigitalPort* port) {
if (port->eliminateDeadband) {
return port->deadbandMaxPwm;
} else {
return port->centerPwm + 1;
}
}
static inline int32_t GetCenterPwm(DigitalPort* port) {
return port->centerPwm;
}
static inline int32_t GetMaxNegativePwm(DigitalPort* port) {
if (port->eliminateDeadband) {
return port->deadbandMinPwm;
} else {
return port->centerPwm - 1;
}
}
static inline int32_t GetMinNegativePwm(DigitalPort* port) {
return port->minPwm;
}
static inline int32_t GetPositiveScaleFactor(DigitalPort* port) {
return GetMaxPositivePwm(port) - GetMinPositivePwm(port);
} ///< The scale for positive speeds.
static inline int32_t GetNegativeScaleFactor(DigitalPort* port) {
return GetMaxNegativePwm(port) - GetMinNegativePwm(port);
} ///< The scale for negative speeds.
static inline int32_t GetFullRangeScaleFactor(DigitalPort* port) {
return GetMaxPositivePwm(port) - GetMinNegativePwm(port);
} ///< The scale for positions.
extern "C" {
HAL_DigitalHandle HAL_InitializePWMPort(int32_t channel,
@@ -103,8 +63,8 @@ HAL_DigitalHandle HAL_InitializePWMPort(int32_t channel,
SimPWMData[origChannel].initialized = true;
// Defaults to allow an always valid config.
HAL_SetPWMConfigMicroseconds(handle, 2000, 1501, 1500, 1499, 1000, status);
// Disable output.
HAL_SetPWMPulseTimeMicroseconds(handle, 0, status);
port->previousAllocation = allocationLocation ? allocationLocation : "";
@@ -125,58 +85,12 @@ HAL_Bool HAL_CheckPWMChannel(int32_t channel) {
return channel < kNumPWMChannels && channel >= 0;
}
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);
void HAL_SetPWMSimDevice(HAL_DigitalHandle handle, HAL_SimDeviceHandle device) {
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
port->maxPwm = max;
port->deadbandMaxPwm = deadbandMax;
port->deadbandMinPwm = deadbandMin;
port->centerPwm = center;
port->minPwm = min;
port->configSet = true;
}
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;
return;
}
*maxPwm = port->maxPwm;
*deadbandMaxPwm = port->deadbandMaxPwm;
*deadbandMinPwm = port->deadbandMinPwm;
*centerPwm = port->centerPwm;
*minPwm = port->minPwm;
}
void HAL_SetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
HAL_Bool eliminateDeadband, int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
port->eliminateDeadband = eliminateDeadband;
}
HAL_Bool HAL_GetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
return port->eliminateDeadband;
SimPWMData[port->channel].simDevice = device;
}
void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
@@ -188,129 +102,6 @@ void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
}
SimPWMData[port->channel].pulseMicrosecond = value;
DigitalPort* dPort = port.get();
double speed = 0.0;
if (value == kPwmDisabled) {
speed = 0.0;
} else if (value > GetMaxPositivePwm(dPort)) {
speed = 1.0;
} else if (value < GetMinNegativePwm(dPort)) {
speed = -1.0;
} else if (value > GetMinPositivePwm(dPort)) {
speed = static_cast<double>(value - GetMinPositivePwm(dPort)) /
static_cast<double>(GetPositiveScaleFactor(dPort));
} else if (value < GetMaxNegativePwm(dPort)) {
speed = static_cast<double>(value - GetMaxNegativePwm(dPort)) /
static_cast<double>(GetNegativeScaleFactor(dPort));
} else {
speed = 0.0;
}
SimPWMData[port->channel].speed = speed;
double pos = 0.0;
if (value < GetMinNegativePwm(dPort)) {
pos = 0.0;
} else if (value > GetMaxPositivePwm(dPort)) {
pos = 1.0;
} else {
pos = static_cast<double>(value - GetMinNegativePwm(dPort)) /
static_cast<double>(GetFullRangeScaleFactor(dPort));
}
SimPWMData[port->channel].position = pos;
}
void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed,
int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return;
}
if (std::isfinite(speed)) {
speed = std::clamp(speed, -1.0, 1.0);
} else {
speed = 0.0;
}
DigitalPort* dPort = port.get();
// calculate the desired output pwm value by scaling the speed appropriately
int32_t rawValue;
if (speed == 0.0) {
rawValue = GetCenterPwm(dPort);
} else if (speed > 0.0) {
rawValue =
std::lround(speed * static_cast<double>(GetPositiveScaleFactor(dPort)) +
static_cast<double>(GetMinPositivePwm(dPort)));
} else {
rawValue =
std::lround(speed * static_cast<double>(GetNegativeScaleFactor(dPort)) +
static_cast<double>(GetMaxNegativePwm(dPort)));
}
if (!((rawValue >= GetMinNegativePwm(dPort)) &&
(rawValue <= GetMaxPositivePwm(dPort))) ||
rawValue == kPwmDisabled) {
*status = HAL_PWM_SCALE_ERROR;
return;
}
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, rawValue, status);
}
void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double pos,
int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return;
}
if (pos < 0.0) {
pos = 0.0;
} else if (pos > 1.0) {
pos = 1.0;
}
DigitalPort* dPort = port.get();
// note, need to perform the multiplication below as floating point before
// converting to int
int32_t rawValue = static_cast<int32_t>(
(pos * static_cast<double>(GetFullRangeScaleFactor(dPort))) +
GetMinNegativePwm(dPort));
if (rawValue == kPwmDisabled) {
*status = HAL_PWM_SCALE_ERROR;
return;
}
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, rawValue, status);
}
void HAL_SetPWMDisabled(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 = 0;
SimPWMData[port->channel].position = 0;
SimPWMData[port->channel].speed = 0;
}
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
@@ -324,88 +115,15 @@ int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
return SimPWMData[port->channel].pulseMicrosecond;
}
double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return 0;
}
double speed = SimPWMData[port->channel].speed;
if (speed > 1) {
speed = 1;
}
if (speed < -1) {
speed = -1;
}
return speed;
}
double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return 0;
}
double position = SimPWMData[port->channel].position;
if (position > 1) {
position = 1;
}
if (position < 0) {
position = 0;
}
return position;
}
void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
void HAL_SetPWMOutputPeriod(HAL_DigitalHandle pwmPortHandle, int32_t period,
int32_t* status) {
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].zeroLatch = true;
SimPWMData[port->channel].zeroLatch = false;
SimPWMData[port->channel].outputPeriod = period;
}
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);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
SimPWMData[port->channel].periodScale = squelchMask;
}
int32_t HAL_GetPWMLoopTiming(int32_t* status) {
return kExpectedLoopTiming;
}
uint64_t HAL_GetPWMCycleStartTime(int32_t* status) {
return 0;
}
} // extern "C"

View File

@@ -17,11 +17,9 @@ void InitializePWMData() {
PWMData* hal::SimPWMData;
void PWMData::ResetData() {
initialized.Reset(false);
simDevice = 0;
pulseMicrosecond.Reset(0);
speed.Reset(0);
position.Reset(0);
periodScale.Reset(0);
zeroLatch.Reset(false);
outputPeriod.Reset(0);
}
extern "C" {
@@ -29,16 +27,17 @@ void HALSIM_ResetPWMData(int32_t index) {
SimPWMData[index].ResetData();
}
HAL_SimDeviceHandle HALSIM_GetPWMSimDevice(int32_t index) {
return SimPWMData[index].simDevice;
}
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PWM##CAPINAME, SimPWMData, \
LOWERNAME)
DEFINE_CAPI(HAL_Bool, Initialized, initialized)
DEFINE_CAPI(int32_t, PulseMicrosecond, pulseMicrosecond)
DEFINE_CAPI(double, Speed, speed)
DEFINE_CAPI(double, Position, position)
DEFINE_CAPI(int32_t, PeriodScale, periodScale)
DEFINE_CAPI(HAL_Bool, ZeroLatch, zeroLatch)
DEFINE_CAPI(int32_t, OutputPeriod, outputPeriod)
#define REGISTER(NAME) \
SimPWMData[index].NAME.RegisterCallback(callback, param, initialNotify)
@@ -47,9 +46,6 @@ void HALSIM_RegisterPWMAllCallbacks(int32_t index, HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {
REGISTER(initialized);
REGISTER(pulseMicrosecond);
REGISTER(speed);
REGISTER(position);
REGISTER(periodScale);
REGISTER(zeroLatch);
REGISTER(outputPeriod);
}
} // extern "C"

View File

@@ -11,20 +11,15 @@ namespace hal {
class PWMData {
HAL_SIMDATAVALUE_DEFINE_NAME(Initialized)
HAL_SIMDATAVALUE_DEFINE_NAME(PulseMicrosecond)
HAL_SIMDATAVALUE_DEFINE_NAME(Speed)
HAL_SIMDATAVALUE_DEFINE_NAME(Position)
HAL_SIMDATAVALUE_DEFINE_NAME(PeriodScale)
HAL_SIMDATAVALUE_DEFINE_NAME(ZeroLatch)
HAL_SIMDATAVALUE_DEFINE_NAME(OutputPeriod)
public:
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetInitializedName> initialized{
false};
std::atomic<HAL_SimDeviceHandle> simDevice;
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};
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetZeroLatchName> zeroLatch{false};
SimDataValue<int32_t, HAL_MakeInt, GetOutputPeriodName> outputPeriod{0};
virtual void ResetData();
};

View File

@@ -22,46 +22,6 @@
using namespace hal;
static inline int32_t GetMaxPositivePwm(SmartIo* port) {
return port->maxPwm;
}
static inline int32_t GetMinPositivePwm(SmartIo* port) {
if (port->eliminateDeadband) {
return port->deadbandMaxPwm;
} else {
return port->centerPwm + 1;
}
}
static inline int32_t GetCenterPwm(SmartIo* port) {
return port->centerPwm;
}
static inline int32_t GetMaxNegativePwm(SmartIo* port) {
if (port->eliminateDeadband) {
return port->deadbandMinPwm;
} else {
return port->centerPwm - 1;
}
}
static inline int32_t GetMinNegativePwm(SmartIo* port) {
return port->minPwm;
}
static inline int32_t GetPositiveScaleFactor(SmartIo* port) {
return GetMaxPositivePwm(port) - GetMinPositivePwm(port);
} ///< The scale for positive speeds.
static inline int32_t GetNegativeScaleFactor(SmartIo* port) {
return GetMaxNegativePwm(port) - GetMinNegativePwm(port);
} ///< The scale for negative speeds.
static inline int32_t GetFullRangeScaleFactor(SmartIo* port) {
return GetMaxPositivePwm(port) - GetMinNegativePwm(port);
} ///< The scale for positions.
namespace hal::init {
void InitializePWM() {}
} // namespace hal::init
@@ -104,8 +64,8 @@ HAL_DigitalHandle HAL_InitializePWMPort(int32_t channel,
return HAL_kInvalidHandle;
}
// Defaults to allow an always valid config.
HAL_SetPWMConfigMicroseconds(handle, 2000, 1501, 1500, 1499, 1000, status);
// Disable the PWM output.
HAL_SetPWMPulseTimeMicroseconds(handle, 0, status);
if (*status != 0) {
smartIoHandles->Free(handle, HAL_HandleEnum::PWM);
return HAL_kInvalidHandle;
@@ -137,63 +97,13 @@ void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle) {
}
}
void HAL_SetPWMSimDevice(HAL_DigitalHandle handle, HAL_SimDeviceHandle device) {
}
HAL_Bool HAL_CheckPWMChannel(int32_t channel) {
return channel < kNumSmartIo && channel >= 0;
}
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 = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
port->maxPwm = max;
port->deadbandMaxPwm = deadbandMax;
port->deadbandMinPwm = deadbandMin;
port->centerPwm = center;
port->minPwm = min;
port->configSet = true;
}
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 = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
*maxPwm = port->maxPwm;
*deadbandMaxPwm = port->deadbandMaxPwm;
*deadbandMinPwm = port->deadbandMinPwm;
*centerPwm = port->centerPwm;
*minPwm = port->minPwm;
}
void HAL_SetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
HAL_Bool eliminateDeadband, int32_t* status) {
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
port->eliminateDeadband = eliminateDeadband;
}
HAL_Bool HAL_GetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle,
int32_t* status) {
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
}
return port->eliminateDeadband;
}
void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
int32_t microsecondPulseTime,
int32_t* status) {
@@ -216,87 +126,6 @@ void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
*status = port->SetPwmMicroseconds(microsecondPulseTime);
}
void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed,
int32_t* status) {
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return;
}
SmartIo* dPort = port.get();
if (std::isfinite(speed)) {
speed = std::clamp(speed, -1.0, 1.0);
} else {
speed = 0.0;
}
// calculate the desired output pwm value by scaling the speed appropriately
int32_t rawValue;
if (speed == 0.0) {
rawValue = GetCenterPwm(dPort);
} else if (speed > 0.0) {
rawValue =
std::lround(speed * static_cast<double>(GetPositiveScaleFactor(dPort)) +
static_cast<double>(GetMinPositivePwm(dPort)));
} else {
rawValue =
std::lround(speed * static_cast<double>(GetNegativeScaleFactor(dPort)) +
static_cast<double>(GetMaxNegativePwm(dPort)));
}
if (!((rawValue >= GetMinNegativePwm(dPort)) &&
(rawValue <= GetMaxPositivePwm(dPort))) ||
rawValue == kPwmDisabled) {
*status = HAL_PWM_SCALE_ERROR;
return;
}
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, rawValue, status);
}
void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double pos,
int32_t* status) {
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return;
}
SmartIo* dPort = port.get();
if (pos < 0.0) {
pos = 0.0;
} else if (pos > 1.0) {
pos = 1.0;
}
// note, need to perform the multiplication below as floating point before
// converting to int
int32_t rawValue = static_cast<int32_t>(
(pos * static_cast<double>(GetFullRangeScaleFactor(dPort))) +
GetMinNegativePwm(dPort));
if (rawValue == kPwmDisabled) {
*status = HAL_PWM_SCALE_ERROR;
return;
}
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, rawValue, status);
}
void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, kPwmDisabled, status);
}
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
int32_t* status) {
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
@@ -310,79 +139,15 @@ int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
return microseconds;
}
double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return 0;
}
int32_t value = HAL_GetPWMPulseTimeMicroseconds(pwmPortHandle, status);
if (*status != 0) {
return 0;
}
SmartIo* dPort = port.get();
if (value == kPwmDisabled) {
return 0.0;
} else if (value > GetMaxPositivePwm(dPort)) {
return 1.0;
} else if (value < GetMinNegativePwm(dPort)) {
return -1.0;
} else if (value > GetMinPositivePwm(dPort)) {
return static_cast<double>(value - GetMinPositivePwm(dPort)) /
static_cast<double>(GetPositiveScaleFactor(dPort));
} else if (value < GetMaxNegativePwm(dPort)) {
return static_cast<double>(value - GetMaxNegativePwm(dPort)) /
static_cast<double>(GetNegativeScaleFactor(dPort));
} else {
return 0.0;
}
}
double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
if (!port->configSet) {
*status = INCOMPATIBLE_STATE;
return 0;
}
int32_t value = HAL_GetPWMPulseTimeMicroseconds(pwmPortHandle, status);
if (*status != 0) {
return 0;
}
SmartIo* dPort = port.get();
if (value < GetMinNegativePwm(dPort)) {
return 0.0;
} else if (value > GetMaxPositivePwm(dPort)) {
return 1.0;
} else {
return static_cast<double>(value - GetMinNegativePwm(dPort)) /
static_cast<double>(GetFullRangeScaleFactor(dPort));
}
}
void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, 0, status);
}
void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
int32_t* status) {
void HAL_SetPWMOutputPeriod(HAL_DigitalHandle pwmPortHandle, int32_t period,
int32_t* status) {
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
switch (squelchMask) {
switch (period) {
case 0:
*status = port->SetPwmOutputPeriod(hal::PwmOutputPeriod::k5ms);
break;
@@ -399,31 +164,4 @@ void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
}
}
void HAL_SetPWMAlwaysHighMode(HAL_DigitalHandle pwmPortHandle,
int32_t* status) {
// Always high is going to have to use a 2ms period
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
*status = port->SetPwmOutputPeriod(hal::PwmOutputPeriod::k2ms);
if (*status != 0) {
return;
}
HAL_SetPWMPulseTimeMicroseconds(pwmPortHandle, kPwmAlwaysHigh, status);
}
int32_t HAL_GetPWMLoopTiming(int32_t* status) {
// TODO(thad) not currently supported
return 0;
}
uint64_t HAL_GetPWMCycleStartTime(int32_t* status) {
// TODO(thad) not currently supported
return 0;
}
} // extern "C"

View File

@@ -36,13 +36,6 @@ enum class PwmOutputPeriod {
struct SmartIo {
uint8_t channel;
bool configSet = false;
bool eliminateDeadband = false;
int32_t maxPwm = 0;
int32_t deadbandMaxPwm = 0;
int32_t centerPwm = 0;
int32_t deadbandMinPwm = 0;
int32_t minPwm = 0;
std::string previousAllocation;
SmartIoMode currentMode{SmartIoMode::DigitalInput};
nt::IntegerPublisher modePublisher;

View File

@@ -9,15 +9,16 @@
extern "C" {
void HALSIM_ResetPWMData(int32_t index) {}
HAL_SimDeviceHandle HALSIM_GetPWMSimDevice(int32_t index) {
return 0;
}
#define DEFINE_CAPI(TYPE, CAPINAME, RETURN) \
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, PWM##CAPINAME, RETURN)
DEFINE_CAPI(HAL_Bool, Initialized, false)
DEFINE_CAPI(int32_t, PulseMicrosecond, 0)
DEFINE_CAPI(double, Speed, 0)
DEFINE_CAPI(double, Position, 0)
DEFINE_CAPI(int32_t, PeriodScale, 0)
DEFINE_CAPI(HAL_Bool, ZeroLatch, false)
DEFINE_CAPI(int32_t, OutputPeriod, 0)
void HALSIM_RegisterPWMAllCallbacks(int32_t index, HAL_NotifyCallback callback,
void* param, HAL_Bool initialNotify) {}