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