Now you can download a copy of these docs so you can use them offline! Download now
IterativeRobot.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 "IterativeRobot.h"
8 
9 #include "DriverStation.h"
10 #include "NetworkCommunication/UsageReporting.h"
11 #include <taskLib.h>
12 #include "SmartDashboard/SmartDashboard.h"
13 #include "LiveWindow/LiveWindow.h"
14 #include "networktables/NetworkTable.h"
15 
16 constexpr double IterativeRobot::kDefaultPeriod;
17 
25  : m_disabledInitialized (false)
26  , m_autonomousInitialized (false)
27  , m_teleopInitialized (false)
28  , m_testInitialized (false)
29  , m_period (kDefaultPeriod)
30 {
31  m_watchdog.SetEnabled(false);
32 }
33 
38 {
39 }
40 
46 void IterativeRobot::SetPeriod(double period)
47 {
48  if (period > 0.0)
49  {
50  // Not syncing with the DS, so start the timer for the main loop
51  m_mainLoopTimer.Reset();
52  m_mainLoopTimer.Start();
53  }
54  else
55  {
56  // Syncing with the DS, don't need the timer
57  m_mainLoopTimer.Stop();
58  }
59  m_period = period;
60 }
61 
68 {
69  return m_period;
70 }
71 
77 {
78  // If syncing to the driver station, we don't know the rate,
79  // so guess something close.
80  if (m_period <= 0.0)
81  return 50.0;
82  return 1.0 / m_period;
83 }
84 
94 {
95  nUsageReporting::report(nUsageReporting::kResourceType_Framework, nUsageReporting::kFramework_Iterative);
96 
98  // first and one-time initialization
99  SmartDashboard::init();
100  NetworkTable::GetTable("LiveWindow")->GetSubTable("~STATUS~")->PutBoolean("LW Enabled", false);
101  RobotInit();
102 
103  // loop forever, calling the appropriate mode-dependent function
104  lw->SetEnabled(false);
105  while (true)
106  {
107  // Call the appropriate function depending upon the current robot mode
108  if (IsDisabled())
109  {
110  // call DisabledInit() if we are now just entering disabled mode from
111  // either a different mode or from power-on
112  if(!m_disabledInitialized)
113  {
114  lw->SetEnabled(false);
115  DisabledInit();
116  m_disabledInitialized = true;
117  // reset the initialization flags for the other modes
118  m_autonomousInitialized = false;
119  m_teleopInitialized = false;
120  m_testInitialized = false;
121  }
122  if (NextPeriodReady())
123  {
124  FRC_NetworkCommunication_observeUserProgramDisabled();
126  }
127  }
128  else if (IsAutonomous())
129  {
130  // call AutonomousInit() if we are now just entering autonomous mode from
131  // either a different mode or from power-on
132  if(!m_autonomousInitialized)
133  {
134  lw->SetEnabled(false);
135  AutonomousInit();
136  m_autonomousInitialized = true;
137  // reset the initialization flags for the other modes
138  m_disabledInitialized = false;
139  m_teleopInitialized = false;
140  m_testInitialized = false;
141  }
142  if (NextPeriodReady())
143  {
144  FRC_NetworkCommunication_observeUserProgramAutonomous();
146  }
147  }
148  else if (IsTest())
149  {
150  // call TestInit() if we are now just entering test mode from
151  // either a different mode or from power-on
152  if(!m_testInitialized)
153  {
154  lw->SetEnabled(true);
155  TestInit();
156  m_testInitialized = true;
157  // reset the initialization flags for the other modes
158  m_disabledInitialized = false;
159  m_autonomousInitialized = false;
160  m_teleopInitialized = false;
161  }
162  if (NextPeriodReady())
163  {
164  FRC_NetworkCommunication_observeUserProgramTest();
165  TestPeriodic();
166  }
167  }
168  else
169  {
170  // call TeleopInit() if we are now just entering teleop mode from
171  // either a different mode or from power-on
172  if(!m_teleopInitialized)
173  {
174  lw->SetEnabled(false);
175  TeleopInit();
176  m_teleopInitialized = true;
177  // reset the initialization flags for the other modes
178  m_disabledInitialized = false;
179  m_autonomousInitialized = false;
180  m_testInitialized = false;
181  Scheduler::GetInstance()->SetEnabled(true);
182  }
183  if (NextPeriodReady())
184  {
185  FRC_NetworkCommunication_observeUserProgramTeleop();
186  TeleopPeriodic();
187  }
188  }
189  // wait for driver station data so the loop doesn't hog the CPU
190  m_ds->WaitForData();
191  }
192 }
193 
204 bool IterativeRobot::NextPeriodReady()
205 {
206  if (m_period > 0.0)
207  {
208  return m_mainLoopTimer.HasPeriodPassed(m_period);
209  }
210  else
211  {
212  return m_ds->IsNewControlData();
213  }
214 }
215 
223 {
224  printf("Default %s() method... Overload me!\n", __FUNCTION__);
225 }
226 
234 {
235  printf("Default %s() method... Overload me!\n", __FUNCTION__);
236 }
237 
245 {
246  printf("Default %s() method... Overload me!\n", __FUNCTION__);
247 }
248 
256 {
257  printf("Default %s() method... Overload me!\n", __FUNCTION__);
258 }
259 
267 {
268  printf("Default %s() method... Overload me!\n", __FUNCTION__);
269 }
270 
278 {
279  static bool firstRun = true;
280  if (firstRun)
281  {
282  printf("Default %s() method... Overload me!\n", __FUNCTION__);
283  firstRun = false;
284  }
285  taskDelay(1);
286 }
287 
295 {
296  static bool firstRun = true;
297  if (firstRun)
298  {
299  printf("Default %s() method... Overload me!\n", __FUNCTION__);
300  firstRun = false;
301  }
302  taskDelay(1);
303 }
304 
312 {
313  static bool firstRun = true;
314  if (firstRun)
315  {
316  printf("Default %s() method... Overload me!\n", __FUNCTION__);
317  firstRun = false;
318  }
319  taskDelay(1);
320 }
321 
329 {
330  static bool firstRun = true;
331  if (firstRun)
332  {
333  printf("Default %s() method... Overload me!\n", __FUNCTION__);
334  firstRun = false;
335  }
336  taskDelay(1);
337 }
338 
NetworkTable * GetSubTable(std::string key)
virtual void DisabledPeriodic()
bool IsNewControlData()
bool IsAutonomous()
Definition: RobotBase.cpp:106
void SetEnabled(bool enabled)
Definition: LiveWindow.cpp:38
virtual void AutonomousInit()
static NetworkTable * GetTable(std::string key)
double GetLoopsPerSec()
void PutBoolean(std::string key, bool value)
bool HasPeriodPassed(double period)
Definition: Timer.cpp:158
bool IsTest()
Definition: RobotBase.cpp:124
virtual void DisabledInit()
virtual void StartCompetition()
void Reset()
Definition: Timer.cpp:110
void Stop()
Definition: Timer.cpp:138
static LiveWindow * GetInstance()
Definition: LiveWindow.cpp:13
void SetPeriod(double period)
virtual void AutonomousPeriodic()
virtual void TestPeriodic()
bool IsDisabled()
Definition: RobotBase.cpp:97
void SetEnabled(bool enabled)
Definition: Watchdog.cpp:131
virtual ~IterativeRobot()
virtual void TeleopPeriodic()
virtual void TestInit()
void Start()
Definition: Timer.cpp:122
virtual void TeleopInit()
virtual void RobotInit()
static Scheduler * GetInstance()
Definition: Scheduler.cpp:47

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