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 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 7 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 8 | #include "IterativeRobot.h" |
| 9 | |
| 10 | #include "DriverStation.h" |
| 11 | #include "SmartDashboard/SmartDashboard.h" |
| 12 | #include "LiveWindow/LiveWindow.h" |
| 13 | #include "networktables/NetworkTable.h" |
| 14 | |
| 15 | //not sure what this is used for yet. |
| 16 | #ifdef _UNIX |
| 17 | #include <unistd.h> |
| 18 | #endif |
| 19 | |
| 20 | const double IterativeRobot::kDefaultPeriod = 0; |
| 21 | |
| 22 | /** |
| 23 | * Set the period for the periodic functions. |
| 24 | * |
| 25 | * @param period The period of the periodic function calls. 0.0 means sync to driver station control data. |
| 26 | */ |
| 27 | void IterativeRobot::SetPeriod(double period) |
| 28 | { |
| 29 | if (period > 0.0) |
| 30 | { |
| 31 | // Not syncing with the DS, so start the timer for the main loop |
| 32 | m_mainLoopTimer.Reset(); |
| 33 | m_mainLoopTimer.Start(); |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | // Syncing with the DS, don't need the timer |
| 38 | m_mainLoopTimer.Stop(); |
| 39 | } |
| 40 | m_period = period; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the period for the periodic functions. |
| 45 | * Returns 0.0 if configured to syncronize with DS control data packets. |
| 46 | * @return Period of the periodic function calls |
| 47 | */ |
| 48 | double IterativeRobot::GetPeriod() |
| 49 | { |
| 50 | return m_period; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the number of loops per second for the IterativeRobot |
| 55 | * @return Frequency of the periodic function calls |
| 56 | */ |
| 57 | double IterativeRobot::GetLoopsPerSec() |
| 58 | { |
| 59 | // If syncing to the driver station, we don't know the rate, |
| 60 | // so guess something close. |
| 61 | if (m_period <= 0.0) |
| 62 | return 50.0; |
| 63 | return 1.0 / m_period; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Provide an alternate "main loop" via StartCompetition(). |
| 68 | * |
| 69 | * This specific StartCompetition() implements "main loop" behavior like that of the FRC |
| 70 | * control system in 2008 and earlier, with a primary (slow) loop that is |
| 71 | * called periodically, and a "fast loop" (a.k.a. "spin loop") that is |
| 72 | * called as fast as possible with no delay between calls. |
| 73 | */ |
| 74 | void IterativeRobot::StartCompetition() |
| 75 | { |
| 76 | LiveWindow *lw = LiveWindow::GetInstance(); |
| 77 | // first and one-time initialization |
| 78 | SmartDashboard::init(); |
| 79 | NetworkTable::GetTable("LiveWindow")->GetSubTable("~STATUS~")->PutBoolean("LW Enabled", false); |
| 80 | RobotInit(); |
| 81 | |
| 82 | // loop forever, calling the appropriate mode-dependent function |
| 83 | lw->SetEnabled(false); |
| 84 | while (true) |
| 85 | { |
| 86 | // Call the appropriate function depending upon the current robot mode |
| 87 | if (IsDisabled()) |
| 88 | { |
| 89 | // call DisabledInit() if we are now just entering disabled mode from |
| 90 | // either a different mode or from power-on |
| 91 | if(!m_disabledInitialized) |
| 92 | { |
| 93 | lw->SetEnabled(false); |
| 94 | DisabledInit(); |
| 95 | m_disabledInitialized = true; |
| 96 | // reset the initialization flags for the other modes |
| 97 | m_autonomousInitialized = false; |
| 98 | m_teleopInitialized = false; |
| 99 | m_testInitialized = false; |
| 100 | } |
| 101 | if (NextPeriodReady()) |
| 102 | { |
| 103 | // TODO: HALNetworkCommunicationObserveUserProgramDisabled(); |
| 104 | DisabledPeriodic(); |
| 105 | } |
| 106 | } |
| 107 | else if (IsAutonomous()) |
| 108 | { |
| 109 | // call AutonomousInit() if we are now just entering autonomous mode from |
| 110 | // either a different mode or from power-on |
| 111 | if(!m_autonomousInitialized) |
| 112 | { |
| 113 | lw->SetEnabled(false); |
| 114 | AutonomousInit(); |
| 115 | m_autonomousInitialized = true; |
| 116 | // reset the initialization flags for the other modes |
| 117 | m_disabledInitialized = false; |
| 118 | m_teleopInitialized = false; |
| 119 | m_testInitialized = false; |
| 120 | } |
| 121 | if (NextPeriodReady()) |
| 122 | { |
| 123 | // TODO: HALNetworkCommunicationObserveUserProgramAutonomous(); |
| 124 | AutonomousPeriodic(); |
| 125 | } |
| 126 | } |
| 127 | else if (IsTest()) |
| 128 | { |
| 129 | // call TestInit() if we are now just entering test mode from |
| 130 | // either a different mode or from power-on |
| 131 | if(!m_testInitialized) |
| 132 | { |
| 133 | lw->SetEnabled(true); |
| 134 | TestInit(); |
| 135 | m_testInitialized = true; |
| 136 | // reset the initialization flags for the other modes |
| 137 | m_disabledInitialized = false; |
| 138 | m_autonomousInitialized = false; |
| 139 | m_teleopInitialized = false; |
| 140 | } |
| 141 | if (NextPeriodReady()) |
| 142 | { |
| 143 | // TODO: HALNetworkCommunicationObserveUserProgramTest(); |
| 144 | TestPeriodic(); |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | // call TeleopInit() if we are now just entering teleop mode from |
| 150 | // either a different mode or from power-on |
| 151 | if(!m_teleopInitialized) |
| 152 | { |
| 153 | lw->SetEnabled(false); |
| 154 | TeleopInit(); |
| 155 | m_teleopInitialized = true; |
| 156 | // reset the initialization flags for the other modes |
| 157 | m_disabledInitialized = false; |
| 158 | m_autonomousInitialized = false; |
| 159 | m_testInitialized = false; |
| 160 | Scheduler::GetInstance()->SetEnabled(true); |
| 161 | } |
| 162 | if (NextPeriodReady()) |
| 163 | { |
| 164 | // TODO: HALNetworkCommunicationObserveUserProgramTeleop(); |
| 165 | TeleopPeriodic(); |
| 166 | } |
| 167 | } |
| 168 | // wait for driver station data so the loop doesn't hog the CPU |
| 169 | m_ds.WaitForData(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Determine if the periodic functions should be called. |
| 175 | * |
| 176 | * If m_period > 0.0, call the periodic function every m_period as compared |
| 177 | * to Timer.Get(). If m_period == 0.0, call the periodic functions whenever |
| 178 | * a packet is received from the Driver Station, or about every 20ms. |
| 179 | * |
| 180 | * @todo Decide what this should do if it slips more than one cycle. |
| 181 | */ |
| 182 | |
| 183 | bool IterativeRobot::NextPeriodReady() |
| 184 | { |
| 185 | if (m_period > 0.0) |
| 186 | { |
| 187 | return m_mainLoopTimer.HasPeriodPassed(m_period); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | // XXX: BROKEN! return m_ds->IsNewControlData(); |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Robot-wide initialization code should go here. |
| 198 | * |
| 199 | * Users should override this method for default Robot-wide initialization which will |
| 200 | * be called when the robot is first powered on. It will be called exactly 1 time. |
| 201 | */ |
| 202 | void IterativeRobot::RobotInit() |
| 203 | { |
| 204 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Initialization code for disabled mode should go here. |
| 209 | * |
| 210 | * Users should override this method for initialization code which will be called each time |
| 211 | * the robot enters disabled mode. |
| 212 | */ |
| 213 | void IterativeRobot::DisabledInit() |
| 214 | { |
| 215 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Initialization code for autonomous mode should go here. |
| 220 | * |
| 221 | * Users should override this method for initialization code which will be called each time |
| 222 | * the robot enters autonomous mode. |
| 223 | */ |
| 224 | void IterativeRobot::AutonomousInit() |
| 225 | { |
| 226 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Initialization code for teleop mode should go here. |
| 231 | * |
| 232 | * Users should override this method for initialization code which will be called each time |
| 233 | * the robot enters teleop mode. |
| 234 | */ |
| 235 | void IterativeRobot::TeleopInit() |
| 236 | { |
| 237 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Initialization code for test mode should go here. |
| 242 | * |
| 243 | * Users should override this method for initialization code which will be called each time |
| 244 | * the robot enters test mode. |
| 245 | */ |
| 246 | void IterativeRobot::TestInit() |
| 247 | { |
| 248 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Periodic code for disabled mode should go here. |
| 253 | * |
| 254 | * Users should override this method for code which will be called periodically at a regular |
| 255 | * rate while the robot is in disabled mode. |
| 256 | */ |
| 257 | void IterativeRobot::DisabledPeriodic() |
| 258 | { |
| 259 | static bool firstRun = true; |
| 260 | if (firstRun) |
| 261 | { |
| 262 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 263 | firstRun = false; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Periodic code for autonomous mode should go here. |
| 269 | * |
| 270 | * Users should override this method for code which will be called periodically at a regular |
| 271 | * rate while the robot is in autonomous mode. |
| 272 | */ |
| 273 | void IterativeRobot::AutonomousPeriodic() |
| 274 | { |
| 275 | static bool firstRun = true; |
| 276 | if (firstRun) |
| 277 | { |
| 278 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 279 | firstRun = false; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Periodic code for teleop mode should go here. |
| 285 | * |
| 286 | * Users should override this method for code which will be called periodically at a regular |
| 287 | * rate while the robot is in teleop mode. |
| 288 | */ |
| 289 | void IterativeRobot::TeleopPeriodic() |
| 290 | { |
| 291 | static bool firstRun = true; |
| 292 | if (firstRun) |
| 293 | { |
| 294 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 295 | firstRun = false; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Periodic code for test mode should go here. |
| 301 | * |
| 302 | * Users should override this method for code which will be called periodically at a regular |
| 303 | * rate while the robot is in test mode. |
| 304 | */ |
| 305 | void IterativeRobot::TestPeriodic() |
| 306 | { |
| 307 | static bool firstRun = true; |
| 308 | if (firstRun) |
| 309 | { |
| 310 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 311 | firstRun = false; |
| 312 | } |
| 313 | } |
| 314 | |