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