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

201 lines
5.6 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 "frc/IterativeRobotBase.h"
2017-07-08 10:50:56 -04:00
#include <fmt/format.h>
#include <hal/DriverStation.h>
#include <networktables/NetworkTableInstance.h>
#include "frc/Errors.h"
#include "frc/livewindow/LiveWindow.h"
#include "frc/shuffleboard/Shuffleboard.h"
#include "frc/smartdashboard/SmartDashboard.h"
2017-07-08 10:50:56 -04:00
using namespace frc;
IterativeRobotBase::IterativeRobotBase(double period)
: IterativeRobotBase(units::second_t(period)) {}
IterativeRobotBase::IterativeRobotBase(units::second_t period)
: m_period(period),
m_watchdog(period, [this] { PrintLoopOverrunMessage(); }) {}
2017-07-08 10:50:56 -04:00
void IterativeRobotBase::RobotInit() {
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
2017-07-08 10:50:56 -04:00
}
void IterativeRobotBase::SimulationInit() {
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
}
2017-07-08 10:50:56 -04:00
void IterativeRobotBase::DisabledInit() {
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
2017-07-08 10:50:56 -04:00
}
void IterativeRobotBase::AutonomousInit() {
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
2017-07-08 10:50:56 -04:00
}
void IterativeRobotBase::TeleopInit() {
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
2017-07-08 10:50:56 -04:00
}
void IterativeRobotBase::TestInit() {
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
2017-07-08 10:50:56 -04:00
}
void IterativeRobotBase::RobotPeriodic() {
static bool firstRun = true;
if (firstRun) {
fmt::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) {
fmt::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) {
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) {
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) {
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) {
fmt::print("Default {}() method... Override me!\n", __FUNCTION__);
2017-07-08 10:50:56 -04:00
firstRun = false;
}
}
void IterativeRobotBase::SetNetworkTablesFlushEnabled(bool enabled) {
m_ntFlushEnabled = enabled;
}
units::second_t IterativeRobotBase::GetPeriod() const {
return m_period;
}
2017-07-08 10:50:56 -04:00
void IterativeRobotBase::LoopFunc() {
m_watchdog.Reset();
2017-07-08 10:50:56 -04:00
// Call the appropriate function depending upon the current robot mode
if (IsDisabled()) {
// Call DisabledInit() if we are now just entering disabled mode from
// either a different mode or from power-on.
2017-07-08 10:50:56 -04:00
if (m_lastMode != Mode::kDisabled) {
LiveWindow::SetEnabled(false);
Shuffleboard::DisableActuatorWidgets();
2017-07-08 10:50:56 -04:00
DisabledInit();
m_watchdog.AddEpoch("DisabledInit()");
2017-07-08 10:50:56 -04:00
m_lastMode = Mode::kDisabled;
}
2017-07-08 10:50:56 -04:00
HAL_ObserveUserProgramDisabled();
DisabledPeriodic();
m_watchdog.AddEpoch("DisabledPeriodic()");
2017-07-08 10:50:56 -04:00
} else if (IsAutonomous()) {
// Call AutonomousInit() if we are now just entering autonomous mode from
// either a different mode or from power-on.
2017-07-08 10:50:56 -04:00
if (m_lastMode != Mode::kAutonomous) {
LiveWindow::SetEnabled(false);
Shuffleboard::DisableActuatorWidgets();
2017-07-08 10:50:56 -04:00
AutonomousInit();
m_watchdog.AddEpoch("AutonomousInit()");
2017-07-08 10:50:56 -04:00
m_lastMode = Mode::kAutonomous;
}
2017-07-08 10:50:56 -04:00
HAL_ObserveUserProgramAutonomous();
AutonomousPeriodic();
m_watchdog.AddEpoch("AutonomousPeriodic()");
2017-07-08 10:50:56 -04:00
} else if (IsOperatorControl()) {
// Call TeleopInit() if we are now just entering teleop mode from
// either a different mode or from power-on.
2017-07-08 10:50:56 -04:00
if (m_lastMode != Mode::kTeleop) {
LiveWindow::SetEnabled(false);
Shuffleboard::DisableActuatorWidgets();
2017-07-08 10:50:56 -04:00
TeleopInit();
m_watchdog.AddEpoch("TeleopInit()");
2017-07-08 10:50:56 -04:00
m_lastMode = Mode::kTeleop;
}
2017-07-08 10:50:56 -04:00
HAL_ObserveUserProgramTeleop();
TeleopPeriodic();
m_watchdog.AddEpoch("TeleopPeriodic()");
2017-07-08 10:50:56 -04:00
} else {
// Call TestInit() if we are now just entering test mode from
// either a different mode or from power-on.
2017-07-08 10:50:56 -04:00
if (m_lastMode != Mode::kTest) {
LiveWindow::SetEnabled(true);
Shuffleboard::EnableActuatorWidgets();
2017-07-08 10:50:56 -04:00
TestInit();
m_watchdog.AddEpoch("TestInit()");
2017-07-08 10:50:56 -04:00
m_lastMode = Mode::kTest;
}
2017-07-08 10:50:56 -04:00
HAL_ObserveUserProgramTest();
TestPeriodic();
m_watchdog.AddEpoch("TestPeriodic()");
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()");
LiveWindow::UpdateValues();
m_watchdog.AddEpoch("LiveWindow::UpdateValues()");
Shuffleboard::Update();
m_watchdog.AddEpoch("Shuffleboard::Update()");
if constexpr (IsSimulation()) {
HAL_SimPeriodicBefore();
SimulationPeriodic();
HAL_SimPeriodicAfter();
m_watchdog.AddEpoch("SimulationPeriodic()");
}
m_watchdog.Disable();
// Flush NetworkTables
if (m_ntFlushEnabled) {
nt::NetworkTableInstance::GetDefault().Flush();
}
// Warn on loop time overruns
if (m_watchdog.IsExpired()) {
m_watchdog.PrintEpochs();
}
}
void IterativeRobotBase::PrintLoopOverrunMessage() {
FRC_ReportError(err::Error, "Loop time of {:.6f}s overrun",
m_period.to<double>());
2017-07-08 10:50:56 -04:00
}