Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ |
| 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 | |
| 10 | #include <cstdio> |
| 11 | |
| 12 | #include <hal/HAL.h> |
| 13 | #include <wpi/SmallString.h> |
| 14 | #include <wpi/raw_ostream.h> |
| 15 | |
| 16 | #include "frc/DriverStation.h" |
| 17 | #include "frc/Timer.h" |
| 18 | #include "frc/commands/Scheduler.h" |
| 19 | #include "frc/livewindow/LiveWindow.h" |
| 20 | #include "frc/smartdashboard/SmartDashboard.h" |
| 21 | |
| 22 | using namespace frc; |
| 23 | |
| 24 | IterativeRobotBase::IterativeRobotBase(double period) |
| 25 | : m_period(period), |
| 26 | m_watchdog(period, [this] { PrintLoopOverrunMessage(); }) {} |
| 27 | |
| 28 | void IterativeRobotBase::RobotInit() { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 29 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void IterativeRobotBase::DisabledInit() { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 33 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void IterativeRobotBase::AutonomousInit() { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 37 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void IterativeRobotBase::TeleopInit() { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 41 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void IterativeRobotBase::TestInit() { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 45 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void IterativeRobotBase::RobotPeriodic() { |
| 49 | static bool firstRun = true; |
| 50 | if (firstRun) { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 51 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 52 | firstRun = false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void IterativeRobotBase::DisabledPeriodic() { |
| 57 | static bool firstRun = true; |
| 58 | if (firstRun) { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 59 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 60 | firstRun = false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void IterativeRobotBase::AutonomousPeriodic() { |
| 65 | static bool firstRun = true; |
| 66 | if (firstRun) { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 67 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 68 | firstRun = false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void IterativeRobotBase::TeleopPeriodic() { |
| 73 | static bool firstRun = true; |
| 74 | if (firstRun) { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 75 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 76 | firstRun = false; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void IterativeRobotBase::TestPeriodic() { |
| 81 | static bool firstRun = true; |
| 82 | if (firstRun) { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame^] | 83 | wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n"; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 84 | firstRun = false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void IterativeRobotBase::LoopFunc() { |
| 89 | m_watchdog.Reset(); |
| 90 | |
| 91 | // Call the appropriate function depending upon the current robot mode |
| 92 | if (IsDisabled()) { |
| 93 | // Call DisabledInit() if we are now just entering disabled mode from |
| 94 | // either a different mode or from power-on. |
| 95 | if (m_lastMode != Mode::kDisabled) { |
| 96 | LiveWindow::GetInstance()->SetEnabled(false); |
| 97 | DisabledInit(); |
| 98 | m_watchdog.AddEpoch("DisabledInit()"); |
| 99 | m_lastMode = Mode::kDisabled; |
| 100 | } |
| 101 | |
| 102 | HAL_ObserveUserProgramDisabled(); |
| 103 | DisabledPeriodic(); |
| 104 | m_watchdog.AddEpoch("DisabledPeriodic()"); |
| 105 | } else if (IsAutonomous()) { |
| 106 | // Call AutonomousInit() if we are now just entering autonomous mode from |
| 107 | // either a different mode or from power-on. |
| 108 | if (m_lastMode != Mode::kAutonomous) { |
| 109 | LiveWindow::GetInstance()->SetEnabled(false); |
| 110 | AutonomousInit(); |
| 111 | m_watchdog.AddEpoch("AutonomousInit()"); |
| 112 | m_lastMode = Mode::kAutonomous; |
| 113 | } |
| 114 | |
| 115 | HAL_ObserveUserProgramAutonomous(); |
| 116 | AutonomousPeriodic(); |
| 117 | m_watchdog.AddEpoch("AutonomousPeriodic()"); |
| 118 | } else if (IsOperatorControl()) { |
| 119 | // Call TeleopInit() if we are now just entering teleop mode from |
| 120 | // either a different mode or from power-on. |
| 121 | if (m_lastMode != Mode::kTeleop) { |
| 122 | LiveWindow::GetInstance()->SetEnabled(false); |
| 123 | TeleopInit(); |
| 124 | m_watchdog.AddEpoch("TeleopInit()"); |
| 125 | m_lastMode = Mode::kTeleop; |
| 126 | Scheduler::GetInstance()->SetEnabled(true); |
| 127 | } |
| 128 | |
| 129 | HAL_ObserveUserProgramTeleop(); |
| 130 | TeleopPeriodic(); |
| 131 | m_watchdog.AddEpoch("TeleopPeriodic()"); |
| 132 | } else { |
| 133 | // Call TestInit() if we are now just entering test mode from |
| 134 | // either a different mode or from power-on. |
| 135 | if (m_lastMode != Mode::kTest) { |
| 136 | LiveWindow::GetInstance()->SetEnabled(true); |
| 137 | TestInit(); |
| 138 | m_watchdog.AddEpoch("TestInit()"); |
| 139 | m_lastMode = Mode::kTest; |
| 140 | } |
| 141 | |
| 142 | HAL_ObserveUserProgramTest(); |
| 143 | TestPeriodic(); |
| 144 | m_watchdog.AddEpoch("TestPeriodic()"); |
| 145 | } |
| 146 | |
| 147 | RobotPeriodic(); |
| 148 | m_watchdog.AddEpoch("RobotPeriodic()"); |
| 149 | m_watchdog.Disable(); |
| 150 | SmartDashboard::UpdateValues(); |
| 151 | |
| 152 | LiveWindow::GetInstance()->UpdateValues(); |
| 153 | |
| 154 | // Warn on loop time overruns |
| 155 | if (m_watchdog.IsExpired()) { |
| 156 | m_watchdog.PrintEpochs(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void IterativeRobotBase::PrintLoopOverrunMessage() { |
| 161 | wpi::SmallString<128> str; |
| 162 | wpi::raw_svector_ostream buf(str); |
| 163 | |
| 164 | buf << "Loop time of " << m_period << "s overrun\n"; |
| 165 | |
| 166 | DriverStation::ReportWarning(str); |
| 167 | } |