Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "SafePWM.h" |
| 9 | |
| 10 | using namespace frc; |
| 11 | |
| 12 | /** |
| 13 | * Constructor for a SafePWM object taking a channel number. |
| 14 | * |
| 15 | * @param channel The PWM channel number 0-9 are on-board, 10-19 are on the MXP |
| 16 | * port |
| 17 | */ |
| 18 | SafePWM::SafePWM(int channel) : PWM(channel) { |
| 19 | m_safetyHelper = std::make_unique<MotorSafetyHelper>(this); |
| 20 | m_safetyHelper->SetSafetyEnabled(false); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Set the expiration time for the PWM object. |
| 25 | * |
| 26 | * @param timeout The timeout (in seconds) for this motor object |
| 27 | */ |
| 28 | void SafePWM::SetExpiration(double timeout) { |
| 29 | m_safetyHelper->SetExpiration(timeout); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Return the expiration time for the PWM object. |
| 34 | * |
| 35 | * @returns The expiration time value. |
| 36 | */ |
| 37 | double SafePWM::GetExpiration() const { |
| 38 | return m_safetyHelper->GetExpiration(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check if the PWM object is currently alive or stopped due to a timeout. |
| 43 | * |
| 44 | * @return a bool value that is true if the motor has NOT timed out and should |
| 45 | * still be running. |
| 46 | */ |
| 47 | bool SafePWM::IsAlive() const { return m_safetyHelper->IsAlive(); } |
| 48 | |
| 49 | /** |
| 50 | * Stop the motor associated with this PWM object. |
| 51 | * |
| 52 | * This is called by the MotorSafetyHelper object when it has a timeout for this |
| 53 | * PWM and needs to stop it from running. |
| 54 | */ |
| 55 | void SafePWM::StopMotor() { SetDisabled(); } |
| 56 | |
| 57 | /** |
| 58 | * Enable/disable motor safety for this device. |
| 59 | * |
| 60 | * Turn on and off the motor safety option for this PWM object. |
| 61 | * |
| 62 | * @param enabled True if motor safety is enforced for this object |
| 63 | */ |
| 64 | void SafePWM::SetSafetyEnabled(bool enabled) { |
| 65 | m_safetyHelper->SetSafetyEnabled(enabled); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Check if motor safety is enabled for this object. |
| 70 | * |
| 71 | * @returns True if motor safety is enforced for this object |
| 72 | */ |
| 73 | bool SafePWM::IsSafetyEnabled() const { |
| 74 | return m_safetyHelper->IsSafetyEnabled(); |
| 75 | } |
| 76 | |
| 77 | void SafePWM::GetDescription(std::ostringstream& desc) const { |
| 78 | desc << "PWM " << GetChannel(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Feed the MotorSafety timer when setting the speed. |
| 83 | * |
| 84 | * This method is called by the subclass motor whenever it updates its speed, |
| 85 | * thereby reseting the timeout value. |
| 86 | * |
| 87 | * @param speed Value to pass to the PWM class |
| 88 | */ |
| 89 | void SafePWM::SetSpeed(double speed) { |
| 90 | PWM::SetSpeed(speed); |
| 91 | m_safetyHelper->Feed(); |
| 92 | } |