001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2008-2018 FIRST. 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
008package edu.wpi.first.wpilibj;
009
010/**
011 * Manages a PWM object.
012 */
013public class SafePWM extends PWM implements MotorSafety {
014  private final MotorSafetyHelper m_safetyHelper;
015
016  /**
017   * Constructor for a SafePWM object taking a channel number.
018   *
019   * @param channel The channel number to be used for the underlying PWM object. 0-9 are on-board,
020   *                10-19 are on the MXP port.
021   */
022  public SafePWM(final int channel) {
023    super(channel);
024
025    m_safetyHelper = new MotorSafetyHelper(this);
026    m_safetyHelper.setExpiration(0.0);
027    m_safetyHelper.setSafetyEnabled(false);
028  }
029
030  /**
031   * Set the expiration time for the PWM object.
032   *
033   * @param timeout The timeout (in seconds) for this motor object
034   */
035  public void setExpiration(double timeout) {
036    m_safetyHelper.setExpiration(timeout);
037  }
038
039  /**
040   * Return the expiration time for the PWM object.
041   *
042   * @return The expiration time value.
043   */
044  public double getExpiration() {
045    return m_safetyHelper.getExpiration();
046  }
047
048  /**
049   * Check if the PWM object is currently alive or stopped due to a timeout.
050   *
051   * @return a bool value that is true if the motor has NOT timed out and should still be running.
052   */
053  public boolean isAlive() {
054    return m_safetyHelper.isAlive();
055  }
056
057  /**
058   * Stop the motor associated with this PWM object. This is called by the MotorSafetyHelper object
059   * when it has a timeout for this PWM and needs to stop it from running.
060   */
061  public void stopMotor() {
062    disable();
063  }
064
065  /**
066   * Check if motor safety is enabled for this object.
067   *
068   * @return True if motor safety is enforced for this object
069   */
070  public boolean isSafetyEnabled() {
071    return m_safetyHelper.isSafetyEnabled();
072  }
073
074  /**
075   * Feed the MotorSafety timer. This method is called by the subclass motor whenever it updates its
076   * speed, thereby resetting the timeout value.
077   *
078   * @deprecated Use {@link #feed()} instead.
079   */
080  @Deprecated
081  @SuppressWarnings("MethodName")
082  public void Feed() {
083    feed();
084  }
085
086  /**
087   * Feed the MotorSafety timer. This method is called by the subclass motor whenever it updates its
088   * speed, thereby resetting the timeout value.
089   */
090  public void feed() {
091    m_safetyHelper.feed();
092  }
093
094  public void setSafetyEnabled(boolean enabled) {
095    m_safetyHelper.setSafetyEnabled(enabled);
096  }
097
098  public String getDescription() {
099    return "PWM " + getChannel();
100  }
101
102  public void disable() {
103    setDisabled();
104  }
105}