Skip to content

Commit

Permalink
change most timer.Delay()'s to SimHooks.stepTiming
Browse files Browse the repository at this point in the history
this way, the tests take signifantly less time.

I need to implement this also on ledCommands so that It doesn't actually wait a whole hour and we can use the wpilib's cool system for it instead
  • Loading branch information
kytpbs committed May 10, 2024
1 parent 27ab608 commit ee14ff3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/test/java/command_tests/arm_tests/BasicShooterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import command_tests.utils.CommandTestBase;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.simulation.PWMSim;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import frc.robot.Constants.ShooterConstants;
import frc.robot.commands.BasicRunShooterCommand;
import frc.robot.subsystems.ShooterSubsystem;
Expand Down Expand Up @@ -44,7 +45,7 @@ void testSetSpeed() {
ShooterConstants.kShooterSpeed,
shooterMotor.getSpeed(),
"Shooter motor should be at shooter speed");
Timer.delay(kWaitTime);
SimHooks.stepTiming(kWaitTime);
commandScheduler.run();
// Motor should be at 0 after waitTime
assertEquals(
Expand All @@ -57,7 +58,7 @@ void testSetSpeed() {
void testLEDLoading() {
double startTime = Timer.getFPGATimestamp();
commandScheduler.run();
Timer.delay(kWaitTime); // load waitTime
SimHooks.stepTiming(kWaitTime); // load waitTime
commandScheduler.run();
testAtTime(ledSubsystem, startTime, kWaitTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import command_tests.utils.CommandTestBase;
import edu.wpi.first.wpilibj.GenericHID.RumbleType;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import edu.wpi.first.wpilibj.simulation.XboxControllerSim;
import frc.robot.commands.VibrateControllerCommand;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -47,7 +47,7 @@ void testItVibrates() {
@Test
void testItStopsVibrating() {
commandScheduler.run();
Timer.delay(kWaitTime);
SimHooks.stepTiming(kWaitTime);
commandScheduler.run();
assertEquals(0, controllerSim.getRumble(RumbleType.kLeftRumble), kDelta);
}
Expand All @@ -63,15 +63,15 @@ void testItRepeats() {
controllerSim.getRumble(RumbleType.kLeftRumble),
kDelta,
"controller should vibrate at index: " + i);
Timer.delay(kWaitTime + 0.1);
SimHooks.stepTiming(kWaitTime + 0.1);
commandScheduler.run();
commandScheduler.run();
assertEquals(
0,
controllerSim.getRumble(RumbleType.kLeftRumble),
kDelta,
"controller should stop vibrating at index: " + i);
Timer.delay(kWaitTime + 0.1);
SimHooks.stepTiming(kWaitTime + 0.1);
commandScheduler.run();
commandScheduler.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import command_tests.utils.CommandTestBase;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import frc.robot.commands.led_commands.LEDLoadingWaitCommand;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -29,7 +30,7 @@ public void tearDown() {
void testLEDLoading(double waitTime) {
double startTime = Timer.getFPGATimestamp();
for (double i = 0.1; Timer.getFPGATimestamp() - startTime + i < waitTime; i += 0.1) {
Timer.delay(i);
SimHooks.stepTiming(i);
commandScheduler.run();
testAtTime(ledSubsystem, startTime, waitTime);
}
Expand Down Expand Up @@ -57,7 +58,7 @@ void itActuallyEnds() {
false,
ledLoadingWaitCommand.isFinished(),
"led wait command shouldn't finish before wait time");
Timer.delay(kWaitTime); // just in case add a little bit extra time
SimHooks.stepTiming(kWaitTime);
assertEquals(
true, ledLoadingWaitCommand.isFinished(), "led wait command should finish after wait time");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import command_tests.utils.CommandTestBase;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import frc.robot.commands.WaitANDConditionCommand;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -43,7 +43,7 @@ void testItCompletesOnCondition() {
@Test
void testItCompletesAfterTime() {
commandScheduler.run();
Timer.delay(kWaitTime);
SimHooks.stepTiming(kWaitTime);
commandScheduler.run();
assertFalse(
waitConditionCommand.isFinished(),
Expand All @@ -54,7 +54,7 @@ void testItCompletesAfterTime() {
void testItFinishesAfterAll() {
commandScheduler.run();
condition = true;
Timer.delay(kWaitTime);
SimHooks.stepTiming(kWaitTime);
commandScheduler.run();
assertTrue(waitConditionCommand.isFinished(), "Command should finish after time and condition");
}
Expand Down

0 comments on commit ee14ff3

Please sign in to comment.