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
008package edu.wpi.first.wpilibj;
009
010/**
011 *
012 * @author brad
013 */
014public 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. 0-9 are on-board, 10-19 are on the MXP port.
031     */
032    public SafePWM(final int channel) {
033        super(channel);
034        initSafePWM();
035    }
036
037    /*
038     * Set the expiration time for the PWM object
039     * @param timeout The timeout (in seconds) for this motor object
040     */
041    public void setExpiration(double timeout) {
042        m_safetyHelper.setExpiration(timeout);
043    }
044
045    /**
046     * Return the expiration time for the PWM object.
047     * @return The expiration time value.
048     */
049    public double getExpiration() {
050        return m_safetyHelper.getExpiration();
051    }
052
053    /**
054     * Check if the PWM object is currently alive or stopped due to a timeout.
055     * @return a bool value that is true if the motor has NOT timed out and should still
056     * be running.
057     */
058    public boolean isAlive() {
059        return m_safetyHelper.isAlive();
060    }
061
062    /**
063     * Stop the motor associated with this PWM object.
064     * This is called by the MotorSafetyHelper object when it has a timeout for this PWM and needs to
065     * stop it from running.
066     */
067    public void stopMotor() {
068        disable();
069    }
070
071    /**
072     * Check if motor safety is enabled for this object
073     * @return True if motor safety is enforced for this object
074     */
075    public boolean isSafetyEnabled() {
076        return m_safetyHelper.isSafetyEnabled();
077    }
078
079    /**
080     * Feed the MotorSafety timer.
081     * This method is called by the subclass motor whenever it updates its speed, thereby reseting
082     * the timeout value.
083     */
084    public void Feed() {
085        m_safetyHelper.feed();
086    }
087
088    public void setSafetyEnabled(boolean enabled) {
089        m_safetyHelper.setSafetyEnabled(enabled);
090    }
091
092    public String getDescription() {
093        return "PWM "+getChannel();
094    }
095
096    public void disable() {
097        setRaw(kPwmDisabled);
098    }
099}