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 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "IterativeRobot.h" |
| 9 | |
| 10 | #include "DriverStation.h" |
| 11 | #include "HAL/HAL.hpp" |
| 12 | #include "SmartDashboard/SmartDashboard.h" |
| 13 | #include "LiveWindow/LiveWindow.h" |
| 14 | #include "networktables/NetworkTable.h" |
| 15 | |
| 16 | constexpr double IterativeRobot::kDefaultPeriod; |
| 17 | |
| 18 | /** |
| 19 | * Provide an alternate "main loop" via StartCompetition(). |
| 20 | * |
| 21 | * This specific StartCompetition() implements "main loop" behaviour synced with |
| 22 | * the DS packets |
| 23 | */ |
| 24 | void IterativeRobot::StartCompetition() { |
| 25 | HALReport(HALUsageReporting::kResourceType_Framework, |
| 26 | HALUsageReporting::kFramework_Iterative); |
| 27 | |
| 28 | LiveWindow *lw = LiveWindow::GetInstance(); |
| 29 | // first and one-time initialization |
| 30 | SmartDashboard::init(); |
| 31 | NetworkTable::GetTable("LiveWindow") |
| 32 | ->GetSubTable("~STATUS~") |
| 33 | ->PutBoolean("LW Enabled", false); |
| 34 | RobotInit(); |
| 35 | |
| 36 | // Tell the DS that the robot is ready to be enabled |
| 37 | HALNetworkCommunicationObserveUserProgramStarting(); |
| 38 | |
| 39 | // loop forever, calling the appropriate mode-dependent function |
| 40 | lw->SetEnabled(false); |
| 41 | while (true) { |
| 42 | // Call the appropriate function depending upon the current robot mode |
| 43 | if (IsDisabled()) { |
| 44 | // call DisabledInit() if we are now just entering disabled mode from |
| 45 | // either a different mode or from power-on |
| 46 | if (!m_disabledInitialized) { |
| 47 | lw->SetEnabled(false); |
| 48 | DisabledInit(); |
| 49 | m_disabledInitialized = true; |
| 50 | // reset the initialization flags for the other modes |
| 51 | m_autonomousInitialized = false; |
| 52 | m_teleopInitialized = false; |
| 53 | m_testInitialized = false; |
| 54 | } |
| 55 | HALNetworkCommunicationObserveUserProgramDisabled(); |
| 56 | DisabledPeriodic(); |
| 57 | } else if (IsAutonomous()) { |
| 58 | // call AutonomousInit() if we are now just entering autonomous mode from |
| 59 | // either a different mode or from power-on |
| 60 | if (!m_autonomousInitialized) { |
| 61 | lw->SetEnabled(false); |
| 62 | AutonomousInit(); |
| 63 | m_autonomousInitialized = true; |
| 64 | // reset the initialization flags for the other modes |
| 65 | m_disabledInitialized = false; |
| 66 | m_teleopInitialized = false; |
| 67 | m_testInitialized = false; |
| 68 | } |
| 69 | HALNetworkCommunicationObserveUserProgramAutonomous(); |
| 70 | AutonomousPeriodic(); |
| 71 | } else if (IsTest()) { |
| 72 | // call TestInit() if we are now just entering test mode from |
| 73 | // either a different mode or from power-on |
| 74 | if (!m_testInitialized) { |
| 75 | lw->SetEnabled(true); |
| 76 | TestInit(); |
| 77 | m_testInitialized = true; |
| 78 | // reset the initialization flags for the other modes |
| 79 | m_disabledInitialized = false; |
| 80 | m_autonomousInitialized = false; |
| 81 | m_teleopInitialized = false; |
| 82 | } |
| 83 | HALNetworkCommunicationObserveUserProgramTest(); |
| 84 | TestPeriodic(); |
| 85 | } else { |
| 86 | // call TeleopInit() if we are now just entering teleop mode from |
| 87 | // either a different mode or from power-on |
| 88 | if (!m_teleopInitialized) { |
| 89 | lw->SetEnabled(false); |
| 90 | TeleopInit(); |
| 91 | m_teleopInitialized = true; |
| 92 | // reset the initialization flags for the other modes |
| 93 | m_disabledInitialized = false; |
| 94 | m_autonomousInitialized = false; |
| 95 | m_testInitialized = false; |
| 96 | Scheduler::GetInstance()->SetEnabled(true); |
| 97 | } |
| 98 | HALNetworkCommunicationObserveUserProgramTeleop(); |
| 99 | TeleopPeriodic(); |
| 100 | } |
| 101 | // wait for driver station data so the loop doesn't hog the CPU |
| 102 | m_ds.WaitForData(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Robot-wide initialization code should go here. |
| 108 | * |
| 109 | * Users should override this method for default Robot-wide initialization which |
| 110 | * will be called when the robot is first powered on. It will be called exactly |
| 111 | * one time. |
| 112 | * |
| 113 | * Warning: the Driver Station "Robot Code" light and FMS "Robot Ready" |
| 114 | * indicators will be off until RobotInit() exits. Code in RobotInit() that |
| 115 | * waits for enable will cause the robot to never indicate that the code is |
| 116 | * ready, causing the robot to be bypassed in a match. |
| 117 | */ |
| 118 | void IterativeRobot::RobotInit() { |
| 119 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Initialization code for disabled mode should go here. |
| 124 | * |
| 125 | * Users should override this method for initialization code which will be |
| 126 | * called each time |
| 127 | * the robot enters disabled mode. |
| 128 | */ |
| 129 | void IterativeRobot::DisabledInit() { |
| 130 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Initialization code for autonomous mode should go here. |
| 135 | * |
| 136 | * Users should override this method for initialization code which will be |
| 137 | * called each time |
| 138 | * the robot enters autonomous mode. |
| 139 | */ |
| 140 | void IterativeRobot::AutonomousInit() { |
| 141 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Initialization code for teleop mode should go here. |
| 146 | * |
| 147 | * Users should override this method for initialization code which will be |
| 148 | * called each time |
| 149 | * the robot enters teleop mode. |
| 150 | */ |
| 151 | void IterativeRobot::TeleopInit() { |
| 152 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Initialization code for test mode should go here. |
| 157 | * |
| 158 | * Users should override this method for initialization code which will be |
| 159 | * called each time |
| 160 | * the robot enters test mode. |
| 161 | */ |
| 162 | void IterativeRobot::TestInit() { |
| 163 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Periodic code for disabled mode should go here. |
| 168 | * |
| 169 | * Users should override this method for code which will be called periodically |
| 170 | * at a regular |
| 171 | * rate while the robot is in disabled mode. |
| 172 | */ |
| 173 | void IterativeRobot::DisabledPeriodic() { |
| 174 | static bool firstRun = true; |
| 175 | if (firstRun) { |
| 176 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 177 | firstRun = false; |
| 178 | } |
| 179 | delayTicks(1); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Periodic code for autonomous mode should go here. |
| 184 | * |
| 185 | * Users should override this method for code which will be called periodically |
| 186 | * at a regular |
| 187 | * rate while the robot is in autonomous mode. |
| 188 | */ |
| 189 | void IterativeRobot::AutonomousPeriodic() { |
| 190 | static bool firstRun = true; |
| 191 | if (firstRun) { |
| 192 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 193 | firstRun = false; |
| 194 | } |
| 195 | delayTicks(1); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Periodic code for teleop mode should go here. |
| 200 | * |
| 201 | * Users should override this method for code which will be called periodically |
| 202 | * at a regular |
| 203 | * rate while the robot is in teleop mode. |
| 204 | */ |
| 205 | void IterativeRobot::TeleopPeriodic() { |
| 206 | static bool firstRun = true; |
| 207 | if (firstRun) { |
| 208 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 209 | firstRun = false; |
| 210 | } |
| 211 | delayTicks(1); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Periodic code for test mode should go here. |
| 216 | * |
| 217 | * Users should override this method for code which will be called periodically |
| 218 | * at a regular |
| 219 | * rate while the robot is in test mode. |
| 220 | */ |
| 221 | void IterativeRobot::TestPeriodic() { |
| 222 | static bool firstRun = true; |
| 223 | if (firstRun) { |
| 224 | printf("Default %s() method... Overload me!\n", __FUNCTION__); |
| 225 | firstRun = false; |
| 226 | } |
| 227 | delayTicks(1); |
| 228 | } |