Last active
December 4, 2023 12:27
-
-
Save tervay/26577b345e828f2c2187d7991dc77d37 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package frc.robot; | |
| import java.util.ArrayList; | |
| import edu.wpi.first.math.controller.PIDController; | |
| import edu.wpi.first.wpilibj.TimedRobot; | |
| public class Robot extends TimedRobot { | |
| PIDController controller = new PIDController(0, 0, 0); | |
| // 2 ways to use it, you can set the consumer at init or later | |
| TunableNT4 kP = new TunableNT4("kP", 0.01, (x) -> { | |
| controller.setP(x); | |
| }); | |
| TunableNT4 kD = new TunableNT4("kD", 0.2); | |
| @Override | |
| public void robotInit() { | |
| kD.addHook((x) -> { | |
| controller.setD(x); | |
| }); | |
| } | |
| } |
This file contains hidden or 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
| package frc.robot; | |
| import java.util.EnumSet; | |
| import java.util.function.Consumer; | |
| import edu.wpi.first.networktables.DoubleSubscriber; | |
| import edu.wpi.first.networktables.DoubleTopic; | |
| import edu.wpi.first.networktables.NetworkTableEvent; | |
| import edu.wpi.first.networktables.NetworkTableInstance; | |
| import edu.wpi.first.wpilibj.DriverStation; | |
| public class TunableNT4 { | |
| private DoubleSubscriber ntSub; | |
| public TunableNT4(String key, double defaultValue) { | |
| DoubleTopic topic = NetworkTableInstance.getDefault().getTable("Tunables").getDoubleTopic(key); | |
| topic.publish().set(defaultValue); | |
| ntSub = topic.subscribe(defaultValue); | |
| } | |
| public TunableNT4(String key, double defaultValue, Consumer<Double> consumer) { | |
| this(key, defaultValue); | |
| addHook(consumer); | |
| } | |
| public void addHook(Consumer<Double> consumer) { | |
| if (!DriverStation.isFMSAttached()) { | |
| NetworkTableInstance.getDefault().addListener(ntSub, EnumSet.of(NetworkTableEvent.Kind.kValueAll), | |
| event -> { | |
| consumer.accept(event.valueData.value.getDouble()); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment