From 5661b59407354e87c91afaa8f9fc646d9a89e8e3 Mon Sep 17 00:00:00 2001 From: Mason-Lam <97353903+Mason-Lam@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:36:47 -0800 Subject: [PATCH] NarDash changes --- build.gradle | 10 + .../java/frc/team3128/RobotContainer.java | 17 +- .../frc/team3128/autonomous/AutoPrograms.java | 8 +- .../narwhaldashboard/ClientInformation.java | 10 - .../narwhaldashboard/DashButtonCallback.java | 8 - .../narwhaldashboard/NarwhalDashboard.java | 346 ------------------ .../NumericalDataCallback.java | 8 - vendordeps/3128-common.json | 19 + vendordeps/AdvantageKit.json | 41 +++ 9 files changed, 85 insertions(+), 382 deletions(-) delete mode 100644 src/main/java/frc/team3128/common/narwhaldashboard/ClientInformation.java delete mode 100644 src/main/java/frc/team3128/common/narwhaldashboard/DashButtonCallback.java delete mode 100644 src/main/java/frc/team3128/common/narwhaldashboard/NarwhalDashboard.java delete mode 100644 src/main/java/frc/team3128/common/narwhaldashboard/NumericalDataCallback.java create mode 100644 vendordeps/3128-common.json create mode 100644 vendordeps/AdvantageKit.json diff --git a/build.gradle b/build.gradle index 2e8bc6d..3e7a304 100644 --- a/build.gradle +++ b/build.gradle @@ -10,6 +10,16 @@ def ROBOT_MAIN_CLASS = "frc.team3128.Main" // Define my targets (RoboRIO) and artifacts (deployable files) // This is added by GradleRIO's backing project DeployUtils. +repositories { + maven { + url = uri("https://maven.pkg.github.com/Mechanical-Advantage/AdvantageKit") + credentials { + username = "Mechanical-Advantage-Bot" + password = "\u0067\u0068\u0070\u005f\u006e\u0056\u0051\u006a\u0055\u004f\u004c\u0061\u0079\u0066\u006e\u0078\u006e\u0037\u0051\u0049\u0054\u0042\u0032\u004c\u004a\u006d\u0055\u0070\u0073\u0031\u006d\u0037\u004c\u005a\u0030\u0076\u0062\u0070\u0063\u0051" + } + } +} + deploy { targets { roborio(getTargetTypeClass('RoboRIO')) { diff --git a/src/main/java/frc/team3128/RobotContainer.java b/src/main/java/frc/team3128/RobotContainer.java index 1179974..28f7bb9 100644 --- a/src/main/java/frc/team3128/RobotContainer.java +++ b/src/main/java/frc/team3128/RobotContainer.java @@ -11,10 +11,11 @@ import frc.team3128.commands.CmdSwerveDrive; import frc.team3128.PositionConstants.Position; import static frc.team3128.commands.CmdManager.*; + +import common.utility.narwhaldashboard.NarwhalDashboard; import frc.team3128.common.hardware.input.NAR_ButtonBoard; import frc.team3128.common.hardware.input.NAR_Joystick; import frc.team3128.common.hardware.input.NAR_XboxController; -import frc.team3128.common.narwhaldashboard.NarwhalDashboard; import frc.team3128.common.utility.NAR_Shuffleboard; import frc.team3128.subsystems.Swerve; import frc.team3128.subsystems.Vision; @@ -34,6 +35,8 @@ public class RobotContainer { public static NAR_XboxController controller; + private NarwhalDashboard dashboard; + public RobotContainer() { swerve = Swerve.getInstance(); @@ -180,15 +183,15 @@ private void initDashboard() { swerve.initShuffleboard(); Vision.getInstance().initShuffleboard(); - NarwhalDashboard.startServer(); + dashboard = NarwhalDashboard.getInstance(); + dashboard.addUpdate("time", ()-> Timer.getMatchTime()); + dashboard.addUpdate("voltage",()-> RobotController.getBatteryVoltage()); + dashboard.addUpdate("x", ()-> swerve.getPose().getX()); + dashboard.addUpdate("y", ()-> swerve.getPose().getY()); } public void updateDashboard() { - NarwhalDashboard.put("time", Timer.getMatchTime()); - NarwhalDashboard.put("voltage", RobotController.getBatteryVoltage()); - NarwhalDashboard.put("x", swerve.getPose().getX()); - NarwhalDashboard.put("y", swerve.getPose().getY()); - + dashboard.update(); if (DriverStation.getMatchType() == MatchType.None) { NAR_Shuffleboard.update(); } diff --git a/src/main/java/frc/team3128/autonomous/AutoPrograms.java b/src/main/java/frc/team3128/autonomous/AutoPrograms.java index 6c563ab..823ab40 100644 --- a/src/main/java/frc/team3128/autonomous/AutoPrograms.java +++ b/src/main/java/frc/team3128/autonomous/AutoPrograms.java @@ -6,9 +6,10 @@ import static edu.wpi.first.wpilibj2.command.Commands.*; import frc.team3128.PositionConstants.Position; import frc.team3128.commands.CmdAutoBalance; -import frc.team3128.common.narwhaldashboard.NarwhalDashboard; import static frc.team3128.commands.CmdManager.*; +import common.utility.narwhaldashboard.NarwhalDashboard; + /** * Class to store information about autonomous routines. * @author Daniel Wang, Mason Lam @@ -34,11 +35,12 @@ private void initAutoSelector() { "scuffedClimb" }; - NarwhalDashboard.addAutos(autoStrings); + NarwhalDashboard.getInstance().addAutos(autoStrings); } public Command getAutonomousCommand() { - String selectedAutoName = NarwhalDashboard.getSelectedAutoName(); + // String selectedAutoName = NarwhalDashboard.getInstance().selectedAutoName; + String selectedAutoName = null; final Command autoCommand; if (selectedAutoName == null) { diff --git a/src/main/java/frc/team3128/common/narwhaldashboard/ClientInformation.java b/src/main/java/frc/team3128/common/narwhaldashboard/ClientInformation.java deleted file mode 100644 index b47a878..0000000 --- a/src/main/java/frc/team3128/common/narwhaldashboard/ClientInformation.java +++ /dev/null @@ -1,10 +0,0 @@ -package frc.team3128.common.narwhaldashboard; - -import java.util.List; - -public class ClientInformation { - public int id; - - public boolean autosPushed; - public List unpushedData; -} \ No newline at end of file diff --git a/src/main/java/frc/team3128/common/narwhaldashboard/DashButtonCallback.java b/src/main/java/frc/team3128/common/narwhaldashboard/DashButtonCallback.java deleted file mode 100644 index df06aaa..0000000 --- a/src/main/java/frc/team3128/common/narwhaldashboard/DashButtonCallback.java +++ /dev/null @@ -1,8 +0,0 @@ -package frc.team3128.common.narwhaldashboard; - -/** - * A callback to be used with clickable buttons on NarwhalDashboard - */ -public interface DashButtonCallback { - void action(boolean down); -} \ No newline at end of file diff --git a/src/main/java/frc/team3128/common/narwhaldashboard/NarwhalDashboard.java b/src/main/java/frc/team3128/common/narwhaldashboard/NarwhalDashboard.java deleted file mode 100644 index 9cefb3d..0000000 --- a/src/main/java/frc/team3128/common/narwhaldashboard/NarwhalDashboard.java +++ /dev/null @@ -1,346 +0,0 @@ -package frc.team3128.common.narwhaldashboard; - -import java.lang.reflect.Array; -import java.lang.reflect.Field; -import java.net.InetSocketAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; - -import org.java_websocket.WebSocket; -import org.java_websocket.handshake.ClientHandshake; -import org.java_websocket.server.WebSocketServer; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; - -import frc.team3128.common.constantsint.ConstantsInt; -import frc.team3128.common.hardware.camera.*; -import frc.team3128.common.hardware.camera.NAR_Camera.Pipeline; -import frc.team3128.common.narwhaldashboard.DashButtonCallback; -import frc.team3128.common.narwhaldashboard.NumericalDataCallback; -import frc.team3128.common.utility.Log; -import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; -import edu.wpi.first.wpilibj2.command.Command; - -public class NarwhalDashboard extends WebSocketServer { - public static int cellSelectedPosX, cellSelectedPosY, gridNum; - public static Boolean buttonClicked = false; - public static Boolean gridSelected = false; - private static final int PORT = 5805; - private final static int UPDATE_WAVELENGTH = 100; - - public static int getUpdateWavelength() { - return UPDATE_WAVELENGTH; - } - - private static HashMap debugValues = new HashMap(); - private static ArrayList autoPrograms = new ArrayList(); - - private static HashMap buttons = new HashMap(); - private static HashMap numDataCallbacks = new HashMap(); - - private static HashMap limelights = new HashMap(); - - private static String selectedAuto = null; - private static String selectedLimelight = null; - private static boolean pushed = false; - private static volatile boolean constantsChanged = true; - - public NarwhalDashboard(int port) throws UnknownHostException { - super(new InetSocketAddress(port)); - } - - public NarwhalDashboard(InetSocketAddress address) { - super(address); - } - - /** - * Publishes a numerical value to NarwhalDashboard - */ - public static void put(String key, double value) { - put(key, Double.toString(value)); - } - - /** - * Publishes a boolean value to NarwhalDashboard - */ - public static void put(String key, boolean value) { - put(key, Boolean.toString(value)); - } - - /** - * Publishes a string value to NarwhalDashboard - */ - public static void put(String key, String value) { - debugValues.put(key, value); - } - - public static void addButton(String key, DashButtonCallback callback) { - buttons.put(key, callback); - } - - public static void addNumDataListener(String key, NumericalDataCallback callback) { - numDataCallbacks.put(key, callback); - } - - /** - * Clears the list of autonomous programs. - */ - public static void clearAutos() { - autoPrograms = new ArrayList(); - } - - /** - * Adds an autonomous program to NarwhalDashboard's auto picker - * - * @param name - The human-readable name of the autonomous program - */ - public static void addAutos(String[] names) { - autoPrograms.addAll(Arrays.asList(names)); - } - - public static void addLimelight(NAR_Camera light) { - limelights.put(light.get_name(), light); - } - - public static String getSelectedAutoName() { - return selectedAuto; - } - - public static void setSelectedLimelight(NAR_Camera ll){ - selectedLimelight = ll.get_name(); - } - - /** - * Starts the NarwhalDashboard server. This opens it up to be able to be - * connected to by client devices (the DS Laptop, a tablet controller, etc) and - * begins streaming data. - */ - public static void startServer() { - NarwhalDashboard s; - try { - s = new NarwhalDashboard(PORT); - s.setReuseAddr(true); - s.start(); - - Log.info("NarwhalDashboard", "Server has started on port " + PORT); - } catch (UnknownHostException e) { - e.printStackTrace(); - } - } - - public static void setGridInformation(int selGrid, int posx, int posy) { - setGrid(selGrid); - setGridCell(posx, posy); - } - - public static void setGridCell(int posx, int posy){ - cellSelectedPosX = posx; - cellSelectedPosY = posy; - buttonClicked = true; - - } - public static void setGrid(int selGrid){ - gridNum = selGrid; - gridSelected = true; - - } - - // Called once on connection with web server - @SuppressWarnings("unchecked") - @Override - public void onOpen(WebSocket conn, ClientHandshake handshake) { - Log.info("NarwhalDashboard", conn.getRemoteSocketAddress().getHostName() + " has opened a connection."); - - pushed = false; - - (new Thread(() -> { - while (conn.isOpen()) { - JSONObject obj = new JSONObject(); - - SmartDashboard.putNumber("Debug", debugValues.size()); - - JSONArray debugArr = new JSONArray(); - for (String key : debugValues.keySet()) { - JSONObject pair = new JSONObject(); - pair.put("key", key); - pair.put("value", debugValues.get(key)); - debugArr.add(pair); - } - obj.put("debug", debugArr); - - obj.put("selected_auto", selectedAuto); - obj.put("selected_limelight", selectedLimelight); - if(selectedLimelight != null) { - obj.put("selected_pipeline", limelights.get(selectedLimelight).getPipelineIndex()); - } - if(buttonClicked){ - JSONObject selCellInfo = new JSONObject(); - selCellInfo.put("x", cellSelectedPosX); - selCellInfo.put ("y", cellSelectedPosY); - obj.put("selectedGridCell", selCellInfo); - buttonClicked = false; - gridSelected = false; - } - - JSONObject constantsObj = new JSONObject(); - for(String category : ConstantsInt.categories.keySet()) { - JSONArray catArr = new JSONArray(); - List fields = ConstantsInt.getConstantInfo(category); - for(Field field : fields) { - try { - // get value from Field - Object value = field.get(null); - JSONObject newConstant = new JSONObject(); - newConstant.put("key", field.getName()); - newConstant.put("value", value); - - catArr.add(newConstant); - Log.info("Narwhal Dashboard", "Constant Of "+newConstant.toJSONString()); - } - catch(IllegalAccessException e) { - continue; - } - } - constantsObj.put(category, catArr); - } - obj.put("constants", constantsObj); - - if(!pushed) { - JSONArray autoProgramArr = new JSONArray(); - for (String autoName : autoPrograms) { - autoProgramArr.add(autoName); - } - obj.put("auto", autoProgramArr); - - JSONArray limelightsArr = new JSONArray(); - for(NAR_Camera lime : limelights.values()) { - limelightsArr.add(lime.get_name()); - } - obj.put("limelight", limelightsArr); - - JSONArray limelightsOptionsArr = new JSONArray(); - for(Pipeline pipeline : Pipeline.values()) { - limelightsOptionsArr.add(pipeline.toString()); - } - obj.put("pipeline", limelightsOptionsArr); - - pushed = true; - } - - conn.send(obj.toJSONString()); - - try { - Thread.sleep(UPDATE_WAVELENGTH); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - })).start(); - } - - @Override - public void onClose(WebSocket conn, int code, String reason, boolean remote) { - // Log.info("NarwhalDashboard", conn.getRemoteSocketAddress().getHostName() + " - // has closed its connection."); - pushed = false; - } - - // Called by request from web server - @Override - public void onMessage(WebSocket conn, String message) { - Log.info("NarwhalDashboard", message); - String[] parts = message.split(":"); - - // Receive auto selection - if (parts[0].equals("selectauto")) { - String programName = parts[1]; - - if (programName.equals("null")) { - selectedAuto = null; - } else if (autoPrograms.contains(programName)) { - selectedAuto = programName; - SmartDashboard.putString("Auto", programName); - Log.info("NarwhalDashboard", "Selected auto program: \"" + selectedAuto + "\""); - } else { - Log.recoverable("NarwhalDashboard", "Auto program \"" + programName + "\" does not exist."); - } - - // Receive numerical data (for debug only afaik) - } else if (parts[0].equals("numData")) { - String key = parts[1]; - String list = parts[2]; - - String[] stringData = list.split(","); - double[] data = new double[stringData.length]; - - for (int i = 0; i < stringData.length; i++) { - data[i] = Double.parseDouble(stringData[i]); - } - - if (numDataCallbacks.containsKey(key)) { - numDataCallbacks.get(key).process(data); - } else { - Log.info("NarwhalDashboard", "Recieved, but will not process, numerical data: " + key + " = " + data); - } - - // Receive input data (clicking a button on the dash to activate commands) - } else if (parts[0].equals("button")) { - String key = parts[1]; - boolean down = parts[2].equals("down"); - - if (buttons.containsKey(key)) { - buttons.get(key).action(down); - } else { - Log.recoverable("NarwhalDashboard", "Button \"" + parts[1] + "\" was never added."); - } - - // Receive limelight selection (could be consolidated with pipeline) - } else if(parts[0].equals("selectlimelight")){ - selectedLimelight = parts[1]; - - if(selectedLimelight.equals("null")){ - selectedLimelight = null; - } else { - Log.info("NarwhalDashboard", "Unable to Parse Limelight Change Request from Dashboard"); - } - - // Receive pipeline selection (could be consolidated with limelight) - } else if(parts[0].equals("selectpipeline")) { - String pipelineStr = parts[1]; - - if(pipelineStr.equals("null")){ - pipelineStr = null; - } else if(limelights.containsKey(selectedLimelight)) { - limelights.get(selectedLimelight).setPipeline(Pipeline.valueOf(pipelineStr)); - } - else { - Log.info("NarwhalDashboard", "Unable to Parse Pipeline Change Request from Dashboard"); - } - } else if(parts[0].equals("changeconstant")) { - String category = parts[1]; - String name = parts[2]; - String value = parts[3]; - ConstantsInt.updateConstant(category, name, value); - //constantsChanged = true; - } else { - Log.info("NarwhalDashboard", "Message recieved: " + message); - } - - } - - @Override - public void onError(WebSocket conn, Exception ex) { - ex.printStackTrace(); - } - - @Override - public void onStart() { - - } - -} diff --git a/src/main/java/frc/team3128/common/narwhaldashboard/NumericalDataCallback.java b/src/main/java/frc/team3128/common/narwhaldashboard/NumericalDataCallback.java deleted file mode 100644 index 79e9803..0000000 --- a/src/main/java/frc/team3128/common/narwhaldashboard/NumericalDataCallback.java +++ /dev/null @@ -1,8 +0,0 @@ -package frc.team3128.common.narwhaldashboard; - -/** - * A callback to be used with data sent by NarwhalDashboard - */ -public interface NumericalDataCallback { - void process(double[] data); -} \ No newline at end of file diff --git a/vendordeps/3128-common.json b/vendordeps/3128-common.json new file mode 100644 index 0000000..bf12bd4 --- /dev/null +++ b/vendordeps/3128-common.json @@ -0,0 +1,19 @@ +{ + "name": "3128-common", + "version": "0.0.7", + "uuid": "ae3fa5a2-78d9-47e8-921a-dba45b889445", + "mavenUrls": [ + "https://jitpack.io" + ], + "jsonUrl": "https://github.com/Team3128/3128-common/releases/latest/download/3128-common.json", + "fileName": "3128-common.json", + "javaDependencies": [ + { + "groupId": "com.github.Team3128", + "artifactId": "3128-common", + "version": "0.0.7" + } + ], + "jniDependencies": [], + "cppDependencies": [] +} \ No newline at end of file diff --git a/vendordeps/AdvantageKit.json b/vendordeps/AdvantageKit.json new file mode 100644 index 0000000..fa08705 --- /dev/null +++ b/vendordeps/AdvantageKit.json @@ -0,0 +1,41 @@ +{ + "fileName": "AdvantageKit.json", + "name": "AdvantageKit", + "version": "2.2.4", + "uuid": "d820cc26-74e3-11ec-90d6-0242ac120003", + "mavenUrls": [], + "jsonUrl": "https://github.com/Mechanical-Advantage/AdvantageKit/releases/latest/download/AdvantageKit.json", + "javaDependencies": [ + { + "groupId": "org.littletonrobotics.akit.junction", + "artifactId": "wpilib-shim", + "version": "2.2.4" + }, + { + "groupId": "org.littletonrobotics.akit.junction", + "artifactId": "junction-core", + "version": "2.2.4" + }, + { + "groupId": "org.littletonrobotics.akit.conduit", + "artifactId": "conduit-api", + "version": "2.2.4" + } + ], + "jniDependencies": [ + { + "groupId": "org.littletonrobotics.akit.conduit", + "artifactId": "conduit-wpilibio", + "version": "2.2.4", + "skipInvalidPlatforms": false, + "isJar": false, + "validPlatforms": [ + "linuxathena", + "windowsx86-64", + "linuxx86-64", + "osxuniversal" + ] + } + ], + "cppDependencies": [] +} \ No newline at end of file