From 89c0cc3a0188994895f5d83bd5327d07768afeb1 Mon Sep 17 00:00:00 2001 From: Gold856 <117957790+Gold856@users.noreply.github.com> Date: Sun, 30 Mar 2025 20:25:11 -0400 Subject: [PATCH] Remove unnecessary assignment In places like pipelines or DataSocketHandler, objects were being instantiated and assigned to a variable, but were then passed into a method, never to be used again. For pipelines especially, that style of code wasn't always consistently used, and other pipelines skipped assignment and instantiated objects in the method call. Make everything consistent by always instantiating in the method call. GenericUSBCameraSettables also had an unnecessary assignment, and that was cleaned up too for readability. --- .../USBCameras/GenericUSBCameraSettables.java | 6 +- .../vision/pipeline/AprilTagPipeline.java | 4 +- .../vision/pipeline/ArucoPipeline.java | 11 +- .../vision/pipeline/Calibrate3dPipeline.java | 14 +- .../vision/pipeline/ColoredShapePipeline.java | 58 ++--- .../vision/pipeline/DriverModePipeline.java | 7 +- .../pipeline/ObjectDetectionPipeline.java | 21 +- .../vision/pipeline/OutputStreamPipeline.java | 35 ++- .../vision/pipeline/ReflectivePipeline.java | 38 ++- .../server/DataSocketHandler.java | 233 ++++++++---------- 10 files changed, 179 insertions(+), 248 deletions(-) diff --git a/photon-core/src/main/java/org/photonvision/vision/camera/USBCameras/GenericUSBCameraSettables.java b/photon-core/src/main/java/org/photonvision/vision/camera/USBCameras/GenericUSBCameraSettables.java index a14218fa6..bfe05e24a 100644 --- a/photon-core/src/main/java/org/photonvision/vision/camera/USBCameras/GenericUSBCameraSettables.java +++ b/photon-core/src/main/java/org/photonvision/vision/camera/USBCameras/GenericUSBCameraSettables.java @@ -271,11 +271,7 @@ public class GenericUSBCameraSettables extends VisionSourceSettables { videoModes = new HashMap<>(); List videoModesList = new ArrayList<>(); try { - VideoMode[] modes; - - modes = camera.enumerateVideoModes(); - - for (VideoMode videoMode : modes) { + for (VideoMode videoMode : camera.enumerateVideoModes()) { // Filter grey modes if (videoMode.pixelFormat == PixelFormat.kGray || videoMode.pixelFormat == PixelFormat.kUnknown) { diff --git a/photon-core/src/main/java/org/photonvision/vision/pipeline/AprilTagPipeline.java b/photon-core/src/main/java/org/photonvision/vision/pipeline/AprilTagPipeline.java index f5cd52c26..3e98f7e9e 100644 --- a/photon-core/src/main/java/org/photonvision/vision/pipeline/AprilTagPipeline.java +++ b/photon-core/src/main/java/org/photonvision/vision/pipeline/AprilTagPipeline.java @@ -134,8 +134,8 @@ public class AprilTagPipeline extends CVPipeline> tagDetectionPipeResult; - tagDetectionPipeResult = aprilTagDetectionPipe.run(frame.processedImage); + CVPipeResult> tagDetectionPipeResult = + aprilTagDetectionPipe.run(frame.processedImage); sumPipeNanosElapsed += tagDetectionPipeResult.nanosElapsed; List detections = tagDetectionPipeResult.output; diff --git a/photon-core/src/main/java/org/photonvision/vision/pipeline/ArucoPipeline.java b/photon-core/src/main/java/org/photonvision/vision/pipeline/ArucoPipeline.java index 8395054a1..b0a4505a6 100644 --- a/photon-core/src/main/java/org/photonvision/vision/pipeline/ArucoPipeline.java +++ b/photon-core/src/main/java/org/photonvision/vision/pipeline/ArucoPipeline.java @@ -145,8 +145,8 @@ public class ArucoPipeline extends CVPipeline> tagDetectionPipeResult; - tagDetectionPipeResult = arucoDetectionPipe.run(frame.processedImage); + CVPipeResult> tagDetectionPipeResult = + arucoDetectionPipe.run(frame.processedImage); sumPipeNanosElapsed += tagDetectionPipeResult.nanosElapsed; // If we want to debug the thresholding steps, draw the first step to the color image @@ -163,14 +163,13 @@ public class ArucoPipeline extends CVPipeline { - // TODO: what is this event? - var data = (Boolean) entryValue; - var dmIsDriverEvent = - new IncomingWebSocketEvent( - DataChangeDestination.DCD_ACTIVEMODULE, - "isDriverMode", - data, - cameraUniqueName, - context); - - dcService.publishEvents(dmIsDriverEvent); - } - case SMT_CHANGECAMERANAME -> { - var ccnEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "cameraNickname", - (String) entryValue, - cameraUniqueName, - context); - dcService.publishEvent(ccnEvent); - } - case SMT_CHANGEPIPELINENAME -> { - var cpnEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "pipelineName", - (String) entryValue, - cameraUniqueName, - context); - dcService.publishEvent(cpnEvent); - } + case SMT_DRIVERMODE -> // TODO: what is this event? + dcService.publishEvents( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "isDriverMode", + (Boolean) entryValue, + cameraUniqueName, + context)); + case SMT_CHANGECAMERANAME -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "cameraNickname", + (String) entryValue, + cameraUniqueName, + context)); + case SMT_CHANGEPIPELINENAME -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "pipelineName", + (String) entryValue, + cameraUniqueName, + context)); case SMT_ADDNEWPIPELINE -> { // HashMap data = (HashMap) entryValue; // var type = (PipelineType) data.get("pipelineType"); @@ -168,109 +159,90 @@ public class DataSocketHandler { var name = (String) arr.get(0); var type = PipelineType.values()[(Integer) arr.get(1) + 2]; - var newPipelineEvent = + dcService.publishEvent( new IncomingWebSocketEvent<>( DataChangeDestination.DCD_ACTIVEMODULE, "newPipelineInfo", Pair.of(name, type), cameraUniqueName, - context); - dcService.publishEvent(newPipelineEvent); - } - case SMT_CHANGEBRIGHTNESS -> { - HardwareManager.getInstance() - .setBrightnessPercent(Integer.parseInt(entryValue.toString())); + context)); } + case SMT_CHANGEBRIGHTNESS -> + HardwareManager.getInstance() + .setBrightnessPercent(Integer.parseInt(entryValue.toString())); case SMT_DUPLICATEPIPELINE -> { var pipeIndex = (Integer) entryValue; logger.info("Duplicating pipe@index" + pipeIndex + " for camera " + cameraUniqueName); - var newPipelineEvent = + dcService.publishEvent( new IncomingWebSocketEvent<>( DataChangeDestination.DCD_ACTIVEMODULE, "duplicatePipeline", pipeIndex, cameraUniqueName, - context); - dcService.publishEvent(newPipelineEvent); - } - case SMT_DELETECURRENTPIPELINE -> { - var deleteCurrentPipelineEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "deleteCurrPipeline", - 0, - cameraUniqueName, - context); - dcService.publishEvent(deleteCurrentPipelineEvent); - } - case SMT_ROBOTOFFSETPOINT -> { - var robotOffsetPointEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "robotOffsetPoint", - (Integer) entryValue, - cameraUniqueName, - null); - dcService.publishEvent(robotOffsetPointEvent); - } - case SMT_CURRENTCAMERA -> { - var changeCurrentCameraEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_OTHER, "changeUICamera", (Integer) entryValue); - dcService.publishEvent(changeCurrentCameraEvent); - } - case SMT_CURRENTPIPELINE -> { - var changePipelineEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "changePipeline", - (Integer) entryValue, - cameraUniqueName, - context); - dcService.publishEvent(changePipelineEvent); - } - case SMT_STARTPNPCALIBRATION -> { - var changePipelineEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "startCalibration", - (Map) entryValue, - cameraUniqueName, - context); - dcService.publishEvent(changePipelineEvent); - } - case SMT_SAVEINPUTSNAPSHOT -> { - var takeInputSnapshotEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "saveInputSnapshot", - 0, - cameraUniqueName, - context); - dcService.publishEvent(takeInputSnapshotEvent); - } - case SMT_SAVEOUTPUTSNAPSHOT -> { - var takeOutputSnapshotEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "saveOutputSnapshot", - 0, - cameraUniqueName, - context); - dcService.publishEvent(takeOutputSnapshotEvent); - } - case SMT_TAKECALIBRATIONSNAPSHOT -> { - var takeCalSnapshotEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "takeCalSnapshot", - 0, - cameraUniqueName, - context); - dcService.publishEvent(takeCalSnapshotEvent); + context)); } + case SMT_DELETECURRENTPIPELINE -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "deleteCurrPipeline", + 0, + cameraUniqueName, + context)); + case SMT_ROBOTOFFSETPOINT -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "robotOffsetPoint", + (Integer) entryValue, + cameraUniqueName, + null)); + case SMT_CURRENTCAMERA -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_OTHER, "changeUICamera", (Integer) entryValue)); + case SMT_CURRENTPIPELINE -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "changePipeline", + (Integer) entryValue, + cameraUniqueName, + context)); + case SMT_STARTPNPCALIBRATION -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "startCalibration", + (Map) entryValue, + cameraUniqueName, + context)); + case SMT_SAVEINPUTSNAPSHOT -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "saveInputSnapshot", + 0, + cameraUniqueName, + context)); + case SMT_SAVEOUTPUTSNAPSHOT -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "saveOutputSnapshot", + 0, + cameraUniqueName, + context)); + case SMT_TAKECALIBRATIONSNAPSHOT -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "takeCalSnapshot", + 0, + cameraUniqueName, + context)); case SMT_PIPELINESETTINGCHANGE -> { HashMap data = (HashMap) entryValue; @@ -280,29 +252,26 @@ public class DataSocketHandler { if (dataEntry.getKey().equals("cameraUniqueName")) { continue; } - var pipelineSettingChangeEvent = + dcService.publishEvent( new IncomingWebSocketEvent( DataChangeDestination.DCD_ACTIVEPIPELINESETTINGS, dataEntry.getKey(), dataEntry.getValue(), cameraIndex2, - context); - dcService.publishEvent(pipelineSettingChangeEvent); + context)); } } else { logger.warn("Unknown message for PSC: " + data.keySet().iterator().next()); } } - case SMT_CHANGEPIPELINETYPE -> { - var changePipelineEvent = - new IncomingWebSocketEvent<>( - DataChangeDestination.DCD_ACTIVEMODULE, - "changePipelineType", - (Integer) entryValue, - cameraUniqueName, - context); - dcService.publishEvent(changePipelineEvent); - } + case SMT_CHANGEPIPELINETYPE -> + dcService.publishEvent( + new IncomingWebSocketEvent<>( + DataChangeDestination.DCD_ACTIVEMODULE, + "changePipelineType", + (Integer) entryValue, + cameraUniqueName, + context)); } } catch (Exception e) { logger.error("Failed to parse message!", e);