Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */ |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/IterativeRobotBase.h" |
| 9 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include <hal/DriverStation.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #include <wpi/Format.h> |
| 12 | #include <wpi/SmallString.h> |
| 13 | #include <wpi/raw_ostream.h> |
| 14 | |
| 15 | #include "frc/DriverStation.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 | |
| 22 | IterativeRobotBase::IterativeRobotBase(double period) |
| 23 | : IterativeRobotBase(units::second_t(period)) {} |
| 24 | |
| 25 | IterativeRobotBase::IterativeRobotBase(units::second_t period) |
| 26 | : m_period(period), |
| 27 | m_watchdog(period, [this] { PrintLoopOverrunMessage(); }) {} |
| 28 | |
| 29 | void IterativeRobotBase::RobotInit() { |
| 30 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 31 | } |
| 32 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 33 | void IterativeRobotBase::SimulationInit() { |
| 34 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 35 | } |
| 36 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 37 | void IterativeRobotBase::DisabledInit() { |
| 38 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 39 | } |
| 40 | |
| 41 | void IterativeRobotBase::AutonomousInit() { |
| 42 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 43 | } |
| 44 | |
| 45 | void IterativeRobotBase::TeleopInit() { |
| 46 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 47 | } |
| 48 | |
| 49 | void IterativeRobotBase::TestInit() { |
| 50 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 51 | } |
| 52 | |
| 53 | void IterativeRobotBase::RobotPeriodic() { |
| 54 | static bool firstRun = true; |
| 55 | if (firstRun) { |
| 56 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 57 | firstRun = false; |
| 58 | } |
| 59 | } |
| 60 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 61 | void IterativeRobotBase::SimulationPeriodic() { |
| 62 | static bool firstRun = true; |
| 63 | if (firstRun) { |
| 64 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 65 | firstRun = false; |
| 66 | } |
| 67 | } |
| 68 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | void IterativeRobotBase::DisabledPeriodic() { |
| 70 | static bool firstRun = true; |
| 71 | if (firstRun) { |
| 72 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 73 | firstRun = false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void IterativeRobotBase::AutonomousPeriodic() { |
| 78 | static bool firstRun = true; |
| 79 | if (firstRun) { |
| 80 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 81 | firstRun = false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void IterativeRobotBase::TeleopPeriodic() { |
| 86 | static bool firstRun = true; |
| 87 | if (firstRun) { |
| 88 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 89 | firstRun = false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void IterativeRobotBase::TestPeriodic() { |
| 94 | static bool firstRun = true; |
| 95 | if (firstRun) { |
| 96 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
| 97 | firstRun = false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void IterativeRobotBase::LoopFunc() { |
| 102 | m_watchdog.Reset(); |
| 103 | |
| 104 | // Call the appropriate function depending upon the current robot mode |
| 105 | if (IsDisabled()) { |
| 106 | // Call DisabledInit() if we are now just entering disabled mode from |
| 107 | // either a different mode or from power-on. |
| 108 | if (m_lastMode != Mode::kDisabled) { |
| 109 | LiveWindow::GetInstance()->SetEnabled(false); |
| 110 | Shuffleboard::DisableActuatorWidgets(); |
| 111 | DisabledInit(); |
| 112 | m_watchdog.AddEpoch("DisabledInit()"); |
| 113 | m_lastMode = Mode::kDisabled; |
| 114 | } |
| 115 | |
| 116 | HAL_ObserveUserProgramDisabled(); |
| 117 | DisabledPeriodic(); |
| 118 | m_watchdog.AddEpoch("DisabledPeriodic()"); |
| 119 | } else if (IsAutonomous()) { |
| 120 | // Call AutonomousInit() if we are now just entering autonomous mode from |
| 121 | // either a different mode or from power-on. |
| 122 | if (m_lastMode != Mode::kAutonomous) { |
| 123 | LiveWindow::GetInstance()->SetEnabled(false); |
| 124 | Shuffleboard::DisableActuatorWidgets(); |
| 125 | AutonomousInit(); |
| 126 | m_watchdog.AddEpoch("AutonomousInit()"); |
| 127 | m_lastMode = Mode::kAutonomous; |
| 128 | } |
| 129 | |
| 130 | HAL_ObserveUserProgramAutonomous(); |
| 131 | AutonomousPeriodic(); |
| 132 | m_watchdog.AddEpoch("AutonomousPeriodic()"); |
| 133 | } else if (IsOperatorControl()) { |
| 134 | // Call TeleopInit() if we are now just entering teleop mode from |
| 135 | // either a different mode or from power-on. |
| 136 | if (m_lastMode != Mode::kTeleop) { |
| 137 | LiveWindow::GetInstance()->SetEnabled(false); |
| 138 | Shuffleboard::DisableActuatorWidgets(); |
| 139 | TeleopInit(); |
| 140 | m_watchdog.AddEpoch("TeleopInit()"); |
| 141 | m_lastMode = Mode::kTeleop; |
| 142 | } |
| 143 | |
| 144 | HAL_ObserveUserProgramTeleop(); |
| 145 | TeleopPeriodic(); |
| 146 | m_watchdog.AddEpoch("TeleopPeriodic()"); |
| 147 | } else { |
| 148 | // Call TestInit() if we are now just entering test mode from |
| 149 | // either a different mode or from power-on. |
| 150 | if (m_lastMode != Mode::kTest) { |
| 151 | LiveWindow::GetInstance()->SetEnabled(true); |
| 152 | Shuffleboard::EnableActuatorWidgets(); |
| 153 | TestInit(); |
| 154 | m_watchdog.AddEpoch("TestInit()"); |
| 155 | m_lastMode = Mode::kTest; |
| 156 | } |
| 157 | |
| 158 | HAL_ObserveUserProgramTest(); |
| 159 | TestPeriodic(); |
| 160 | m_watchdog.AddEpoch("TestPeriodic()"); |
| 161 | } |
| 162 | |
| 163 | RobotPeriodic(); |
| 164 | m_watchdog.AddEpoch("RobotPeriodic()"); |
| 165 | |
| 166 | SmartDashboard::UpdateValues(); |
| 167 | m_watchdog.AddEpoch("SmartDashboard::UpdateValues()"); |
| 168 | LiveWindow::GetInstance()->UpdateValues(); |
| 169 | m_watchdog.AddEpoch("LiveWindow::UpdateValues()"); |
| 170 | Shuffleboard::Update(); |
| 171 | m_watchdog.AddEpoch("Shuffleboard::Update()"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 172 | |
| 173 | if constexpr (IsSimulation()) { |
| 174 | SimulationPeriodic(); |
| 175 | m_watchdog.AddEpoch("SimulationPeriodic()"); |
| 176 | } |
| 177 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 178 | m_watchdog.Disable(); |
| 179 | |
| 180 | // Warn on loop time overruns |
| 181 | if (m_watchdog.IsExpired()) { |
| 182 | m_watchdog.PrintEpochs(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void IterativeRobotBase::PrintLoopOverrunMessage() { |
| 187 | wpi::SmallString<128> str; |
| 188 | wpi::raw_svector_ostream buf(str); |
| 189 | |
| 190 | buf << "Loop time of " << wpi::format("%.6f", m_period.to<double>()) |
| 191 | << "s overrun\n"; |
| 192 | |
| 193 | DriverStation::ReportWarning(str); |
| 194 | } |