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] Measure unit metadata #7653

Open
wants to merge 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,24 @@ default <S> void log(String identifier, Collection<S> value, Struct<S> struct) {
log(identifier, array, struct);
}

/**
* Logs a unit's symbol.
*
* @param identifier the identifier of the data field
* @param value the new value of the data field
*/
default void log(String identifier, Unit value) {
log(identifier, value.symbol());
}

/**
* Logs a measurement's value in terms of its base unit.
*
* <p>If the base unit is different from a prior call with the same identifier the value will be
* ignored.
*
* @param identifier the identifier of the data field
* @param value the new value of the data field
* @param <U> the dimension of the unit
*/
default <U extends Unit> void log(String identifier, Measure<U> value) {
log(identifier, value, value.baseUnit());
Expand All @@ -213,7 +225,7 @@ default <U extends Unit> void log(String identifier, Measure<U> value) {
* @param <U> the dimension of the unit
*/
default <U extends Unit> void log(String identifier, Measure<U> value, U unit) {
log(identifier + " (" + unit.symbol() + ")", value.in(unit));
log(identifier, value.in(unit));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;

import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Unit;
import edu.wpi.first.util.datalog.BooleanArrayLogEntry;
import edu.wpi.first.util.datalog.BooleanLogEntry;
import edu.wpi.first.util.datalog.DataLog;
Expand All @@ -31,6 +33,7 @@ public class FileBackend implements EpilogueBackend {
private final DataLog m_dataLog;
private final Map<String, DataLogEntry> m_entries = new HashMap<>();
private final Map<String, NestedBackend> m_subLoggers = new HashMap<>();
private final Map<String, Unit> m_units = new HashMap<>();

/**
* Creates a new file-based backend.
Expand Down Expand Up @@ -141,4 +144,15 @@ public <S> void log(String identifier, S[] value, Struct<S> struct) {
m_dataLog.addSchema(struct);
getEntry(identifier, (log, k) -> StructArrayLogEntry.create(log, k, struct)).append(value);
}

@Override
public <U extends Unit> void log(String identifier, Measure<U> value, U unit) {
DoubleLogEntry entry = getEntry(identifier, DoubleLogEntry::new);
entry.append(value.in(unit));
Unit cachedUnit = m_units.get(identifier);
if (m_units.containsKey(identifier) || !cachedUnit.equivalent(unit)) {
entry.setMetadata("{ \"unit\": \"" + unit.symbol() + "\" }");
m_units.put(identifier, unit);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import edu.wpi.first.networktables.StringPublisher;
import edu.wpi.first.networktables.StructArrayPublisher;
import edu.wpi.first.networktables.StructPublisher;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Unit;
import edu.wpi.first.util.struct.Struct;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -32,6 +34,7 @@ public class NTEpilogueBackend implements EpilogueBackend {

private final Map<String, Publisher> m_publishers = new HashMap<>();
private final Map<String, NestedBackend> m_nestedBackends = new HashMap<>();
private final Map<String, Unit> m_units = new HashMap<>();

/**
* Creates a logging backend that sends information to NetworkTables.
Expand Down Expand Up @@ -164,4 +167,17 @@ public <S> void log(String identifier, S[] value, Struct<S> struct) {
identifier, k -> m_nt.getStructArrayTopic(k, struct).publish()))
.set(value);
}

@Override
public <U extends Unit> void log(String identifier, Measure<U> value, U unit) {
DoublePublisher pub =
(DoublePublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getDoubleTopic(k).publish());
pub.set(value.in(unit));
Unit cachedUnit = m_units.get(identifier);
if (cachedUnit == null || !cachedUnit.equivalent(unit)) {
pub.getTopic().setProperty(identifier, identifier);
m_units.put(identifier, unit);
}
}
}
Loading