[wpilib] Add option to flush NetworkTables every iterative loop

This functionality is disabled by default, but can be enabled by the user
program by calling setNetworkTablesFlushEnabled.
This commit is contained in:
Peter Johnson
2020-12-12 23:16:50 -08:00
parent acfbb1a44a
commit 581b7ec553
3 changed files with 34 additions and 0 deletions

View File

@@ -8,6 +8,7 @@
#include "frc/IterativeRobotBase.h"
#include <hal/DriverStation.h>
#include <networktables/NetworkTableInstance.h>
#include <wpi/Format.h>
#include <wpi/SmallString.h>
#include <wpi/raw_ostream.h>
@@ -98,6 +99,10 @@ void IterativeRobotBase::TestPeriodic() {
}
}
void IterativeRobotBase::SetNetworkTablesFlushEnabled(bool enabled) {
m_ntFlushEnabled = enabled;
}
void IterativeRobotBase::LoopFunc() {
m_watchdog.Reset();
@@ -179,6 +184,9 @@ void IterativeRobotBase::LoopFunc() {
m_watchdog.Disable();
// Flush NetworkTables
if (m_ntFlushEnabled) nt::NetworkTableInstance::GetDefault().Flush();
// Warn on loop time overruns
if (m_watchdog.IsExpired()) {
m_watchdog.PrintEpochs();

View File

@@ -155,6 +155,14 @@ class IterativeRobotBase : public RobotBase {
*/
virtual void TestPeriodic();
/**
* Enables or disables flushing NetworkTables every loop iteration.
* By default, this is disabled.
*
* @param enabled True to enable, false to disable
*/
void SetNetworkTablesFlushEnabled(bool enabled);
/**
* Constructor for IterativeRobotBase.
*
@@ -188,6 +196,7 @@ class IterativeRobotBase : public RobotBase {
Mode m_lastMode = Mode::kNone;
Watchdog m_watchdog;
bool m_ntFlushEnabled = false;
void PrintLoopOverrunMessage();
};

View File

@@ -8,6 +8,7 @@
package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
@@ -55,6 +56,7 @@ public abstract class IterativeRobotBase extends RobotBase {
private Mode m_lastMode = Mode.kNone;
private final Watchdog m_watchdog;
private boolean m_ntFlushEnabled;
/**
* Constructor for IterativeRobotBase.
@@ -218,6 +220,16 @@ public abstract class IterativeRobotBase extends RobotBase {
}
}
/**
* Enables or disables flushing NetworkTables every loop iteration.
* By default, this is disabled.
*
* @param enabled True to enable, false to disable
*/
public void setNetworkTablesFlushEnabled(boolean enabled) {
m_ntFlushEnabled = enabled;
}
@SuppressWarnings("PMD.CyclomaticComplexity")
protected void loopFunc() {
m_watchdog.reset();
@@ -300,6 +312,11 @@ public abstract class IterativeRobotBase extends RobotBase {
m_watchdog.disable();
// Flush NetworkTables
if (m_ntFlushEnabled) {
NetworkTableInstance.getDefault().flush();
}
// Warn on loop time overruns
if (m_watchdog.isExpired()) {
m_watchdog.printEpochs();