Now you can download a copy of these docs so you can use them offline! Download now
MotorSafetyHelper.cpp
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 #include "MotorSafetyHelper.h"
8 
9 #include "DriverStation.h"
10 #include "MotorSafety.h"
11 #include "Timer.h"
12 #include "WPIErrors.h"
13 
14 #include <stdio.h>
15 
16 MotorSafetyHelper *MotorSafetyHelper::m_headHelper = NULL;
17 ReentrantSemaphore MotorSafetyHelper::m_listMutex;
18 
29 {
30  m_safeObject = safeObject;
31  m_enabled = false;
32  m_expiration = DEFAULT_SAFETY_EXPIRATION;
33  m_stopTime = Timer::GetFPGATimestamp();
34 
35  Synchronized sync(m_listMutex);
36  m_nextHelper = m_headHelper;
37  m_headHelper = this;
38 }
39 
40 
41 MotorSafetyHelper::~MotorSafetyHelper()
42 {
43  Synchronized sync(m_listMutex);
44  if (m_headHelper == this)
45  {
46  m_headHelper = m_nextHelper;
47  }
48  else
49  {
50  MotorSafetyHelper *prev = NULL;
51  MotorSafetyHelper *cur = m_headHelper;
52  while (cur != this && cur != NULL)
53  prev = cur, cur = cur->m_nextHelper;
54  if (cur == this)
55  prev->m_nextHelper = cur->m_nextHelper;
56  }
57 }
58 
59 /*
60  * Feed the motor safety object.
61  * Resets the timer on this object that is used to do the timeouts.
62  */
63 void MotorSafetyHelper::Feed()
64 {
65  Synchronized sync(m_syncMutex);
66  m_stopTime = Timer::GetFPGATimestamp() + m_expiration;
67 }
68 
69 /*
70  * Set the expiration time for the corresponding motor safety object.
71  * @param expirationTime The timeout value in seconds.
72  */
73 void MotorSafetyHelper::SetExpiration(float expirationTime)
74 {
75  Synchronized sync(m_syncMutex);
76  m_expiration = expirationTime;
77 }
78 
84 {
85  Synchronized sync(m_syncMutex);
86  return m_expiration;
87 }
88 
94 {
95  Synchronized sync(m_syncMutex);
96  return !m_enabled || m_stopTime > Timer::GetFPGATimestamp();
97 }
98 
106 {
108  if (!m_enabled || ds->IsDisabled() || ds->IsTest()) return;
109 
110  Synchronized sync(m_syncMutex);
111  if (m_stopTime < Timer::GetFPGATimestamp())
112  {
113  char buf[128];
114  char desc[64];
115  m_safeObject->GetDescription(desc);
116  snprintf(buf, 128, "%s... Output not updated often enough.", desc);
117  wpi_setWPIErrorWithContext(Timeout, buf);
118  m_safeObject->StopMotor();
119  }
120 }
121 
128 {
129  Synchronized sync(m_syncMutex);
130  m_enabled = enabled;
131 }
132 
139 {
140  Synchronized sync(m_syncMutex);
141  return m_enabled;
142 }
143 
150 {
151  Synchronized sync(m_listMutex);
152  for (MotorSafetyHelper *msh = m_headHelper; msh != NULL; msh = msh->m_nextHelper)
153  {
154  msh->Check();
155  }
156 }
MotorSafetyHelper(MotorSafety *safeObject)
void SetSafetyEnabled(bool enabled)
static DriverStation * GetInstance()
static void CheckMotors()

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6