diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 1773852..b4e6534 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -4,7 +4,6 @@ package frc.robot; -import frc.robot.subsystems.ExampleSubsystem; /** * The Constants class provides a convenient place for teams to hold robot-wide numerical or boolean diff --git a/src/main/java/frc/robot/subsystems/ExampleSubsystem.java b/src/main/java/frc/robot/subsystems/ExampleSubsystem.java index a568f00..94a5b23 100644 --- a/src/main/java/frc/robot/subsystems/ExampleSubsystem.java +++ b/src/main/java/frc/robot/subsystems/ExampleSubsystem.java @@ -9,38 +9,51 @@ import edu.wpi.first.wpilibj2.command.SubsystemBase; import frc.robot.Constants.exampleSubsystem_Constants; -public class ExampleSubsystem extends SubsystemBase{ - /** Creates a new ExampleSubsystem. */ - private Encoder m_encoder; - - public ExampleSubsystem() { - m_encoder = new Encoder(exampleSubsystem_Constants.encoderA, - exampleSubsystem_Constants.encoderB); - - } - public ExampleSubsystem(Encoder encoder) { - m_encoder = encoder; - } - - @Override - public void periodic() { - // This method will be called once per scheduler run - } - - @Override - public void simulationPeriodic() { - // This method will be called once per scheduler run during simulation - } - - public String helloWorld(String string){ - System.out.println(string); - return string; - } - -public double getDistance_Inch() { - System.out.println("Encoder Distance = " + m_encoder.getDistance()); - return m_encoder.getDistance(); -} +public class ExampleSubsystem extends SubsystemBase implements AutoCloseable { + /** Creates a new ExampleSubsystem. */ + private Encoder m_encoder; + public EncoderSim mEncoderSim; + + public ExampleSubsystem() {this(false);} + + public ExampleSubsystem(boolean simulation) { + m_encoder = new Encoder(exampleSubsystem_Constants.encoderA, + exampleSubsystem_Constants.encoderB); + if (simulation) { + mEncoderSim = new EncoderSim(m_encoder); + } else { + + } + } + + @Override + public void close() throws Exception { + m_encoder.close(); + } + + // public ExampleSubsystem(Encoder encoder) { + // m_encoder = encoder; + // } + + @Override + public void periodic() { + // This method will be called once per scheduler run + } + + @Override + public void simulationPeriodic() { + // This method will be called once per scheduler run during simulation + } + + public String helloWorld(String string) { + System.out.println(string); + return string; + } + + public double getDistance_Inch() { + System.out.println("Encoder Distance = " + m_encoder.getDistance()); + return m_encoder.getDistance(); + } } diff --git a/src/test/java/frc/commands/ExampleCommandTest.java b/src/test/java/frc/commands/ExampleCommandTest.java index 93c1aed..209ec1d 100644 --- a/src/test/java/frc/commands/ExampleCommandTest.java +++ b/src/test/java/frc/commands/ExampleCommandTest.java @@ -4,7 +4,6 @@ import edu.wpi.first.hal.HAL; import edu.wpi.first.wpilibj.Encoder; import edu.wpi.first.wpilibj.simulation.EncoderSim; -import frc.robot.Constants.exampleSubsystem_Constants; import frc.robot.commands.ExampleCommand; import frc.robot.subsystems.ExampleSubsystem; @@ -20,18 +19,17 @@ public class ExampleCommandTest { @Before // this method will run before each test public void setup() { - assert HAL.initialize(500, 0); // initialize the HAL, crash if failed - encoder = new Encoder(exampleSubsystem_Constants.encoderA, - exampleSubsystem_Constants.encoderB); - encoderSim = new EncoderSim(encoder); - subsystem = new ExampleSubsystem(encoder); // create our ExampleSubsystem. + assert HAL.initialize(500, 0); // initialize the HAL, crash if failed + + subsystem = new ExampleSubsystem(true); // create our ExampleSubsystem. + + encoderSim = subsystem.mEncoderSim; } @After // this method will run after each test public void shutdown() throws Exception { - // exampleSubsystem.close(); // destroy our exampleSubsystem object - encoder.close(); + subsystem.close(); // destroy our exampleSubsystem object } @Test // Is Initilized diff --git a/src/test/java/frc/subsystems/ExampleSubsystemTest.java b/src/test/java/frc/subsystems/ExampleSubsystemTest.java index a5e9dd6..074f89a 100644 --- a/src/test/java/frc/subsystems/ExampleSubsystemTest.java +++ b/src/test/java/frc/subsystems/ExampleSubsystemTest.java @@ -1,42 +1,50 @@ package frc.subsystems; + import static org.junit.Assert.*; import edu.wpi.first.hal.HAL; import edu.wpi.first.wpilibj.Encoder; import edu.wpi.first.wpilibj.simulation.EncoderSim; -import frc.robot.Constants.exampleSubsystem_Constants; import frc.robot.subsystems.ExampleSubsystem; import org.junit.*; public class ExampleSubsystemTest { - ExampleSubsystem exampleSubsystem; - Encoder encoder; - EncoderSim encoderSim; - - @Before // this method will run before each test - public void setup() { - assert HAL.initialize(500, 0); // initialize the HAL, crash if failed - encoder = new Encoder(exampleSubsystem_Constants.encoderA, - exampleSubsystem_Constants.encoderB); - encoderSim = new EncoderSim(encoder); - exampleSubsystem = new ExampleSubsystem(encoder); // create our ExampleSubsystem. - } - - @After // this method will run after each test - public void shutdown() throws Exception { - // exampleSubsystem.close(); // destroy our exampleSubsystem object - encoder.close(); - } - - @Test // marks this method as a test - public void hello_World() { - System.out.print("Hello "); - assertEquals("World", exampleSubsystem.helloWorld("World")); - } - - @Test // Checks that Capsensitive fails if not Equal - public void NOT_Hello_World() { - System.out.print("Hello "); - assertNotEquals("World", exampleSubsystem.helloWorld("world")); - } + private double delta = 1e-5; + + ExampleSubsystem exampleSubsystem; + Encoder encoder; + EncoderSim encoderSim; + + @Before // this method will run before each test + public void setup() { + assert HAL.initialize(500, 0); // initialize the HAL, crash if failed + + exampleSubsystem = new ExampleSubsystem(true); // create our ExampleSubsystem. + + encoderSim = exampleSubsystem.mEncoderSim; + } + + @After // this method will run after each test + public void shutdown() throws Exception { + // exampleSubsystem.close(); // destroy our exampleSubsystem object + exampleSubsystem.close(); + } + + @Test // marks this method as a test + public void hello_World() { + System.out.print("Hello "); + assertEquals("World", exampleSubsystem.helloWorld("World")); + } + + @Test // Checks that Capsensitive fails if not Equal + public void NOT_Hello_World() { + System.out.print("Hello "); + assertNotEquals("World", exampleSubsystem.helloWorld("world")); + } + + @Test + public void encoderTest() { + encoderSim.setDistance(5); + assertEquals(5.0, exampleSubsystem.getDistance_Inch(), delta); + } }