Remove MotorSafetyHelper, create MotorSafety base class instead (#562)

Most of the MotorSafety implementation was moved into the MotorSafety base
class. SafePWM's inheritance of MotorSafety was moved into PWM to
eliminate Java needing a helper class.

In Java, a helper class for Sendable (SendableImpl) was added due to
lack of multiple inheritance.
This commit is contained in:
Tyler Veness
2018-11-22 21:15:26 -08:00
committed by Peter Johnson
parent df347e3d80
commit acb786a791
39 changed files with 710 additions and 1023 deletions

View File

@@ -1127,7 +1127,7 @@ public class DriverStation {
safetyCounter++;
if (safetyCounter >= 4) {
MotorSafetyHelper.checkMotors();
MotorSafety.checkMotors();
safetyCounter = 0;
}
if (m_userInDisabled) {

View File

@@ -7,23 +7,144 @@
package edu.wpi.first.wpilibj;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Shuts off motors when their outputs aren't updated often enough.
* This base class runs a watchdog timer and calls the subclass's StopMotor()
* function if the timeout expires.
*
* <p>The subclass should call feed() whenever the motor value is updated.
*/
public interface MotorSafety {
double DEFAULT_SAFETY_EXPIRATION = 0.1;
public abstract class MotorSafety {
private static final double kDefaultSafetyExpiration = 0.1;
void setExpiration(double timeout);
private double m_expiration = kDefaultSafetyExpiration;
private boolean m_enabled;
private double m_stopTime = Timer.getFPGATimestamp();
private final Object m_thisMutex = new Object();
private static final Set<MotorSafety> m_instanceList = new LinkedHashSet<>();
private static final Object m_listMutex = new Object();
double getExpiration();
/**
* MotorSafety constructor.
*/
public MotorSafety() {
synchronized (m_listMutex) {
m_instanceList.add(this);
}
}
boolean isAlive();
/**
* Feed the motor safety object.
*
* <p>Resets the timer on this object that is used to do the timeouts.
*/
public void feed() {
synchronized (m_thisMutex) {
m_stopTime = Timer.getFPGATimestamp() + m_expiration;
}
}
void stopMotor();
/**
* Set the expiration time for the corresponding motor safety object.
*
* @param expirationTime The timeout value in seconds.
*/
public void setExpiration(double expirationTime) {
synchronized (m_thisMutex) {
m_expiration = expirationTime;
}
}
void setSafetyEnabled(boolean enabled);
/**
* Retrieve the timeout value for the corresponding motor safety object.
*
* @return the timeout value in seconds.
*/
public double getExpiration() {
synchronized (m_thisMutex) {
return m_expiration;
}
}
boolean isSafetyEnabled();
/**
* Determine of the motor is still operating or has timed out.
*
* @return a true value if the motor is still operating normally and hasn't timed out.
*/
public boolean isAlive() {
synchronized (m_thisMutex) {
return !m_enabled || m_stopTime > Timer.getFPGATimestamp();
}
}
String getDescription();
/**
* Check if this motor has exceeded its timeout. This method is called periodically to determine
* if this motor has exceeded its timeout value. If it has, the stop method is called, and the
* motor is shut down until its value is updated again.
*/
public void check() {
boolean enabled;
double stopTime;
synchronized (m_thisMutex) {
enabled = m_enabled;
stopTime = m_stopTime;
}
if (!enabled || RobotState.isDisabled() || RobotState.isTest()) {
return;
}
if (stopTime < Timer.getFPGATimestamp()) {
DriverStation.reportError(getDescription() + "... Output not updated often enough.", false);
stopMotor();
}
}
/**
* Enable/disable motor safety for this device.
*
* <p>Turn on and off the motor safety option for this PWM object.
*
* @param enabled True if motor safety is enforced for this object
*/
public void setSafetyEnabled(boolean enabled) {
synchronized (m_thisMutex) {
m_enabled = enabled;
}
}
/**
* Return the state of the motor safety enabled flag.
*
* <p>Return if the motor safety is currently enabled for this device.
*
* @return True if motor safety is enforced for this device
*/
public boolean isSafetyEnabled() {
synchronized (m_thisMutex) {
return m_enabled;
}
}
/**
* Check the motors to see if any have timed out.
*
* <p>This static method is called periodically to poll all the motors and stop any that have
* timed out.
*/
public static void checkMotors() {
synchronized (m_listMutex) {
for (MotorSafety elem : m_instanceList) {
elem.check();
}
}
}
public abstract void stopMotor();
public abstract String getDescription();
}

View File

@@ -1,150 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* The MotorSafetyHelper object is constructed for every object that wants to implement the Motor
* Safety protocol. The helper object has the code to actually do the timing and call the motors
* Stop() method when the timeout expires. The motor object is expected to call the Feed() method
* whenever the motors value is updated.
*/
public final class MotorSafetyHelper {
private double m_expiration;
private boolean m_enabled;
private double m_stopTime;
private final Object m_thisMutex = new Object();
private final MotorSafety m_safeObject;
private static final Set<MotorSafetyHelper> m_helperList = new LinkedHashSet<>();
private static final Object m_listMutex = new Object();
/**
* The constructor for a MotorSafetyHelper object. The helper object is constructed for every
* object that wants to implement the Motor Safety protocol. The helper object has the code to
* actually do the timing and call the motors Stop() method when the timeout expires. The motor
* object is expected to call the Feed() method whenever the motors value is updated.
*
* @param safeObject a pointer to the motor object implementing MotorSafety. This is used to call
* the Stop() method on the motor.
*/
public MotorSafetyHelper(MotorSafety safeObject) {
m_safeObject = safeObject;
m_enabled = false;
m_expiration = MotorSafety.DEFAULT_SAFETY_EXPIRATION;
m_stopTime = Timer.getFPGATimestamp();
synchronized (m_listMutex) {
m_helperList.add(this);
}
}
/**
* Feed the motor safety object. Resets the timer on this object that is used to do the timeouts.
*/
public void feed() {
synchronized (m_thisMutex) {
m_stopTime = Timer.getFPGATimestamp() + m_expiration;
}
}
/**
* Set the expiration time for the corresponding motor safety object.
*
* @param expirationTime The timeout value in seconds.
*/
public void setExpiration(double expirationTime) {
synchronized (m_thisMutex) {
m_expiration = expirationTime;
}
}
/**
* Retrieve the timeout value for the corresponding motor safety object.
*
* @return the timeout value in seconds.
*/
public double getExpiration() {
synchronized (m_thisMutex) {
return m_expiration;
}
}
/**
* Determine of the motor is still operating or has timed out.
*
* @return a true value if the motor is still operating normally and hasn't timed out.
*/
public boolean isAlive() {
synchronized (m_thisMutex) {
return !m_enabled || m_stopTime > Timer.getFPGATimestamp();
}
}
/**
* Check if this motor has exceeded its timeout. This method is called periodically to determine
* if this motor has exceeded its timeout value. If it has, the stop method is called, and the
* motor is shut down until its value is updated again.
*/
public void check() {
boolean enabled;
double stopTime;
synchronized (m_thisMutex) {
enabled = m_enabled;
stopTime = m_stopTime;
}
if (!enabled || RobotState.isDisabled() || RobotState.isTest()) {
return;
}
if (stopTime < Timer.getFPGATimestamp()) {
DriverStation.reportError(m_safeObject.getDescription() + "... Output not updated often "
+ "enough.", false);
m_safeObject.stopMotor();
}
}
/**
* Enable/disable motor safety for this device Turn on and off the motor safety option for this
* PWM object.
*
* @param enabled True if motor safety is enforced for this object
*/
public void setSafetyEnabled(boolean enabled) {
synchronized (m_thisMutex) {
m_enabled = enabled;
}
}
/**
* Return the state of the motor safety enabled flag Return if the motor safety is currently
* enabled for this device.
*
* @return True if motor safety is enforced for this device
*/
public boolean isSafetyEnabled() {
synchronized (m_thisMutex) {
return m_enabled;
}
}
/**
* Check the motors to see if any have timed out. This static method is called periodically to
* poll all the motors and stop any that have timed out.
*/
public static void checkMotors() {
synchronized (m_listMutex) {
for (MotorSafetyHelper elem : m_helperList) {
elem.check();
}
}
}
}

View File

@@ -14,14 +14,16 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
/**
* Nidec Brushless Motor.
*/
public class NidecBrushless extends SendableBase implements SpeedController, MotorSafety, Sendable {
private final MotorSafetyHelper m_safetyHelper;
public class NidecBrushless extends MotorSafety implements SpeedController, Sendable,
AutoCloseable {
private boolean m_isInverted;
private final DigitalOutput m_dio;
private final PWM m_pwm;
private volatile double m_speed;
private volatile boolean m_disabled;
private final SendableImpl m_sendableImpl;
/**
* Constructor.
*
@@ -31,9 +33,9 @@ public class NidecBrushless extends SendableBase implements SpeedController, Mot
* 0-9 are on-board, 10-25 are on the MXP port
*/
public NidecBrushless(final int pwmChannel, final int dioChannel) {
m_safetyHelper = new MotorSafetyHelper(this);
m_safetyHelper.setExpiration(0.0);
m_safetyHelper.setSafetyEnabled(false);
m_sendableImpl = new SendableImpl(true);
setSafetyEnabled(false);
// the dio controls the output (in PWM mode)
m_dio = new DigitalOutput(dioChannel);
@@ -51,11 +53,61 @@ public class NidecBrushless extends SendableBase implements SpeedController, Mot
@Override
public void close() {
super.close();
m_sendableImpl.close();
m_dio.close();
m_pwm.close();
}
@Override
public final synchronized String getName() {
return m_sendableImpl.getName();
}
@Override
public final synchronized void setName(String name) {
m_sendableImpl.setName(name);
}
/**
* Sets the name of the sensor with a channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param channel The channel number the device is plugged into
*/
protected final void setName(String moduleType, int channel) {
m_sendableImpl.setName(moduleType, channel);
}
/**
* Sets the name of the sensor with a module and channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param moduleNumber The number of the particular module type
* @param channel The channel number the device is plugged into (usually PWM)
*/
protected final void setName(String moduleType, int moduleNumber, int channel) {
m_sendableImpl.setName(moduleType, moduleNumber, channel);
}
@Override
public final synchronized String getSubsystem() {
return m_sendableImpl.getSubsystem();
}
@Override
public final synchronized void setSubsystem(String subsystem) {
m_sendableImpl.setSubsystem(subsystem);
}
/**
* Add a child component.
*
* @param child child component
*/
protected final void addChild(Object child) {
m_sendableImpl.addChild(child);
}
/**
* Set the PWM value.
*
@@ -71,7 +123,8 @@ public class NidecBrushless extends SendableBase implements SpeedController, Mot
m_dio.updateDutyCycle(0.5 + 0.5 * (m_isInverted ? -speed : speed));
m_pwm.setRaw(0xffff);
}
m_safetyHelper.feed();
feed();
}
/**
@@ -105,39 +158,8 @@ public class NidecBrushless extends SendableBase implements SpeedController, Mot
}
/**
* Set the safety expiration time.
*
* @param timeout The timeout (in seconds) for this motor object
*/
@Override
public void setExpiration(double timeout) {
m_safetyHelper.setExpiration(timeout);
}
/**
* Return the safety expiration time.
*
* @return The expiration time value.
*/
@Override
public double getExpiration() {
return m_safetyHelper.getExpiration();
}
/**
* Check if the motor is currently alive or stopped due to a timeout.
*
* @return a bool value that is true if the motor has NOT timed out and should still be running.
*/
@Override
public boolean isAlive() {
return m_safetyHelper.isAlive();
}
/**
* Stop the motor. This is called by the MotorSafetyHelper object
* when it has a timeout for this PWM and needs to stop it from running.
* Calling set() will re-enable the motor.
* Stop the motor. This is called by the MotorSafety object when it has a timeout for this PWM and
* needs to stop it from running. Calling set() will re-enable the motor.
*/
@Override
public void stopMotor() {
@@ -145,21 +167,6 @@ public class NidecBrushless extends SendableBase implements SpeedController, Mot
m_pwm.setDisabled();
}
/**
* Check if motor safety is enabled.
*
* @return True if motor safety is enforced for this object
*/
@Override
public boolean isSafetyEnabled() {
return m_safetyHelper.isSafetyEnabled();
}
@Override
public void setSafetyEnabled(boolean enabled) {
m_safetyHelper.setSafetyEnabled(enabled);
}
@Override
public String getDescription() {
return "Nidec " + getChannel();

View File

@@ -25,7 +25,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
* 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 SendableBase {
public class PWM extends MotorSafety implements Sendable, AutoCloseable {
/**
* Represents the amount to multiply the minimum servo-pulse pwm period by.
*/
@@ -47,12 +47,16 @@ public class PWM extends SendableBase {
private final int m_channel;
private int m_handle;
private final SendableImpl m_sendableImpl;
/**
* Allocate a PWM given a channel.
*
* @param channel The PWM channel number. 0-9 are on-board, 10-19 are on the MXP port
*/
public PWM(final int channel) {
m_sendableImpl = new SendableImpl(true);
SensorUtil.checkPWMChannel(channel);
m_channel = channel;
@@ -64,6 +68,8 @@ public class PWM extends SendableBase {
HAL.report(tResourceType.kResourceType_PWM, channel);
setName("PWM", channel);
setSafetyEnabled(false);
}
/**
@@ -71,7 +77,8 @@ public class PWM extends SendableBase {
*/
@Override
public void close() {
super.close();
m_sendableImpl.close();
if (m_handle == 0) {
return;
}
@@ -80,6 +87,66 @@ public class PWM extends SendableBase {
m_handle = 0;
}
@Override
public final synchronized String getName() {
return m_sendableImpl.getName();
}
@Override
public final synchronized void setName(String name) {
m_sendableImpl.setName(name);
}
/**
* Sets the name of the sensor with a channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param channel The channel number the device is plugged into
*/
protected final void setName(String moduleType, int channel) {
m_sendableImpl.setName(moduleType, channel);
}
/**
* Sets the name of the sensor with a module and channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param moduleNumber The number of the particular module type
* @param channel The channel number the device is plugged into (usually PWM)
*/
protected final void setName(String moduleType, int moduleNumber, int channel) {
m_sendableImpl.setName(moduleType, moduleNumber, channel);
}
@Override
public final synchronized String getSubsystem() {
return m_sendableImpl.getSubsystem();
}
@Override
public final synchronized void setSubsystem(String subsystem) {
m_sendableImpl.setSubsystem(subsystem);
}
/**
* Add a child component.
*
* @param child child component
*/
protected final void addChild(Object child) {
m_sendableImpl.addChild(child);
}
@Override
public void stopMotor() {
setDisabled();
}
@Override
public String getDescription() {
return "PWM " + getChannel();
}
/**
* Optionally eliminate the deadband from a speed controller.
*

View File

@@ -12,7 +12,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
/**
* Common base class for all PWM Speed Controllers.
*/
public abstract class PWMSpeedController extends SafePWM implements SpeedController {
public abstract class PWMSpeedController extends PWM implements SpeedController {
private boolean m_isInverted;
/**
@@ -64,6 +64,11 @@ public abstract class PWMSpeedController extends SafePWM implements SpeedControl
return m_isInverted;
}
@Override
public void disable() {
setDisabled();
}
/**
* Write out the PID value as seen in the PIDOutput base object.
*

View File

@@ -27,9 +27,7 @@ import static java.util.Objects.requireNonNull;
* channels (forward and reverse) to be used independently for something that does not care about
* voltage polarity (like a solenoid).
*/
public class Relay extends SendableBase implements MotorSafety {
private MotorSafetyHelper m_safetyHelper;
public class Relay extends MotorSafety implements Sendable, AutoCloseable {
/**
* This class represents errors in trying to set relay values contradictory to the direction to
* which the relay is set.
@@ -95,6 +93,8 @@ public class Relay extends SendableBase implements MotorSafety {
private Direction m_direction;
private final SendableImpl m_sendableImpl;
/**
* Common relay initialization method. This code is common to all Relay constructors and
* initializes the relay and reserves all resources that need to be locked. Initially the relay is
@@ -113,8 +113,7 @@ public class Relay extends SendableBase implements MotorSafety {
HAL.report(tResourceType.kResourceType_Relay, m_channel + 128);
}
m_safetyHelper = new MotorSafetyHelper(this);
m_safetyHelper.setSafetyEnabled(false);
setSafetyEnabled(false);
setName("Relay", m_channel);
}
@@ -126,6 +125,8 @@ public class Relay extends SendableBase implements MotorSafety {
* @param direction The direction that the Relay object will control.
*/
public Relay(final int channel, Direction direction) {
m_sendableImpl = new SendableImpl(true);
m_channel = channel;
m_direction = requireNonNull(direction, "Null Direction was given");
initRelay();
@@ -143,7 +144,7 @@ public class Relay extends SendableBase implements MotorSafety {
@Override
public void close() {
super.close();
m_sendableImpl.close();
freeRelay();
}
@@ -166,6 +167,56 @@ public class Relay extends SendableBase implements MotorSafety {
m_reverseHandle = 0;
}
@Override
public final synchronized String getName() {
return m_sendableImpl.getName();
}
@Override
public final synchronized void setName(String name) {
m_sendableImpl.setName(name);
}
/**
* Sets the name of the sensor with a channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param channel The channel number the device is plugged into
*/
protected final void setName(String moduleType, int channel) {
m_sendableImpl.setName(moduleType, channel);
}
/**
* Sets the name of the sensor with a module and channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param moduleNumber The number of the particular module type
* @param channel The channel number the device is plugged into (usually PWM)
*/
protected final void setName(String moduleType, int moduleNumber, int channel) {
m_sendableImpl.setName(moduleType, moduleNumber, channel);
}
@Override
public final synchronized String getSubsystem() {
return m_sendableImpl.getSubsystem();
}
@Override
public final synchronized void setSubsystem(String subsystem) {
m_sendableImpl.setSubsystem(subsystem);
}
/**
* Add a child component.
*
* @param child child component
*/
protected final void addChild(Object child) {
m_sendableImpl.addChild(child);
}
/**
* Set the relay state.
*
@@ -276,36 +327,11 @@ public class Relay extends SendableBase implements MotorSafety {
return m_channel;
}
@Override
public void setExpiration(double timeout) {
m_safetyHelper.setExpiration(timeout);
}
@Override
public double getExpiration() {
return m_safetyHelper.getExpiration();
}
@Override
public boolean isAlive() {
return m_safetyHelper.isAlive();
}
@Override
public void stopMotor() {
set(Value.kOff);
}
@Override
public boolean isSafetyEnabled() {
return m_safetyHelper.isSafetyEnabled();
}
@Override
public void setSafetyEnabled(boolean enabled) {
m_safetyHelper.setSafetyEnabled(enabled);
}
@Override
public String getDescription() {
return "Relay ID " + getChannel();

View File

@@ -26,9 +26,7 @@ import static java.util.Objects.requireNonNull;
*/
@Deprecated
@SuppressWarnings({"PMD.GodClass", "PMD.TooManyMethods"})
public class RobotDrive implements MotorSafety, AutoCloseable {
protected MotorSafetyHelper m_safetyHelper;
public class RobotDrive extends MotorSafety implements AutoCloseable {
/**
* The location of a motor on the robot for the purpose of driving.
*/
@@ -76,7 +74,7 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
m_frontRightMotor = null;
m_rearRightMotor = new Talon(rightMotorChannel);
m_allocatedSpeedControllers = true;
setupMotorSafety();
setSafetyEnabled(true);
drive(0, 0);
}
@@ -99,7 +97,7 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
m_frontLeftMotor = new Talon(frontLeftMotor);
m_frontRightMotor = new Talon(frontRightMotor);
m_allocatedSpeedControllers = true;
setupMotorSafety();
setSafetyEnabled(true);
drive(0, 0);
}
@@ -123,7 +121,7 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
m_sensitivity = kDefaultSensitivity;
m_maxOutput = kDefaultMaxOutput;
m_allocatedSpeedControllers = false;
setupMotorSafety();
setSafetyEnabled(true);
drive(0, 0);
}
@@ -145,7 +143,7 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
m_sensitivity = kDefaultSensitivity;
m_maxOutput = kDefaultMaxOutput;
m_allocatedSpeedControllers = false;
setupMotorSafety();
setSafetyEnabled(true);
drive(0, 0);
}
@@ -480,9 +478,7 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
m_rearLeftMotor.set(wheelSpeeds[MotorType.kRearLeft.value] * m_maxOutput);
m_rearRightMotor.set(wheelSpeeds[MotorType.kRearRight.value] * m_maxOutput);
if (m_safetyHelper != null) {
m_safetyHelper.feed();
}
feed();
}
/**
@@ -524,9 +520,7 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
m_rearLeftMotor.set(wheelSpeeds[MotorType.kRearLeft.value] * m_maxOutput);
m_rearRightMotor.set(wheelSpeeds[MotorType.kRearRight.value] * m_maxOutput);
if (m_safetyHelper != null) {
m_safetyHelper.feed();
}
feed();
}
/**
@@ -566,9 +560,7 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
}
m_rearRightMotor.set(-limit(rightOutput) * m_maxOutput);
if (m_safetyHelper != null) {
m_safetyHelper.feed();
}
feed();
}
/**
@@ -689,31 +681,6 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
}
}
@Override
public void setExpiration(double timeout) {
m_safetyHelper.setExpiration(timeout);
}
@Override
public double getExpiration() {
return m_safetyHelper.getExpiration();
}
@Override
public boolean isAlive() {
return m_safetyHelper.isAlive();
}
@Override
public boolean isSafetyEnabled() {
return m_safetyHelper.isSafetyEnabled();
}
@Override
public void setSafetyEnabled(boolean enabled) {
m_safetyHelper.setSafetyEnabled(enabled);
}
@Override
public String getDescription() {
return "Robot Drive";
@@ -733,15 +700,8 @@ public class RobotDrive implements MotorSafety, AutoCloseable {
if (m_rearRightMotor != null) {
m_rearRightMotor.stopMotor();
}
if (m_safetyHelper != null) {
m_safetyHelper.feed();
}
}
private void setupMotorSafety() {
m_safetyHelper = new MotorSafetyHelper(this);
m_safetyHelper.setExpiration(kDefaultExpirationTime);
m_safetyHelper.setSafetyEnabled(true);
feed();
}
protected int getNumMotors() {

View File

@@ -1,112 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
/**
* Manages a PWM object.
*/
public class SafePWM extends PWM implements MotorSafety {
private final MotorSafetyHelper m_safetyHelper;
/**
* Constructor for a SafePWM object taking a channel number.
*
* @param channel The channel number to be used for the underlying PWM object. 0-9 are on-board,
* 10-19 are on the MXP port.
*/
public SafePWM(final int channel) {
super(channel);
m_safetyHelper = new MotorSafetyHelper(this);
m_safetyHelper.setExpiration(0.0);
m_safetyHelper.setSafetyEnabled(false);
}
/**
* Set the expiration time for the PWM object.
*
* @param timeout The timeout (in seconds) for this motor object
*/
@Override
public void setExpiration(double timeout) {
m_safetyHelper.setExpiration(timeout);
}
/**
* Return the expiration time for the PWM object.
*
* @return The expiration time value.
*/
@Override
public double getExpiration() {
return m_safetyHelper.getExpiration();
}
/**
* Check if the PWM object is currently alive or stopped due to a timeout.
*
* @return a bool value that is true if the motor has NOT timed out and should still be running.
*/
@Override
public boolean isAlive() {
return m_safetyHelper.isAlive();
}
/**
* Stop the motor associated with this PWM object. This is called by the MotorSafetyHelper object
* when it has a timeout for this PWM and needs to stop it from running.
*/
@Override
public void stopMotor() {
disable();
}
/**
* Check if motor safety is enabled for this object.
*
* @return True if motor safety is enforced for this object
*/
@Override
public boolean isSafetyEnabled() {
return m_safetyHelper.isSafetyEnabled();
}
/**
* Feed the MotorSafety timer. This method is called by the subclass motor whenever it updates its
* speed, thereby resetting the timeout value.
*
* @deprecated Use {@link #feed()} instead.
*/
@Deprecated
@SuppressWarnings("MethodName")
public void Feed() {
feed();
}
/**
* Feed the MotorSafety timer. This method is called by the subclass motor whenever it updates its
* speed, thereby resetting the timeout value.
*/
public void feed() {
m_safetyHelper.feed();
}
@Override
public void setSafetyEnabled(boolean enabled) {
m_safetyHelper.setSafetyEnabled(enabled);
}
@Override
public String getDescription() {
return "PWM " + getChannel();
}
public void disable() {
setDisabled();
}
}

View File

@@ -0,0 +1,101 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
/**
* The base interface for objects that can be sent over the network through network tables.
*/
public class SendableImpl implements Sendable, AutoCloseable {
private String m_name = "";
private String m_subsystem = "Ungrouped";
/**
* Creates an instance of the sensor base.
*/
public SendableImpl() {
this(true);
}
/**
* Creates an instance of the sensor base.
*
* @param addLiveWindow if true, add this Sendable to LiveWindow
*/
public SendableImpl(boolean addLiveWindow) {
if (addLiveWindow) {
LiveWindow.add(this);
}
}
@Deprecated
public void free() {
close();
}
@Override
public void close() {
LiveWindow.remove(this);
}
@Override
public synchronized String getName() {
return m_name;
}
@Override
public synchronized void setName(String name) {
m_name = name;
}
/**
* Sets the name of the sensor with a channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param channel The channel number the device is plugged into
*/
public void setName(String moduleType, int channel) {
setName(moduleType + "[" + channel + "]");
}
/**
* Sets the name of the sensor with a module and channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param moduleNumber The number of the particular module type
* @param channel The channel number the device is plugged into (usually PWM)
*/
public void setName(String moduleType, int moduleNumber, int channel) {
setName(moduleType + "[" + moduleNumber + "," + channel + "]");
}
@Override
public synchronized String getSubsystem() {
return m_subsystem;
}
@Override
public synchronized void setSubsystem(String subsystem) {
m_subsystem = subsystem;
}
@Override
public void initSendable(SendableBuilder builder) {
}
/**
* Add a child component.
*
* @param child child component
*/
public void addChild(Object child) {
LiveWindow.addChild(this, child);
}
}

View File

@@ -191,7 +191,7 @@ public class DifferentialDrive extends RobotDriveBase {
m_leftMotor.set(limit(leftMotorOutput) * m_maxOutput);
m_rightMotor.set(limit(rightMotorOutput) * m_maxOutput * m_rightSideInvertMultiplier);
m_safetyHelper.feed();
feed();
}
/**
@@ -275,7 +275,7 @@ public class DifferentialDrive extends RobotDriveBase {
m_leftMotor.set(leftMotorOutput * m_maxOutput);
m_rightMotor.set(rightMotorOutput * m_maxOutput * m_rightSideInvertMultiplier);
m_safetyHelper.feed();
feed();
}
/**
@@ -323,7 +323,7 @@ public class DifferentialDrive extends RobotDriveBase {
m_leftMotor.set(leftSpeed * m_maxOutput);
m_rightMotor.set(rightSpeed * m_maxOutput * m_rightSideInvertMultiplier);
m_safetyHelper.feed();
feed();
}
/**
@@ -379,7 +379,7 @@ public class DifferentialDrive extends RobotDriveBase {
public void stopMotor() {
m_leftMotor.stopMotor();
m_rightMotor.stopMotor();
m_safetyHelper.feed();
feed();
}
@Override

View File

@@ -162,7 +162,7 @@ public class KilloughDrive extends RobotDriveBase {
m_rightMotor.set(wheelSpeeds[MotorType.kRight.value] * m_maxOutput);
m_backMotor.set(wheelSpeeds[MotorType.kBack.value] * m_maxOutput);
m_safetyHelper.feed();
feed();
}
/**
@@ -193,7 +193,7 @@ public class KilloughDrive extends RobotDriveBase {
m_leftMotor.stopMotor();
m_rightMotor.stopMotor();
m_backMotor.stopMotor();
m_safetyHelper.feed();
feed();
}
@Override

View File

@@ -148,7 +148,7 @@ public class MecanumDrive extends RobotDriveBase {
m_rearRightMotor.set(wheelSpeeds[MotorType.kRearRight.value] * m_maxOutput
* m_rightSideInvertMultiplier);
m_safetyHelper.feed();
feed();
}
/**
@@ -197,7 +197,7 @@ public class MecanumDrive extends RobotDriveBase {
m_frontRightMotor.stopMotor();
m_rearLeftMotor.stopMotor();
m_rearRightMotor.stopMotor();
m_safetyHelper.feed();
feed();
}
@Override

View File

@@ -8,19 +8,20 @@
package edu.wpi.first.wpilibj.drive;
import edu.wpi.first.wpilibj.MotorSafety;
import edu.wpi.first.wpilibj.MotorSafetyHelper;
import edu.wpi.first.wpilibj.SendableBase;
import edu.wpi.first.wpilibj.Sendable;
import edu.wpi.first.wpilibj.SendableImpl;
/**
* Common base class for drive platforms.
*/
public abstract class RobotDriveBase extends SendableBase implements MotorSafety {
public abstract class RobotDriveBase extends MotorSafety implements Sendable, AutoCloseable {
public static final double kDefaultDeadband = 0.02;
public static final double kDefaultMaxOutput = 1.0;
protected double m_deadband = kDefaultDeadband;
protected double m_maxOutput = kDefaultMaxOutput;
protected MotorSafetyHelper m_safetyHelper = new MotorSafetyHelper(this);
private final SendableImpl m_sendableImpl;
/**
* The location of a motor on the robot for the purpose of driving.
@@ -37,11 +38,71 @@ public abstract class RobotDriveBase extends SendableBase implements MotorSafety
}
}
/**
* RobotDriveBase constructor.
*/
public RobotDriveBase() {
m_safetyHelper.setSafetyEnabled(true);
m_sendableImpl = new SendableImpl(true);
setSafetyEnabled(true);
setName("RobotDriveBase");
}
@Override
public void close() {
m_sendableImpl.close();
}
@Override
public final synchronized String getName() {
return m_sendableImpl.getName();
}
@Override
public final synchronized void setName(String name) {
m_sendableImpl.setName(name);
}
/**
* Sets the name of the sensor with a channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param channel The channel number the device is plugged into
*/
protected final void setName(String moduleType, int channel) {
m_sendableImpl.setName(moduleType, channel);
}
/**
* Sets the name of the sensor with a module and channel number.
*
* @param moduleType A string that defines the module name in the label for the value
* @param moduleNumber The number of the particular module type
* @param channel The channel number the device is plugged into (usually PWM)
*/
protected final void setName(String moduleType, int moduleNumber, int channel) {
m_sendableImpl.setName(moduleType, moduleNumber, channel);
}
@Override
public final synchronized String getSubsystem() {
return m_sendableImpl.getSubsystem();
}
@Override
public final synchronized void setSubsystem(String subsystem) {
m_sendableImpl.setSubsystem(subsystem);
}
/**
* Add a child component.
*
* @param child child component
*/
protected final void addChild(Object child) {
m_sendableImpl.addChild(child);
}
/**
* Sets the deadband applied to the drive inputs (e.g., joystick values).
*
@@ -70,40 +131,15 @@ public abstract class RobotDriveBase extends SendableBase implements MotorSafety
/**
* Feed the motor safety object. Resets the timer that will stop the motors if it completes.
*
* @see MotorSafetyHelper#feed()
* @see MotorSafety#feed()
*/
public void feedWatchdog() {
m_safetyHelper.feed();
}
@Override
public void setExpiration(double timeout) {
m_safetyHelper.setExpiration(timeout);
}
@Override
public double getExpiration() {
return m_safetyHelper.getExpiration();
}
@Override
public boolean isAlive() {
return m_safetyHelper.isAlive();
feed();
}
@Override
public abstract void stopMotor();
@Override
public boolean isSafetyEnabled() {
return m_safetyHelper.isSafetyEnabled();
}
@Override
public void setSafetyEnabled(boolean enabled) {
m_safetyHelper.setSafetyEnabled(enabled);
}
@Override
public abstract String getDescription();