mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[wpilib] Add method to enable/disable LiveWindow in test mode (#4678)
This commit is contained in:
@@ -94,6 +94,18 @@ void IterativeRobotBase::SetNetworkTablesFlushEnabled(bool enabled) {
|
||||
m_ntFlushEnabled = enabled;
|
||||
}
|
||||
|
||||
void IterativeRobotBase::EnableLiveWindowInTest(bool testLW) {
|
||||
if (IsTest()) {
|
||||
throw FRC_MakeError(err::IncompatibleMode,
|
||||
"Can't configure test mode while in test mode!");
|
||||
}
|
||||
m_lwEnabledInTest = testLW;
|
||||
}
|
||||
|
||||
bool IterativeRobotBase::IsLiveWindowEnabledInTest() {
|
||||
return m_lwEnabledInTest;
|
||||
}
|
||||
|
||||
units::second_t IterativeRobotBase::GetPeriod() const {
|
||||
return m_period;
|
||||
}
|
||||
@@ -125,8 +137,10 @@ void IterativeRobotBase::LoopFunc() {
|
||||
} else if (m_lastMode == Mode::kTeleop) {
|
||||
TeleopExit();
|
||||
} else if (m_lastMode == Mode::kTest) {
|
||||
LiveWindow::SetEnabled(false);
|
||||
Shuffleboard::DisableActuatorWidgets();
|
||||
if (m_lwEnabledInTest) {
|
||||
LiveWindow::SetEnabled(false);
|
||||
Shuffleboard::DisableActuatorWidgets();
|
||||
}
|
||||
TestExit();
|
||||
}
|
||||
|
||||
@@ -141,8 +155,10 @@ void IterativeRobotBase::LoopFunc() {
|
||||
TeleopInit();
|
||||
m_watchdog.AddEpoch("TeleopInit()");
|
||||
} else if (mode == Mode::kTest) {
|
||||
LiveWindow::SetEnabled(true);
|
||||
Shuffleboard::EnableActuatorWidgets();
|
||||
if (m_lwEnabledInTest) {
|
||||
LiveWindow::SetEnabled(true);
|
||||
Shuffleboard::EnableActuatorWidgets();
|
||||
}
|
||||
TestInit();
|
||||
m_watchdog.AddEpoch("TestInit()");
|
||||
}
|
||||
|
||||
@@ -201,6 +201,19 @@ class IterativeRobotBase : public RobotBase {
|
||||
*/
|
||||
void SetNetworkTablesFlushEnabled(bool enabled);
|
||||
|
||||
/**
|
||||
* Sets whether LiveWindow operation is enabled during test mode.
|
||||
*
|
||||
* @param testLW True to enable, false to disable. Defaults to true.
|
||||
* @throws if called in test mode.
|
||||
*/
|
||||
void EnableLiveWindowInTest(bool testLW);
|
||||
|
||||
/**
|
||||
* Whether LiveWindow operation is enabled during test mode.
|
||||
*/
|
||||
bool IsLiveWindowEnabledInTest();
|
||||
|
||||
/**
|
||||
* Gets time period between calls to Periodic() functions.
|
||||
*/
|
||||
@@ -228,6 +241,7 @@ class IterativeRobotBase : public RobotBase {
|
||||
units::second_t m_period;
|
||||
Watchdog m_watchdog;
|
||||
bool m_ntFlushEnabled = true;
|
||||
bool m_lwEnabledInTest = true;
|
||||
|
||||
void PrintLoopOverrunMessage();
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
#include "frc/livewindow/LiveWindow.h"
|
||||
#include "frc/simulation/DriverStationSim.h"
|
||||
#include "frc/simulation/SimHooks.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -16,7 +17,7 @@
|
||||
using namespace frc;
|
||||
|
||||
namespace {
|
||||
class TimedRobotTest : public ::testing::Test {
|
||||
class TimedRobotTest : public ::testing::TestWithParam<bool> {
|
||||
protected:
|
||||
void SetUp() override { frc::sim::PauseTiming(); }
|
||||
|
||||
@@ -304,8 +305,11 @@ TEST_F(TimedRobotTest, TeleopMode) {
|
||||
robotThread.join();
|
||||
}
|
||||
|
||||
TEST_F(TimedRobotTest, TestMode) {
|
||||
TEST_P(TimedRobotTest, TestMode) {
|
||||
bool isTestLW = GetParam();
|
||||
|
||||
MockRobot robot;
|
||||
robot.EnableLiveWindowInTest(isTestLW);
|
||||
|
||||
std::thread robotThread{[&] { robot.StartCompetition(); }};
|
||||
|
||||
@@ -321,6 +325,7 @@ TEST_F(TimedRobotTest, TestMode) {
|
||||
EXPECT_EQ(0u, robot.m_autonomousInitCount);
|
||||
EXPECT_EQ(0u, robot.m_teleopInitCount);
|
||||
EXPECT_EQ(0u, robot.m_testInitCount);
|
||||
EXPECT_FALSE(frc::LiveWindow::IsEnabled());
|
||||
|
||||
EXPECT_EQ(0u, robot.m_robotPeriodicCount);
|
||||
EXPECT_EQ(0u, robot.m_simulationPeriodicCount);
|
||||
@@ -342,6 +347,9 @@ TEST_F(TimedRobotTest, TestMode) {
|
||||
EXPECT_EQ(0u, robot.m_autonomousInitCount);
|
||||
EXPECT_EQ(0u, robot.m_teleopInitCount);
|
||||
EXPECT_EQ(1u, robot.m_testInitCount);
|
||||
EXPECT_EQ(isTestLW, frc::LiveWindow::IsEnabled());
|
||||
|
||||
EXPECT_THROW(robot.EnableLiveWindowInTest(isTestLW), std::runtime_error);
|
||||
|
||||
EXPECT_EQ(1u, robot.m_robotPeriodicCount);
|
||||
EXPECT_EQ(1u, robot.m_simulationPeriodicCount);
|
||||
@@ -376,6 +384,32 @@ TEST_F(TimedRobotTest, TestMode) {
|
||||
EXPECT_EQ(0u, robot.m_teleopExitCount);
|
||||
EXPECT_EQ(0u, robot.m_testExitCount);
|
||||
|
||||
frc::sim::DriverStationSim::SetEnabled(false);
|
||||
frc::sim::DriverStationSim::SetAutonomous(false);
|
||||
frc::sim::DriverStationSim::SetTest(false);
|
||||
frc::sim::DriverStationSim::NotifyNewData();
|
||||
frc::sim::StepTiming(20_ms); // Wait for Notifiers
|
||||
|
||||
EXPECT_EQ(1u, robot.m_robotInitCount);
|
||||
EXPECT_EQ(1u, robot.m_simulationInitCount);
|
||||
EXPECT_EQ(1u, robot.m_disabledInitCount);
|
||||
EXPECT_EQ(0u, robot.m_autonomousInitCount);
|
||||
EXPECT_EQ(0u, robot.m_teleopInitCount);
|
||||
EXPECT_EQ(1u, robot.m_testInitCount);
|
||||
EXPECT_FALSE(frc::LiveWindow::IsEnabled());
|
||||
|
||||
EXPECT_EQ(3u, robot.m_robotPeriodicCount);
|
||||
EXPECT_EQ(3u, robot.m_simulationPeriodicCount);
|
||||
EXPECT_EQ(1u, robot.m_disabledPeriodicCount);
|
||||
EXPECT_EQ(0u, robot.m_autonomousPeriodicCount);
|
||||
EXPECT_EQ(0u, robot.m_teleopPeriodicCount);
|
||||
EXPECT_EQ(2u, robot.m_testPeriodicCount);
|
||||
|
||||
EXPECT_EQ(0u, robot.m_disabledExitCount);
|
||||
EXPECT_EQ(0u, robot.m_autonomousExitCount);
|
||||
EXPECT_EQ(0u, robot.m_teleopExitCount);
|
||||
EXPECT_EQ(1u, robot.m_testExitCount);
|
||||
|
||||
robot.EndCompetition();
|
||||
robotThread.join();
|
||||
}
|
||||
@@ -572,3 +606,5 @@ TEST_F(TimedRobotTest, AddPeriodicWithOffset) {
|
||||
robot.EndCompetition();
|
||||
robotThread.join();
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(TimedRobotTests, TimedRobotTest, testing::Bool());
|
||||
|
||||
Reference in New Issue
Block a user