I am trying to create a unit test for my FRC swerve modules using the FRC simulation library, but my test keeps failing. The swerve module code works fine when using the full Java robot simulator, but when I try to use the simulation library, the motors do not move and their positions do not change. I think the problem is with the simulation, but I am not sure what part of it is causing the issue.
Here is my GitHub repo on the branch with my testing
To reproduce the issue, you can run the SwerveModuleTest
in the test
package. The test currently fails with the following error message:
expected: <1.0> but was: <0.0>
Expected :1.0 Actual :0.0
<Click to see difference>
I have tried printing out various debug information from the swerve module, including the position, velocity, angle, drive and steer motor values, and power and current of the drive motor. However, this information has not helped me find the root cause of the issue.
I would greatly appreciate any help or suggestions on how to debug this issue. Thank you in advance!
here is the test
var swerveModule: SwerveModule? = null
var simEncoder: EncoderSim? = null
@BeforeEach
fun setUp() {
// initialize the HAL to start the simulation.
// pass in 500 as the timeout in milliseconds and 0 as the device ID
assert(HAL.initialize(500, 0))
SimHooks.setHALRuntimeType(HALUtil.getHALRuntimeType())
// create our simulation encoder at the correct CAN index
simEncoder = EncoderSim.createForIndex(Constants.FrontLeftEncoder)
// create our swerve module using the appropriate motor and encoder IDs and a Translation2d object
swerveModule = SwerveModule(
"fl",
Constants.FrontLeftDriveMotor,
Constants.FrontLeftSteerMotor,
Constants.FrontLeftEncoder,
Translation2d(1.0, 1.0)
)
}
@Test
fun testMove() {
// before running the test, initialize the HAL and observe the user program teleop
HAL.initialize(500, 0)
HAL.observeUserProgramTeleop()
// run the simulation and the swerve module's move method 100 times
for (i in 0..100) {
// run the command scheduler and physics simulation
CommandScheduler.getInstance().run()
PhysicsSim.instance.run()
// run the move method on the swerve module, passing in a drive value of 1.0 and an angle value of 1.0
swerveModule!!.move(1.0, 1.0)
}
// assert that the velocity and angle of the swerve module are as expected
assertEquals(1.0, swerveModule!!.velocity, DELTA)
assertEquals(1.0, swerveModule!!.angle, DELTA)
}