[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

@@ -16,22 +16,17 @@ import edu.wpi.first.util.sendable.SendableRegistry;
* Class implements the PWM generation in the FPGA.
*
* <p>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.
*
* <p>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 .5ms) - 0 = disabled (i.e. PWM output is held low)
* 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.
*/
public class PWM implements Sendable, AutoCloseable {
/** Represents the amount to multiply the minimum servo-pulse pwm period by. */
public enum PeriodMultiplier {
/** Period Multiplier: don't skip pulses. PWM pulses occur every 5.005 ms */
/** Period Multiplier: don't skip pulses. PWM pulses occur every 5.05 ms */
k1X,
/** Period Multiplier: skip every other pulse. PWM pulses occur every 10.010 ms */
/** Period Multiplier: skip every other pulse. PWM pulses occur every 10.10 ms */
k2X,
/** Period Multiplier: skip three out of four pulses. PWM pulses occur every 20.020 ms */
/** Period Multiplier: skip three out of four pulses. PWM pulses occur every 20.20 ms */
k4X
}
@@ -103,15 +98,15 @@ public class PWM implements Sendable, AutoCloseable {
* type of controller. 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
*/
public void setBounds(
double max, double deadbandMax, double center, double deadbandMin, double min) {
PWMJNI.setPWMConfig(m_handle, max, deadbandMax, center, deadbandMin, min);
public void setBoundsMicroseconds(
int max, int deadbandMax, int center, int deadbandMin, int min) {
PWMJNI.setPWMConfigMicroseconds(m_handle, max, deadbandMax, center, deadbandMin, min);
}
/**
@@ -121,8 +116,8 @@ public class PWM implements Sendable, AutoCloseable {
*
* @return The bounds on the PWM pulse widths.
*/
public PWMConfigDataResult getRawBounds() {
return PWMJNI.getPWMConfigRaw(m_handle);
public PWMConfigDataResult getBoundsMicroseconds() {
return PWMJNI.getPWMConfigMicroseconds(m_handle);
}
/**
@@ -140,8 +135,7 @@ public class PWM implements Sendable, AutoCloseable {
* <p>This is intended to be used by servos.
*
* @param pos The position to set the servo between 0.0 and 1.0.
* @pre SetMaxPositivePwm() called.
* @pre SetMinNegativePwm() called.
* @pre setBoundsMicroseconds() called.
*/
public void setPosition(double pos) {
PWMJNI.setPWMPosition(m_handle, pos);
@@ -153,8 +147,7 @@ public class PWM implements Sendable, AutoCloseable {
* <p>This is intended to be used by servos.
*
* @return The position the servo is set to between 0.0 and 1.0.
* @pre SetMaxPositivePwm() called.
* @pre SetMinNegativePwm() called.
* @pre setBoundsMicroseconds() called.
*/
public double getPosition() {
return PWMJNI.getPWMPosition(m_handle);
@@ -166,11 +159,7 @@ public class PWM implements Sendable, AutoCloseable {
* <p>This is intended to be used by motor controllers.
*
* @param speed The speed to set the motor controller between -1.0 and 1.0.
* @pre SetMaxPositivePwm() called.
* @pre SetMinPositivePwm() called.
* @pre SetCenterPwm() called.
* @pre SetMaxNegativePwm() called.
* @pre SetMinNegativePwm() called.
* @pre setBoundsMicroseconds() called.
*/
public void setSpeed(double speed) {
PWMJNI.setPWMSpeed(m_handle, speed);
@@ -182,10 +171,7 @@ public class PWM implements Sendable, AutoCloseable {
* <p>This is intended to be used by motor controllers.
*
* @return The most recently set speed between -1.0 and 1.0.
* @pre SetMaxPositivePwm() called.
* @pre SetMinPositivePwm() called.
* @pre SetMaxNegativePwm() called.
* @pre SetMinNegativePwm() called.
* @pre setBoundsMicroseconds() called.
*/
public double getSpeed() {
return PWMJNI.getPWMSpeed(m_handle);
@@ -194,12 +180,12 @@ public class PWM implements Sendable, AutoCloseable {
/**
* Set the PWM value directly to the hardware.
*
* <p>Write a raw value to a PWM channel.
* <p>Write a microsecond pulse value to a PWM channel.
*
* @param value Raw PWM value. Range 0 - 255.
* @param microsecondPulseTime Microsecond pulse PWM value. Range 0 - 4096.
*/
public void setRaw(int value) {
PWMJNI.setPWMRaw(m_handle, (short) value);
public void setPulseTimeMicroseconds(int microsecondPulseTime) {
PWMJNI.setPulseTimeMicroseconds(m_handle, microsecondPulseTime);
}
/**
@@ -207,10 +193,10 @@ public class PWM implements Sendable, AutoCloseable {
*
* <p>Read a raw value from a PWM channel.
*
* @return Raw PWM control value. Range: 0 - 255.
* @return Microsecond pulse PWM control value. Range: 0 - 4096.
*/
public int getRaw() {
return PWMJNI.getPWMRaw(m_handle);
public int getPulseTimeMicroseconds() {
return PWMJNI.getPulseTimeMicroseconds(m_handle);
}
/** Temporarily disables the PWM output. The next set call will re-enable the output. */
@@ -246,6 +232,11 @@ public class PWM implements Sendable, AutoCloseable {
PWMJNI.latchPWMZero(m_handle);
}
/** Sets the PWM output to be a continous high signal while enabled. */
public void setAlwaysHighMode() {
PWMJNI.setAlwaysHighMode(m_handle);
}
/**
* Get the underlying handle.
*
@@ -260,6 +251,7 @@ public class PWM implements Sendable, AutoCloseable {
builder.setSmartDashboardType("PWM");
builder.setActuator(true);
builder.setSafeState(this::setDisabled);
builder.addDoubleProperty("Value", this::getRaw, value -> setRaw((int) value));
builder.addDoubleProperty(
"Value", this::getPulseTimeMicroseconds, value -> setPulseTimeMicroseconds((int) value));
}
}

View File

@@ -19,8 +19,8 @@ public class Servo extends PWM {
private static final double kMaxServoAngle = 180.0;
private static final double kMinServoAngle = 0.0;
protected static final double kDefaultMaxServoPWM = 2.4;
protected static final double kDefaultMinServoPWM = 0.6;
protected static final int kDefaultMaxServoPWM = 2400;
protected static final int kDefaultMinServoPWM = 600;
/**
* Constructor.<br>
@@ -33,7 +33,7 @@ public class Servo extends PWM {
*/
public Servo(final int channel) {
super(channel);
setBounds(kDefaultMaxServoPWM, 0, 0, 0, kDefaultMinServoPWM);
setBoundsMicroseconds(kDefaultMaxServoPWM, 0, 0, 0, kDefaultMinServoPWM);
setPeriodMultiplier(PeriodMultiplier.k4X);
HAL.report(tResourceType.kResourceType_Servo, getChannel() + 1);

View File

@@ -35,7 +35,7 @@ public class DMC60 extends PWMMotorController {
public DMC60(final int channel) {
super("DMC60", channel);
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setBoundsMicroseconds(2004, 1520, 1500, 1480, 997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -34,7 +34,7 @@ public class Jaguar extends PWMMotorController {
public Jaguar(final int channel) {
super("Jaguar", channel);
m_pwm.setBounds(2.31, 1.55, 1.507, 1.454, 0.697);
m_pwm.setBoundsMicroseconds(2310, 1550, 1507, 1454, 697);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -67,7 +67,7 @@ public class NidecBrushless extends MotorSafety
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

@@ -34,7 +34,7 @@ public class PWMSparkMax extends PWMMotorController {
public PWMSparkMax(final int channel) {
super("PWMSparkMax", channel);
m_pwm.setBounds(2.003, 1.55, 1.50, 1.46, 0.999);
m_pwm.setBoundsMicroseconds(2003, 1550, 1500, 1460, 999);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -35,7 +35,7 @@ public class PWMTalonFX extends PWMMotorController {
public PWMTalonFX(final int channel) {
super("PWMTalonFX", channel);
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setBoundsMicroseconds(2004, 1520, 1500, 1480, 997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -35,7 +35,7 @@ public class PWMTalonSRX extends PWMMotorController {
public PWMTalonSRX(final int channel) {
super("PWMTalonSRX", channel);
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setBoundsMicroseconds(2004, 1520, 1500, 1480, 997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -34,7 +34,7 @@ public class PWMVenom extends PWMMotorController {
public PWMVenom(final int channel) {
super("PWMVenom", channel);
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setBoundsMicroseconds(2004, 1520, 1500, 1480, 997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -35,7 +35,7 @@ public class PWMVictorSPX extends PWMMotorController {
public PWMVictorSPX(final int channel) {
super("PWMVictorSPX", channel);
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setBoundsMicroseconds(2004, 1520, 1500, 1480, 997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -35,7 +35,7 @@ public class SD540 extends PWMMotorController {
public SD540(final int channel) {
super("SD540", channel);
m_pwm.setBounds(2.05, 1.55, 1.50, 1.44, 0.94);
m_pwm.setBoundsMicroseconds(2050, 1550, 1500, 1440, 940);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -35,7 +35,7 @@ public class Spark extends PWMMotorController {
public Spark(final int channel) {
super("Spark", channel);
m_pwm.setBounds(2.003, 1.55, 1.50, 1.46, 0.999);
m_pwm.setBoundsMicroseconds(2003, 1550, 1500, 1460, 999);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -34,7 +34,7 @@ public class Talon extends PWMMotorController {
public Talon(final int channel) {
super("Talon", channel);
m_pwm.setBounds(2.037, 1.539, 1.513, 1.487, 0.989);
m_pwm.setBoundsMicroseconds(2037, 1539, 1513, 1487, 989);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -37,7 +37,7 @@ public class Victor extends PWMMotorController {
public Victor(final int channel) {
super("Victor", channel);
m_pwm.setBounds(2.027, 1.525, 1.507, 1.49, 1.026);
m_pwm.setBoundsMicroseconds(2027, 1525, 1507, 1490, 1026);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k2X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -35,7 +35,7 @@ public class VictorSP extends PWMMotorController {
public VictorSP(final int channel) {
super("VictorSP", channel);
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setBoundsMicroseconds(2004, 1520, 1500, 1480, 997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();

View File

@@ -79,27 +79,28 @@ public class PWMSim {
* @return the {@link CallbackStore} object associated with this callback. Save a reference to
* this object so GC doesn't cancel the callback.
*/
public CallbackStore registerRawValueCallback(NotifyCallback callback, boolean initialNotify) {
int uid = PWMDataJNI.registerRawValueCallback(m_index, callback, initialNotify);
return new CallbackStore(m_index, uid, PWMDataJNI::cancelRawValueCallback);
public CallbackStore registerPulseMicrosecondCallback(
NotifyCallback callback, boolean initialNotify) {
int uid = PWMDataJNI.registerPulseMicrosecondCallback(m_index, callback, initialNotify);
return new CallbackStore(m_index, uid, PWMDataJNI::cancelPulseMicrosecondCallback);
}
/**
* Get the PWM raw value.
* Get the PWM pulse microsecond value.
*
* @return the PWM raw value
* @return the PWM pulse microsecond value
*/
public int getRawValue() {
return PWMDataJNI.getRawValue(m_index);
public int getPulseMicrosecond() {
return PWMDataJNI.getPulseMicrosecond(m_index);
}
/**
* Set the PWM raw value.
* Set the PWM pulse microsecond value.
*
* @param rawValue the PWM raw value
* @param microsecondPulseTime the PWM pulse microsecond value
*/
public void setRawValue(int rawValue) {
PWMDataJNI.setRawValue(m_index, rawValue);
public void setPulseMicrosecond(int microsecondPulseTime) {
PWMDataJNI.setPulseMicrosecond(m_index, microsecondPulseTime);
}
/**