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 "MotorSafetyHelper.h" |
| 9 | |
| 10 | #include "DriverStation.h" |
| 11 | #include "MotorSafety.h" |
| 12 | #include "Timer.h" |
| 13 | #include "WPIErrors.h" |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <sstream> |
| 17 | |
| 18 | std::set<MotorSafetyHelper*> MotorSafetyHelper::m_helperList; |
| 19 | priority_recursive_mutex MotorSafetyHelper::m_listMutex; |
| 20 | |
| 21 | /** |
| 22 | * The constructor for a MotorSafetyHelper object. |
| 23 | * The helper object is constructed for every object that wants to implement the |
| 24 | * Motor |
| 25 | * Safety protocol. The helper object has the code to actually do the timing and |
| 26 | * call the |
| 27 | * motors Stop() method when the timeout expires. The motor object is expected |
| 28 | * to call the |
| 29 | * Feed() method whenever the motors value is updated. |
| 30 | * @param safeObject a pointer to the motor object implementing MotorSafety. |
| 31 | * This is used |
| 32 | * to call the Stop() method on the motor. |
| 33 | */ |
| 34 | MotorSafetyHelper::MotorSafetyHelper(MotorSafety *safeObject) |
| 35 | : m_safeObject(safeObject) { |
| 36 | m_enabled = false; |
| 37 | m_expiration = DEFAULT_SAFETY_EXPIRATION; |
| 38 | m_stopTime = Timer::GetFPGATimestamp(); |
| 39 | |
| 40 | std::lock_guard<priority_recursive_mutex> sync(m_listMutex); |
| 41 | m_helperList.insert(this); |
| 42 | } |
| 43 | |
| 44 | MotorSafetyHelper::~MotorSafetyHelper() { |
| 45 | std::lock_guard<priority_recursive_mutex> sync(m_listMutex); |
| 46 | m_helperList.erase(this); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Feed the motor safety object. |
| 51 | * Resets the timer on this object that is used to do the timeouts. |
| 52 | */ |
| 53 | void MotorSafetyHelper::Feed() { |
| 54 | std::lock_guard<priority_recursive_mutex> sync(m_syncMutex); |
| 55 | m_stopTime = Timer::GetFPGATimestamp() + m_expiration; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the expiration time for the corresponding motor safety object. |
| 60 | * @param expirationTime The timeout value in seconds. |
| 61 | */ |
| 62 | void MotorSafetyHelper::SetExpiration(float expirationTime) { |
| 63 | std::lock_guard<priority_recursive_mutex> sync(m_syncMutex); |
| 64 | m_expiration = expirationTime; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Retrieve the timeout value for the corresponding motor safety object. |
| 69 | * @return the timeout value in seconds. |
| 70 | */ |
| 71 | float MotorSafetyHelper::GetExpiration() const { |
| 72 | std::lock_guard<priority_recursive_mutex> sync(m_syncMutex); |
| 73 | return m_expiration; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Determine if the motor is still operating or has timed out. |
| 78 | * @return a true value if the motor is still operating normally and hasn't |
| 79 | * timed out. |
| 80 | */ |
| 81 | bool MotorSafetyHelper::IsAlive() const { |
| 82 | std::lock_guard<priority_recursive_mutex> sync(m_syncMutex); |
| 83 | return !m_enabled || m_stopTime > Timer::GetFPGATimestamp(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Check if this motor has exceeded its timeout. |
| 88 | * This method is called periodically to determine if this motor has exceeded |
| 89 | * its timeout |
| 90 | * value. If it has, the stop method is called, and the motor is shut down until |
| 91 | * its value is |
| 92 | * updated again. |
| 93 | */ |
| 94 | void MotorSafetyHelper::Check() { |
| 95 | DriverStation &ds = DriverStation::GetInstance(); |
| 96 | if (!m_enabled || ds.IsDisabled() || ds.IsTest()) return; |
| 97 | |
| 98 | std::lock_guard<priority_recursive_mutex> sync(m_syncMutex); |
| 99 | if (m_stopTime < Timer::GetFPGATimestamp()) { |
| 100 | std::ostringstream desc; |
| 101 | m_safeObject->GetDescription(desc); |
| 102 | desc << "... Output not updated often enough."; |
| 103 | wpi_setWPIErrorWithContext(Timeout, desc.str().c_str()); |
| 104 | m_safeObject->StopMotor(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Enable/disable motor safety for this device |
| 110 | * Turn on and off the motor safety option for this PWM object. |
| 111 | * @param enabled True if motor safety is enforced for this object |
| 112 | */ |
| 113 | void MotorSafetyHelper::SetSafetyEnabled(bool enabled) { |
| 114 | std::lock_guard<priority_recursive_mutex> sync(m_syncMutex); |
| 115 | m_enabled = enabled; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Return the state of the motor safety enabled flag |
| 120 | * Return if the motor safety is currently enabled for this devicce. |
| 121 | * @return True if motor safety is enforced for this device |
| 122 | */ |
| 123 | bool MotorSafetyHelper::IsSafetyEnabled() const { |
| 124 | std::lock_guard<priority_recursive_mutex> sync(m_syncMutex); |
| 125 | return m_enabled; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Check the motors to see if any have timed out. |
| 130 | * This static method is called periodically to poll all the motors and stop |
| 131 | * any that have |
| 132 | * timed out. |
| 133 | */ |
| 134 | void MotorSafetyHelper::CheckMotors() { |
| 135 | std::lock_guard<priority_recursive_mutex> sync(m_listMutex); |
| 136 | for (auto elem : m_helperList) { |
| 137 | elem->Check(); |
| 138 | } |
| 139 | } |