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