Skip to content

Commit b932002

Browse files
committed
worked some more advanced subsystem test
1 parent 88433cc commit b932002

File tree

5 files changed

+63
-26
lines changed

5 files changed

+63
-26
lines changed

src/main/java/frc/lib/AdvancedSubsystem.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package frc.lib;
22

3+
import static edu.wpi.first.wpilibj2.command.Commands.*;
4+
35
import dev.doglog.DogLog;
46
import edu.wpi.first.networktables.NetworkTableInstance;
57
import edu.wpi.first.wpilibj2.command.Command;
6-
import edu.wpi.first.wpilibj2.command.Commands;
78
import edu.wpi.first.wpilibj2.command.SubsystemBase;
89
import frc.lib.FaultsTable.Fault;
910
import frc.lib.FaultsTable.FaultType;
1011
import java.util.HashSet;
1112
import java.util.Set;
1213

13-
public class AdvancedSubsystem extends SubsystemBase implements SelfChecked, AutoCloseable {
14+
public abstract class AdvancedSubsystem extends SubsystemBase
15+
implements SelfChecked, AutoCloseable {
1416
// faults and the table containing them
1517
private Set<Fault> _faults = new HashSet<Fault>();
1618
private FaultsTable _faultsTable =
@@ -31,12 +33,17 @@ protected final void clearFaults() {
3133

3234
/** Adds a new fault under this subsystem. */
3335
protected final void addFault(String description, FaultType faultType) {
36+
_hasError = (faultType == FaultType.ERROR);
37+
3438
Fault fault = new Fault(description, faultType);
3539

3640
_faults.add(fault);
3741
_faultsTable.set(_faults);
42+
}
3843

39-
_hasError = faultType == FaultType.ERROR;
44+
/** Returns the faults belonging to this subsystem. */
45+
public final Set<Fault> getFaults() {
46+
return _faults;
4047
}
4148

4249
/** Returns whether this subsystem has errors (has fault type of error). */
@@ -47,20 +54,12 @@ public final boolean hasError() {
4754
/** Returns a full Command that self checks this Subsystem for pre-match. */
4855
public final Command fullSelfCheck() {
4956
Command selfCheck =
50-
Commands.sequence(
51-
runOnce(
52-
this::clearFaults), // clear all faults and hasError (also adds this subsystem
53-
// as a requirement)
54-
selfCheck(this::addFault).until(this::hasError) // self check this subsystem
55-
)
57+
sequence(runOnce(this::clearFaults), selfCheck(this::addFault).until(this::hasError))
5658
.withName(getName() + " Self Check");
5759

5860
return selfCheck;
5961
}
6062

61-
@Override
62-
public void close() throws Exception {}
63-
6463
@Override
6564
public void periodic() {
6665
String currentCommandName = "None";

src/main/java/frc/lib/FaultsTable.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ public class FaultsTable {
1717
private final StringArrayPublisher infos;
1818

1919
/** An individual fault, containing necessary information. */
20-
public static record Fault(String description, FaultType type) {}
20+
public static record Fault(String description, FaultType type) {
21+
@Override
22+
public final boolean equals(Object other) {
23+
if (other == this) return true;
24+
25+
if (other instanceof Fault o) {
26+
return o.description == description && o.type == type;
27+
}
28+
29+
return false;
30+
}
31+
}
2132

2233
/**
2334
* The type of fault, used for detecting whether the fallible is in a failure state and displaying
@@ -26,7 +37,7 @@ public static record Fault(String description, FaultType type) {}
2637
public static enum FaultType {
2738
INFO,
2839
WARNING,
29-
ERROR,
40+
ERROR
3041
}
3142

3243
public FaultsTable(NetworkTable base, String name) {

src/main/java/frc/lib/SelfChecked.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package frc.lib;
22

3+
import static edu.wpi.first.wpilibj2.command.Commands.*;
4+
35
import edu.wpi.first.wpilibj2.command.Command;
4-
import edu.wpi.first.wpilibj2.command.Commands;
56
import frc.lib.FaultsTable.FaultType;
67
import java.util.function.BiConsumer;
78

@@ -13,6 +14,6 @@ public interface SelfChecked {
1314
* type.
1415
*/
1516
public default Command selfCheck(BiConsumer<String, FaultType> faultAdder) {
16-
return Commands.none();
17+
return none();
1718
}
1819
}

src/main/java/frc/lib/UnitTestingUtil.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import edu.wpi.first.hal.HAL;
66
import edu.wpi.first.units.measure.Time;
7+
import edu.wpi.first.wpilibj.Timer;
78
import edu.wpi.first.wpilibj.simulation.DriverStationSim;
89
import edu.wpi.first.wpilibj.simulation.SimHooks;
910
import edu.wpi.first.wpilibj2.command.Command;
@@ -18,11 +19,15 @@ public class UnitTestingUtil {
1819
/** Sets up DS and initializes HAL with default values and asserts that it doesn't fail. */
1920
public static void setupTests() {
2021
assert HAL.initialize(500, 0);
22+
2123
DriverStationSim.setEnabled(true);
22-
DriverStationSim.setTest(true);
2324
DriverStationSim.notifyNewData();
25+
2426
FaultLogger.clear();
2527
FaultLogger.unregisterAll();
28+
29+
// delay 100 ms to wait for CTRE device enable
30+
Timer.delay(0.100);
2631
}
2732

2833
/**
@@ -97,7 +102,9 @@ public static void run(Command command, int runs) {
97102
*/
98103
public static void runToCompletion(Command command) {
99104
command.schedule();
105+
100106
fastForward(1);
107+
101108
while (command.isScheduled()) {
102109
fastForward(1);
103110
}

src/test/java/frc/lib/AdvancedSubsystemTest.java

+28-9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
package frc.lib;
66

7+
import static edu.wpi.first.wpilibj2.command.Commands.*;
78
import static frc.lib.UnitTestingUtil.*;
89

10+
import edu.wpi.first.wpilibj.DriverStation;
911
import edu.wpi.first.wpilibj2.command.Command;
10-
import edu.wpi.first.wpilibj2.command.Commands;
12+
import frc.lib.FaultsTable.Fault;
1113
import frc.lib.FaultsTable.FaultType;
1214
import java.util.function.BiConsumer;
1315
import org.junit.jupiter.api.AfterEach;
@@ -17,21 +19,35 @@
1719
public class AdvancedSubsystemTest {
1820
static final double DELTA = 1e-2; // acceptable deviation range
1921

20-
private TestImpl sub = new TestImpl();
22+
private TestImpl _sub;
2123

2224
@BeforeEach
2325
public void setup() {
2426
setupTests();
27+
28+
_sub = new TestImpl();
2529
}
2630

2731
@AfterEach
2832
public void close() throws Exception {
29-
reset(sub);
33+
reset(_sub);
34+
}
35+
36+
@Test
37+
public void robotIsEnabled() {
38+
assert DriverStation.isEnabled();
3039
}
3140

3241
@Test
3342
public void subsystemSelfCheck() {
34-
// TODO
43+
runToCompletion(_sub.fullSelfCheck());
44+
45+
// should give FAULT 1 error
46+
assert _sub.hasError();
47+
48+
// should only give FAULT 1 error and stop there
49+
assert _sub.getFaults().contains(new Fault("FAULT 1", FaultType.ERROR));
50+
assert !_sub.getFaults().contains(new Fault("FAULT 2", FaultType.ERROR));
3551
}
3652

3753
public class TestImpl extends AdvancedSubsystem {
@@ -54,16 +70,16 @@ public double getEncoderSpeed() {
5470

5571
@Override
5672
public Command selfCheck(BiConsumer<String, FaultType> faultAdder) {
57-
return Commands.sequence(
58-
Commands.runOnce(
73+
return sequence(
74+
runOnce(
5975
() -> {
6076
if (_fault1) faultAdder.accept("FAULT 1", FaultType.ERROR);
6177
}),
62-
Commands.runOnce(
78+
runOnce(
6379
() -> {
6480
if (_fault2) faultAdder.accept("FAULT 2", FaultType.ERROR);
6581
}),
66-
Commands.runOnce(
82+
runOnce(
6783
() -> {
6884
if (_fault3) faultAdder.accept("FAULT 3", FaultType.WARNING);
6985
}));
@@ -77,13 +93,16 @@ public double speed() {
7793

7894
@Override
7995
public Command selfCheck(BiConsumer<String, FaultType> faultAdder) {
80-
return Commands.sequence(
96+
return sequence(
8197
_io.selfCheck(faultAdder), // self check io devices first
8298
runOnce(
8399
() -> {
84100
if (speed() < 2) faultAdder.accept("TOO SLOW", FaultType.WARNING);
85101
}) // then check the whole subsystem
86102
);
87103
}
104+
105+
@Override
106+
public void close() throws Exception {}
88107
}
89108
}

0 commit comments

Comments
 (0)