Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[epilogue] Backend refactoring and interface fixes #7548

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions epilogue-runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencies {
api(project(':ntcore'))
api(project(':wpiutil'))
api(project(':wpiunits'))
api(project(':wpilibj'))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
package edu.wpi.first.epilogue;

import edu.wpi.first.epilogue.logging.EpilogueBackend;
import edu.wpi.first.epilogue.logging.NTEpilogueBackend;
import edu.wpi.first.epilogue.logging.NTBackend;
import edu.wpi.first.epilogue.logging.errors.ErrorHandler;
import edu.wpi.first.epilogue.logging.errors.ErrorPrinter;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.units.measure.Time;

/**
Expand All @@ -22,7 +21,7 @@ public class EpilogueConfiguration {
* NetworkTables. NetworkTable data can be mirrored to a log file on disk by calling {@code
* DataLogManager.start()} in your {@code robotInit} method.
*/
public EpilogueBackend backend = new NTEpilogueBackend(NetworkTableInstance.getDefault());
public EpilogueBackend backend = new NTBackend();

/**
* The period Epilogue will log at. By default this is the period that the robot runs at. This is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import edu.wpi.first.util.datalog.StructArrayLogEntry;
import edu.wpi.first.util.datalog.StructLogEntry;
import edu.wpi.first.util.struct.Struct;
import edu.wpi.first.wpilibj.DataLogManager;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
Expand All @@ -37,10 +38,20 @@ public class FileBackend implements EpilogueBackend {
*
* @param dataLog the data log to save data to
*/
public FileBackend(DataLog dataLog) {
FileBackend(DataLog dataLog) {
this.m_dataLog = requireNonNullParam(dataLog, "dataLog", "FileBackend");
}

/**
* Creates a new file-based backend.
*
* @param enableNtCapture enables capture of networktables data to datalog if true
*/
public FileBackend(boolean enableNtCapture) {
this.m_dataLog = DataLogManager.getLog();
DataLogManager.logNetworkTables(enableNtCapture);
}

@Override
public EpilogueBackend getNested(String path) {
return m_subLoggers.computeIfAbsent(path, k -> new NestedBackend(k, this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import edu.wpi.first.util.struct.Struct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BooleanSupplier;

/**
* A backend implementation that sends data over network tables. Be careful when using this, since
* sending too much data may cause bandwidth or CPU starvation.
*/
public class NTEpilogueBackend implements EpilogueBackend {
public class NTBackend implements EpilogueBackend {
private final NetworkTableInstance m_nt;
private BooleanSupplier m_disabledSupplier;

private final Map<String, Publisher> m_publishers = new HashMap<>();
private final Map<String, NestedBackend> m_nestedBackends = new HashMap<>();
Expand All @@ -38,52 +40,92 @@ public class NTEpilogueBackend implements EpilogueBackend {
*
* @param nt the NetworkTable instance to use to send data to
*/
public NTEpilogueBackend(NetworkTableInstance nt) {
NTBackend(NetworkTableInstance nt) {
this.m_nt = nt;
}

/** Creates a logging backend that sends information to NetworkTables. */
public NTBackend() {
this.m_nt = NetworkTableInstance.getDefault();
}

/**
* Creates a new NTBackend that is disabled whenever the disabledSupplier returns true.
*
* @param disabledSupplier the disable condition for NT logging
* @return a new NTBackend
*/
public NTBackend disableWhen(BooleanSupplier disabledSupplier) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this better than just changing the backend on the fly?

if (condition()) {
  Epilogue.configure(config -> {
    config.backend = new NullBackend();
  });
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more succint, and prevents you from having to cache different kinds of backends yourself.
For instance, if you only want NT logging enabled when fms is not attached, you would have to cache a file only backend, a file and nt backend, and use an addPeriodic call.

In addition, the configure() method accepts a lambda, and if it is called every loop, it will spawn a new object every loop as well

var newBackend = new NTBackend(this.m_nt);
if (m_disabledSupplier == null) {
newBackend.m_disabledSupplier = disabledSupplier;
} else {
newBackend.m_disabledSupplier =
() -> m_disabledSupplier.getAsBoolean() || disabledSupplier.getAsBoolean();
}
return newBackend;
}

@Override
public EpilogueBackend getNested(String path) {
return m_nestedBackends.computeIfAbsent(path, k -> new NestedBackend(k, this));
}

@Override
public void log(String identifier, int value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((IntegerPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getIntegerTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, long value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((IntegerPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getIntegerTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, float value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((FloatPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getFloatTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, double value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((DoublePublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getDoubleTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, boolean value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((BooleanPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getBooleanTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, byte[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((RawPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getRawTopic(k).publish("raw")))
.set(value);
Expand All @@ -92,6 +134,9 @@ public void log(String identifier, byte[] value) {
@Override
@SuppressWarnings("PMD.UnnecessaryCastRule")
public void log(String identifier, int[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
// NT backend only supports int64[], so we have to manually widen to 64 bits before sending
long[] widened = new long[value.length];

Expand All @@ -106,41 +151,59 @@ public void log(String identifier, int[] value) {

@Override
public void log(String identifier, long[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((IntegerArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getIntegerArrayTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, float[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((FloatArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getFloatArrayTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, double[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((DoubleArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getDoubleArrayTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, boolean[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((BooleanArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getBooleanArrayTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, String value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((StringPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getStringTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, String[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((StringArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getStringArrayTopic(k).publish()))
.set(value);
Expand All @@ -149,6 +212,9 @@ public void log(String identifier, String[] value) {
@Override
@SuppressWarnings("unchecked")
public <S> void log(String identifier, S value, Struct<S> struct) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
m_nt.addSchema(struct);
((StructPublisher<S>)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getStructTopic(k, struct).publish()))
Expand All @@ -158,6 +224,9 @@ public <S> void log(String identifier, S value, Struct<S> struct) {
@Override
@SuppressWarnings("unchecked")
public <S> void log(String identifier, S[] value, Struct<S> struct) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
m_nt.addSchema(struct);
((StructArrayPublisher<S>)
m_publishers.computeIfAbsent(
Expand Down
Loading