generated from wpilibsuite/vendor-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddc04e0
commit 531a9e3
Showing
5 changed files
with
288 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) 2024 FRC 6328 | ||
// http://github.com/Mechanical-Advantage | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file at | ||
// the root directory of this project. | ||
|
||
package coppercore.controls; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BooleanSupplier; | ||
import java.util.function.Consumer; | ||
import java.util.function.DoubleSupplier; | ||
import org.littletonrobotics.junction.networktables.LoggedDashboardNumber; | ||
|
||
/** | ||
* Class for a tunable number. Gets value from dashboard in tuning mode, returns default if not or | ||
* value not in dashboard. | ||
*/ | ||
public class LoggedTunableNumber implements DoubleSupplier { | ||
private static final String tableKey = "TunableNumbers"; | ||
|
||
private final String key; | ||
private boolean hasDefault = false; | ||
private double defaultValue; | ||
private LoggedDashboardNumber dashboardNumber; | ||
private Map<Integer, Double> lastHasChangedValues = new HashMap<>(); | ||
private BooleanSupplier condition = () -> true; | ||
|
||
/** | ||
* Create a new LoggedTunableNumber with the default value | ||
* | ||
* @param dashboardKey Key on dashboard | ||
* @param defaultValue Default value | ||
*/ | ||
public LoggedTunableNumber(String dashboardKey, double defaultValue) { | ||
this.key = tableKey + "/" + dashboardKey; | ||
initDefault(defaultValue); | ||
} | ||
|
||
/** | ||
* Set the default value of the number. The default value can only be set once. | ||
* | ||
* @param defaultValue The default value | ||
*/ | ||
private void initDefault(double defaultValue) { | ||
if (!hasDefault) { | ||
hasDefault = true; | ||
this.defaultValue = defaultValue; | ||
if (condition.getAsBoolean()) { | ||
dashboardNumber = new LoggedDashboardNumber(key, defaultValue); | ||
} | ||
} | ||
} | ||
|
||
public void setInitCondition(BooleanSupplier condition) { | ||
this.condition = condition; | ||
} | ||
|
||
/** | ||
* Get the current value, from dashboard if available and in tuning mode. | ||
* | ||
* @return The current value | ||
*/ | ||
@Override | ||
public double getAsDouble() { | ||
if (!hasDefault) { | ||
return 0.0; | ||
} else { | ||
return condition.getAsBoolean() && dashboardNumber != null | ||
? dashboardNumber.get() | ||
: defaultValue; | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether the number has changed since our last check | ||
* | ||
* @param id Unique identifier for the caller to avoid conflicts when shared between multiple | ||
* objects. Recommended approach is to pass the result of "hashCode()" | ||
* @return True if the number has changed since the last time this method was called, false | ||
* otherwise. | ||
*/ | ||
public boolean hasChanged(int id) { | ||
double currentValue = getAsDouble(); | ||
Double lastValue = lastHasChangedValues.get(id); | ||
if (lastValue == null || currentValue != lastValue) { | ||
lastHasChangedValues.put(id, currentValue); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Runs action if any of the tunableNumbers have changed | ||
* | ||
* @param id Unique identifier for the caller to avoid conflicts when shared between multiple * | ||
* objects. Recommended approach is to pass the result of "hashCode()" | ||
* @param action Callback to run when any of the tunable numbers have changed. Access tunable | ||
* numbers in order inputted in method | ||
* @param tunableNumbers All tunable numbers to check | ||
*/ | ||
public static void ifChanged( | ||
int id, Consumer<double[]> action, LoggedTunableNumber... tunableNumbers) { | ||
if (Arrays.stream(tunableNumbers).anyMatch(tunableNumber -> tunableNumber.hasChanged(id))) { | ||
action.accept( | ||
Arrays.stream(tunableNumbers) | ||
.mapToDouble(LoggedTunableNumber::getAsDouble) | ||
.toArray()); | ||
} | ||
} | ||
|
||
/** Runs action if any of the tunableNumbers have changed */ | ||
public static void ifChanged(int id, Runnable action, LoggedTunableNumber... tunableNumbers) { | ||
ifChanged(id, values -> action.run(), tunableNumbers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"fileName": "AdvantageKit.json", | ||
"name": "AdvantageKit", | ||
"version": "3.2.1", | ||
"uuid": "d820cc26-74e3-11ec-90d6-0242ac120003", | ||
"frcYear": "2024", | ||
"mavenUrls": [], | ||
"jsonUrl": "https://github.com/Mechanical-Advantage/AdvantageKit/releases/latest/download/AdvantageKit.json", | ||
"javaDependencies": [ | ||
{ | ||
"groupId": "org.littletonrobotics.akit.junction", | ||
"artifactId": "wpilib-shim", | ||
"version": "3.2.1" | ||
}, | ||
{ | ||
"groupId": "org.littletonrobotics.akit.junction", | ||
"artifactId": "junction-core", | ||
"version": "3.2.1" | ||
}, | ||
{ | ||
"groupId": "org.littletonrobotics.akit.conduit", | ||
"artifactId": "conduit-api", | ||
"version": "3.2.1" | ||
} | ||
], | ||
"jniDependencies": [ | ||
{ | ||
"groupId": "org.littletonrobotics.akit.conduit", | ||
"artifactId": "conduit-wpilibio", | ||
"version": "3.2.1", | ||
"skipInvalidPlatforms": false, | ||
"isJar": false, | ||
"validPlatforms": [ | ||
"linuxathena", | ||
"windowsx86-64", | ||
"linuxx86-64", | ||
"osxuniversal" | ||
] | ||
} | ||
], | ||
"cppDependencies": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"fileName": "WPILibNewCommands.json", | ||
"name": "WPILib-New-Commands", | ||
"version": "1.0.0", | ||
"uuid": "111e20f7-815e-48f8-9dd6-e675ce75b266", | ||
"frcYear": "2024", | ||
"mavenUrls": [], | ||
"jsonUrl": "", | ||
"javaDependencies": [ | ||
{ | ||
"groupId": "edu.wpi.first.wpilibNewCommands", | ||
"artifactId": "wpilibNewCommands-java", | ||
"version": "wpilib" | ||
} | ||
], | ||
"jniDependencies": [], | ||
"cppDependencies": [ | ||
{ | ||
"groupId": "edu.wpi.first.wpilibNewCommands", | ||
"artifactId": "wpilibNewCommands-cpp", | ||
"version": "wpilib", | ||
"libName": "wpilibNewCommands", | ||
"headerClassifier": "headers", | ||
"sourcesClassifier": "sources", | ||
"sharedLibrary": true, | ||
"skipInvalidPlatforms": true, | ||
"binaryPlatforms": [ | ||
"linuxathena", | ||
"linuxarm32", | ||
"linuxarm64", | ||
"windowsx86-64", | ||
"windowsx86", | ||
"linuxx86-64", | ||
"osxuniversal" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters