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
|
|
|
|
2025-11-08 15:08:38 -08:00
|
|
|
#include "wpi/framework/IterativeRobotBase.hpp"
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2026-04-18 19:56:45 -07:00
|
|
|
#include "wpi/driverstation/RobotState.hpp"
|
|
|
|
|
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/DriverStation.h"
|
2025-12-12 21:25:57 -07:00
|
|
|
#include "wpi/hal/DriverStationTypes.h"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/nt/NetworkTableInstance.hpp"
|
|
|
|
|
#include "wpi/smartdashboard/SmartDashboard.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/system/Errors.hpp"
|
|
|
|
|
#include "wpi/util/print.hpp"
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi;
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
IterativeRobotBase::IterativeRobotBase(wpi::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
|
|
|
|
2023-06-21 14:53:34 -07:00
|
|
|
void IterativeRobotBase::DriverStationConnected() {}
|
|
|
|
|
|
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
|
|
|
|
2026-04-20 20:29:25 -07:00
|
|
|
void IterativeRobotBase::UtilityInit() {}
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
void IterativeRobotBase::RobotPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::util::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) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::util::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) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::util::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) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::util::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) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::util::print("Default {}() method... Override me!\n", __FUNCTION__);
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 20:29:25 -07:00
|
|
|
void IterativeRobotBase::UtilityPeriodic() {
|
2017-07-08 10:50:56 -04:00
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::util::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() {}
|
|
|
|
|
|
2026-04-20 20:29:25 -07:00
|
|
|
void IterativeRobotBase::UtilityExit() {}
|
2021-08-05 19:08:29 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::units::second_t IterativeRobotBase::GetPeriod() const {
|
2019-04-13 14:26:42 -07:00
|
|
|
return m_period;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
void IterativeRobotBase::LoopFunc() {
|
2026-04-18 19:56:45 -07:00
|
|
|
wpi::internal::DriverStationBackend::RefreshData();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.Reset();
|
|
|
|
|
|
2025-12-12 21:25:57 -07:00
|
|
|
// Get current mode; treat disabled as unknown
|
|
|
|
|
wpi::hal::ControlWord word = wpi::hal::GetControlWord();
|
|
|
|
|
bool enabled = word.IsEnabled();
|
|
|
|
|
RobotMode mode = enabled ? word.GetRobotMode() : RobotMode::UNKNOWN;
|
2021-08-05 19:08:29 -07:00
|
|
|
|
2023-06-21 14:53:34 -07:00
|
|
|
if (!m_calledDsConnected && word.IsDSAttached()) {
|
|
|
|
|
m_calledDsConnected = true;
|
|
|
|
|
DriverStationConnected();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 19:08:29 -07:00
|
|
|
// If mode changed, call mode exit and entry functions
|
2025-12-12 21:25:57 -07:00
|
|
|
if (m_lastMode != static_cast<int>(mode)) {
|
2021-08-05 19:08:29 -07:00
|
|
|
// Call last mode's exit function
|
2025-12-12 21:25:57 -07:00
|
|
|
if (m_lastMode == static_cast<int>(RobotMode::UNKNOWN)) {
|
2021-08-05 19:08:29 -07:00
|
|
|
DisabledExit();
|
2025-12-12 21:25:57 -07:00
|
|
|
} else if (m_lastMode == static_cast<int>(RobotMode::AUTONOMOUS)) {
|
2021-08-05 19:08:29 -07:00
|
|
|
AutonomousExit();
|
2025-12-12 21:25:57 -07:00
|
|
|
} else if (m_lastMode == static_cast<int>(RobotMode::TELEOPERATED)) {
|
2021-08-05 19:08:29 -07:00
|
|
|
TeleopExit();
|
2026-04-20 20:29:25 -07:00
|
|
|
} else if (m_lastMode == static_cast<int>(RobotMode::UTILITY)) {
|
|
|
|
|
UtilityExit();
|
2021-08-05 19:08:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call current mode's entry function
|
2025-12-12 21:25:57 -07:00
|
|
|
if (mode == RobotMode::UNKNOWN) {
|
2017-07-08 10:50:56 -04:00
|
|
|
DisabledInit();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("DisabledInit()");
|
2025-12-12 21:25:57 -07:00
|
|
|
} else if (mode == RobotMode::AUTONOMOUS) {
|
2021-08-05 19:08:29 -07:00
|
|
|
AutonomousInit();
|
|
|
|
|
m_watchdog.AddEpoch("AutonomousInit()");
|
2025-12-12 21:25:57 -07:00
|
|
|
} else if (mode == RobotMode::TELEOPERATED) {
|
2021-08-05 19:08:29 -07:00
|
|
|
TeleopInit();
|
|
|
|
|
m_watchdog.AddEpoch("TeleopInit()");
|
2026-04-20 20:29:25 -07:00
|
|
|
} else if (mode == RobotMode::UTILITY) {
|
|
|
|
|
UtilityInit();
|
|
|
|
|
m_watchdog.AddEpoch("UtilityInit()");
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2025-12-12 21:25:57 -07:00
|
|
|
m_lastMode = static_cast<int>(mode);
|
2021-08-05 19:08:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the appropriate function depending upon the current robot mode
|
2025-12-12 21:25:57 -07:00
|
|
|
HAL_ObserveUserProgram(word.GetValue());
|
|
|
|
|
if (mode == RobotMode::UNKNOWN) {
|
2017-07-08 10:50:56 -04:00
|
|
|
DisabledPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("DisabledPeriodic()");
|
2025-12-12 21:25:57 -07:00
|
|
|
} else if (mode == RobotMode::AUTONOMOUS) {
|
2017-07-08 10:50:56 -04:00
|
|
|
AutonomousPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("AutonomousPeriodic()");
|
2025-12-12 21:25:57 -07:00
|
|
|
} else if (mode == RobotMode::TELEOPERATED) {
|
2017-07-08 10:50:56 -04:00
|
|
|
TeleopPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("TeleopPeriodic()");
|
2026-04-20 20:29:25 -07:00
|
|
|
} else if (mode == RobotMode::UTILITY) {
|
|
|
|
|
UtilityPeriodic();
|
|
|
|
|
m_watchdog.AddEpoch("UtilityPeriodic()");
|
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()");
|
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
|
2026-03-15 00:28:31 -04:00
|
|
|
wpi::nt::NetworkTableInstance::GetDefault().FlushLocal();
|
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() {
|
2025-11-07 20:01:58 -05:00
|
|
|
WPILIB_ReportError(err::Error, "Loop time of {:.6f}s overrun",
|
|
|
|
|
m_period.value());
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
2024-05-05 01:19:36 +08:00
|
|
|
|
|
|
|
|
void IterativeRobotBase::PrintWatchdogEpochs() {
|
|
|
|
|
m_watchdog.PrintEpochs();
|
|
|
|
|
}
|