2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/IterativeRobotBase.h"
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/DriverStation.h>
|
2020-12-12 23:16:50 -08:00
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2021-05-24 23:36:26 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/livewindow/LiveWindow.h"
|
2019-04-27 22:23:21 -07:00
|
|
|
#include "frc/shuffleboard/Shuffleboard.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/smartdashboard/SmartDashboard.h"
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2018-06-24 02:29:21 -05:00
|
|
|
IterativeRobotBase::IterativeRobotBase(double period)
|
2019-08-24 21:13:29 -07:00
|
|
|
: IterativeRobotBase(units::second_t(period)) {}
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
IterativeRobotBase::IterativeRobotBase(units::second_t period)
|
2019-09-03 15:58:31 -07:00
|
|
|
: m_period(period),
|
|
|
|
|
m_watchdog(period, [this] { PrintLoopOverrunMessage(); }) {}
|
2019-08-17 00:56:48 -04:00
|
|
|
|
2021-08-05 23:54:50 -07:00
|
|
|
void IterativeRobotBase::RobotInit() {}
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-08-05 23:54:50 -07:00
|
|
|
void IterativeRobotBase::SimulationInit() {}
|
2020-02-19 02:05:16 -05:00
|
|
|
|
2021-08-05 23:54:50 -07:00
|
|
|
void IterativeRobotBase::DisabledInit() {}
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-08-05 23:54:50 -07:00
|
|
|
void IterativeRobotBase::AutonomousInit() {}
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-08-05 23:54:50 -07:00
|
|
|
void IterativeRobotBase::TeleopInit() {}
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-08-05 23:54:50 -07:00
|
|
|
void IterativeRobotBase::TestInit() {}
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
void IterativeRobotBase::RobotPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 02:05:16 -05:00
|
|
|
void IterativeRobotBase::SimulationPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
|
2020-02-19 02:05:16 -05:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
void IterativeRobotBase::DisabledPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::AutonomousPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::TeleopPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::TestPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 19:08:29 -07:00
|
|
|
void IterativeRobotBase::DisabledExit() {}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::AutonomousExit() {}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::TeleopExit() {}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::TestExit() {}
|
|
|
|
|
|
2020-12-12 23:16:50 -08:00
|
|
|
void IterativeRobotBase::SetNetworkTablesFlushEnabled(bool enabled) {
|
|
|
|
|
m_ntFlushEnabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-13 14:26:42 -07:00
|
|
|
units::second_t IterativeRobotBase::GetPeriod() const {
|
|
|
|
|
return m_period;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
void IterativeRobotBase::LoopFunc() {
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.Reset();
|
|
|
|
|
|
2021-08-05 19:08:29 -07:00
|
|
|
// Get current mode
|
|
|
|
|
Mode mode = Mode::kNone;
|
2017-07-08 10:50:56 -04:00
|
|
|
if (IsDisabled()) {
|
2021-08-05 19:08:29 -07:00
|
|
|
mode = Mode::kDisabled;
|
|
|
|
|
} else if (IsAutonomous()) {
|
|
|
|
|
mode = Mode::kAutonomous;
|
|
|
|
|
} else if (IsOperatorControl()) {
|
|
|
|
|
mode = Mode::kTeleop;
|
|
|
|
|
} else if (IsTest()) {
|
|
|
|
|
mode = Mode::kTest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If mode changed, call mode exit and entry functions
|
|
|
|
|
if (m_lastMode != mode) {
|
|
|
|
|
// Call last mode's exit function
|
|
|
|
|
if (m_lastMode == Mode::kDisabled) {
|
|
|
|
|
DisabledExit();
|
|
|
|
|
} else if (m_lastMode == Mode::kAutonomous) {
|
|
|
|
|
AutonomousExit();
|
|
|
|
|
} else if (m_lastMode == Mode::kTeleop) {
|
|
|
|
|
TeleopExit();
|
|
|
|
|
} else if (m_lastMode == Mode::kTest) {
|
2021-06-15 23:06:03 -07:00
|
|
|
LiveWindow::SetEnabled(false);
|
2019-04-27 22:23:21 -07:00
|
|
|
Shuffleboard::DisableActuatorWidgets();
|
2021-08-05 19:08:29 -07:00
|
|
|
TestExit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call current mode's entry function
|
|
|
|
|
if (mode == Mode::kDisabled) {
|
2017-07-08 10:50:56 -04:00
|
|
|
DisabledInit();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("DisabledInit()");
|
2021-08-05 19:08:29 -07:00
|
|
|
} else if (mode == Mode::kAutonomous) {
|
|
|
|
|
AutonomousInit();
|
|
|
|
|
m_watchdog.AddEpoch("AutonomousInit()");
|
|
|
|
|
} else if (mode == Mode::kTeleop) {
|
|
|
|
|
TeleopInit();
|
|
|
|
|
m_watchdog.AddEpoch("TeleopInit()");
|
|
|
|
|
} else if (mode == Mode::kTest) {
|
|
|
|
|
LiveWindow::SetEnabled(true);
|
|
|
|
|
Shuffleboard::EnableActuatorWidgets();
|
|
|
|
|
TestInit();
|
|
|
|
|
m_watchdog.AddEpoch("TestInit()");
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2021-08-05 19:08:29 -07:00
|
|
|
m_lastMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the appropriate function depending upon the current robot mode
|
|
|
|
|
if (mode == Mode::kDisabled) {
|
2017-07-08 10:50:56 -04:00
|
|
|
HAL_ObserveUserProgramDisabled();
|
|
|
|
|
DisabledPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("DisabledPeriodic()");
|
2021-08-05 19:08:29 -07:00
|
|
|
} else if (mode == Mode::kAutonomous) {
|
2017-07-08 10:50:56 -04:00
|
|
|
HAL_ObserveUserProgramAutonomous();
|
|
|
|
|
AutonomousPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("AutonomousPeriodic()");
|
2021-08-05 19:08:29 -07:00
|
|
|
} else if (mode == Mode::kTeleop) {
|
2017-07-08 10:50:56 -04:00
|
|
|
HAL_ObserveUserProgramTeleop();
|
|
|
|
|
TeleopPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("TeleopPeriodic()");
|
2021-08-05 19:08:29 -07:00
|
|
|
} else if (mode == Mode::kTest) {
|
2017-07-08 10:50:56 -04:00
|
|
|
HAL_ObserveUserProgramTest();
|
|
|
|
|
TestPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("TestPeriodic()");
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
RobotPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("RobotPeriodic()");
|
|
|
|
|
|
2019-04-27 22:23:21 -07:00
|
|
|
SmartDashboard::UpdateValues();
|
|
|
|
|
m_watchdog.AddEpoch("SmartDashboard::UpdateValues()");
|
2021-06-15 23:06:03 -07:00
|
|
|
LiveWindow::UpdateValues();
|
2019-04-27 22:23:21 -07:00
|
|
|
m_watchdog.AddEpoch("LiveWindow::UpdateValues()");
|
|
|
|
|
Shuffleboard::Update();
|
|
|
|
|
m_watchdog.AddEpoch("Shuffleboard::Update()");
|
2020-02-19 02:05:16 -05:00
|
|
|
|
2020-10-03 22:26:19 -07:00
|
|
|
if constexpr (IsSimulation()) {
|
2020-11-30 23:55:36 -08:00
|
|
|
HAL_SimPeriodicBefore();
|
2020-02-19 02:05:16 -05:00
|
|
|
SimulationPeriodic();
|
2020-11-30 23:55:36 -08:00
|
|
|
HAL_SimPeriodicAfter();
|
2020-02-19 02:05:16 -05:00
|
|
|
m_watchdog.AddEpoch("SimulationPeriodic()");
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-27 22:23:21 -07:00
|
|
|
m_watchdog.Disable();
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2020-12-12 23:16:50 -08:00
|
|
|
// Flush NetworkTables
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_ntFlushEnabled) {
|
|
|
|
|
nt::NetworkTableInstance::GetDefault().Flush();
|
|
|
|
|
}
|
2020-12-12 23:16:50 -08:00
|
|
|
|
2018-06-24 02:29:21 -05:00
|
|
|
// Warn on loop time overruns
|
|
|
|
|
if (m_watchdog.IsExpired()) {
|
|
|
|
|
m_watchdog.PrintEpochs();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::PrintLoopOverrunMessage() {
|
2021-05-24 23:36:26 -07:00
|
|
|
FRC_ReportError(err::Error, "Loop time of {:.6f}s overrun",
|
|
|
|
|
m_period.to<double>());
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|