001 /*----------------------------------------------------------------------------*/
002 /* Copyright (c) FIRST 2008-2012. All Rights Reserved. */
003 /* Open Source Software - may be modified and shared by FRC teams. The code */
004 /* must be accompanied by the FIRST BSD license file in the root directory of */
005 /* the project. */
006 /*----------------------------------------------------------------------------*/
007
008 package edu.wpi.first.wpilibj;
009
010 /**
011 *
012 * @author brad
013 */
014 public class SafePWM extends PWM implements MotorSafety {
015
016 private MotorSafetyHelper m_safetyHelper;
017
018 /**
019 * Initialize a SafePWM object by setting defaults
020 */
021 void initSafePWM() {
022 m_safetyHelper = new MotorSafetyHelper(this);
023 m_safetyHelper.setExpiration(0.0);
024 m_safetyHelper.setSafetyEnabled(false);
025 }
026
027
028 /**
029 * Constructor for a SafePWM object taking a channel number
030 * @param channel The channel number to be used for the underlying PWM object
031 */
032 public SafePWM(final int channel) {
033 super(channel);
034 initSafePWM();
035 }
036
037 /**
038 * Constructor for a SafePWM object taking channel and slot numbers.
039 * @param slot The slot number of the digital module for this PWM object
040 * @param channel The channel number in the module for this PWM object
041 */
042 public SafePWM(final int slot, final int channel) {
043 super(slot, channel);
044 initSafePWM();
045 }
046
047 /*
048 * Set the expiration time for the PWM object
049 * @param timeout The timeout (in seconds) for this motor object
050 */
051 public void setExpiration(double timeout) {
052 m_safetyHelper.setExpiration(timeout);
053 }
054
055 /**
056 * Return the expiration time for the PWM object.
057 * @return The expiration time value.
058 */
059 public double getExpiration() {
060 return m_safetyHelper.getExpiration();
061 }
062
063 /**
064 * Check if the PWM object is currently alive or stopped due to a timeout.
065 * @return a bool value that is true if the motor has NOT timed out and should still
066 * be running.
067 */
068 public boolean isAlive() {
069 return m_safetyHelper.isAlive();
070 }
071
072 /**
073 * Stop the motor associated with this PWM object.
074 * This is called by the MotorSafetyHelper object when it has a timeout for this PWM and needs to
075 * stop it from running.
076 */
077 public void stopMotor() {
078 disable();
079 }
080
081 /**
082 * Check if motor safety is enabled for this object
083 * @return True if motor safety is enforced for this object
084 */
085 public boolean isSafetyEnabled() {
086 return m_safetyHelper.isSafetyEnabled();
087 }
088
089 /**
090 * Feed the MotorSafety timer.
091 * This method is called by the subclass motor whenever it updates its speed, thereby reseting
092 * the timeout value.
093 */
094 public void Feed() {
095 m_safetyHelper.feed();
096 }
097
098 public void setSafetyEnabled(boolean enabled) {
099 m_safetyHelper.setSafetyEnabled(enabled);
100 }
101
102 public String getDescription() {
103 return "PWM "+getChannel()+" on module "+getModuleNumber();
104 }
105
106 public void disable() {
107 setRaw(kPwmDisabled);
108 }
109 }