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