Now you can download a copy of these docs so you can use them offline! Download now
IterativeRobot.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 "IterativeRobot.h" 00008 00009 #include "DriverStation.h" 00010 #include <taskLib.h> 00011 00012 const double IterativeRobot::kDefaultPeriod; 00013 00020 IterativeRobot::IterativeRobot() 00021 : m_disabledInitialized (false) 00022 , m_autonomousInitialized (false) 00023 , m_teleopInitialized (false) 00024 , m_period (kDefaultPeriod) 00025 { 00026 m_watchdog.SetEnabled(false); 00027 } 00028 00032 IterativeRobot::~IterativeRobot() 00033 { 00034 } 00035 00041 void IterativeRobot::SetPeriod(double period) 00042 { 00043 if (period > 0.0) 00044 { 00045 // Not syncing with the DS, so start the timer for the main loop 00046 m_mainLoopTimer.Reset(); 00047 m_mainLoopTimer.Start(); 00048 } 00049 else 00050 { 00051 // Syncing with the DS, don't need the timer 00052 m_mainLoopTimer.Stop(); 00053 } 00054 m_period = period; 00055 } 00056 00062 double IterativeRobot::GetPeriod() 00063 { 00064 return m_period; 00065 } 00066 00071 double IterativeRobot::GetLoopsPerSec() 00072 { 00073 // If syncing to the driver station, we don't know the rate, 00074 // so guess something close. 00075 if (m_period <= 0.0) 00076 return 50.0; 00077 return 1.0 / m_period; 00078 } 00079 00088 void IterativeRobot::StartCompetition() 00089 { 00090 // first and one-time initialization 00091 RobotInit(); 00092 00093 // loop forever, calling the appropriate mode-dependent function 00094 while (true) 00095 { 00096 // Call the appropriate function depending upon the current robot mode 00097 if (IsDisabled()) 00098 { 00099 // call DisabledInit() if we are now just entering disabled mode from 00100 // either a different mode or from power-on 00101 if(!m_disabledInitialized) 00102 { 00103 DisabledInit(); 00104 m_disabledInitialized = true; 00105 // reset the initialization flags for the other modes 00106 m_autonomousInitialized = false; 00107 m_teleopInitialized = false; 00108 } 00109 if (NextPeriodReady()) 00110 { 00111 FRC_NetworkCommunication_observeUserProgramDisabled(); 00112 DisabledPeriodic(); 00113 } 00114 DisabledContinuous(); 00115 } 00116 else if (IsAutonomous()) 00117 { 00118 // call AutonomousInit() if we are now just entering autonomous mode from 00119 // either a different mode or from power-on 00120 if(!m_autonomousInitialized) 00121 { 00122 AutonomousInit(); 00123 m_autonomousInitialized = true; 00124 // reset the initialization flags for the other modes 00125 m_disabledInitialized = false; 00126 m_teleopInitialized = false; 00127 } 00128 if (NextPeriodReady()) 00129 { 00130 FRC_NetworkCommunication_observeUserProgramAutonomous(); 00131 AutonomousPeriodic(); 00132 } 00133 AutonomousContinuous(); 00134 } 00135 else 00136 { 00137 // call TeleopInit() if we are now just entering teleop mode from 00138 // either a different mode or from power-on 00139 if(!m_teleopInitialized) 00140 { 00141 TeleopInit(); 00142 m_teleopInitialized = true; 00143 // reset the initialization flags for the other modes 00144 m_disabledInitialized = false; 00145 m_autonomousInitialized = false; 00146 } 00147 if (NextPeriodReady()) 00148 { 00149 FRC_NetworkCommunication_observeUserProgramTeleop(); 00150 TeleopPeriodic(); 00151 } 00152 TeleopContinuous(); 00153 } 00154 } 00155 } 00156 00167 bool IterativeRobot::NextPeriodReady() 00168 { 00169 if (m_period > 0.0) 00170 { 00171 return m_mainLoopTimer.HasPeriodPassed(m_period); 00172 } 00173 else 00174 { 00175 return m_ds->IsNewControlData(); 00176 } 00177 } 00178 00185 void IterativeRobot::RobotInit() 00186 { 00187 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00188 } 00189 00196 void IterativeRobot::DisabledInit() 00197 { 00198 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00199 } 00200 00207 void IterativeRobot::AutonomousInit() 00208 { 00209 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00210 } 00211 00218 void IterativeRobot::TeleopInit() 00219 { 00220 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00221 } 00222 00229 void IterativeRobot::DisabledPeriodic() 00230 { 00231 static bool firstRun = true; 00232 if (firstRun) 00233 { 00234 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00235 firstRun = false; 00236 } 00237 taskDelay(1); 00238 } 00239 00246 void IterativeRobot::AutonomousPeriodic() 00247 { 00248 static bool firstRun = true; 00249 if (firstRun) 00250 { 00251 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00252 firstRun = false; 00253 } 00254 taskDelay(1); 00255 } 00256 00263 void IterativeRobot::TeleopPeriodic() 00264 { 00265 static bool firstRun = true; 00266 if (firstRun) 00267 { 00268 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00269 firstRun = false; 00270 } 00271 taskDelay(1); 00272 } 00273 00280 void IterativeRobot::DisabledContinuous() 00281 { 00282 static bool firstRun = true; 00283 if (firstRun) 00284 { 00285 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00286 firstRun = false; 00287 } 00288 m_ds->WaitForData(); 00289 } 00290 00297 void IterativeRobot::AutonomousContinuous() 00298 { 00299 static bool firstRun = true; 00300 if (firstRun) 00301 { 00302 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00303 firstRun = false; 00304 } 00305 m_ds->WaitForData(); 00306 } 00307 00314 void IterativeRobot::TeleopContinuous() 00315 { 00316 static bool firstRun = true; 00317 if (firstRun) 00318 { 00319 printf("Default %s() method... Overload me!\n", __FUNCTION__); 00320 firstRun = false; 00321 } 00322 m_ds->WaitForData(); 00323 }
Generated on Thu Jan 12 2012 22:35:20 for WPILibC++ by
1.7.1