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
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/DriverStation.h>
|
2020-12-12 23:16:50 -08:00
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2019-05-19 12:24:40 -07:00
|
|
|
#include <wpi/Format.h>
|
2018-06-24 02:29:21 -05:00
|
|
|
#include <wpi/SmallString.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/raw_ostream.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/DriverStation.h"
|
|
|
|
|
#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
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
void IterativeRobotBase::RobotInit() {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 02:05:16 -05:00
|
|
|
void IterativeRobotBase::SimulationInit() {
|
|
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
void IterativeRobotBase::DisabledInit() {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::AutonomousInit() {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::TeleopInit() {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::TestInit() {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::RobotPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
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) {
|
|
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
|
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
void IterativeRobotBase::DisabledPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::AutonomousPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::TeleopPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::TestPeriodic() {
|
|
|
|
|
static bool firstRun = true;
|
|
|
|
|
if (firstRun) {
|
2019-01-25 00:45:05 -06:00
|
|
|
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
|
2017-07-08 10:50:56 -04:00
|
|
|
firstRun = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 23:16:50 -08:00
|
|
|
void IterativeRobotBase::SetNetworkTablesFlushEnabled(bool enabled) {
|
|
|
|
|
m_ntFlushEnabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
void IterativeRobotBase::LoopFunc() {
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.Reset();
|
|
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
// Call the appropriate function depending upon the current robot mode
|
|
|
|
|
if (IsDisabled()) {
|
2017-11-16 00:33:51 -08:00
|
|
|
// 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::GetInstance()->SetEnabled(false);
|
2019-04-27 22:23:21 -07:00
|
|
|
Shuffleboard::DisableActuatorWidgets();
|
2017-07-08 10:50:56 -04:00
|
|
|
DisabledInit();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("DisabledInit()");
|
2017-07-08 10:50:56 -04:00
|
|
|
m_lastMode = Mode::kDisabled;
|
|
|
|
|
}
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
HAL_ObserveUserProgramDisabled();
|
|
|
|
|
DisabledPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("DisabledPeriodic()");
|
2017-07-08 10:50:56 -04:00
|
|
|
} else if (IsAutonomous()) {
|
2017-11-16 00:33:51 -08:00
|
|
|
// 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::GetInstance()->SetEnabled(false);
|
2019-04-27 22:23:21 -07:00
|
|
|
Shuffleboard::DisableActuatorWidgets();
|
2017-07-08 10:50:56 -04:00
|
|
|
AutonomousInit();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("AutonomousInit()");
|
2017-07-08 10:50:56 -04:00
|
|
|
m_lastMode = Mode::kAutonomous;
|
|
|
|
|
}
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
HAL_ObserveUserProgramAutonomous();
|
|
|
|
|
AutonomousPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("AutonomousPeriodic()");
|
2017-07-08 10:50:56 -04:00
|
|
|
} else if (IsOperatorControl()) {
|
2017-11-16 00:33:51 -08:00
|
|
|
// 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::GetInstance()->SetEnabled(false);
|
2019-04-27 22:23:21 -07:00
|
|
|
Shuffleboard::DisableActuatorWidgets();
|
2017-07-08 10:50:56 -04:00
|
|
|
TeleopInit();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("TeleopInit()");
|
2017-07-08 10:50:56 -04:00
|
|
|
m_lastMode = Mode::kTeleop;
|
|
|
|
|
}
|
2018-06-24 02:29:21 -05:00
|
|
|
|
2017-07-08 10:50:56 -04:00
|
|
|
HAL_ObserveUserProgramTeleop();
|
|
|
|
|
TeleopPeriodic();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("TeleopPeriodic()");
|
2017-07-08 10:50:56 -04:00
|
|
|
} else {
|
2017-11-16 00:33:51 -08:00
|
|
|
// 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::GetInstance()->SetEnabled(true);
|
2019-04-27 22:23:21 -07:00
|
|
|
Shuffleboard::EnableActuatorWidgets();
|
2017-07-08 10:50:56 -04:00
|
|
|
TestInit();
|
2018-06-24 02:29:21 -05:00
|
|
|
m_watchdog.AddEpoch("TestInit()");
|
2017-07-08 10:50:56 -04:00
|
|
|
m_lastMode = Mode::kTest;
|
|
|
|
|
}
|
2018-06-24 02:29:21 -05:00
|
|
|
|
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()");
|
2017-12-04 23:28:33 -08:00
|
|
|
LiveWindow::GetInstance()->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
|
|
|
|
|
if (m_ntFlushEnabled) nt::NetworkTableInstance::GetDefault().Flush();
|
|
|
|
|
|
2018-06-24 02:29:21 -05:00
|
|
|
// Warn on loop time overruns
|
|
|
|
|
if (m_watchdog.IsExpired()) {
|
|
|
|
|
m_watchdog.PrintEpochs();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IterativeRobotBase::PrintLoopOverrunMessage() {
|
|
|
|
|
wpi::SmallString<128> str;
|
|
|
|
|
wpi::raw_svector_ostream buf(str);
|
|
|
|
|
|
2019-09-03 15:58:31 -07:00
|
|
|
buf << "Loop time of " << wpi::format("%.6f", m_period.to<double>())
|
|
|
|
|
<< "s overrun\n";
|
2018-06-24 02:29:21 -05:00
|
|
|
|
|
|
|
|
DriverStation::ReportWarning(str);
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|