001/************************ PROJECT ZIXI ************************/ 002/* Copyright (c) 2026 StuyPulse Robotics. All rights reserved.*/ 003/* This work is licensed under the terms of the MIT license. */ 004/**************************************************************/ 005package com.stuypulse.robot.util.auton; 006 007import com.stuypulse.robot.commands.auton.Auton; 008import com.stuypulse.robot.commands.auton.PathPlannerAuton; 009import com.stuypulse.robot.opmodes.AutonOpMode; 010import com.stuypulse.robot.util.auton.AutonUtil.AutonType; 011 012import org.wpilib.driverstation.DriverStationErrors; 013 014import com.pathplanner.lib.path.PathPlannerPath; 015import java.util.function.Function; 016import org.littletonrobotics.junction.networktables.LoggedNetworkChooser; 017 018/** 019 * 020 * 021 * <h2>PathPlanner Utility</h2> 022 * 023 * <p>Loads PathPlanner path (.path) files from the deploy directory and builds named autonomous 024 * routines from those paths. 025 * 026 * <h3>Usage</h3> 027 * 028 * <p>For example, inside {@link com.stuypulse.robot.Robot#configureAutons()}, you can create a 029 * {@link PathPlannerConfig}, pass in the auton name, factory method, and path names, then register 030 * it with {@link AutonOpMode} via {@link PathPlannerConfig#register()}. 031 * 032 * <pre>{@code 033 * AutonFactory factory = new AutonFactory(swerve); 034 * 035 * PathPlannerConfig scoreAutonConfig = new PathPlannerConfig( 036 * "Score", 037 * factory::score, 038 * "Score Path"); 039 * 040 * scoreAutonConfig.register(); 041 * }</pre> 042 * 043 * <p>The factory method receives the loaded paths in the order listed and returns a {@link 044 * PathPlannerAuton}. Use {@link PathPlannerConfig#register()} for a regular option, or {@link 045 * PathPlannerConfig#registerDefault()} for the chooser's default option. A config can also register 046 * directly with a {@link LoggedNetworkChooser} when a custom chooser is used. The methods without a 047 * chooser register the routine with {@link AutonOpMode}. 048 * 049 * <p>Path names are checked when the configuration is created. Missing paths are reported to the 050 * Driver Station with a suggested closest match when one is available. 051 */ 052public final class PathPlannerUtil { 053 private PathPlannerUtil() { 054 throw new UnsupportedOperationException( 055 "This is a utility class and cannot be instantiated"); 056 } 057 058 /** 059 * 060 * 061 * <h2>PathPlanner Configuration</h2> 062 * 063 * <p>Associates an autonomous routine name and one or more PathPlanner path names with a 064 * factory method that creates the corresponding {@link PathPlannerAuton}. Create one 065 * configuration for each routine, then call one of the {@code register} methods to add it to an 066 * autonomous chooser. 067 */ 068 public static class PathPlannerConfig { 069 070 private final String name; 071 072 private final Function<PathPlannerPath[], PathPlannerAuton> auton; 073 074 private final String[] paths; 075 076 /** 077 * 078 * 079 * <h4>Constructor for a PathPlannerConfig</h4> 080 * 081 * <p>Stores the routine name, path names, and factory. Checks if every path exists and 082 * reports missing paths and suggested closest matches to the Driver Station. 083 * 084 * @param name The name to display for this autonomous routine 085 * @param auton Factory that creates the routine from the loaded paths 086 * @param paths PathPlanner path names used by the routine, in the order expected by the 087 * factory 088 */ 089 public PathPlannerConfig( 090 String name, Function<PathPlannerPath[], PathPlannerAuton> auton, String... paths) { 091 this.name = name; 092 this.auton = auton; 093 this.paths = paths; 094 for (String path : paths) { 095 try { 096 PathPlannerPath.fromPathFile(path); 097 } catch (Exception e) { 098 DriverStationErrors.reportError( 099 "Path \"" 100 + path 101 + "\" not found. Did you mean \"" 102 + AutonUtil.findClosestMatch( 103 AutonUtil.getPathFileNames(AutonType.PATHPLANNER), path) 104 + "\"?", 105 false); 106 } 107 } 108 } 109 110 /** 111 * Loads the configured paths and passes them to the routine factory. 112 * 113 * @return The autonomous routine created from the configured paths 114 */ 115 private PathPlannerAuton buildPathPlannerAuton() { 116 return auton.apply(loadPaths(paths)); 117 } 118 119 /** 120 * Adds this routine to a {@link LoggedNetworkChooser} under the configured name. 121 * 122 * @param chooser The chooser that will display this routine 123 * @return This instance of the class, for method chaining 124 */ 125 public PathPlannerConfig register(LoggedNetworkChooser<Auton> chooser) { 126 chooser.add(name, buildPathPlannerAuton()); 127 return this; 128 } 129 130 /** 131 * Registers this routine with {@link AutonOpMode}'s internal chooser. 132 * 133 * @return This instance of the class, for method chaining 134 */ 135 public PathPlannerConfig register() { 136 AutonOpMode.addAuton(name, buildPathPlannerAuton()); 137 return this; 138 } 139 140 /** 141 * Adds this routine to a {@link LoggedNetworkChooser} as its default option. 142 * 143 * @param chooser The chooser that will display this routine. 144 * @return This instance of the class, for method chaining 145 */ 146 public PathPlannerConfig registerDefault(LoggedNetworkChooser<Auton> chooser) { 147 chooser.addDefault(name, buildPathPlannerAuton()); 148 return this; 149 } 150 151 /** 152 * Registers this routine as the default with {@link AutonOpMode}'s internal chooser. 153 * 154 * @return This instance of the class, for method chaining 155 */ 156 public PathPlannerConfig registerDefault() { 157 AutonOpMode.addDefaultAuton(name, buildPathPlannerAuton()); 158 return this; 159 } 160 } 161 162 // PATH LOADING METHODS 163 164 /** 165 * Loads multiple PathPlanner paths in the order given. Each name is passed to {@link 166 * #load(String)}. 167 * 168 * <p>If a path cannot be loaded or found, an error is reported to the Driver Station, and null 169 * is returned in the corresponding position. 170 * 171 * @param names PathPlanner path names to load 172 * @return The corresponding paths where entries that fail to load are {@code null} 173 */ 174 public static PathPlannerPath[] loadPaths(String... names) { 175 PathPlannerPath[] output = new PathPlannerPath[names.length]; 176 for (int i = 0; i < names.length; i++) { 177 output[i] = load(names[i]); 178 } 179 return output; 180 } 181 182 /** 183 * Loads a PathPlanner path by name. 184 * 185 * <p>If the path cannot be loaded or found, an error is reported to the Driver Station, and 186 * null is returned. 187 * 188 * @param name PathPlanner path name 189 * @return The loaded path, or {@code null} if not found 190 */ 191 public static PathPlannerPath load(String name) { 192 try { 193 return PathPlannerPath.fromPathFile(name); 194 } catch (Exception e) { 195 DriverStationErrors.reportError("Path not found.", false); 196 return null; 197 } 198 } 199}