Files
allwpilib/wpilibc/src/main/native/cpp/framework/IterativeRobotBase.cpp

187 lines
5.2 KiB
C++
Raw Normal View History

// 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
#include "wpi/framework/IterativeRobotBase.hpp"
2017-07-08 10:50:56 -04: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"
#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)
: m_period(period),
m_watchdog(period, [this] { PrintLoopOverrunMessage(); }) {}
void IterativeRobotBase::DriverStationConnected() {}
void IterativeRobotBase::SimulationInit() {}
void IterativeRobotBase::DisabledInit() {}
2017-07-08 10:50:56 -04:00
void IterativeRobotBase::AutonomousInit() {}
2017-07-08 10:50:56 -04:00
void IterativeRobotBase::TeleopInit() {}
2017-07-08 10:50:56 -04: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;
}
}
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__);
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;
}
}
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;
}
}
void IterativeRobotBase::DisabledExit() {}
void IterativeRobotBase::AutonomousExit() {}
void IterativeRobotBase::TeleopExit() {}
void IterativeRobotBase::UtilityExit() {}
2025-11-07 20:00:05 -05:00
wpi::units::second_t IterativeRobotBase::GetPeriod() const {
return m_period;
}
2017-07-08 10:50:56 -04:00
void IterativeRobotBase::LoopFunc() {
wpi::internal::DriverStationBackend::RefreshData();
m_watchdog.Reset();
// 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;
if (!m_calledDsConnected && word.IsDSAttached()) {
m_calledDsConnected = true;
DriverStationConnected();
}
// If mode changed, call mode exit and entry functions
if (m_lastMode != static_cast<int>(mode)) {
// Call last mode's exit function
if (m_lastMode == static_cast<int>(RobotMode::UNKNOWN)) {
DisabledExit();
} else if (m_lastMode == static_cast<int>(RobotMode::AUTONOMOUS)) {
AutonomousExit();
} else if (m_lastMode == static_cast<int>(RobotMode::TELEOPERATED)) {
TeleopExit();
} else if (m_lastMode == static_cast<int>(RobotMode::UTILITY)) {
UtilityExit();
}
// Call current mode's entry function
if (mode == RobotMode::UNKNOWN) {
2017-07-08 10:50:56 -04:00
DisabledInit();
m_watchdog.AddEpoch("DisabledInit()");
} else if (mode == RobotMode::AUTONOMOUS) {
AutonomousInit();
m_watchdog.AddEpoch("AutonomousInit()");
} else if (mode == RobotMode::TELEOPERATED) {
TeleopInit();
m_watchdog.AddEpoch("TeleopInit()");
} else if (mode == RobotMode::UTILITY) {
UtilityInit();
m_watchdog.AddEpoch("UtilityInit()");
2017-07-08 10:50:56 -04:00
}
m_lastMode = static_cast<int>(mode);
}
// Call the appropriate function depending upon the current robot mode
HAL_ObserveUserProgram(word.GetValue());
if (mode == RobotMode::UNKNOWN) {
2017-07-08 10:50:56 -04:00
DisabledPeriodic();
m_watchdog.AddEpoch("DisabledPeriodic()");
} else if (mode == RobotMode::AUTONOMOUS) {
2017-07-08 10:50:56 -04:00
AutonomousPeriodic();
m_watchdog.AddEpoch("AutonomousPeriodic()");
} else if (mode == RobotMode::TELEOPERATED) {
2017-07-08 10:50:56 -04:00
TeleopPeriodic();
m_watchdog.AddEpoch("TeleopPeriodic()");
} else if (mode == RobotMode::UTILITY) {
UtilityPeriodic();
m_watchdog.AddEpoch("UtilityPeriodic()");
2017-07-08 10:50:56 -04:00
}
2017-07-08 10:50:56 -04:00
RobotPeriodic();
m_watchdog.AddEpoch("RobotPeriodic()");
SmartDashboard::UpdateValues();
m_watchdog.AddEpoch("SmartDashboard::UpdateValues()");
if constexpr (IsSimulation()) {
HAL_SimPeriodicBefore();
SimulationPeriodic();
HAL_SimPeriodicAfter();
m_watchdog.AddEpoch("SimulationPeriodic()");
}
m_watchdog.Disable();
// Flush NetworkTables
2026-03-15 00:28:31 -04:00
wpi::nt::NetworkTableInstance::GetDefault().FlushLocal();
// 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
}
void IterativeRobotBase::PrintWatchdogEpochs() {
m_watchdog.PrintEpochs();
}