-
Notifications
You must be signed in to change notification settings - Fork 714
[robotpy] Port more wpilibc tests to python #8903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PeterJohnson
merged 11 commits into
wpilibsuite:main
from
bzlmodRio:claude_wpilic_tests
Jun 22, 2026
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ba5f094
[robotpy] Port wpilibc tests to python
pjreiniger 962a2eb
Fixup some claude-isms
pjreiniger 5a340f8
Prefer tests written by human for joysticks
pjreiniger 05fa352
Merge remote-tracking branch 'wpilibsuite/main' into claude_wpilic_tests
pjreiniger fbaf2dd
Run black on examples
pjreiniger 180ccc1
Merge branch 'main' into claude_wpilic_tests
pjreiniger 583eb6a
Fix all the __setitem__ things
pjreiniger 8aaa327
clazz -> class
pjreiniger 451fd51
Merge branch 'main' into claude_wpilic_tests
pjreiniger 879efa5
Prefer existing generic_hid test
pjreiniger fdd9659
Fix timed robot test
pjreiniger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
434 changes: 434 additions & 0 deletions
434
wpilibc/src/test/python/drive/test_differential_drive.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| import math | ||
|
|
||
| import pytest | ||
| from wpimath import Rotation2d | ||
|
|
||
| from wpilib import MecanumDrive | ||
|
|
||
|
|
||
| class MockMotorController: | ||
| def __init__(self): | ||
| self.throttle = 0 | ||
|
|
||
| def setThrottle(self, throttle): | ||
| self.throttle = throttle | ||
|
|
||
| def getThrottle(self): | ||
| return self.throttle | ||
|
|
||
|
|
||
| def test_cartesian_ik(): | ||
| # Forward | ||
| v = MecanumDrive.driveCartesianIK(1.0, 0.0, 0.0) | ||
| assert v.frontLeft == pytest.approx(1.0) | ||
| assert v.frontRight == pytest.approx(1.0) | ||
| assert v.rearLeft == pytest.approx(1.0) | ||
| assert v.rearRight == pytest.approx(1.0) | ||
|
|
||
| # Left | ||
| v = MecanumDrive.driveCartesianIK(0.0, -1.0, 0.0) | ||
| assert v.frontLeft == pytest.approx(-1.0) | ||
| assert v.frontRight == pytest.approx(1.0) | ||
| assert v.rearLeft == pytest.approx(1.0) | ||
| assert v.rearRight == pytest.approx(-1.0) | ||
|
|
||
| # Right | ||
| v = MecanumDrive.driveCartesianIK(0.0, 1.0, 0.0) | ||
| assert v.frontLeft == pytest.approx(1.0) | ||
| assert v.frontRight == pytest.approx(-1.0) | ||
| assert v.rearLeft == pytest.approx(-1.0) | ||
| assert v.rearRight == pytest.approx(1.0) | ||
|
|
||
| # Rotate CCW | ||
| v = MecanumDrive.driveCartesianIK(0.0, 0.0, -1.0) | ||
| assert v.frontLeft == pytest.approx(-1.0) | ||
| assert v.frontRight == pytest.approx(1.0) | ||
| assert v.rearLeft == pytest.approx(-1.0) | ||
| assert v.rearRight == pytest.approx(1.0) | ||
|
|
||
| # Rotate CW | ||
| v = MecanumDrive.driveCartesianIK(0.0, 0.0, 1.0) | ||
| assert v.frontLeft == pytest.approx(1.0) | ||
| assert v.frontRight == pytest.approx(-1.0) | ||
| assert v.rearLeft == pytest.approx(1.0) | ||
| assert v.rearRight == pytest.approx(-1.0) | ||
|
|
||
|
|
||
| def test_cartesian_ik_gyro_90_cw(): | ||
| gyro = Rotation2d.fromDegrees(90) | ||
|
|
||
| # Forward in global frame; left in robot frame | ||
| v = MecanumDrive.driveCartesianIK(1.0, 0.0, 0.0, gyro) | ||
| assert v.frontLeft == pytest.approx(-1.0) | ||
| assert v.frontRight == pytest.approx(1.0) | ||
| assert v.rearLeft == pytest.approx(1.0) | ||
| assert v.rearRight == pytest.approx(-1.0) | ||
|
|
||
| # Left in global frame; backward in robot frame | ||
| v = MecanumDrive.driveCartesianIK(0.0, -1.0, 0.0, gyro) | ||
| assert v.frontLeft == pytest.approx(-1.0) | ||
| assert v.frontRight == pytest.approx(-1.0) | ||
| assert v.rearLeft == pytest.approx(-1.0) | ||
| assert v.rearRight == pytest.approx(-1.0) | ||
|
|
||
| # Right in global frame; forward in robot frame | ||
| v = MecanumDrive.driveCartesianIK(0.0, 1.0, 0.0, gyro) | ||
| assert v.frontLeft == pytest.approx(1.0) | ||
| assert v.frontRight == pytest.approx(1.0) | ||
| assert v.rearLeft == pytest.approx(1.0) | ||
| assert v.rearRight == pytest.approx(1.0) | ||
|
|
||
| # Rotate CCW | ||
| v = MecanumDrive.driveCartesianIK(0.0, 0.0, -1.0, gyro) | ||
| assert v.frontLeft == pytest.approx(-1.0) | ||
| assert v.frontRight == pytest.approx(1.0) | ||
| assert v.rearLeft == pytest.approx(-1.0) | ||
| assert v.rearRight == pytest.approx(1.0) | ||
|
|
||
| # Rotate CW | ||
| v = MecanumDrive.driveCartesianIK(0.0, 0.0, 1.0, gyro) | ||
| assert v.frontLeft == pytest.approx(1.0) | ||
| assert v.frontRight == pytest.approx(-1.0) | ||
| assert v.rearLeft == pytest.approx(1.0) | ||
| assert v.rearRight == pytest.approx(-1.0) | ||
|
|
||
|
|
||
| def test_cartesian(wpilib_state): | ||
| fl = MockMotorController() | ||
| rl = MockMotorController() | ||
| fr = MockMotorController() | ||
| rr = MockMotorController() | ||
| drive = MecanumDrive( | ||
| fl.setThrottle, | ||
| rl.setThrottle, | ||
| fr.setThrottle, | ||
| rr.setThrottle, | ||
| ) | ||
| drive.setDeadband(0.0) | ||
|
|
||
| # Forward | ||
| drive.driveCartesian(1.0, 0.0, 0.0) | ||
| assert fl.getThrottle() == pytest.approx(1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(1.0) | ||
|
|
||
| # Left | ||
| drive.driveCartesian(0.0, -1.0, 0.0) | ||
| assert fl.getThrottle() == pytest.approx(-1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(-1.0) | ||
|
|
||
| # Right | ||
| drive.driveCartesian(0.0, 1.0, 0.0) | ||
| assert fl.getThrottle() == pytest.approx(1.0) | ||
| assert fr.getThrottle() == pytest.approx(-1.0) | ||
| assert rl.getThrottle() == pytest.approx(-1.0) | ||
| assert rr.getThrottle() == pytest.approx(1.0) | ||
|
|
||
| # Rotate CCW | ||
| drive.driveCartesian(0.0, 0.0, -1.0) | ||
| assert fl.getThrottle() == pytest.approx(-1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(-1.0) | ||
| assert rr.getThrottle() == pytest.approx(1.0) | ||
|
|
||
| # Rotate CW | ||
| drive.driveCartesian(0.0, 0.0, 1.0) | ||
| assert fl.getThrottle() == pytest.approx(1.0) | ||
| assert fr.getThrottle() == pytest.approx(-1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(-1.0) | ||
|
|
||
|
|
||
| def test_cartesian_gyro_90_cw(wpilib_state): | ||
| fl = MockMotorController() | ||
| rl = MockMotorController() | ||
| fr = MockMotorController() | ||
| rr = MockMotorController() | ||
| drive = MecanumDrive( | ||
| fl.setThrottle, | ||
| rl.setThrottle, | ||
| fr.setThrottle, | ||
| rr.setThrottle, | ||
| ) | ||
| drive.setDeadband(0.0) | ||
|
|
||
| gyro = Rotation2d.fromDegrees(90) | ||
|
|
||
| # Forward in global frame; left in robot frame | ||
| drive.driveCartesian(1.0, 0.0, 0.0, gyro) | ||
| assert fl.getThrottle() == pytest.approx(-1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(-1.0) | ||
|
|
||
| # Left in global frame; backward in robot frame | ||
| drive.driveCartesian(0.0, -1.0, 0.0, gyro) | ||
| assert fl.getThrottle() == pytest.approx(-1.0) | ||
| assert fr.getThrottle() == pytest.approx(-1.0) | ||
| assert rl.getThrottle() == pytest.approx(-1.0) | ||
| assert rr.getThrottle() == pytest.approx(-1.0) | ||
|
|
||
| # Right in global frame; forward in robot frame | ||
| drive.driveCartesian(0.0, 1.0, 0.0, gyro) | ||
| assert fl.getThrottle() == pytest.approx(1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(1.0) | ||
|
|
||
| # Rotate CCW | ||
| drive.driveCartesian(0.0, 0.0, -1.0, gyro) | ||
| assert fl.getThrottle() == pytest.approx(-1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(-1.0) | ||
| assert rr.getThrottle() == pytest.approx(1.0) | ||
|
|
||
| # Rotate CW | ||
| drive.driveCartesian(0.0, 0.0, 1.0, gyro) | ||
| assert fl.getThrottle() == pytest.approx(1.0) | ||
| assert fr.getThrottle() == pytest.approx(-1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(-1.0) | ||
|
|
||
|
|
||
| def test_polar(wpilib_state): | ||
| fl = MockMotorController() | ||
| rl = MockMotorController() | ||
| fr = MockMotorController() | ||
| rr = MockMotorController() | ||
| drive = MecanumDrive( | ||
| fl.setThrottle, | ||
| rl.setThrottle, | ||
| fr.setThrottle, | ||
| rr.setThrottle, | ||
| ) | ||
| drive.setDeadband(0.0) | ||
|
|
||
| # Forward | ||
| drive.drivePolar(1.0, Rotation2d.fromDegrees(0), 0.0) | ||
| assert fl.getThrottle() == pytest.approx(1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(1.0) | ||
|
|
||
| # Left | ||
| drive.drivePolar(1.0, Rotation2d.fromDegrees(-90), 0.0) | ||
| assert fl.getThrottle() == pytest.approx(-1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(-1.0) | ||
|
|
||
| # Right | ||
| drive.drivePolar(1.0, Rotation2d.fromDegrees(90), 0.0) | ||
| assert fl.getThrottle() == pytest.approx(1.0) | ||
| assert fr.getThrottle() == pytest.approx(-1.0) | ||
| assert rl.getThrottle() == pytest.approx(-1.0) | ||
| assert rr.getThrottle() == pytest.approx(1.0) | ||
|
|
||
| # Rotate CCW | ||
| drive.drivePolar(0.0, Rotation2d.fromDegrees(0), -1.0) | ||
| assert fl.getThrottle() == pytest.approx(-1.0) | ||
| assert fr.getThrottle() == pytest.approx(1.0) | ||
| assert rl.getThrottle() == pytest.approx(-1.0) | ||
| assert rr.getThrottle() == pytest.approx(1.0) | ||
|
|
||
| # Rotate CW | ||
| drive.drivePolar(0.0, Rotation2d.fromDegrees(0), 1.0) | ||
| assert fl.getThrottle() == pytest.approx(1.0) | ||
| assert fr.getThrottle() == pytest.approx(-1.0) | ||
| assert rl.getThrottle() == pytest.approx(1.0) | ||
| assert rr.getThrottle() == pytest.approx(-1.0) |
45 changes: 45 additions & 0 deletions
45
wpilibc/src/test/python/driverstation/joystick_test_macros.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| def axis_test(controller_clazz, sim_clazz, axis_name): | ||
| joy = controller_clazz(2) | ||
| joysim = sim_clazz(joy) | ||
|
|
||
| sim_setter_func = getattr(joysim, "set" + axis_name) | ||
| joy_get_func = getattr(joy, "get" + axis_name) | ||
|
|
||
| sim_setter_func(0.35) | ||
| joysim.notifyNewData() | ||
| assert joy_get_func() == pytest.approx(0.35, abs=0.001) | ||
|
|
||
|
|
||
| def button_test(controller_clazz, sim_clazz, btn_name): | ||
| joy = controller_clazz(1) | ||
| joysim = sim_clazz(joy) | ||
|
|
||
| joy_get_func = getattr(joy, "get" + btn_name) | ||
| joy_get_pressed_func = getattr(joy, "get" + btn_name + "Pressed") | ||
| joy_get_released_func = getattr(joy, "get" + btn_name + "Released") | ||
|
|
||
| sim_setter_func = getattr(joysim, "set" + btn_name) | ||
|
|
||
| sim_setter_func(False) | ||
| joysim.notifyNewData() | ||
| assert False == joy_get_func() | ||
|
|
||
| # need to call pressed and released to clear flags | ||
| joy_get_pressed_func() | ||
| joy_get_released_func() | ||
|
|
||
| sim_setter_func(True) | ||
| joysim.notifyNewData() | ||
| assert True == joy_get_func() | ||
| assert True == joy_get_pressed_func() | ||
| assert False == joy_get_released_func() | ||
|
|
||
|
|
||
| sim_setter_func(False) | ||
| joysim.notifyNewData() | ||
| assert False == joy_get_func() | ||
| assert False == joy_get_pressed_func() | ||
| assert True == joy_get_released_func() | ||
53 changes: 53 additions & 0 deletions
53
wpilibc/src/test/python/driverstation/test_driver_station.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import pytest | ||
|
|
||
| from wpilib import DriverStationBackend, Joystick | ||
| from wpilib.simulation import DriverStationSim, stepTiming | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "axes_max, buttons_max, povs_max, expected", | ||
| [ | ||
| (0, 0, 0, False), | ||
| (1, 0, 0, True), | ||
| (0, 1, 0, True), | ||
| (0, 0, 1, True), | ||
| (1, 1, 1, True), | ||
| (4, 10, 1, True), | ||
| ], | ||
| ) | ||
| def test_is_joystick_connected(wpilib_state, axes_max, buttons_max, povs_max, expected): | ||
| DriverStationSim.setJoystickAxesMaximumIndex(1, axes_max) | ||
| DriverStationSim.setJoystickButtonsMaximumIndex(1, buttons_max) | ||
| DriverStationSim.setJoystickPOVsMaximumIndex(1, povs_max) | ||
| DriverStationSim.notifyNewData() | ||
|
|
||
| assert DriverStationBackend.isJoystickConnected(1) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "fms_attached, silenced, expected_silenced, expected_warning", | ||
| [ | ||
| (False, True, True, ""), | ||
| (False, False, False, "Warning: Joystick on port 0 not available, check if all controllers are plugged in"), | ||
| (True, True, False, "Warning: Joystick on port 0 not available, check if all controllers are plugged in"), # FMS overrides silence | ||
| (True, False, False, "Warning: Joystick on port 0 not available, check if all controllers are plugged in"), | ||
| ], | ||
| ) | ||
| def test_joystick_connection_warnings( | ||
| wpilib_state, fms_attached, silenced, expected_silenced, expected_warning, capsys | ||
| ): | ||
| DriverStationSim.setFmsAttached(fms_attached) | ||
| DriverStationSim.notifyNewData() | ||
| DriverStationBackend.silenceJoystickConnectionWarning(silenced) | ||
|
|
||
| joystick = Joystick(0) | ||
| joystick.getRawButton(1) | ||
|
|
||
| stepTiming(1.0) | ||
| assert ( | ||
| DriverStationBackend.isJoystickConnectionWarningSilenced() == expected_silenced | ||
| ) | ||
|
|
||
| # Capture stderr to check for warnings | ||
| captured = capsys.readouterr() | ||
| assert expected_warning in captured.err | ||
|
pjreiniger marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.