Skip to content

Commit 8beb140

Browse files
authored
Merge branch 'master' into safe-mode-2
2 parents ddbb99d + 3569b17 commit 8beb140

7 files changed

Lines changed: 93 additions & 0 deletions

File tree

src/main/java/org/carlmontrobotics/lib199/MotorControllerFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public static CANSparkMax createSparkMax(int id, MotorConfig config) {
144144
return spark;
145145
}
146146

147+
/**
148+
* @deprecated Use {@link SensorFactory#createCANCoder(int)} instead.
149+
*/
150+
@Deprecated
147151
public static CANCoder createCANCoder(int port) {
148152
CANCoder canCoder = new CANCoder(port);
149153
if(RobotBase.isSimulation()) new MockedCANCoder(canCoder);
@@ -156,7 +160,10 @@ public static CANCoder createCANCoder(int port) {
156160
* This MUST be called AFTER AHRS initialization or the code will be unable to connect to the gyro.
157161
*
158162
* @return The configured camera
163+
*
164+
* @deprecated Use {@link SensorFactory#configureCamera()} instead.
159165
*/
166+
@Deprecated
160167
public static UsbCamera configureCamera() {
161168
UsbCamera camera = CameraServer.startAutomaticCapture();
162169
camera.setConnectionStrategy(ConnectionStrategy.kKeepOpen);
@@ -171,7 +178,10 @@ public static UsbCamera configureCamera() {
171178
*
172179
* @param numCameras The number of cameras to configure
173180
* @return The configured cameras.
181+
*
182+
* @deprecated Use {@link SensorFactory#configureCameras(int)} instead.
174183
*/
184+
@Deprecated
175185
public static UsbCamera[] configureCameras(int numCameras) {
176186
UsbCamera[] cameras = new UsbCamera[numCameras];
177187
for(int i = 0; i < numCameras; i++) cameras[i] = configureCamera();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.carlmontrobotics.lib199;
2+
3+
import org.carlmontrobotics.lib199.sim.MockedCANCoder;
4+
5+
import com.ctre.phoenix.sensors.CANCoder;
6+
7+
import edu.wpi.first.cameraserver.CameraServer;
8+
import edu.wpi.first.cscore.UsbCamera;
9+
import edu.wpi.first.cscore.VideoSource.ConnectionStrategy;
10+
import edu.wpi.first.wpilibj.RobotBase;
11+
12+
/**
13+
* A class containing methods to create and configure sensors.
14+
*/
15+
public class SensorFactory {
16+
17+
/**
18+
* Creates a CANCoder object, linking it to the simulator if necessary
19+
*
20+
* @param port The CAN ID of the CANCoder
21+
* @return The CANCoder object
22+
*/
23+
public static CANCoder createCANCoder(int port) {
24+
CANCoder canCoder = new CANCoder(port);
25+
if (RobotBase.isSimulation())
26+
new MockedCANCoder(canCoder);
27+
return canCoder;
28+
}
29+
30+
/**
31+
* Configures a USB Camera.
32+
* See {@link CameraServer#startAutomaticCapture} for more details.
33+
* This MUST be called AFTER AHRS initialization or the code will be unable to
34+
* connect to the gyro.
35+
*
36+
* @return The configured camera
37+
*/
38+
public static UsbCamera configureCamera() {
39+
UsbCamera camera = CameraServer.startAutomaticCapture();
40+
camera.setConnectionStrategy(ConnectionStrategy.kKeepOpen);
41+
CameraServer.getServer().setSource(camera);
42+
return camera;
43+
}
44+
45+
/**
46+
* This method is equivalent to calling {@link #configureCamera()}
47+
* {@code numCameras} times.
48+
* The last camera will be set as the primary Camera feed.
49+
* To change it, call {@code CameraServer.getServer().setSource()}.
50+
*
51+
* @param numCameras The number of cameras to configure
52+
* @return The configured cameras.
53+
*/
54+
public static UsbCamera[] configureCameras(int numCameras) {
55+
UsbCamera[] cameras = new UsbCamera[numCameras];
56+
for (int i = 0; i < numCameras; i++)
57+
cameras[i] = configureCamera();
58+
return cameras;
59+
}
60+
61+
}

src/main/java/org/carlmontrobotics/lib199/safeMode/SafeCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
*/
99
public class SafeCommand extends WrapperCommand {
1010

11+
static {
12+
SafeMode.ensureInitialized();
13+
}
14+
1115
/**
1216
* Creates a new SafeCommand
1317
* @param command The command to run

src/main/java/org/carlmontrobotics/lib199/safeMode/SafeExecuteBlockingCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
*/
1212
public class SafeExecuteBlockingCommand extends WrapperCommand {
1313

14+
static {
15+
SafeMode.ensureInitialized();
16+
}
17+
1418
public SafeExecuteBlockingCommand(Command command) {
1519
super(command);
1620
}

src/main/java/org/carlmontrobotics/lib199/safeMode/SafeMode.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public class SafeMode {
4646
Lib199Subsystem.registerPeriodic(SafeMode::updateCallbacks);
4747
}
4848

49+
/**
50+
* Ensures that the static block of this class is called, setting up safe-mode.
51+
* It is unlikely that you will need to call this function.
52+
*/
53+
public static void ensureInitialized() {}
54+
4955
/**
5056
* Enables safe-mode
5157
*/

src/main/java/org/carlmontrobotics/lib199/safeMode/UnsafeCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
*/
99
public class UnsafeCommand extends WrapperCommand {
1010

11+
static {
12+
SafeMode.ensureInitialized();
13+
}
14+
1115
public UnsafeCommand(Command command) {
1216
super(new EndBlockingCommand(command));
1317
}

src/main/java/org/carlmontrobotics/lib199/safeMode/UnsafeExecuteBlockingCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
*/
1212
public class UnsafeExecuteBlockingCommand extends WrapperCommand {
1313

14+
static {
15+
SafeMode.ensureInitialized();
16+
}
17+
1418
public UnsafeExecuteBlockingCommand(Command command) {
1519
super(command);
1620
}

0 commit comments

Comments
 (0)