[wpilib] PWMSpeedController: Use PWM by composition (#3248)

This cleans up the user experience by removing lower-level functions from the
interface.

Also remove MotorSafety from "raw" PWM.
This commit is contained in:
Peter Johnson
2021-03-21 11:12:49 -07:00
committed by GitHub
parent 160fb740f4
commit 9550777b9d
32 changed files with 242 additions and 252 deletions

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Digilent DMC 60 Speed Controller.
@@ -33,14 +32,13 @@ public class DMC60 extends PWMSpeedController {
* the MXP port
*/
public DMC60(final int channel) {
super(channel);
super("DMC60", channel);
setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_DigilentDMC60, getChannel() + 1);
SendableRegistry.setName(this, "DMC60", getChannel());
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Texas Instruments / Vex Robotics Jaguar Speed Controller as a PWM device.
@@ -32,14 +31,13 @@ public class Jaguar extends PWMSpeedController {
* the MXP port
*/
public Jaguar(final int channel) {
super(channel);
super("Jaguar", channel);
setBounds(2.31, 1.55, 1.507, 1.454, 0.697);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.31, 1.55, 1.507, 1.454, 0.697);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_Jaguar, getChannel() + 1);
SendableRegistry.setName(this, "Jaguar", getChannel());
}
}

View File

@@ -23,7 +23,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
* 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)
*/
public class PWM extends MotorSafety implements Sendable, AutoCloseable {
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 */
@@ -42,9 +42,24 @@ public class PWM extends MotorSafety implements Sendable, AutoCloseable {
/**
* Allocate a PWM given a channel.
*
* <p>Checks channel value range and allocates the appropriate channel. The allocation is only
* done to help users ensure that they don't double assign channels.
*
* <p>By default, adds itself to SendableRegistry and LiveWindow.
*
* @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port
*/
public PWM(final int channel) {
this(channel, true);
}
/**
* Allocate a PWM given a channel.
*
* @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port
* @param registerSendable If true, adds this instance to SendableRegistry and LiveWindow
*/
public PWM(final int channel, final boolean registerSendable) {
SensorUtil.checkPWMChannel(channel);
m_channel = channel;
@@ -55,9 +70,9 @@ public class PWM extends MotorSafety implements Sendable, AutoCloseable {
PWMJNI.setPWMEliminateDeadband(m_handle, false);
HAL.report(tResourceType.kResourceType_PWM, channel + 1);
SendableRegistry.addLW(this, "PWM", channel);
setSafetyEnabled(false);
if (registerSendable) {
SendableRegistry.addLW(this, "PWM", channel);
}
}
/** Free the resource associated with the PWM channel and set the value to 0. */
@@ -72,16 +87,6 @@ public class PWM extends MotorSafety implements Sendable, AutoCloseable {
m_handle = 0;
}
@Override
public void stopMotor() {
setDisabled();
}
@Override
public String getDescription() {
return "PWM " + getChannel();
}
/**
* Optionally eliminate the deadband from a speed controller.
*

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* REV Robotics SPARK MAX Speed Controller with PWM control.
@@ -28,14 +27,13 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
public class PWMSparkMax extends PWMSpeedController {
/** Common initialization code called by all constructors. */
public PWMSparkMax(final int channel) {
super(channel);
super("PWMSparkMax", channel);
setBounds(2.003, 1.55, 1.50, 1.46, 0.999);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.003, 1.55, 1.50, 1.46, 0.999);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_RevSparkMaxPWM, getChannel() + 1);
SendableRegistry.setName(this, "PWMSparkMax", getChannel());
}
}

View File

@@ -5,24 +5,31 @@
package edu.wpi.first.wpilibj;
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/** Common base class for all PWM Speed Controllers. */
public abstract class PWMSpeedController extends PWM implements SpeedController {
public abstract class PWMSpeedController extends MotorSafety
implements SpeedController, Sendable, AutoCloseable {
private boolean m_isInverted;
protected PWM m_pwm;
/**
* Constructor.
*
* @param name Name to use for SendableRegistry
* @param channel The PWM channel that the controller is attached to. 0-9 are on-board, 10-19 are
* on the MXP port
*/
protected PWMSpeedController(int channel) {
super(channel);
protected PWMSpeedController(final String name, final int channel) {
m_pwm = new PWM(channel, false);
SendableRegistry.addLW(this, name, channel);
}
/** Free the resource associated with the PWM channel and set the value to 0. */
@Override
public String getDescription() {
return "PWM " + getChannel();
public void close() {
SendableRegistry.remove(this);
m_pwm.close();
}
/**
@@ -35,7 +42,7 @@ public abstract class PWMSpeedController extends PWM implements SpeedController
*/
@Override
public void set(double speed) {
setSpeed(m_isInverted ? -speed : speed);
m_pwm.setSpeed(m_isInverted ? -speed : speed);
feed();
}
@@ -48,7 +55,7 @@ public abstract class PWMSpeedController extends PWM implements SpeedController
*/
@Override
public double get() {
return getSpeed() * (m_isInverted ? -1.0 : 1.0);
return m_pwm.getSpeed() * (m_isInverted ? -1.0 : 1.0);
}
@Override
@@ -63,7 +70,26 @@ public abstract class PWMSpeedController extends PWM implements SpeedController
@Override
public void disable() {
setDisabled();
m_pwm.setDisabled();
}
@Override
public void stopMotor() {
disable();
}
@Override
public String getDescription() {
return "PWM " + getChannel();
}
/**
* Gets the PWM channel number.
*
* @return The channel number.
*/
public int getChannel() {
return m_pwm.getChannel();
}
/**
@@ -80,7 +106,7 @@ public abstract class PWMSpeedController extends PWM implements SpeedController
public void initSendable(SendableBuilder builder) {
builder.setSmartDashboardType("Speed Controller");
builder.setActuator(true);
builder.setSafeState(this::setDisabled);
builder.addDoubleProperty("Value", this::getSpeed, this::setSpeed);
builder.setSafeState(this::disable);
builder.addDoubleProperty("Value", this::get, this::set);
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Cross the Road Electronics (CTRE) Talon FX Speed Controller with PWM control.
@@ -33,14 +32,13 @@ public class PWMTalonFX extends PWMSpeedController {
* the MXP port
*/
public PWMTalonFX(final int channel) {
super(channel);
super("PWMTalonFX", channel);
setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_TalonFX, getChannel() + 1);
SendableRegistry.setName(this, "PWMTalonFX", getChannel());
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Cross the Road Electronics (CTRE) Talon SRX Speed Controller with PWM control.
@@ -33,14 +32,13 @@ public class PWMTalonSRX extends PWMSpeedController {
* on the MXP port
*/
public PWMTalonSRX(final int channel) {
super(channel);
super("PWMTalonSRX", channel);
setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_PWMTalonSRX, getChannel() + 1);
SendableRegistry.setName(this, "PWMTalonSRX", getChannel());
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Playing with Fusion Venom Smart Motor with PWM control.
@@ -32,14 +31,13 @@ public class PWMVenom extends PWMSpeedController {
* the MXP port
*/
public PWMVenom(final int channel) {
super(channel);
super("PWMVenom", channel);
setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_FusionVenom, getChannel() + 1);
SendableRegistry.setName(this, "PWMVenom", getChannel());
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Cross the Road Electronics (CTRE) Victor SPX Speed Controller with PWM control.
@@ -33,14 +32,13 @@ public class PWMVictorSPX extends PWMSpeedController {
* are on the MXP port
*/
public PWMVictorSPX(final int channel) {
super(channel);
super("PWMVictorSPX", channel);
setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_PWMVictorSPX, getChannel() + 1);
SendableRegistry.setName(this, "PWMVictorSPX", getChannel());
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Mindsensors SD540 Speed Controller.
@@ -26,17 +25,6 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
* </ul>
*/
public class SD540 extends PWMSpeedController {
/** Common initialization code called by all constructors. */
protected void initSD540() {
setBounds(2.05, 1.55, 1.50, 1.44, 0.94);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
HAL.report(tResourceType.kResourceType_MindsensorsSD540, getChannel() + 1);
SendableRegistry.setName(this, "SD540", getChannel());
}
/**
* Constructor.
*
@@ -44,7 +32,13 @@ public class SD540 extends PWMSpeedController {
* the MXP port
*/
public SD540(final int channel) {
super(channel);
initSD540();
super("SD540", channel);
m_pwm.setBounds(2.05, 1.55, 1.50, 1.44, 0.94);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_MindsensorsSD540, getChannel() + 1);
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* REV Robotics SPARK Speed Controller.
@@ -26,17 +25,6 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
* </ul>
*/
public class Spark extends PWMSpeedController {
/** Common initialization code called by all constructors. */
protected void initSpark() {
setBounds(2.003, 1.55, 1.50, 1.46, 0.999);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
HAL.report(tResourceType.kResourceType_RevSPARK, getChannel() + 1);
SendableRegistry.setName(this, "Spark", getChannel());
}
/**
* Constructor.
*
@@ -44,7 +32,13 @@ public class Spark extends PWMSpeedController {
* the MXP port
*/
public Spark(final int channel) {
super(channel);
initSpark();
super("Spark", channel);
m_pwm.setBounds(2.003, 1.55, 1.50, 1.46, 0.999);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_RevSPARK, getChannel() + 1);
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Cross the Road Electronics (CTRE) Talon and Talon SR Speed Controller.
@@ -32,14 +31,13 @@ public class Talon extends PWMSpeedController {
* the MXP port
*/
public Talon(final int channel) {
super(channel);
super("Talon", channel);
setBounds(2.037, 1.539, 1.513, 1.487, 0.989);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.037, 1.539, 1.513, 1.487, 0.989);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_Talon, getChannel() + 1);
SendableRegistry.setName(this, "Talon", getChannel());
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* VEX Robotics Victor 888 Speed Controller The Vex Robotics Victor 884 Speed Controller can also be
@@ -35,14 +34,13 @@ public class Victor extends PWMSpeedController {
* the MXP port
*/
public Victor(final int channel) {
super(channel);
super("Victor", channel);
setBounds(2.027, 1.525, 1.507, 1.49, 1.026);
setPeriodMultiplier(PeriodMultiplier.k2X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.027, 1.525, 1.507, 1.49, 1.026);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k2X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_Victor, getChannel() + 1);
SendableRegistry.setName(this, "Victor", getChannel());
}
}

View File

@@ -6,7 +6,6 @@ package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* VEX Robotics Victor SP Speed Controller.
@@ -33,14 +32,13 @@ public class VictorSP extends PWMSpeedController {
* the MXP port
*/
public VictorSP(final int channel) {
super(channel);
super("VictorSP", channel);
setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
setPeriodMultiplier(PeriodMultiplier.k1X);
setSpeed(0.0);
setZeroLatch();
m_pwm.setBounds(2.004, 1.52, 1.50, 1.48, 0.997);
m_pwm.setPeriodMultiplier(PWM.PeriodMultiplier.k1X);
m_pwm.setSpeed(0.0);
m_pwm.setZeroLatch();
HAL.report(tResourceType.kResourceType_VictorSP, getChannel() + 1);
SendableRegistry.setName(this, "VictorSP", getChannel());
}
}