From 6f113bdfd8a2a64475eb7d0a684945d35e43dada Mon Sep 17 00:00:00 2001 From: oh-yes-0-fps Date: Tue, 7 Jan 2025 14:29:59 -0500 Subject: [PATCH 1/3] add measures to smartdashboard --- .../smartdashboard/SmartDashboard.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java index 81fbaa6c197..5c69406ee63 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java @@ -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; @@ -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 boolean putMeasure(String key, Measure value, U unit) { + NetworkTableEntry entry = getEntry(key); + if (entry.getType() != NetworkTableType.kDouble) { + return false; + } + String publishedUnitSymbol = entry.getTopic().getProperty("unit"); + if (publishedUnitSymbol == "null") { + 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 boolean putMeasure(String key, Measure 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 Measure getMeasure(String key, Measure defaultValue) { + NetworkTableEntry entry = getEntry(key); + if (entry.getType() != NetworkTableType.kDouble) { + return defaultValue; + } + String publishedUnitSymbol = entry.getTopic().getProperty("unit"); + if (publishedUnitSymbol == "null") { + return defaultValue; + } + U unit = defaultValue.baseUnit(); + if (!publishedUnitSymbol.equals(unit.symbol())) { + return defaultValue; + } + return (Measure) 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()}. From b5cd08c1173650f62307f971dce5d4bf53100611 Mon Sep 17 00:00:00 2001 From: oh-yes-0-fps Date: Tue, 7 Jan 2025 14:44:53 -0500 Subject: [PATCH 2/3] fixed formatting and checkstyle --- .../wpilibj/smartdashboard/SmartDashboard.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java index 5c69406ee63..97f19f3a159 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java @@ -504,7 +504,7 @@ public static byte[] getRaw(String key, byte[] 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 @@ -515,7 +515,7 @@ public static boolean putMeasure(String key, Measure value, return false; } String publishedUnitSymbol = entry.getTopic().getProperty("unit"); - if (publishedUnitSymbol == "null") { + if (publishedUnitSymbol.equals("null")) { entry.getTopic().setProperty("unit", unit.symbol()); } else if (!publishedUnitSymbol.equals(unit.symbol())) { return false; @@ -526,7 +526,7 @@ public static boolean putMeasure(String key, Measure value, /** * 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 @@ -536,13 +536,13 @@ public static boolean putMeasure(String key, Measure value) } /** - * 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. - * + * 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 + * associated with the key */ @SuppressWarnings("unchecked") public static Measure getMeasure(String key, Measure defaultValue) { @@ -551,7 +551,7 @@ public static Measure getMeasure(String key, Measure defa return defaultValue; } String publishedUnitSymbol = entry.getTopic().getProperty("unit"); - if (publishedUnitSymbol == "null") { + if (publishedUnitSymbol.equals("null")) { return defaultValue; } U unit = defaultValue.baseUnit(); From 672e2b828ce2485d59809d5645af4b0b0a47d825 Mon Sep 17 00:00:00 2001 From: oh-yes-0-fps Date: Tue, 7 Jan 2025 15:20:03 -0500 Subject: [PATCH 3/3] formatted --- .../edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java index 97f19f3a159..0478ba252ed 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java @@ -515,7 +515,7 @@ public static boolean putMeasure(String key, Measure value, return false; } String publishedUnitSymbol = entry.getTopic().getProperty("unit"); - if (publishedUnitSymbol.equals("null")) { + if ("null".equals(publishedUnitSymbol)) { entry.getTopic().setProperty("unit", unit.symbol()); } else if (!publishedUnitSymbol.equals(unit.symbol())) { return false; @@ -551,7 +551,7 @@ public static Measure getMeasure(String key, Measure defa return defaultValue; } String publishedUnitSymbol = entry.getTopic().getProperty("unit"); - if (publishedUnitSymbol.equals("null")) { + if ("null".equals(publishedUnitSymbol)) { return defaultValue; } U unit = defaultValue.baseUnit();