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