Now you can download a copy of these docs so you can use them offline! Download now
MotorSafetyHelper.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "MotorSafetyHelper.h" 00008 00009 #include "DriverStation.h" 00010 #include "MotorSafety.h" 00011 #include "Synchronized.h" 00012 #include "Timer.h" 00013 #include "WPIErrors.h" 00014 00015 #include <stdio.h> 00016 00017 MotorSafetyHelper *MotorSafetyHelper::m_headHelper = NULL; 00018 SEM_ID MotorSafetyHelper::m_listMutex = NULL; 00019 00029 MotorSafetyHelper::MotorSafetyHelper(MotorSafety *safeObject) 00030 { 00031 if (m_listMutex == NULL) 00032 m_listMutex = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE); 00033 m_safeObject = safeObject; 00034 m_enabled = false; 00035 m_expiration = DEFAULT_SAFETY_EXPIRATION; 00036 m_stopTime = Timer::GetFPGATimestamp(); 00037 00038 Synchronized sync(m_listMutex); 00039 m_nextHelper = m_headHelper; 00040 m_headHelper = this; 00041 } 00042 00043 00044 MotorSafetyHelper::~MotorSafetyHelper() 00045 { 00046 semTake(m_listMutex, WAIT_FOREVER); 00047 if (m_headHelper == this) 00048 { 00049 m_headHelper = m_nextHelper; 00050 } 00051 else 00052 { 00053 MotorSafetyHelper *prev = NULL; 00054 MotorSafetyHelper *cur = m_headHelper; 00055 while (cur != this && cur != NULL) 00056 prev = cur, cur = cur->m_nextHelper; 00057 if (cur == this) 00058 prev->m_nextHelper = cur->m_nextHelper; 00059 } 00060 if (m_headHelper == NULL) 00061 { 00062 semDelete(m_listMutex); 00063 m_listMutex = NULL; 00064 } 00065 else 00066 { 00067 semGive(m_listMutex); 00068 } 00069 } 00070 00071 /* 00072 * Feed the motor safety object. 00073 * Resets the timer on this object that is used to do the timeouts. 00074 */ 00075 void MotorSafetyHelper::Feed() 00076 { 00077 m_stopTime = Timer::GetFPGATimestamp() + m_expiration; 00078 } 00079 00080 /* 00081 * Set the expiration time for the corresponding motor safety object. 00082 * @param expirationTime The timeout value in seconds. 00083 */ 00084 void MotorSafetyHelper::SetExpiration(float expirationTime) 00085 { 00086 m_expiration = expirationTime; 00087 } 00088 00093 float MotorSafetyHelper::GetExpiration() 00094 { 00095 return m_expiration; 00096 } 00097 00102 bool MotorSafetyHelper::IsAlive() 00103 { 00104 return !m_enabled || m_stopTime > Timer::GetFPGATimestamp(); 00105 } 00106 00113 void MotorSafetyHelper::Check() 00114 { 00115 if (!m_enabled) return; 00116 if (DriverStation::GetInstance()->IsDisabled()) return; 00117 if (m_stopTime < Timer::GetFPGATimestamp()) 00118 { 00119 char buf[128]; 00120 char desc[64]; 00121 m_safeObject->GetDescription(desc); 00122 snprintf(buf, 128, "%s... Output not updated often enough.", desc); 00123 wpi_setWPIErrorWithContext(Timeout, buf); 00124 m_safeObject->StopMotor(); 00125 } 00126 } 00127 00133 void MotorSafetyHelper::SetSafetyEnabled(bool enabled) 00134 { 00135 m_enabled = enabled; 00136 } 00137 00143 bool MotorSafetyHelper::IsSafetyEnabled() 00144 { 00145 return m_enabled; 00146 } 00147 00153 //TODO: these should be synchronized with the setting methods in case it's called from a different thread 00154 void MotorSafetyHelper::CheckMotors() 00155 { 00156 Synchronized sync(m_listMutex); 00157 for (MotorSafetyHelper *msh = m_headHelper; msh != NULL; msh = msh->m_nextHelper) 00158 { 00159 msh->Check(); 00160 } 00161 }
Generated on Thu Jan 12 2012 22:35:21 for WPILibC++ by
1.7.1