Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/IterativeRobotBase.h" |
| 6 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 7 | #include <frc/DriverStation.h> |
| 8 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 9 | #include <fmt/format.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include <hal/DriverStation.h> |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 11 | #include <hal/FRCUsageReporting.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 12 | #include <networktables/NetworkTableInstance.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 14 | #include "frc/DSControlWord.h" |
| 15 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | #include "frc/livewindow/LiveWindow.h" |
| 17 | #include "frc/shuffleboard/Shuffleboard.h" |
| 18 | #include "frc/smartdashboard/SmartDashboard.h" |
| 19 | |
| 20 | using namespace frc; |
| 21 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 22 | IterativeRobotBase::IterativeRobotBase(units::second_t period) |
| 23 | : m_period(period), |
| 24 | m_watchdog(period, [this] { PrintLoopOverrunMessage(); }) {} |
| 25 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 26 | void IterativeRobotBase::RobotInit() {} |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 28 | void IterativeRobotBase::DriverStationConnected() {} |
| 29 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 30 | void IterativeRobotBase::SimulationInit() {} |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 32 | void IterativeRobotBase::DisabledInit() {} |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 34 | void IterativeRobotBase::AutonomousInit() {} |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 36 | void IterativeRobotBase::TeleopInit() {} |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 38 | void IterativeRobotBase::TestInit() {} |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 39 | |
| 40 | void IterativeRobotBase::RobotPeriodic() { |
| 41 | static bool firstRun = true; |
| 42 | if (firstRun) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 43 | fmt::print("Default {}() method... Override me!\n", __FUNCTION__); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 44 | firstRun = false; |
| 45 | } |
| 46 | } |
| 47 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 48 | void IterativeRobotBase::SimulationPeriodic() { |
| 49 | static bool firstRun = true; |
| 50 | if (firstRun) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 51 | fmt::print("Default {}() method... Override me!\n", __FUNCTION__); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 52 | firstRun = false; |
| 53 | } |
| 54 | } |
| 55 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 56 | void IterativeRobotBase::DisabledPeriodic() { |
| 57 | static bool firstRun = true; |
| 58 | if (firstRun) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 59 | fmt::print("Default {}() method... Override me!\n", __FUNCTION__); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 60 | firstRun = false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void IterativeRobotBase::AutonomousPeriodic() { |
| 65 | static bool firstRun = true; |
| 66 | if (firstRun) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 67 | fmt::print("Default {}() method... Override me!\n", __FUNCTION__); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 68 | firstRun = false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void IterativeRobotBase::TeleopPeriodic() { |
| 73 | static bool firstRun = true; |
| 74 | if (firstRun) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 75 | fmt::print("Default {}() method... Override me!\n", __FUNCTION__); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 76 | firstRun = false; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void IterativeRobotBase::TestPeriodic() { |
| 81 | static bool firstRun = true; |
| 82 | if (firstRun) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 83 | fmt::print("Default {}() method... Override me!\n", __FUNCTION__); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 84 | firstRun = false; |
| 85 | } |
| 86 | } |
| 87 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 88 | void IterativeRobotBase::DisabledExit() {} |
| 89 | |
| 90 | void IterativeRobotBase::AutonomousExit() {} |
| 91 | |
| 92 | void IterativeRobotBase::TeleopExit() {} |
| 93 | |
| 94 | void IterativeRobotBase::TestExit() {} |
| 95 | |
| 96 | void IterativeRobotBase::SetNetworkTablesFlushEnabled(bool enabled) { |
| 97 | m_ntFlushEnabled = enabled; |
| 98 | } |
| 99 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 100 | void IterativeRobotBase::EnableLiveWindowInTest(bool testLW) { |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 101 | static bool hasReported; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 102 | if (IsTestEnabled()) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 103 | throw FRC_MakeError(err::IncompatibleMode, |
| 104 | "Can't configure test mode while in test mode!"); |
| 105 | } |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 106 | if (!hasReported && testLW) { |
| 107 | HAL_Report(HALUsageReporting::kResourceType_SmartDashboard, |
| 108 | HALUsageReporting::kSmartDashboard_LiveWindow); |
| 109 | hasReported = true; |
| 110 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 111 | m_lwEnabledInTest = testLW; |
| 112 | } |
| 113 | |
| 114 | bool IterativeRobotBase::IsLiveWindowEnabledInTest() { |
| 115 | return m_lwEnabledInTest; |
| 116 | } |
| 117 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 118 | units::second_t IterativeRobotBase::GetPeriod() const { |
| 119 | return m_period; |
| 120 | } |
| 121 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 122 | void IterativeRobotBase::LoopFunc() { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 123 | DriverStation::RefreshData(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | m_watchdog.Reset(); |
| 125 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 126 | // Get current mode |
| 127 | DSControlWord word; |
| 128 | Mode mode = Mode::kNone; |
| 129 | if (word.IsDisabled()) { |
| 130 | mode = Mode::kDisabled; |
| 131 | } else if (word.IsAutonomous()) { |
| 132 | mode = Mode::kAutonomous; |
| 133 | } else if (word.IsTeleop()) { |
| 134 | mode = Mode::kTeleop; |
| 135 | } else if (word.IsTest()) { |
| 136 | mode = Mode::kTest; |
| 137 | } |
| 138 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame] | 139 | if (!m_calledDsConnected && word.IsDSAttached()) { |
| 140 | m_calledDsConnected = true; |
| 141 | DriverStationConnected(); |
| 142 | } |
| 143 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 144 | // If mode changed, call mode exit and entry functions |
| 145 | if (m_lastMode != mode) { |
| 146 | // Call last mode's exit function |
| 147 | if (m_lastMode == Mode::kDisabled) { |
| 148 | DisabledExit(); |
| 149 | } else if (m_lastMode == Mode::kAutonomous) { |
| 150 | AutonomousExit(); |
| 151 | } else if (m_lastMode == Mode::kTeleop) { |
| 152 | TeleopExit(); |
| 153 | } else if (m_lastMode == Mode::kTest) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 154 | if (m_lwEnabledInTest) { |
| 155 | LiveWindow::SetEnabled(false); |
| 156 | Shuffleboard::DisableActuatorWidgets(); |
| 157 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 158 | TestExit(); |
| 159 | } |
| 160 | |
| 161 | // Call current mode's entry function |
| 162 | if (mode == Mode::kDisabled) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 163 | DisabledInit(); |
| 164 | m_watchdog.AddEpoch("DisabledInit()"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 165 | } else if (mode == Mode::kAutonomous) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 166 | AutonomousInit(); |
| 167 | m_watchdog.AddEpoch("AutonomousInit()"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 168 | } else if (mode == Mode::kTeleop) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 169 | TeleopInit(); |
| 170 | m_watchdog.AddEpoch("TeleopInit()"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 171 | } else if (mode == Mode::kTest) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 172 | if (m_lwEnabledInTest) { |
| 173 | LiveWindow::SetEnabled(true); |
| 174 | Shuffleboard::EnableActuatorWidgets(); |
| 175 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 176 | TestInit(); |
| 177 | m_watchdog.AddEpoch("TestInit()"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 180 | m_lastMode = mode; |
| 181 | } |
| 182 | |
| 183 | // Call the appropriate function depending upon the current robot mode |
| 184 | if (mode == Mode::kDisabled) { |
| 185 | HAL_ObserveUserProgramDisabled(); |
| 186 | DisabledPeriodic(); |
| 187 | m_watchdog.AddEpoch("DisabledPeriodic()"); |
| 188 | } else if (mode == Mode::kAutonomous) { |
| 189 | HAL_ObserveUserProgramAutonomous(); |
| 190 | AutonomousPeriodic(); |
| 191 | m_watchdog.AddEpoch("AutonomousPeriodic()"); |
| 192 | } else if (mode == Mode::kTeleop) { |
| 193 | HAL_ObserveUserProgramTeleop(); |
| 194 | TeleopPeriodic(); |
| 195 | m_watchdog.AddEpoch("TeleopPeriodic()"); |
| 196 | } else if (mode == Mode::kTest) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 197 | HAL_ObserveUserProgramTest(); |
| 198 | TestPeriodic(); |
| 199 | m_watchdog.AddEpoch("TestPeriodic()"); |
| 200 | } |
| 201 | |
| 202 | RobotPeriodic(); |
| 203 | m_watchdog.AddEpoch("RobotPeriodic()"); |
| 204 | |
| 205 | SmartDashboard::UpdateValues(); |
| 206 | m_watchdog.AddEpoch("SmartDashboard::UpdateValues()"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 207 | LiveWindow::UpdateValues(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 208 | m_watchdog.AddEpoch("LiveWindow::UpdateValues()"); |
| 209 | Shuffleboard::Update(); |
| 210 | m_watchdog.AddEpoch("Shuffleboard::Update()"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 211 | |
| 212 | if constexpr (IsSimulation()) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 213 | HAL_SimPeriodicBefore(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 214 | SimulationPeriodic(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 215 | HAL_SimPeriodicAfter(); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 216 | m_watchdog.AddEpoch("SimulationPeriodic()"); |
| 217 | } |
| 218 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 219 | m_watchdog.Disable(); |
| 220 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 221 | // Flush NetworkTables |
| 222 | if (m_ntFlushEnabled) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 223 | nt::NetworkTableInstance::GetDefault().FlushLocal(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 226 | // Warn on loop time overruns |
| 227 | if (m_watchdog.IsExpired()) { |
| 228 | m_watchdog.PrintEpochs(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void IterativeRobotBase::PrintLoopOverrunMessage() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 233 | FRC_ReportError(err::Error, "Loop time of {:.6f}s overrun", m_period.value()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 234 | } |