diff --git a/src/main/java/org/carlmontrobotics/lib199/MotorControllerFactory.java b/src/main/java/org/carlmontrobotics/lib199/MotorControllerFactory.java index 0f6baa46..1aeb5c8f 100644 --- a/src/main/java/org/carlmontrobotics/lib199/MotorControllerFactory.java +++ b/src/main/java/org/carlmontrobotics/lib199/MotorControllerFactory.java @@ -144,6 +144,10 @@ public static CANSparkMax createSparkMax(int id, MotorConfig config) { return spark; } + /** + * @deprecated Use {@link SensorFactory#createCANCoder(int)} instead. + */ + @Deprecated public static CANCoder createCANCoder(int port) { CANCoder canCoder = new CANCoder(port); if(RobotBase.isSimulation()) new MockedCANCoder(canCoder); @@ -156,7 +160,10 @@ public static CANCoder createCANCoder(int port) { * This MUST be called AFTER AHRS initialization or the code will be unable to connect to the gyro. * * @return The configured camera + * + * @deprecated Use {@link SensorFactory#configureCamera()} instead. */ + @Deprecated public static UsbCamera configureCamera() { UsbCamera camera = CameraServer.startAutomaticCapture(); camera.setConnectionStrategy(ConnectionStrategy.kKeepOpen); @@ -171,7 +178,10 @@ public static UsbCamera configureCamera() { * * @param numCameras The number of cameras to configure * @return The configured cameras. + * + * @deprecated Use {@link SensorFactory#configureCameras(int)} instead. */ + @Deprecated public static UsbCamera[] configureCameras(int numCameras) { UsbCamera[] cameras = new UsbCamera[numCameras]; for(int i = 0; i < numCameras; i++) cameras[i] = configureCamera(); diff --git a/src/main/java/org/carlmontrobotics/lib199/SensorFactory.java b/src/main/java/org/carlmontrobotics/lib199/SensorFactory.java new file mode 100644 index 00000000..7b778844 --- /dev/null +++ b/src/main/java/org/carlmontrobotics/lib199/SensorFactory.java @@ -0,0 +1,61 @@ +package org.carlmontrobotics.lib199; + +import org.carlmontrobotics.lib199.sim.MockedCANCoder; + +import com.ctre.phoenix.sensors.CANCoder; + +import edu.wpi.first.cameraserver.CameraServer; +import edu.wpi.first.cscore.UsbCamera; +import edu.wpi.first.cscore.VideoSource.ConnectionStrategy; +import edu.wpi.first.wpilibj.RobotBase; + +/** + * A class containing methods to create and configure sensors. + */ +public class SensorFactory { + + /** + * Creates a CANCoder object, linking it to the simulator if necessary + * + * @param port The CAN ID of the CANCoder + * @return The CANCoder object + */ + public static CANCoder createCANCoder(int port) { + CANCoder canCoder = new CANCoder(port); + if (RobotBase.isSimulation()) + new MockedCANCoder(canCoder); + return canCoder; + } + + /** + * Configures a USB Camera. + * See {@link CameraServer#startAutomaticCapture} for more details. + * This MUST be called AFTER AHRS initialization or the code will be unable to + * connect to the gyro. + * + * @return The configured camera + */ + public static UsbCamera configureCamera() { + UsbCamera camera = CameraServer.startAutomaticCapture(); + camera.setConnectionStrategy(ConnectionStrategy.kKeepOpen); + CameraServer.getServer().setSource(camera); + return camera; + } + + /** + * This method is equivalent to calling {@link #configureCamera()} + * {@code numCameras} times. + * The last camera will be set as the primary Camera feed. + * To change it, call {@code CameraServer.getServer().setSource()}. + * + * @param numCameras The number of cameras to configure + * @return The configured cameras. + */ + public static UsbCamera[] configureCameras(int numCameras) { + UsbCamera[] cameras = new UsbCamera[numCameras]; + for (int i = 0; i < numCameras; i++) + cameras[i] = configureCamera(); + return cameras; + } + +}