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

[smartdashboard-api-java] Add measures support to smartdashboard #7654

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -10,6 +10,9 @@
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.NetworkTableType;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Unit;
import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.util.sendable.SendableRegistry;
import java.util.HashMap;
Expand Down Expand Up @@ -499,6 +502,65 @@ public static byte[] getRaw(String key, byte[] defaultValue) {
return getEntry(key).getRaw(defaultValue);
}

/**
* Put a value with units in the table.
*
* @param key the key to be assigned to
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
public static <U extends Unit> boolean putMeasure(String key, Measure<U> value, U unit) {
NetworkTableEntry entry = getEntry(key);
if (entry.getType() != NetworkTableType.kDouble) {
return false;
}
String publishedUnitSymbol = entry.getTopic().getProperty("unit");
if ("null".equals(publishedUnitSymbol)) {
entry.getTopic().setProperty("unit", unit.symbol());
} else if (!publishedUnitSymbol.equals(unit.symbol())) {
return false;
}
entry.setDouble(value.in(unit));
return true;
}

/**
* Put a value with units in the table.
*
* @param key the key to be assigned to
* @param value the value that will be assigned
* @return False if the table key already exists with a different type
*/
public static <U extends Unit> boolean putMeasure(String key, Measure<U> value) {
return putMeasure(key, value, value.baseUnit());
}

/**
* Returns the value with units the key maps to. If the key does not exist or is of different
* type, it will return the default value.
*
* @param key the key to look up
* @param defaultValue the value to be returned if no value is found
* @return the value associated with the given key or the given default value if there is no value
* associated with the key
*/
@SuppressWarnings("unchecked")
public static <U extends Unit> Measure<U> getMeasure(String key, Measure<U> defaultValue) {
NetworkTableEntry entry = getEntry(key);
if (entry.getType() != NetworkTableType.kDouble) {
return defaultValue;
}
String publishedUnitSymbol = entry.getTopic().getProperty("unit");
if ("null".equals(publishedUnitSymbol)) {
return defaultValue;
}
U unit = defaultValue.baseUnit();
if (!publishedUnitSymbol.equals(unit.symbol())) {
return defaultValue;
}
return (Measure<U>) unit.of(entry.getDouble(defaultValue.in(unit)));
}

/**
* Posts a task from a listener to the ListenerExecutor, so that it can be run synchronously from
* the main loop on the next call to {@link SmartDashboard#updateValues()}.
Expand Down
Loading